sql查询练习

建表语句:

CREATE TABLE students
(sno VARCHAR(3) NOT NULL, 
sname VARCHAR(4) NOT NULL,
ssex VARCHAR(2) NOT NULL, 
sbirthday DATETIME,
class VARCHAR(5));

CREATE TABLE courses
(cno VARCHAR(5) NOT NULL, 
cname VARCHAR(10) NOT NULL, 
tno VARCHAR(10) NOT NULL);



CREATE TABLE scores 
(sno VARCHAR(3) NOT NULL, 
cno VARCHAR(5) NOT NULL, 
degree NUMERIC(10, 1) NOT NULL);

CREATE TABLE teachers 
(tno VARCHAR(3) NOT NULL, 
tname VARCHAR(4) NOT NULL, tsex VARCHAR(2) NOT NULL, 
tbirthday DATETIME NOT NULL, prof VARCHAR(6), 
depart VARCHAR(10) NOT NULL);

准备数据:

INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,'曾华' ,'男' ,'1977-09-01',95033);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,'匡明' ,'男' ,'1975-10-02',95031);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,'王丽' ,'女' ,'1976-01-23',95033);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,'李军' ,'男' ,'1976-02-20',95033);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,'王芳' ,'女' ,'1975-02-10',95031);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,'陆君' ,'男' ,'1974-06-03',95031);

INSERT INTO COURSES(CNO,CNAME,TNO)VALUES ('3-105' ,'计算机导论',825);
INSERT INTO COURSES(CNO,CNAME,TNO)VALUES ('3-245' ,'操作系统' ,804);
INSERT INTO COURSES(CNO,CNAME,TNO)VALUES ('6-166' ,'数据电路' ,856);
INSERT INTO COURSES(CNO,CNAME,TNO)VALUES ('9-888' ,'高等数学' ,100);

INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (103,'3-245',86);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (105,'3-245',75);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (109,'3-245',68);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (103,'3-105',92);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (105,'3-105',88);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (109,'3-105',76);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (101,'3-105',64);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (107,'3-105',91);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (108,'3-105',78);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (101,'6-166',85);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (107,'6-106',79);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (108,'6-166',81);

