SQL的基础查询

新建表

CREATE TABLE student
(
–学号
sno VARCHAR(3) NOT NULL PRIMARY KEY,
–姓名
sname VARCHAR(20) NOT NULL,
–性别
ssex VARCHAR(2) NOT NULL,
–出生年月
sbirthday DATETIME,
–所在班级
class VARCHAR(5)
)

CREATE TABLE teacher
(
–教师编号
tno VARCHAR(3) NOT NULL PRIMARY KEY,
–教师姓名
tname VARCHAR(20) NOT NULL,
–教师性别
tsex VARCHAR(2) NOT NULL,
–教师出生年月
tbirthday DATETIME,
–职称
prof VARCHAR(6),
–所在部门
depart VARCHAR(10)
)

CREATE TABLE course
(
–课程号
cno VARCHAR(5) NOT NULL PRIMARY KEY,
–课程名称
cname VARCHAR(10) NOT NULL,
–教师编号
tno VARCHAR(3) REFERENCES teacher(tno)
)

CREATE TABLE score
(
–学号
sno VARCHAR(3) NOT NULL REFERENCES student(sno),
–课程号
cno VARCHAR(5) NOT NULL REFERENCES course(cno),
–成绩
degree DECIMAL(4,1)
)

insert into student
values(‘108’,’曾华’,’男’,’1977-09-01’,’95033’)

insert into student
values(‘105’,’匡明’,’男’,’1975-10-02’,’95031’)

insert into student
values(‘107’,’王丽’,’女’,’1976-01-23’,’95033’)

insert into student
values(‘101’,’李军’,’男’,’1976-02-20’,’95033’)

insert into student
values(‘109’,’王芳’,’女’,’1975-02-10’,’95031’)

insert into student
values(‘103’,’陆君’,’男’,’1974-06-03’,’95031’)

insert into teacher
values(‘804’,’李诚’,’男’,’1958-12-02’,’副教授’,’计算机系’)
insert into teacher
values(‘856’,’张旭’,’男’,’1969-03-12’,’讲师’,’电子工程系’)
insert into teacher
values(‘825’,’王萍’,’女’,’1972-05-05’,’助教’,’计算机系’)
insert into teacher
values(‘831’,’刘冰’,’女’,’1958-08-14’,’助教’,’电子工程系’)

insert into course
values(‘3-105’,’计算机导论’,’825’)
insert into course
values(‘3-245’,’操作系统’,’804’)
insert into course
values(‘6-166’,’数字电路’,’856’)
insert into course
values(‘9-888’,’高等数学’,’831’)

insert into score
values(‘103’,’3-245’,’86’)
insert into score
values(‘105’,’3-245’,’75’)
insert into score
values(‘109’,’3-245’,’68’)
insert into score
values(‘103’,’3-105’,’92’)
insert into score
values(‘105’,’3-105’,’88’)
insert into score
values(‘109’,’3-105’,’76’)
insert into score
values(‘101’,’3-105’,’64’)
insert into score
values(‘107’,’3-105’,’91’)
insert into score
values(‘108’,’3-105’,’78’)
insert into score
values(‘101’,’6-166’,’85’)
insert into score
values(‘107’,’6-166’,’79’)
insert into score
values(‘108’,’6-166’,’81’)

问题

1、 查询Student表中的所有记录的Sname、Ssex和Class列。
2、 查询教师所有的单位即不重复的Depart列。
3、 查询Student表的所有记录。
4、 查询Score表中成绩在60到80之间的所有记录。
5、 查询Score表中成绩为85,86或88的记录。
6、 查询Student表中“95031”班或性别为“女”的同学记录。
7、 以Class降序查询Student表的所有记录。
8、 以Cno升序、Degree降序查询Score表的所有记录。
9、 查询“95031”班的学生人数。
10、查询Score表中的最高分的学生学号和课程号。
11、查询‘3-105’号课程的平均分。
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
13、查询最低分大于70,最高分小于90的Sno列。
14、查询所有学生的Sname、Cno和Degree列。
15、查询所有学生的Sno、Cname和Degree列。
16、查询所有学生的 Sname、Cname和Degree列。
17、查询“95033”班所选课程的平均分。
18、假设使用如下命令建立了一个grade 表:
create table grade(low number(3,0),upp number(3),rank char(1));
insert into grade values(90,100,’A’);
insert into grade values(80,89,’B’);
insert into grade values(70,79,’C’);
insert into grade values(60,69,’D’);
insert into grade values(0,59,’E’);
commit;
现查询所有同学的Sno、Cno和rank列。
19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
23、查询“张旭“教师任课的学生成绩。
24、查询选修某课程的同学人数多于5人的教师姓名。
25、查询95033班和95031班全体学生的记录。
26、查询存在有85分以上成绩的课程Cno.
27、查询出 “计算机系“教师所教课程的成绩表。
28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
31、查询所有教师和同学的 name、sex和birthday.
32、查询所有“女”教师和“女”同学的name、sex和birthday.
33、查询成绩比该课程平均成绩低的同学的成绩表。
34、查询所有任课教师的Tname和Depart.
35、查询所有未讲课的教师的Tname和Depart.
36、查询至少有2名男生的班号。
37、查询Student表中不姓“王”的同学记录。
38、查询Student表中每个学生的姓名和年龄。
39、查询Student表中最大和最小的Sbirthday日期值。
40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
41、查询“男”教师及其所上的课程。
42、查询最高分同学的Sno、 Cno和Degree列。
43、查询和“李军”同性别的所有同学的Sname.
44、查询和“李军”同性别并同班的同学Sname.
45、查询所有选修“计算机导论”课程的“男”同学的成绩表
46、查询score表中分数最高的学生的信息。//多层嵌套
47、查询score表中的平均分在80分以上的学生信息。//相关查询。无关查询

