美文网首页
SQL查询功能实践

SQL查询功能实践

作者: 数据分析_在路上 | 来源:发表于2017-11-24 11:24 被阅读0次

准备数据

建表语句 6127d7a3c4e68587c4339bac5e87549.png
插入数据
180de96cf05c5a82e48cb4f1963a7c8.png

题目及解析
1、 查询Student表中的所有记录的Sname、Ssex和Class列。

SELECT sname,ssex,class
FROM students;

2、 查询教师所有的单位即不重复的Depart列。

SELECT DISTINCT depart
FROM teachers;

3、 查询Student表的所有记录。

SELECT *
FROM students;

4、查询Score表中成绩在60到80之间的所有记录。

SELECT *
FROM Scores
WHERE Degree BETWEEN 60 AND 80;

5、查询Score表中成绩为85,86或88的记录。

SELECT *
FROM scores
WHERE degree in (85,86,88);

6、查询Student表中“95031”班或性别为“女”的同学记录。

SELECT *
FROM students
WHERE class='95031' or ssex='女';

7、以Class降序查询Student表的所有记录。

SELECT *
FROM students
ORDER BY class DESC;

8、以Cno升序、Degree降序查询Score表的所有记录。

SELECT *
FROM scores
ORDER BY cno,degree DESC;

9、查询“95031”班的学生人数。

SELECT COUNT(sno) AS studnum
FROM students
WHERE class='95031';

10、查询Score表中的最高分的学生学号和课程号。

SELECT sno,cno
FROM scores
ORDER BY degree DESC
LIMIT 1;

11、查询‘3-105’号课程的平均分。

SELECT AVG(degree) AS avgdeg
FROM scores
WHERE cno='3-105';

12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

SELECT cno,AVG(degree)
FROM scores
WHERE cno LIKE '3%'
GROUP BY cno
HAVING count(sno)>=5;

13、查询最低分大于70,最高分小于90的Sno列。

SELECT sno
FROM scores
GROUP BY sno
HAVING Min(degree)>70 and Max(degree)<90;

14、查询所有学生的Sname、Cno和Degree列。

SELECT (SELECT sname FROM students WHERE students.sno=scores.sno)AS SEsname,cno,degree
FROM scores
ORDER BY SEsname;

SELECT sname,cno,degree
FROM students INNER JOIN scores
ON students.sno=scores.sno
ORDER BY sname;

15、查询所有学生的Sno、Cname和Degree列。

SELECT sno,cname,degree
FROM scores INNER JOIN courses
ON scores.cno=courses.cno
ORDER BY sno;

16、查询所有学生的Sname、Cname和Degree列。

SELECT sname,cname,degree
FROM students INNER JOIN scores
ON students.sno=scores.sno INNER JOIN courses
ON scores.cno=courses.cno
ORDER BY sname;

17、查询“95033”班所选课程的平均分。

SELECT Cname,AVG(Degree)
FROM Students INNER JOIN Scores
ON(Students.Sno=Scores.Sno) INNER JOIN Courses
ON(Scores.Cno=Courses.Cno)
WHERE Class='95033'
GROUP BY Courses.Cname
ORDER BY Cname;

18、假设使用如下命令建立了一个grade表:


0351476375e714952a4be81ea210a2b.png

查询所有同学的Sno、Cno和rank列。

SELECT sno,cno,rank
FROM scores INNER JOIN grade
ON scores.degree>=grade.low and scores.degree<=grade.upp
ORDER BY sno;

19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。

SELECT s1.sno,s1.degree
FROM scores AS s1 INNER JOIN scores AS s2
ON s1.cno=s2.cno and s1.degree>s2.degree
WHERE s1.cno='3-105' and s2.sno='109'
ORDER BY s1.sno;

SELECT s1.sno,s1.degree
FROM scores AS s1,scores AS s2
WHERE s1.cno=s2.cno and s1.degree>s2.degree and s1.cno='3-105' and s2.sno='109'
ORDER BY s1.sno;

20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。

SELECT *
FROM Scores
GROUP BY Sno
HAVING COUNT(cno)>1 AND Degree!=MAX(Degree);

21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。

SELECT s1.sno,s1.degree
FROM scores AS s1 INNER JOIN scores AS s2
ON s1.cno=s2.cno and s1.degree>s2.degree
WHERE s1.cno='3-105' and s2.sno='109'
ORDER BY s1.sno;

22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

SELECT s1.sno,s1.sname,s1.sbirthday
FROM students AS s1 INNER JOIN students AS s2
ON YEAR(s1.sbirthday)=YEAR(s2.`sbirthday`)
WHERE s2.sno='108';

23、查询“张旭“教师任课的学生成绩。

SELECT sno,degree
FROM teachers,courses,scores
WHERE teachers.tno=courses.tno and courses.cno=scores.cno and teachers.tno='856'
ORDER BY sno;

SELECT Sno,Degree
FROM Scores INNER JOIN Courses
ON Scores.Cno=Courses.Cno  INNER JOIN Teachers
ON Courses.Tno=Teachers.Tno
WHERE Teachers.Tname='张旭';

