(1)分别查询STUDENT、 COURSE、 SC的基本信息。
SELECT * FROM Student;
(2)查询全体学生的姓名、学号、所在系、家庭地址。
SELECT Sname,Sno,Sdept,Saddr FROM Student;
(3)查询全体学生的姓名、出生年份和所在系、班级,要求用小写字母表示所在系名,并用别名表达列标题。
SELECT sname 姓名,2010-sage 出生年份,lower(sdept) 系别,class 班级FROM Student;
(4)查询那些学生选修了课程。
SELECT distinct sno FROM SC WHERE grade is null;
(5)查询所有年龄在20岁以上的学生姓名、班级及其年龄。
SELECT sname,class,sage FROM Student WHERE sage>20;
(6)查询年龄在17~19岁(包括17岁和19岁)之间的学生的姓名、系别、班级和年龄。
SELECT sname,sdept,class,sage FROM Student WHERE sgae between 17 and 19;
(7)查询既不是信息系、数学系,也不是计算机科学系的学生的姓名和性别。
SELECT sname,ssex FROM Student WHERE sdept not in(‘IS’,’MA’,’cs’);
(8)查询所有姓张学生的姓名、学号和性别。
SELECT sname,sno,ssex FROM Student WHERE sname like ‘张%’;
(9)查询姓"欧阳"且全名为四个汉字的学生的姓名。
SELECT sname FROM Student WHERE sname like ‘欧阳__’;
(10)查询所有家庭地址中包含‘成都’的同学的基本信息
SELECT * FROM Student WHERE saddr like ‘%成都%’;
2. 单表查询
(1) 查询信息系(IS)、数学系(MA)和计算机科学系(CS)中女学生的姓名和性别
(分别用in and 和 and or 两种方式查询)
SELECT sname,ssex
FROM Studen
WHERE sdept in(‘IS’,’CS’,’MA’) and ssex=’女’;
SELECT sname,ssex
FROM Student
WHERE (sdept=’IS’ or sdept=’CS’ or sdept=”MA’) and ssex=’女’;
(2) 查询选修表中1号课程成绩为空(即1号课程选修了而未参加考试)的同学学号.
SELECT sno FROM SC WHERE grade is null and cno=’1’;
(3) 查询课程表中第三学期开设了那些选修课.
SELECT * FROM Course WHERE ctype=’选修’;
(4) 查询SC的基本信息,结果按学号升序排列,同一学号按课程号降序排列.
SELECT * FROM SC order by
(5) 查询学生表的学号、姓名、班级,结果按班级号升序排列。
SELECT FROM Student,Course,SC WHERE order by sno,class desc
(6)查询学生表的学号、姓名、班级,结果按 班级号升序排列,同一班级学生按学号升序排列。
SELECT sno,sname,class FROM Student WHERE order by class,sno desc
(7)查询家在成都的信息系(IS)的男同学基本信息。
SELECT * FROM Student WHERE saddr like ‘%成都%’and ssex=’男’;
(8)查询200507班的女同学的姓名、学号。
SELECT sname,sno FROM Student WHERE class=’200507’;
3.SELECT语句的连接查询(用连接查询)
(1)列出“计算机”(CS) 系选修了编号为“2”的课程的学生的学号和姓名
SE
LECT sname,student.sno
FROM Student,SC
WHERE student.sno=sc.sno and sdept=’CS’and cno=’2’
(2)列出选修了学分大于3,并且成绩不及格的学生的学号、姓名、课程编号
SELECT sc.sno,sname,sco
FROM Student,course,SC
WHERE student.sno=sc.sno and courseo=sco and grade<60 and ccredit>3;
(3)列出“信息”(IS)、“数学”(MA)、“计算机”三个系的学生成绩在85分以上的选修课程编号、成绩;
SELECT distinct cno,grade
FROM Student,SC
WHERE student.sno=sc.sno and courseo=sco
And vsdept in (‘IS’,’MA’,’CS’) and grade>85;
(4)查询成绩在80到90分之间的学生的学号、姓名、选修的课程名称、成绩
SELECT sc.sno,sname,cname,grade
FROM Student,Course,SC
WHERE student.sno=sc.sno and courseo=sco and grade between 80 and 90;
(6)查询所有学生的学号、姓名、选修的课程名及成绩(并对其按学号升序排序、课程名降序排列。
SELECT sc.sno,sname,cname,grade
FROM Student,Course,SC
WHERE student.sno=sc.sno and courseo=sco
order by sc.sno,cname desc;
(7)查询选修“数据库”这门课且成绩在85分以上的所有学生的学号、姓名,并对其按学号升序排序
SELECT sc.sno,sname
FROM Student,Course,SC
WHERE student.sno=sc.sno and courseo=sco and cname=’数据库’
And grade>=85
Order by sno
(8)查询学分在2-4范围内的课程的基本信息
SELECT * FROM Course WHERE ccredit between 2 and 4
(9)查选修了2号课程,并且成绩在85-----100之间的学生的学号、姓名、所在系、成绩,结果以成绩降序排列,成绩相同者以学号升序排列
SELECT sc.sno,sname,sdept,grade
FROM Student,SC
WHERE student.sno=sc.sno and cno=’2’ and grade between 85 and 100
Order by grade desc,sc.sno
(10)查询信息系选修“PASCAL语言”课程的同学学号及姓名
SELECT sc.sno,sname
FROM Student,Course,SC
WHERE student.sno=sc.sno and courseo=sco and
Cname=’PASCAL语言’;
(11)列出所有学生的的学号、姓名、不及格的课程编号及具体成绩
SELECT sc,sno,sname,cno,grade
FROM Student,SC
WHERE student.sno=sc.sno and grade<60;
(12)列出所有课程的选修情况,包括没有选的课程信息
SELECT * FROM Course join left outer SC courseo=sco’
(13)列出所有同学的选课信息,包括未选课的同学
SELECT * FROM Student join left SC student.sno=sc.sno
4. 聚合函数及分组查询
(1)统计有成绩的学生的人数。
SELECT count(distinct sno)
FROM SC
WHERE grade id null
(2)统计编号为“2”的课程的平均成绩。
SELECT avg(grade) FROM SC WHERE cno=’2’;
(4)求全校有多少个系。
SELECT count(distinct sdept) FROM Student
(5)求“MA”系成绩大于85分的学生的人数。
SELECT count(distinct sc.sno)
FROM Student,SC
WHERE student.sno=sc.sno and gr
ade>=85;
(6)求学号为“95001”的学生的总分和平均分。
SELECT sum(grade),avg(grade)
FROM SC
WHERE sno=’95001’;
(7)查询“1”号课程的最高分、最低分及之间相差的分数。
SELECT max(grade),min(grade),max(grade)-min(grade)
FROM SC WHERE cno=’1’;
(8)求“计算机”系学生的人数。
SELECT count(sno) FROM Student WHERE sdept=’CS’;
(9)查询每门课的选课人数及平均成绩。
SELECT cno,count(sno) FROM SC
Group by cno
(10)查询有两门课以上不及格的学生学号及不及格的课程门数。
SELECT sno,count(cno)
FROM SC
WHERE grade <60
Group by sno
Having count(cno)>=2;
(11) 统计每门选修课程不及格的人数,列出课程编号和不及格的人数。
SELECT cno,count(sno)
FROM ,SC
WHERE grade<60
Group by cno
(12) 统计每个同学所修的学分总数
SELECT sno,sum(ccredit)
FROM Course,SC
WHERE courseo=sco
Group by sno
(13)查询每个系的学生人数、并按人数的降序排列。
SELECT sdept,count(sno)
FROM Student
Group by sdept
Order by count(sno) desc;
(15)查询每门课的平均分、最高分、最低分。
SELECT avg(grade),max(grade),min(grade)
FROM SC
Group by cno
(17)分课程统计成绩在85分以上的学生的人数,列出课程编号、课程名称和学生人数
SELECT sco,cname,count(sno)
FROM Course,SC
WHERE courseo=sco and grade>=85
Group by sco,sname
(18)列出平均成绩在85分以上的学生的学号和姓名
SELECT sc.sno,sname
FROM Student,SC
Group by sc.sno,sname
Having avg(grade);
(20)列出选修人数小于5的课程的编号和实际选修人数
SELECT cno,count(sno)
FROM SC
Group by cno
Having count(sno)<5;
(21)列出“信息”系每个学生不及格的课程门数
SELECT sc.sno,count(cno)
FROM Student,SC
WHERE student.sno=sc.sno and sdept=’IS’and grade<60
Group by sc.sno
嵌套查询(子查询)
(1) 查询选修了一号课程的同学姓名
ELECT sname FROM Student
WHERE sno in (select sno
From sc
Where cno=’1’)
(2)查家庭住址包含“成都”的学生选修的课程的名称。
SELECT cname FROM Course
WHERE cno in (select cno
From sc
Where sno in (select sno
From student
Where saddr like (%成都%));
distinct查询(3)查询选修了数据库这门课的同学的学号、姓名
SELECT sno,sname FROM Student
WHERE sno in (select sno
From sc
Where cno in(select cno
From course
Where cname=’数据库’));
(4)查询选修了‘1’号课程,并且成绩在该课程平均分以下的学生的学号、姓名、成绩
SELECT sc.sno,sname,grade
FROM Student, SC
WHERE student.sno=sc.sno and cno=’1’ and grade<
(select avg(grade)
From
sc
Where cno=’1’);
(5)查询选修了“数据库系统”这门课且成绩在85分以上的所有学生的学号、姓名。
SELECT sno,sname
FROM Student
WHERE sno in (select sno
From sc
Grade>=85 and cno in( select cno
From course
Where cname=’数据库’);
(7)列出选修的课程学分都大于3的学生的学号、姓名
SELECT sno,sname FROM Student
WHERE sno in(select sno
From sc
Where cno in(select cno
From course where ccredit>3));
(8)查询其他系中比信息系所有学生年龄都小的学生姓名及年龄
SELECT sname,sage
FROM Student
WHERE sdept<>’IS’ and sage <all(select sage
From student
Where sdept=’IS’);
(9)查询同时选修了一号课程和选修了二号课程的同学姓名。
SELECT sname FROM Student
WHERE sno in(select sno
From sc
Where cno=’1’ and sno in(select sno
From sc
Where cno=’2’));
(10)列出“2”号课程得分最高的学生的学号,姓名,成绩
SELECT sc.sno,sname,grade
FROM Student, SC
WHERE cno=’2’ and grade>=all(select grade
From sc
Where cno=’2’);
1. 列出没有任何一门课成绩不及格的学生的学号、姓名
SELECT sno,sname
FROM Student
WHERE sno not in(select sno
From sc
Grade<60);
2. 查询李丽同学不学的课程的课程号
SELECT cno FROM Course
WHERE cno not in(select cno
From sc
Where sno in(select sno
From student
Where sname=’ 李丽’));
3. 求年龄最大的学生姓名
SELECT sname FROM Student
WHERE sage>=(select max(sage)
From student);
4. 求年龄最小的学生姓名
SELECT sname FROM Student
WHERE sage<=all(select distinct sage
From student);
5. 求得分最低的学生的姓名、课程名及成绩
SELECT sname,cname,grade
FROM Student,Course,SC
WHERE student.sno=sc.sno and courseo=sco and
Grade<=(select min(grade)
From sc);
6. 求未选修3号课程的同学学号。
SELECT sno FROM Student
WHERE sno not in(select sno
From sc
Where cno=’3’);
7. 求平均成绩最高的学生的姓名及平均分
SELECT sname,avg(grade)
FROM Student,SC
Where student.sno=sc.sno
Group by sname
Having avg(grade)>=all(select avg(grade)
From sc
Group by sno);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论