建表语句:
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)
网友评论