drop table students
删除数据表
https://blog.csdn.net/yamatou/article/details/82760286
查询Student表中的所有记录的Sname、Ssex和Class列。
答:mysql> select sname,ssex,class from students;
+-------+------+-------+
| sname | ssex | class |
+-------+------+-------+
| 曾华 | 男 | 95033 |
| 匡明 | 男 | 95031 |
| 王丽 | 女 | 95033 |
| 李军 | 男 | 95033 |
| 王芳 | 女 | 95031 |
| 陆君 | 男 | 95031 |
+-------+------+-------+
查询教师所有的单位即不重复的Depart列。
mysql> select depart from teachers group by depart;
+------------+
| depart |
+------------+
| 电子工程系 |
| 计算机系 |
+------------+
查询Student表的所有记录。
mysql> select * from students;
+-----+-------+------+---------------------+-------+
| sno | sname | ssex | sbirthday | class |
+-----+-------+------+---------------------+-------+
| 108 | 曾华 | 男 | 1977-09-01 00:00:00 | 95033 |
| 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 107 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 |
| 101 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 |
| 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
| 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
+-----+-------+------+---------------------+-------+
查询Score表中成绩在60到80之间的所有记录。
mysql> select * from scores where degree between 60 and 80;
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 105 | 3-245 | 75.0 |
| 109 | 3-245 | 68.0 |
| 109 | 3-105 | 76.0 |
| 101 | 3-105 | 64.0 |
| 108 | 3-105 | 78.0 |
| 107 | 6-106 | 79.0 |
+-----+-------+--------+
查询Score表中成绩为85,86或88的记录。
mysql> select * from scores where degree = 85 or degree = 86 or degree = 88;
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 103 | 3-245 | 86.0 |
| 105 | 3-105 | 88.0 |
| 101 | 6-166 | 85.0 |
查询Student表中“95031”班或性别为“女”的同学记录。
mysql> select * from students where class = 95031 or ssex = "女";
+-----+-------+------+---------------------+-------+
| sno | sname | ssex | sbirthday | class |
+-----+-------+------+---------------------+-------+
| 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 107 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 |
| 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
| 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
+-----+-------+------+---------------------+-------+
以Class降序查询Student表的所有记录。
mysql> select * from students order by class desc;
+-----+-------+------+---------------------+-------+
| sno | sname | ssex | sbirthday | class |
+-----+-------+------+---------------------+-------+
| 108 | 曾华 | 男 | 1977-09-01 00:00:00 | 95033 |
| 107 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 |
| 101 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 |
| 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
| 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
+-----+-------+------+---------------------+-------+
以Cno升序、Degree降序查询Score表的所有记录。
mysql> select * from scores order by cno asc,degree desc;
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 103 | 3-105 | 92.0 |
| 107 | 3-105 | 91.0 |
| 105 | 3-105 | 88.0 |
| 108 | 3-105 | 78.0 |
| 109 | 3-105 | 76.0 |
| 101 | 3-105 | 64.0 |
| 103 | 3-245 | 86.0 |
| 105 | 3-245 | 75.0 |
| 109 | 3-245 | 68.0 |
| 107 | 6-106 | 79.0 |
| 101 | 6-166 | 85.0 |
| 108 | 6-166 | 81.0 |
+-----+-------+--------+
查询“95031”班的学生人数。
mysql> select count(1) from students where class = 95031;
+----------+
| count(1) |
+----------+
| 3 |
+----------+
查询Score表中的最高分的学生学号和课程号。
mysql> select sno,cno from scores where degree=(select max(degree) from scores);
+-----+-------+
| sno | cno |
+-----+-------+
| 103 | 3-105 |
+-----+-------+
查询‘3-105’号课程的平均分。
mysql> select avg(degree) from scores where cno = "3-105";
+-------------+
| avg(degree) |
+-------------+
| 81.50000 |
+-------------+
查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
SELECT Cno,AVG(Degree) FROM Scores WHERE Cno LIKE '3%' GROUP BY Cno HAVING COUNT(Sno) >= 5;
查询最低分大于70,最高分小于90的Sno列。
mysql> select sno,degree from scores where degree between 70 and 90;
+-----+--------+
| sno | degree |
+-----+--------+
| 103 | 86.0 |
| 105 | 75.0 |
| 105 | 88.0 |
| 109 | 76.0 |
| 108 | 78.0 |
| 101 | 85.0 |
| 107 | 79.0 |
| 108 | 81.0 |
+-----+--------+
查询所有学生的Sname、Cno和Degree列。
mysql> select Sname,Cno,Degree from students left join scores on students.sno = scores.sno;
+-------+-------+--------+
| Sname | Cno | Degree |
+-------+-------+--------+
| 曾华 | 3-105 | 78.0 |
| 曾华 | 6-166 | 81.0 |
| 匡明 | 3-245 | 75.0 |
| 匡明 | 3-105 | 88.0 |
| 王丽 | 3-105 | 91.0 |
| 王丽 | 6-106 | 79.0 |
| 李军 | 3-105 | 64.0 |
| 李军 | 6-166 | 85.0 |
| 王芳 | 3-245 | 68.0 |
| 王芳 | 3-105 | 76.0 |
| 陆君 | 3-245 | 86.0 |
| 陆君 | 3-105 | 92.0 |
+-------+-------+--------+
查询所有学生的Sno、Cname和Degree列。
mysql> SELECT Sno,Cname,Degree FROM Scores INNER JOIN Courses ON(Scores.Cno=Courses.Cno) ;
+-----+------------+--------+
| Sno | Cname | Degree |
+-----+------------+--------+
| 103 | 操作系统 | 86.0 |
| 105 | 操作系统 | 75.0 |
| 109 | 操作系统 | 68.0 |
| 103 | 计算机导论 | 92.0 |
| 105 | 计算机导论 | 88.0 |
| 109 | 计算机导论 | 76.0 |
| 101 | 计算机导论 | 64.0 |
| 107 | 计算机导论 | 91.0 |
| 108 | 计算机导论 | 78.0 |
| 101 | 数据电路 | 85.0 |
| 108 | 数据电路 | 81.0 |
+-----+------------+--------+
查询所有学生的Sname、Cname和Degree列。
mysql> SELECT Sname,Cname,Degree FROM Students left JOIN Scores ON(Students.Sno=Scores.Sno) left JOIN Courses ON(Scores.Cno=Courses.Cno) ;
+-------+------------+--------+
| Sname | Cname | Degree |
+-------+------------+--------+
| 曾华 | 计算机导论 | 78.0 |
| 曾华 | 数据电路 | 81.0 |
| 匡明 | 操作系统 | 75.0 |
| 匡明 | 计算机导论 | 88.0 |
| 王丽 | 计算机导论 | 91.0 |
| 王丽 | NULL | 79.0 |
| 李军 | 计算机导论 | 64.0 |
| 李军 | 数据电路 | 85.0 |
| 王芳 | 操作系统 | 68.0 |
| 王芳 | 计算机导论 | 76.0 |
| 陆君 | 操作系统 | 86.0 |
| 陆君 | 计算机导论 | 92.0 |
+-------+------------+--------+
查询“95033”班所选课程的平均分。
mysql> SELECT cname,avg(Degree) FROM Students left JOIN Scores ON (Students.Sno=Scores.Sno) left JOIN Courses ON (Scores.Cno=Courses.Cno) where class=95033 group by cname;
+------------+-------------+
| cname | avg(Degree) |
+------------+-------------+
| NULL | 79.00000 |
| 数据电路 | 83.00000 |
| 计算机导论 | 77.66667 |
+------------+-------------+
18、假设使用如下命令建立了一个grade表:*******
现查询所有同学的Sno、Cno和rank列。
mysql> SELECT Sno,Cno,rank FROM Scores INNER JOIN grade ON (Scores.Degree >= grade.low AND Scores.Degree <= grade.upp );
+-----+-------+------+
| Sno | Cno | rank |
+-----+-------+------+
| 101 | 3-105 | D |
| 101 | 6-166 | B |
| 103 | 3-105 | A |
| 103 | 3-245 | B |
| 105 | 3-105 | B |
| 105 | 3-245 | C |
| 107 | 3-105 | A |
| 107 | 6-106 | C |
| 108 | 6-166 | B |
| 108 | 3-105 | C |
| 109 | 3-105 | C |
| 109 | 3-245 | D |
+-----+-------+------+
查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
mysql> 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';
Select * from students left join scores on (Scores.sno=students.sno) where degree >
(Select degree from students left join scores on (Scores.sno=students.sno) where
(cno='3-105' and students.sno='109' ));
+-----+-------+------+---------------------+-------+------+-------+--------+
| sno | sname | ssex | sbirthday | class | sno | cno | degree |
+-----+-------+------+---------------------+-------+------+-------+--------+
| 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 | 103 | 3-245 | 86.0 |
| 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 | 103 | 3-105 | 92.0 |
| 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 | 105 | 3-105 | 88.0 |
| 107 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 | 107 | 3-105 | 91.0 |
| 108 | 曾华 | 男 | 1977-09-01 00:00:00 | 95033 | 108 | 3-105 | 78.0 |
| 101 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 | 101 | 6-166 | 85.0 |
| 107 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 | 107 | 6-106 | 79.0 |
| 108 | 曾华 | 男 | 1977-09-01 00:00:00 | 95033 | 108 | 6-166 | 81.0 |
+-----+-------+------+---------------------+-------+------+-------+--------+
查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
SELECT sc1.* from scores sc1 INNER JOIN scores sc2 on (sc1.sno=sc2.sno and sc1.degree<sc2.degree) ORDER BY sc1.sno;
mysql> SELECT sc1.* from scores sc1 INNER JOIN scores sc2 on sc1.sno=sc2.sno and sc1.degree<sc2.degree;
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 103 | 3-245 | 86.0 |
| 105 | 3-245 | 75.0 |
| 109 | 3-245 | 68.0 |
| 107 | 6-106 | 79.0 |
| 101 | 3-105 | 64.0 |
| 108 | 3-105 | 78.0 |
+-----+-------+--------+
查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
Select s1.* from scores s1 inner join scores s2 on (s1.cno=s2.cno and s1.degree >s2.degree) where (s2.sno=’109’ and s2.cno=’3-105’);
mysql> select * from scores where degree>(select degree from scores where sno=109 and cno='3-105')and cno='3-105';
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 103 | 3-105 | 92.0 |
| 105 | 3-105 | 88.0 |
| 107 | 3-105 | 91.0 |
| 108 | 3-105 | 78.0 |
查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
SELECT s1.Sno,s1.Sname,s1.Sbirthday FROM Students s1 INNER JOIN Students s2 ON(YEAR(s1.Sbirthday)=YEAR(s2.Sbirthday)) WHERE s2.Sno='108'
mysql> select sno ,sname,sbirthday from students where year(sbirthday)=( select year(sbirthday) from students where sno=108);
+-----+-------+---------------------+
| sno | sname | sbirthday |
+-----+-------+---------------------+
| 108 | 曾华 | 1977-09-01 00:00:00 |
+-----+-------+---------------------+
查询“张旭“教师任课的学生成绩。
Select st.sno,st.sname,s.degree from teachers t inner join courses c on(t.tno=c.tno) inner join scores s on(s.cno=c.cno) inner join students st on(st.sno=s.sno)where t.tname=”张旭”;
查询选修某课程的同学人数多于5人的教师姓名。
Select * from teachers t inner join courses c on(t.tno=c.tno) inner join scores s on(s.cno=c.cno) inner join students st on(st.sno=s.sno) group by t.tname having count(s.cno)>=5;
select * from scores sc LEFT JOIN courses co on (sc.cno=co.cno) LEFT JOIN teachers tea on tea.tno=co.tno GROUP BY sc.cno HAVING COUNT(sc.sno)>5
查询95033班和95031班全体学生的记录。
Select * from students where class in (95033,95031);
查询存在有85分以上成绩的课程Cno.
Select * from scores group by cno having degree >85;
查询出“计算机系“教师所教课程的成绩表。
Select * from teachers t inner join courses c on(t.tno=c.tno) inner join scores s on(s.cno=c.cno) inner join students st on(st.sno=s.sno) where t.depart=”计算机系”;
查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
Select * from teachers t order by depart,prof;
SELECT Tname,Prof FROM Teachers WHERE Depart='计算机系' AND Prof NOT IN(
SELECT DISTINCT Prof FROM Teachers
WHERE Depart='电子工程系');
查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
Select s1.Cno,s1.Sno,s1.Degree from scores s1 inner join scores s2 on(s1.degree>s2.degree)where s2.cno=’3-245’order by s1.degree desc;
查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
mysql> select sc1.* from scores sc1 inner join scores sc2 on (sc1.cno='3-105' and sc1.degree>sc2.degree and sc1.sno=sc2.sno)where (sc2.cno='3-245');
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 103 | 3-105 | 92.0 |
| 105 | 3-105 | 88.0 |
| 109 | 3-105 | 76.0 |
+-----+-------+--------+
31、查询所有教师和同学的name、sex和birthday.
32、查询所有“女”教师和“女”同学的name、sex和birthday.
33、查询成绩比该课程平均成绩低的同学的成绩表。
select avg(degree) from scores;
查询所有任课教师的Tname和Depart.
select te.tname,te.depart from teachers te inner join courses co on te.tno=co.tno;
+-------+------------+
| tname | depart |
+-------+------------+
| 王萍 | 计算机系 |
| 李诚 | 计算机系 |
| 张旭 | 电子工程系 |
+-------+------------+
35 查询所有未讲课的教师的Tname和Depart.
select te.tname,te.depart from teachers te left join courses co on te.tno=co.tno where te.tname not in (select te.tname depart from teachers te inner join courses co on te.tno=co.tno);
+-------+------------+
| tname | depart |
+-------+------------+
| 刘冰 | 电子工程系 |
+-------+------------+
查询至少有2名男生的班号。
Select distinct st1.class from students st1 inner join students st2 on( st1.class=st2.class and st1.sname!=st2.sname and st1.ssex='男') where st2.ssex='男';
+-------+
| class |
+-------+
| 95033 |
| 95031 |
+-------+
查询Student表中不姓“王”的同学记录。
Select st.* from students st where sname not like '王%';
+-----+-------+------+---------------------+-------+
| sno | sname | ssex | sbirthday | class |
+-----+-------+------+---------------------+-------+
| 108 | 曾华 | 男 | 1977-09-01 00:00:00 | 95033 |
| 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 101 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 |
| 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
+-----+-------+------+---------------------+-------+
查询Student表中每个学生的姓名和年龄。
查询Student表中最大和最小的Sbirthday日期值。
Select max(Sbirthday) ,min(Sbirthday) from students;
+---------------------+---------------------+
| max(Sbirthday) | min(Sbirthday) |
+---------------------+---------------------+
| 1977-09-01 00:00:00 | 1974-06-03 00:00:00 |
+---------------------+---------------------+
以班号和年龄从大到小的顺序查询Student表中的全部记录。
Select st.* from students st order by class desc ,Sbirthday;
+-----+-------+------+---------------------+-------+
| sno | sname | ssex | sbirthday | class |
+-----+-------+------+---------------------+-------+
| 107 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 |
| 101 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 |
| 108 | 曾华 | 男 | 1977-09-01 00:00:00 | 95033 |
| 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
| 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
| 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
+-----+-------+------+---------------------+-------+
查询“男”教师及其所上的课程。
42、查询最高分同学的Sno、Cno和Degree列。
43、查询和“李军”同性别的所有同学的Sname.
44、查询和“李军”同性别并同班的同学Sname.
45、查询所有选修“计算机导论”课程的“男”同学的成绩表
Select sc.*,st.*,co.* from scores sc inner join students st on sc.sno=st.sno inner join courses co on co.cno=sc.cno where (st.ssex='男'and co.cname='计算机导论');
+-----+-------+--------+-----+-------+------+---------------------+-------+-------+------------+-----+
| sno | cno | degree | sno | sname | ssex | sbirthday | class | cno | cname | tno |
+-----+-------+--------+-----+-------+------+---------------------+-------+-------+------------+-----+
| 103 | 3-105 | 92.0 | 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 | 3-105 | 计算机导论 | 825 |
| 105 | 3-105 | 88.0 | 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 | 3-105 | 计算机导论 | 825 |
| 101 | 3-105 | 64.0 | 101 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 | 3-105 | 计算机导论 | 825 |
| 108 | 3-105 | 78.0 | 108 | 曾华 | 男 | 1977-09-01 00:00:00 | 95033 | 3-105 | 计算机导论 | 825 |
+-----+-------+--------+-----+-------+------+---------------------+-------+-------+------------+-----+