建表語句:
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);
準備數(shù)據(jù):
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' ,'操作系統(tǒng)' ,804);
INSERT INTO COURSES(CNO,CNAME,TNO)VALUES ('6-166' ,'數(shù)據(jù)電路' ,856);
INSERT INTO COURSES(CNO,CNAME,TNO)VALUES ('9-888' ,'高等數(shù)學' ,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表中成績?yōu)?5棵里,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”班的學生人數(shù)截驮。
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開頭的課程的平均分數(shù)坡锡。
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 | 操作系統(tǒng) | 86.0 |
| 105 | 操作系統(tǒng) | 75.0 |
| 107 | 計算機導論 | 91.0 |
| 108 | 計算機導論 | 78.0 |
| 109 | 操作系統(tǒng) | 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 |
| 陸君 | 操作系統(tǒng) | 86.0 |
| 匡明 | 操作系統(tǒng) | 75.0 |
| 王麗 | 計算機導論 | 91.0 |
| 曾華 | 計算機導論 | 78.0 |
| 王芳 | 操作系統(tǒng) | 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 | 數(shù)據(jù)電路 | 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;
現(xiàn)查詢所有同學的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中選學一門以上課程的同學中分數(shù)為非最高分成績的記錄如叼。
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追葡、查詢選修某課程的同學人數(shù)多于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 |
+--------+--------------+
| 張旭 | 數(shù)據(jù)電路 |
| 李誠 | 操作系統(tǒng) |
+--------+--------------+
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)