INSERT INTO TEACHERS(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (804,'李诚','男','1958-12-02','副教授','计算机系');
INSERT INTO TEACHERS(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (856,'张旭','男','1969-03-12','讲师','电子工程系');
INSERT INTO TEACHERS(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (825,'王萍','女','1972-05-05','助教','计算机系');
INSERT INTO TEACHERS(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (831,'刘冰','女','1977-08-14','助教','电子工程系');

mysql> show tables;
+-------------------+
| Tables_in_db_demo |
+-------------------+
| courses           |
| scores            |
| students          |
| teachers          |
+-------------------+
4 rows in set (0.00 sec)

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

mysql> select distinct Sname,Ssex,Class from students;
+--------+------+-------+
| Sname  | Ssex | Class |
+--------+------+-------+
| 曾华   | 男   | 95033 |
| 匡明   | 男   | 95031 |
| 王丽   | 女   | 95033 |
| 李军   | 男   | 95033 |
| 王芳   | 女   | 95031 |
| 陆君   | 男   | 95031 |
+--------+------+-------+
6 rows in set (0.00 sec)

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

mysql> select distinct depart from teachers;
+-----------------+
| depart          |
+-----------------+
| 计算机系        |
| 电子工程系      |
+-----------------+
2 rows in set (0.00 sec)

3、 查询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 |
| 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 |
+-----+--------+------+---------------------+-------+
12 rows in set (0.00 sec)


4、 查询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 |
+-----+-------+--------+
6 rows in set (0.01 sec)

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

mysql> select * from scores where degree in (85,86,88);
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-245 |   86.0 |
| 103 | 3-245 |   86.0 |
| 105 | 3-105 |   88.0 |
| 101 | 6-166 |   85.0 |
+-----+-------+--------+
4 rows in set (0.00 sec)

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

mysql> select sname,ssex,class from students where class = '95031'  or ssex ='女' group by sname;
+--------+------+-------+
| sname  | ssex | Class |
+--------+------+-------+
| 匡明   | 男   | 95031 |
| 王丽   | 女   | 95033 |
| 王芳   | 女   | 95031 |
| 陆君   | 男   | 95031 |
+--------+------+-------+
4 rows in set (0.00 sec)

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

mysql> select sno,sname ,class from students group by sno order by class desc;
+-----+--------+-------+
| sno | sname  | class |
+-----+--------+-------+
| 108 | 曾华   | 95033 |
| 107 | 王丽   | 95033 |
| 101 | 李军   | 95033 |
| 103 | 陆君   | 95031 |
| 105 | 匡明   | 95031 |
| 109 | 王芳   | 95031 |
+-----+--------+-------+
6 rows in set (0.00 sec)

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

mysql> select cno,degree from scores order by cno,degree desc;
+-------+--------+
| cno   | degree |
+-------+--------+
| 3-105 |   92.0 |
| 3-105 |   91.0 |
| 3-105 |   88.0 |
| 3-105 |   78.0 |
| 3-105 |   76.0 |
| 3-105 |   64.0 |
| 3-245 |   86.0 |
| 3-245 |   86.0 |
| 3-245 |   75.0 |
| 3-245 |   68.0 |
| 6-106 |   79.0 |
| 6-166 |   85.0 |
| 6-166 |   81.0 |
+-------+--------+
13 rows in set (0.00 sec)

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


mysql> select count(*) from students where class = 95031;
+----------+
| count(*) |
+----------+
|        6 |
+----------+
1 row in set (0.00 sec)

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

mysql> select max(degree) from scores;
+-------------+
| max(degree) |
+-------------+
|        92.0 |
+-------------+
1 row in set (0.00 sec)

mysql> select * from scores where degree = (select max(degree) from scores);
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-105 |   92.0 |
+-----+-------+--------+
1 row in set (0.00 sec)

mysql>

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

mysql> select avg(DEGREE) from SCORES  where cno = '3-105';
+-------------+
| avg(DEGREE) |
+-------------+
|    81.50000 |
+-------------+
1 row in set (0.00 sec)

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

mysql> select cno ,avg(degree) from scores where cno like '3%' group by cno having count(1)>5;
+-------+-------------+
| cno   | avg(degree) |
+-------+-------------+
| 3-105 |    81.50000 |
+-------+-------------+
1 row in set (0.00 sec)

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

mysql> select sc.sno from scores sc,students st where st.sno=sc.sno group by sc.sno having min(sc.degree)>70 and max(sc.degree)<90;
+-----+
| sno |
+-----+
| 105 |
| 108 |
+-----+
2 rows in set (0.00 sec)

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

mysql>  select sname,cno,degree from students as std left join scores sc on std.sno = sc.sno group by sname;
+--------+-------+--------+
| sname  | cno   | degree |
+--------+-------+--------+
| 匡明   | 3-245 |   75.0 |
| 曾华   | 3-105 |   78.0 |
| 李军   | 3-105 |   64.0 |
| 王丽   | 3-105 |   91.0 |
| 王芳   | 3-245 |   68.0 |
| 陆君   | 3-245 |   86.0 |
+--------+-------+--------+
6 rows in set (0.00 sec)

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

mysql> select students.sno,courses.cname ,scores.degree from students ,courses,scores where students.sno =  scores.sno and courses.cno = scores.cno group by students.sno;
+-----+-----------------+--------+
| sno | cname           | degree |
+-----+-----------------+--------+
| 101 | 计算机导论      |   64.0 |
| 103 | 操作系统        |   86.0 |
| 105 | 操作系统        |   75.0 |
| 107 | 计算机导论      |   91.0 |
| 108 | 计算机导论      |   78.0 |
| 109 | 操作系统        |   68.0 |
+-----+-----------------+--------+
6 rows in set (0.00 sec)

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

mysql> select students.sname,courses.cname ,scores.degree from students ,courses,scores where students.sno =  scores.sno and courses.cno = scores.cno group by students.sno;
+--------+-----------------+--------+
| sname  | cname           | degree |
+--------+-----------------+--------+
| 李军   | 计算机导论      |   64.0 |
| 陆君   | 操作系统        |   86.0 |
| 匡明   | 操作系统        |   75.0 |
| 王丽   | 计算机导论      |   91.0 |
| 曾华   | 计算机导论      |   78.0 |
| 王芳   | 操作系统        |   68.0 |
+--------+-----------------+--------+
6 rows in set (0.00 sec)

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

mysql> select cr.cno,cr.cname,avg(sc.degree) from courses cr,scores sc,students st where  st.sno=sc.sno and st.class='95033' and sc.cno=cr.cno group by cr.cno,cr.cname;
+-------+-----------------+----------------+
| cno   | cname           | avg(sc.degree) |
+-------+-----------------+----------------+
| 3-105 | 计算机导论      |       77.66667 |
| 6-166 | 数据电路        |       83.00000 |
+-------+-----------------+----------------+
2 rows in set (0.00 sec)

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

create table grade(low int(3),upp int(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列。


select sno from scores inner join students on scores.sno = students.sno;

select * from students inner join grade where students.

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

mysql> select  distinct st.sno,st.sname,sc.degree from students st,scores sc where sc.cno='3-105' and sc.degree> (select degree from scores where cno='3-105'and sno='109') and st.sno=sc.sno;;
+-----+--------+--------+
| sno | sname  | degree |
+-----+--------+--------+
| 108 | 曾华   |   78.0 |
| 105 | 匡明   |   88.0 |
| 107 | 王丽   |   91.0 |
| 103 | 陆君   |   92.0 |
+-----+--------+--------+
4 rows in set (0.00 sec)

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

mysql> select * from scores where degree < (select max(degree) from scores) having count(*) >1;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-245 |   86.0 |
+-----+-------+--------+
1 row in set (0.00 sec)

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

mysql> select distinct * from scores where degree > all (select degree from scores where sno = '109' and cno = '3-105');
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-245 |   86.0 |
| 103 | 3-105 |   92.0 |
| 105 | 3-105 |   88.0 |
| 107 | 3-105 |   91.0 |
| 108 | 3-105 |   78.0 |
| 101 | 6-166 |   85.0 |
| 107 | 6-106 |   79.0 |
| 108 | 6-166 |   81.0 |
+-----+-------+--------+
8 rows in set (0.00 sec)

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

mysql> select distinct sno,sname ,sbirthday from students where sbirthday = (select distinct sbirthday from students where sno = 108);
+-----+--------+---------------------+
| sno | sname  | sbirthday           |
+-----+--------+---------------------+
| 108 | 曾华   | 1977-09-01 00:00:00 |
+-----+--------+---------------------+
1 row in set (0.00 sec)

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

mysql> select distinct * from scores  where cno = (select distinct cno from  courses inner join teachers  where  teachers.tno = courses.tno and teachers.tname = '张旭');
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 101 | 6-166 |   85.0 |
| 108 | 6-166 |   81.0 |
+-----+-------+--------+
2 rows in set (0.00 sec)

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

mysql> select tname from teachers where tno in (select tno from courses where cno in (select cno from scores group by cno having COUNT(*)>5));
+--------+
| tname  |
+--------+
| 王萍   |
+--------+
1 row in set (0.00 sec)

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

mysql> select * from students where class = '95033' or class = '95031';
+-----+--------+------+---------------------+-------+
| 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 |
| 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 |
+-----+--------+------+---------------------+-------+
12 rows in set (0.00 sec)

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

mysql> select cno from scores where degree > 85;
+-------+
| cno   |
+-------+
| 3-245 |
| 3-245 |
| 3-105 |
| 3-105 |
| 3-105 |
+-------+
5 rows in set (0.00 sec)

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

mysql> select * from scores where cno in (select cno from courses where tno in (select tno from teachers where depart='计算机系'));
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-245 |   86.0 |
| 103 | 3-245 |   86.0 |
| 105 | 3-245 |   75.0 |
| 109 | 3-245 |   68.0 |
| 103 | 3-105 |   92.0 |
| 105 | 3-105 |   88.0 |
| 109 | 3-105 |   76.0 |
| 101 | 3-105 |   64.0 |
| 107 | 3-105 |   91.0 |
| 108 | 3-105 |   78.0 |
+-----+-------+--------+
10 rows in set (0.00 sec)

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

mysql> select * from teachers where depart = '计算机系' or depart = '电子工程系' group by prof ,tname;
+-----+--------+------+---------------------+-----------+-----------------+
| tno | tname  | tsex | tbirthday           | prof      | depart          |
+-----+--------+------+---------------------+-----------+-----------------+
| 804 | 李诚   | 男   | 1958-12-02 00:00:00 | 副教授    | 计算机系        |
| 831 | 刘冰   | 女   | 1977-08-14 00:00:00 | 助教      | 电子工程系      |
| 825 | 王萍   | 女   | 1972-05-05 00:00:00 | 助教      | 计算机系        |
| 856 | 张旭   | 男   | 1969-03-12 00:00:00 | 讲师      | 电子工程系      |
+-----+--------+------+---------------------+-----------+-----------------+
4 rows in set (0.00 sec)

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

mysql> select * from scores where cno='3-105' and degree > all (select degree  from scores where cno='3-245') order by degree desc;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-105 |   92.0 |
| 107 | 3-105 |   91.0 |
| 105 | 3-105 |   88.0 |
+-----+-------+--------+
3 rows in set (0.00 sec)

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

mysql> select * from scores where cno='3-105' and degree > all (select degree from scores where cno='3-245');
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-105 |   92.0 |
| 105 | 3-105 |   88.0 |
| 107 | 3-105 |   91.0 |
+-----+-------+--------+
3 rows in set (0.00 sec)

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

mysql> select distinct sname as name ,ssex as sex ,sbirthday as birthday from students union select distinct tname as name ,tsex as sex ,tbirthday as birthday from teachers ;
+--------+-----+---------------------+
| name   | sex | birthday            |
+--------+-----+---------------------+
| 曾华   | 男  | 1977-09-01 00:00:00 |
| 匡明   | 男  | 1975-10-02 00:00:00 |
| 王丽   | 女  | 1976-01-23 00:00:00 |
| 李军   | 男  | 1976-02-20 00:00:00 |
| 王芳   | 女  | 1975-02-10 00:00:00 |
| 陆君   | 男  | 1974-06-03 00:00:00 |
| 李诚   | 男  | 1958-12-02 00:00:00 |
| 张旭   | 男  | 1969-03-12 00:00:00 |
| 王萍   | 女  | 1972-05-05 00:00:00 |
| 刘冰   | 女  | 1977-08-14 00:00:00 |
+--------+-----+---------------------+
10 rows in set (0.00 sec)

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

mysql> select distinct sname as name,ssex as sex,sbirthday as birthday from students where ssex='女' union select distinct tname as name,tsex as sex,tbirthday as birthday from teachers where tsex='女';
+--------+-----+---------------------+
| name   | sex | birthday            |
+--------+-----+---------------------+
| 王丽   | 女  | 1976-01-23 00:00:00 |
| 王芳   | 女  | 1975-02-10 00:00:00 |
| 王萍   | 女  | 1972-05-05 00:00:00 |
| 刘冰   | 女  | 1977-08-14 00:00:00 |
+--------+-----+---------------------+
4 rows in set (0.00 sec)

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

mysql> select * from scores sc where sc.degree between 0 and(select avg(degree) from scores sc2 where sc.cno=sc2.cno) ;
+-----+-------+--------+
| 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 |
| 108 | 6-166 |   81.0 |
+-----+-------+--------+
7 rows in set (0.00 sec)

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

mysql> select tname,depart from teachers inner join courses on courses.tno = teachers.tno group by tname;
+--------+-----------------+
| tname  | depart          |
+--------+-----------------+
| 张旭   | 电子工程系      |
| 李诚   | 计算机系        |
| 王萍   | 计算机系        |
+--------+-----------------+
3 rows in set (0.00 sec)

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

mysql> select tname,depart from teachers where tno not in  (select teachers.tno from teachers inner join courses on courses.tno = teachers.tno);
+--------+-----------------+
| tname  | depart          |
+--------+-----------------+
| 刘冰   | 电子工程系      |
+--------+-----------------+
1 row in set (0.00 sec)

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

mysql> select class from students where ssex = '男' group by class having count(sno)>=2;
+-------+
| class |
+-------+
| 95031 |
| 95033 |
+-------+
2 rows in set (0.00 sec)

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

mysql> select * from students where sname not like '王%' group by sname;
+-----+--------+------+---------------------+-------+
| sno | sname  | ssex | sbirthday           | class |
+-----+--------+------+---------------------+-------+
| 105 | 匡明   | 男   | 1975-10-02 00:00:00 | 95031 |
| 108 | 曾华   | 男   | 1977-09-01 00:00:00 | 95033 |
| 101 | 李军   | 男   | 1976-02-20 00:00:00 | 95033 |
| 103 | 陆君   | 男   | 1974-06-03 00:00:00 | 95031 |
+-----+--------+------+---------------------+-------+
4 rows in set (0.00 sec)

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

mysql> SELECT sname, ROUND(DATEDIFF(CURDATE(), sbirthday)/365.2422) as age from students group by sname;
+--------+------+
| sname  | age  |
+--------+------+
| 匡明   |   43 |
| 曾华   |   41 |
| 李军   |   42 |
| 王丽   |   42 |
| 王芳   |   43 |
| 陆君   |   44 |
+--------+------+
6 rows in set (0.00 sec)

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

mysql> select max(sbirthday) as maxTime ,  min(sbirthday) as minTime  from students;
+---------------------+---------------------+
| maxTime             | minTime             |
+---------------------+---------------------+
| 1977-09-01 00:00:00 | 1974-06-03 00:00:00 |
+---------------------+---------------------+
1 row in set (0.00 sec)

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

mysql> select * from students  order by class desc , sbirthday asc;
+-----+--------+------+---------------------+-------+
| sno | sname  | ssex | sbirthday           | class |
+-----+--------+------+---------------------+-------+
| 107 | 王丽   | 女   | 1976-01-23 00:00:00 | 95033 |
| 107 | 王丽   | 女   | 1976-01-23 00:00:00 | 95033 |
| 101 | 李军   | 男   | 1976-02-20 00:00:00 | 95033 |
| 101 | 李军   | 男   | 1976-02-20 00:00:00 | 95033 |
| 108 | 曾华   | 男   | 1977-09-01 00:00:00 | 95033 |
| 108 | 曾华   | 男   | 1977-09-01 00:00:00 | 95033 |
| 103 | 陆君   | 男   | 1974-06-03 00:00:00 | 95031 |
| 103 | 陆君   | 男   | 1974-06-03 00:00:00 | 95031 |
| 109 | 王芳   | 女   | 1975-02-10 00:00:00 | 95031 |
| 109 | 王芳   | 女   | 1975-02-10 00:00:00 | 95031 |
| 105 | 匡明   | 男   | 1975-10-02 00:00:00 | 95031 |
| 105 | 匡明   | 男   | 1975-10-02 00:00:00 | 95031 |
+-----+--------+------+---------------------+-------+
12 rows in set (0.00 sec)

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

mysql> select t1.tname ,cr.cname from teachers as t1  left join courses as cr on t1.tno = cr.tno  where tsex = '男' group by t1.tname ;
+--------+--------------+
| tname  | cname        |
+--------+--------------+
| 张旭   | 数据电路     |
| 李诚   | 操作系统     |
+--------+--------------+
2 rows in set (0.00 sec)

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

mysql> select students.sno,courses.cno,scores.degree from students , courses , scores where scores.degree = (select max(degree) from scores)  group by students.sname ;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 105 | 3-105 |   92.0 |
| 108 | 3-105 |   92.0 |
| 101 | 3-105 |   92.0 |
| 107 | 3-105 |   92.0 |
| 109 | 3-105 |   92.0 |
| 103 | 3-105 |   92.0 |
+-----+-------+--------+
6 rows in set (0.00 sec)

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

mysql> select sname from students where ssex = (select ssex from students where sname = '李军' limit 1);
+--------+
| sname  |
+--------+
| 曾华   |
| 匡明   |
| 李军   |
| 陆君   |
| 曾华   |
| 匡明   |
| 李军   |
| 陆君   |
+--------+
8 rows in set (0.00 sec)

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

mysql> select sname from students where ssex = (select ssex from students where sname = '李军' limit 1) and class = (select class from students where sname = '李军' limit 1) group by sname;
+--------+
| sname  |
+--------+
| 曾华   |
| 李军   |
+--------+
2 rows in set (0.00 sec)

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

mysql> select * from students, courses, scores where courses.cname = '计算机导论' and students.ssex = '男' group by sname;
+-----+--------+------+---------------------+-------+-------+-----------------+-----+-----+-------+--------+
| sno | sname  | ssex | sbirthday           | class | cno   | cname           | tno | sno | cno   | degree |
+-----+--------+------+---------------------+-------+-------+-----------------+-----+-----+-------+--------+
| 105 | 匡明   | 男   | 1975-10-02 00:00:00 | 95031 | 3-105 | 计算机导论      | 825 | 103 | 3-245 |   86.0 |
| 108 | 曾华   | 男   | 1977-09-01 00:00:00 | 95033 | 3-105 | 计算机导论      | 825 | 103 | 3-245 |   86.0 |
| 101 | 李军   | 男   | 1976-02-20 00:00:00 | 95033 | 3-105 | 计算机导论      | 825 | 103 | 3-245 |   86.0 |
| 103 | 陆君   | 男   | 1974-06-03 00:00:00 | 95031 | 3-105 | 计算机导论      | 825 | 103 | 3-245 |   86.0 |
+-----+--------+------+---------------------+-------+-------+-----------------+-----+-----+-------+--------+
4 rows in set (0.00 sec)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,753评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,668评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,090评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,010评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,054评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,806评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,484评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,380评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,873评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,021评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,158评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,838评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,499评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,044评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,159评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,449评论 3 374
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,136评论 2 356

推荐阅读更多精彩内容