24、查询选修某课程的同学人数多于5人的教师姓名。

SELECT tname
FROM teachers,courses,scores
WHERE teachers.tno=courses.tno and courses.cno=scores.cno 
GROUP BY tname
HAVING count(sno)>5;

SELECT DISTINCT Tname
FROM Scores INNER JOIN Courses
ON(Scores.Cno=Courses.Cno) INNER JOIN Teachers
ON(Courses.Tno=Teachers.Tno)
WHERE Courses.Cno IN(SELECT Cno FROM Scores GROUP BY Cno HAVING COUNT(Sno)>5);

25、查询95033班和95031班全体学生的记录。

SELECT *
FROM students
WHERE class in('95033','95031')
ORDER BY class;

26、查询存在有85分以上成绩的课程Cno.

SELECT DISTINCT cno
FROM scores
WHERE degree>=85;

27、查询出“计算机系“教师所教课程的成绩表。

SELECT  tname,cname,sname,degree
FROM teachers INNER JOIN courses
ON teachers.tno=course.tno INNER JOIN scores
ON courses.con=scores.con INNER JOIN students
ON scores.sno=students.sno
WHERE teachers.depart='计算机系'
ORDER BY tname,cname,sname DESC;

28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。

SELECT tname,prof
FROM teachers
WHERE depart='计算机系' and prof NOT IN
(SELECT DISTINCT prof FROM teachers WHERE depart='电子工程系');

29、查询选修编号为“3-105“课程且成绩至少高于任意选修编号为“3-245”的同学的成绩的Cno、Sno和Degree,并按Degree从高到低次序排序。

SELECT cno,sno,degree
FROM scores
WHERE cno='3-105' and degree > ANY
(SELECT degree FROM scores WHERE cno='3-245')
ORDER BY degree DESC;

30、查询选修编号为“3-105”且成绩高于所有选修编号为“3-245”课程的同学的Cno、Sno和Degree.

SELECT cno,sno,degree
FROM scores
WHERE cno='3-105' and degree > ALL
(SELECT degree FROM scores WHERE cno='3-245')
ORDER BY degree DESC;

31、查询所有教师和同学的name、sex和birthday.

SELECT tname,tsex,tbirthday
FROM teachers
union
SELECT sname,ssex,sbirthday
FROM students;

32、查询所有“女”教师和“女”同学的name、sex和birthday.

SELECT tname,tsex,tbirthday
FROM teachers
WHERE tsex='女'
union
SELECT sname,ssex,sbirthday
FROM students
WHERE ssex='女';

33、查询成绩比该课程平均成绩低的同学的成绩表。

SELECT s1.*
FROM Scores AS s1 INNER JOIN (
    SELECT Cno,AVG(Degree) AS aDegree
    FROM Scores
    GROUP BY Cno) s2
ON(s1.Cno=s2.Cno AND s1.Degree<s2.aDegree); 

34、查询所有任课教师的Tname和Depart.

SELECT tname,depart
FROM teachers
WHERE tno IN (SELECT tno FROM courses);

35、查询所有未讲课的教师的Tname和Depart.

SELECT tname,depart
FROM teachers
WHERE tno NOT IN (SELECT tno FROM courses);

36、查询至少有2名男生的班号。

SELECT class,count(ssex) AS boycount
FROM students
WHERE ssex='男'
GROUP BY class
HAVING boycount>=2;

37、查询Student表中不姓“王”的同学记录。

SELECT *
FROM students
WHERE sname NOT LIKE '王%';

38、查询Student表中每个学生的姓名和年龄。

SELECT sname,YEAR(NOW())-YEAR(sbirthday) AS sage
FROM students;

39、查询Student表中最大和最小的Sbirthday日期值。

SELECT MAX(sbirthday),MIN(sbirthday)
FROM students;

40、以班号和年龄从大到小的顺序查询Student表中的全部记录。

SELECT *
FROM students
ORDER BY class DESC,sbirthday DESC;

41、查询“男”教师及其所上的课程。

SELECT tname,cname
FROM teachers INNER JOIN courses
ON teachers.tno=courses.tno
WHERE tsex='男';

42、查询最高分同学的Sno、Cno和Degree列。

SELECT *
FROM Scores
GROUP BY Cno
HAVING Degree=Max(Degree);

43、查询和“李军”同性别的所有同学的Sname.

SELECT s1.sname
FROM students AS s1 INNER JOIN students AS s2
ON s1.ssex=s2.ssex
WHERE s2.sname='李军';

44、查询和“李军”同性别并同班的同学Sname.

SELECT s1.sname
FROM students AS s1 INNER JOIN students AS s2
ON s1.ssex=s2.ssex and s1.class=s2.class
WHERE s2.sname='李军';

45、查询所有选修“计算机导论”课程的“男”同学的成绩表

SELECT *
FROM scores
WHERE sno in(SELECT sno FROM students WHERE ssex='男') 
AND cno in(SELECT cno FROM courses WHERE cname='计算机导论');

相关文章

网友评论

      本文标题:SQL查询功能实践

      本文链接:https://www.haomeiwen.com/subject/bovwvxtx.html