答案

1、SELECT sname,ssex,class FROM dbo.student
2、SELECT distinct depart FROM dbo.teacher
3、SELECT sno AS ‘学号’,sname AS ‘姓名’,ssex AS ‘性别’,sbirthday AS ‘出生日期’,class AS ‘班级’ FROM dbo.student
SELECT sno AS 学号,sname AS 姓名,ssex AS 性别,sbirthday AS 出生日期,class AS 班级 FROM dbo.student
4、SELECT * FROM dbo.score WHERE degree BETWEEN 60 AND 80
SELECT * FROM dbo.score WHERE degree>=60 AND degree<=80
5、SELECT * FROM dbo.score WHERE degree IN(86,86,88)
6、SELECT * FROM dbo.student WHERE class=’95031’ OR ssex=’女’
7、SELECT * FROM dbo.student ORDER BY class DESC
8、SELECT * FROM dbo.score ORDER BY cno ASC,degree DESC
SELECT * FROM dbo.score ORDER BY cno,degree DESC
9、SELECT COUNT(*) AS 班级人数 FROM dbo.student WHERE class=’95031’
10、SELECT sno AS 学号,cno AS 课程号 FROM dbo.score WHERE degree=(SELECT MAX(degree) FROM dbo.score)
11、SELECT AVG(degree) AS 课程平均分 FROM dbo.score WHERE cno=’3-105’
12、SELECT cno,AVG(degree) AS 课程平均分 FROM dbo.score WHERE cno LIKE ‘3%’ GROUP BY cno HAVING COUNT(*)>5
13、SELECT sno AS 学号 FROM dbo.score GROUP BY sno HAVING MIN(degree)>70 AND MAX(degree)<90
14、SELECT A.sname,B.cno,B.degree FROM dbo.student A INNER JOIN dbo.score B ON B.sno = A.sno
SELECT dbo.student.sname,dbo.score.cno,dbo.score.degree FROM dbo.student,dbo.score WHERE dbo.student.sno=dbo.score.sno
15、SELECT A.sno,B.cname,A.degree FROM dbo.score A INNER JOIN dbo.course B ON A.cno = B.cno
SELECT A.sno,B.cname,A.degree FROM dbo.score A,dbo.course B WHERE A.cno=B.cno
16、SELECT B.sname AS 姓名,C.cname AS 课程,A.degree AS 成绩 FROM dbo.score A
LEFT JOIN dbo.student B ON B.sno = A.sno
LEFT JOIN dbo.course C ON C.cno = A.cno
SELECT B.sname,C.cname,A.degree FROM dbo.score A,dbo.student B,dbo.course C WHERE A.sno=B.sno AND A.cno=C.cno
17、SELECT B.cno,AVG(B.degree) FROM dbo.student A
INNER JOIN score B ON B.sno = A.sno WHERE A.class=’95033’ GROUP BY B.cno
SELECT B.cno,AVG(B.degree) FROM dbo.student A,dbo.score B WHERE A.sno=B.sno AND A.class=’95033’ GROUP BY B.cno
18、SELECT sno,cno,rank FROM dbo.score,grade WHERE degree BETWEEN low AND upp ORDER BY rank
19、SELECT A.cno,A.sno,A.degree FROM dbo.score A,dbo.score B
WHERE A.cno=’3-105’ AND A.degree>B.degree AND B.sno=’109’ AND B.cno=’3-105’
20、–1,查询成绩非本科最高
SELECT * FROM dbo.score A WHERE degree<(SELECT MAX(degree) FROM dbo.score B WHERE A.cno=B.cno)
–2,查询成绩非本科最高并且选2门以上的学生的成绩:
21、SELECT * FROM dbo.score WHERE degree>(SELECT degree FROM dbo.score WHERE sno=’109’ AND cno=’3-105’)
SELECT A.sno,A.cno,A.degree FROM dbo.score A,dbo.score B
WHERE A.degree>B.degree AND B.sno=’109’ AND B.cno=’3-105’
22、SELECT sno,sname,sbirthday FROM dbo.student
WHERE YEAR(sbirthday)=(SELECT YEAR(sbirthday) FROM dbo.student WHERE sno=’108’)
23、SELECT C.cno,C.sno,C.degree FROM dbo.teacher A
LEFT JOIN dbo.course B ON B.tno = A.tno
LEFT JOIN dbo.score C ON B.cno=C.cno
WHERE A.tname=’张旭’
SELECT cno,sno,degree FROM dbo.score WHERE cno=
(SELECT A.cno FROM dbo.course A,dbo.teacher B WHERE A.tno=B.tno AND B.tname=’张旭’)
24、SELECT tname FROM dbo.teacher WHERE tno IN
(SELECT A.tno FROM dbo.course A,dbo.score B WHERE A.cno=B.cno GROUP BY A.tno HAVING COUNT(A.tno) > 5)
25、SELECT * FROM dbo.student WHERE class=’95033’ OR class=’95031’
SELECT * FROM dbo.student WHERE class IN(‘95033’,’95031’)
26、SELECT DISTINCT cno FROM dbo.score WHERE degree IN(SELECT degree FROM dbo.score WHERE degree>85)
27、SELECT * FROM dbo.score A
LEFT JOIN dbo.course B ON B.cno = A.cno
LEFT JOIN dbo.teacher C ON C.tno = B.tno
WHERE C.depart=’计算机系’
SELECT * FROM dbo.score where cno IN
(SELECT A.cno FROM dbo.course A,dbo.teacher B WHERE A.tno=B.tno AND B.depart=’计算机系’)
28、SELECT tname,prof FROM dbo.teacher WHERE depart=’计算机系’ AND prof NOT IN
(select prof FROM dbo.teacher WHERE depart=’电子工程系’)
29、SELECT cno,sno,degree FROM dbo.score where cno=’3-105’ AND degree>ANY (SELECT degree FROM dbo.score WHERE cno=’3-245’)ORDER BY dbo.score.degree DESC
30、SELECT cno,sno,degree FROM dbo.score where cno=’3-105’ AND degree>ALL (SELECT degree FROM dbo.score WHERE cno=’3-245’)ORDER BY dbo.score.degree DESC
31、SELECT tname,tsex,tbirthday FROM dbo.teacher UNION SELECT sname,ssex,sbirthday FROM dbo.student
32、SELECT tname,tsex,tbirthday FROM dbo.teacher WHERE tsex=’女’
UNION SELECT sname,ssex,sbirthday FROM dbo.student WHERE ssex=’女’
33、SELECT * FROM dbo.score A WHERE degree<(SELECT AVG(degree) FROM dbo.score B WHERE A.cno=B.cno)
34、SELECT tname,depart FROM dbo.teacher A WHERE EXISTS(SELECT * FROM dbo.course B WHERE A.tno=B.tno)
35、SELECT tname,depart FROM dbo.teacher A WHERE NOT EXISTS(SELECT * FROM dbo.course B WHERE A.tno=B.tno)
36、SELECT class FROM dbo.student WHERE ssex=’男’ GROUP BY class HAVING COUNT(*)>=2
37、SELECT * FROM dbo.student WHERE sname NOT LIKE ‘王%’
SELECT * FROM dbo.student WHERE sname NOT LIKE ‘王_’
38、SELECT sname AS 姓名,(YEAR(GETDATE())-YEAR(sbirthday)) AS 年龄 FROM dbo.student
39、SELECT sname AS 姓名,sbirthday AS 最小 FROM dbo.student
WHERE sbirthday = (SELECT MIN(sbirthday) FROM dbo.student)
UNION SELECT sname AS姓名,sbirthday AS 最大 FROM dbo.student
WHERE sbirthday = (SELECT MAX(sbirthday) FROM dbo.student)
40、SELECT * FROM dbo.student ORDER BY class DESC,sbirthday
41、SELECT A.tname,B.cname FROM dbo.teacher A
INNER JOIN dbo.course B ON B.tno = A.tno
WHERE A.tsex=’男’
42、SELECT * FROM dbo.score WHERE degree=(SELECT MAX(degree) FROM dbo.score)
43、SELECT sname FROM dbo.student WHERE ssex=(SELECT ssex FROM dbo.student WHERE sname=’李军’)
44、SELECT sname FROM dbo.student
WHERE ssex=(SELECT ssex FROM dbo.student WHERE sname=’李军’)
AND class=(SELECT class FROM dbo.student WHERE sname=’李军’)
45、SELECT * FROM dbo.score
WHERE sno IN(SELECT sno FROM dbo.student WHERE ssex=’男’)
AND cno=(SELECT cno FROM dbo.course WHERE cname=’计算机导论’)
46、SELECT A.,B.* FROM dbo.score A
LEFT JOIN dbo.student B ON B.sno = A.sno
WHERE A.degree=(SELECT MAX(degree) FROM dbo.score)
47、SELECT A.,B.* FROM dbo.score A
LEFT JOIN dbo.student B ON B.sno = A.sno
WHERE A.degree>(SELECT AVG(degree) FROM dbo.score)

    原文作者:SQL
    原文地址: https://blog.csdn.net/mo_qingli/article/details/44560415
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