mysql select查詢語句大全
student
學號,姓名,性別,出生,所在班級
create table student(
sno varchar(20) primary key,
sname varchar(20) not null,
ssex varchar(10) not null,
sbirthday datetime,
sclass varchar(20)
);
teacher
教師編號,教師名字,教師性別,出生,職稱,所在部門
create table teacher(
tno varchar(20) primary key,
tname varchar(20) not null,
tsex varchar(10) not null,
tbirthday datetime,
prof varchar(20) not null,
depart varchar(20) not null
course
課程號,課程名稱,教師編號
create table course(
cno varchar(20) primary key,
cname varchar(20) not null,
tno varchar(20) not null,
foreign key(tno) references teacher(tno)
);
score
學號,課程號,成績
create table score(
sno varchar(20) not null,
cno varchar(20) not null,
degree decimal,
foreign key(sno) references student(sno),
foreign key(cno) references course(cno),
primary key(sno, cno)
);
insert into student value('101','曾華','男','1977-09-01','95033');
insert into student value('102','匡明','男','1975-10-02','95031');
insert into student value('103','王麗','女','1977-01-23','95033');
insert into student value('104','李軍','男','1977-02-20','95033');
insert into student value('105','王芳','女','1977-02-10','95031');
insert into student value('106','陸軍','男','1977-06-03','95031');
insert into student value('107','李縮','男','1977-02-21','95033');
insert into student value('108','王解','女','1975-02-13','95031');
insert into student value('109','陸稍','男','1977-06-01','95031');
insert into teacher value('834','李成','男','1958-12-01','副教授','計算機系');
insert into teacher value('856','張旭','男','1968-11-21','講師','電子工程系');
insert into teacher value('825','王品','女','1972-05-05','助教','計算機系');
insert into teacher value('831','劉冰','女','1977-08-14','助教','電子工程系');
insert into course value('3-245','計算機導論','825');
insert into course value('3-105','操作系統','834');
insert into course value('6-166','數字電路','856');
insert into course value('9-888','高等數學','831');
insert into score value('103','3-245','86');
insert into score value('105','3-245','75');
insert into score value('109','3-245','68');
insert into score value('103','3-105','92');
insert into score value('105','3-105','88');
insert into score value('109','3-105','76');
insert into score value('101','3-105','91');
insert into score value('102','3-105','78');
insert into score value('103','6-166','85');
insert into score value('105','6-166','79');
insert into score value('109','6-166','81');
+-----+--------+------+---------------------+--------+
| sno | sname | ssex | sbirthday | sclass |
+-----+--------+------+---------------------+--------+
| 101 | 曾華 | 男 | 1977-09-01 00:00:00 | 95033 |
| 102 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 103 | 王麗 | 女 | 1977-01-23 00:00:00 | 95033 |
| 104 | 李軍 | 男 | 1977-02-20 00:00:00 | 95033 |
| 105 | 王芳 | 女 | 1977-02-10 00:00:00 | 95031 |
| 106 | 陸軍 | 男 | 1977-06-03 00:00:00 | 95031 |
| 107 | 李縮 | 男 | 1977-02-21 00:00:00 | 95033 |
| 108 | 王解 | 女 | 1975-02-13 00:00:00 | 95031 |
| 109 | 陸稍 | 男 | 1977-06-01 00:00:00 | 95031 |
+-----+--------+------+---------------------+--------+
+-----+--------+------+---------------------+-----------+-----------------+
| tno | tname | tsex | tbirthday | prof | depart |
+-----+--------+------+---------------------+-----------+-----------------+
| 825 | 王品 | 女 | 1972-05-05 00:00:00 | 助教 | 計算機系 |
| 831 | 劉冰 | 女 | 1977-08-14 00:00:00 | 助教 | 電子工程系 |
| 834 | 李成 | 男 | 1958-12-01 00:00:00 | 副教授 | 計算機系 |
| 856 | 張旭 | 男 | 1968-11-21 00:00:00 | 講師 | 電子工程系 |
+-----+--------+------+---------------------+-----------+-----------------+
+-------+-----------------+-----+
| cno | cname | tno |
+-------+-----------------+-----+
| 3-105 | 操作系統 | 834 |
| 3-245 | 計算機導論 | 825 |
| 6-166 | 數字電路 | 856 |
| 9-888 | 高等數學 | 831 |
+-------+-----------------+-----+
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 101 | 3-105 | 91 |
| 102 | 3-105 | 78 |
| 103 | 3-105 | 92 |
| 103 | 3-245 | 86 |
| 103 | 6-166 | 85 |
| 105 | 3-105 | 88 |
| 105 | 3-245 | 75 |
| 105 | 6-166 | 79 |
| 109 | 3-105 | 76 |
| 109 | 3-245 | 68 |
| 109 | 6-166 | 81 |
+-----+-------+--------+
1、查詢student表的所有記錄
select * from student;
2、查詢student表中的所有記錄的sname、ssex和sclass列
select sname, ssex, sclass from student;
3、查詢教師所有的單位即不重複的depart列
select distinct depart from teacher; //distinct排重
4、查詢score表中成績在60-80之間的所有記錄
select * from score where degree between 60 and 80;
select * from score where degree > 60 and degree < 80;
5、查詢score表中成績爲85,86或87的記錄
select * from score where degree in(85, 86, 87);
6、查詢student表中"95031"班,或性別爲'女'的同學記錄
select * from student where sclass='95031' or ssex='女';
7、以sclass降序查詢student表中的所有數據//desc降序 asc升序默認是升序的一般不會寫
select * from student order by sclass desc;
8、以cno升序、degree降序查詢score表中的所有記錄//遇到相同的就降序
select * from score order by cno asc ,degree desc;
9、查詢"95031"班級的人數
select count(*) from student where sclass = '95031';
10、查詢score表中最高分的學生學號和課程號。(子查詢或排序)
select sno, cno from score where degree = (select max(degree) from score);
select sno, cno, degree from score order by degree desc limit 0, 1;
如果按照分數排序,select的時候一定要把degree放進去,0表示從第一行開始數一行,如果是1, 2則表示從第二行開始數兩條
11、查詢每門課的平均成績
select * from course;
select avg(degree) from score where cno='3-105';
一門一門查詢
下面是寫在一句話中
select cno, avg(degree) from score group by cno;
12、查詢score表中至少有兩名學生選修的並以3開頭的課程的平均分數
select cno, avg(degree),count(*) from score group by cno having count(cno)>=2 and cno like '3%';
count(*)表示給出這個group的個數,此題也可以不加
13、查詢分數大於70小於90的sno列
select sno,degree from score where degree>70 and degree<90;
select sno,degree from score where degree between 70 and 90;
14、查詢所有學生的sname, cno和degree列
select sno, sname from student;
select sno, cno, degree from score;
需要用第一句話中的sname替換第二行的sno,下面使用多表查尋語句
select sname, cno, degree from student, score where student.sno=score.sno;
15、查尋所有學生的sno, cname和degree
select sno, cname, degree from course, score where course.cno=score.cno;
16、查詢所有學生的sname、cname、degree列(來自三張表)
select sname, cname, degree from student, course, score where student.sno=score.sno and course.cno=score.cno;
select sname, cname, degree, student.sno as stu_sno, course.cno from student, course, score where student.sno=score.sno and course.cno=score.cno;
因爲sno有歧義所以要加student.sno,可以用as去改名
17、查詢95031班學生每門課的平均分
select cno, avg(degree) from score where sno in (select sno from student where sclass='95031') group by cno;
18、查詢選修“3-105”課程的成績高於109號同學“3-105”成績的所有同學記錄
select * from score where cno='3-105' and degree>(select degree from score where sno='109' and cno='3-105');
19、查詢成績高於學號爲"109"、課程號爲"3-105"的成績的所有記錄
select * from score where degree>(select degree from score where sno='109' and cno='3-105');
20、查詢和學號108、101的同學童年出生的所有學生的sno、sname和sbirthday列
select sno,sname,sbirthday from student where year(sbirthday) in (select year(sbirthday) from student where sno in (108,101));
21、查詢“張旭”教師任課的學生成績
select degree from score where cno=(select cno from course where tno=(select tno from teacher where tname='張旭'));
22、查詢選修某課程的同學多於5人的教師姓名
select tname from teacher where tno = (select tno from course where cno=(select cno from score group by cno having count(*) > 5));
23、查詢95033和95031班全體學生記錄
select * from student where sclass in ('95033', '95031');
24、查詢85分以上成績的課程cno
select cno from score where degree>85;
25、查詢出“計算機系”教師所有課程的成績表
select * from score where cno in(select cno from course where tno in (select tno from teacher where depart='計算機系'));
26、查詢計算機系和電子工程系不同職稱的教師的tname和prof(職稱在別的部門沒有出現過)
union求並集
select * from teacher where depart='計算機系' and prof not in (select prof from teacher where depart='電子工程系')
union
select * from teacher where depart='電子工程系' and prof not in (select prof from teacher where depart='計算機系');
27、查詢選修編號爲3-105,且至少高於選修編號爲3-245的同學的信息並按照degree降序排列
(至少表示大於其中任意一個,用any)
select * from score where cno = '3-105' and degree > any(select degree from score where cno = '3-245') order by degree desc;
28、查詢選修編號爲3-105,且成績高於選修編號爲3-245的同學的信息
(且表示大於所有選項,用all)
select * from score where cno = '3-105' and degree > all(select degree from score where cno = '3-245’);
29、查詢所有教師和同學的name、sex和birthday(別名as,默認會寫第一組的名字所以只要改第一組的名字就ok)
select sname as name, ssex as sex, sbirthday as birthdy from student
union
select tname,tsex,tbirthday from teacher;
30、查詢所有女教師和女同學的name、sex和birthday
select sname as name, ssex as sex, sbirthday as birthday from student where ssex = '女'
union
select tname, tsex, tbirthday from teacher where tsex = '女';
31、查詢成績比該課程平均成績低的同學的成績表
select cno, avg(degree) from score group by cno;//查詢一類課程的平均分
select * from score a where degree < (select avg(degree) from score b where a.cno = b.cno);
32、查詢所有任課教師的tname和depart(任課說明在課程表中有課程)
select tname, depart from teacher where tno in (select tno from course);
33、查詢至少有兩名男生的班號(加條件分組篩選)
select sclass from student where ssex = '男' group by sclass having count(*)>1;
34、查詢student表中不姓王的同學記錄
select * from student where sname not like '王%';
35、查詢student表中每個學生的姓名和年齡
年齡=當前年份-出生年份
select sname, year(now())-year(sbirthday) from student;
36、查詢student表中最大最小sbirthday日期值
select sbirthday from student order by sbirthday;
select max(sbirthday) as '最大',min(sbirthday) as '最小' from student;
37、以班號和年齡從大到小排序查詢student表中的全部記錄
select * from student order by sclass desc, year(now)-year(sbirthday) desc;
select * from student order by sclass desc, sbirthday;
38、查詢男教師及其所教的課程
select cname from course where tno in (select tno from teacher where tsex='男');
39、查詢最高分同學的sno、cno和degree列
select * from score where degree = (select max(degree) from score);
40、查詢和李軍同性別的所有同學的sname
select sname from student where ssex = (select ssex from student where sname = '李軍');
41、查詢和李軍同性別並且同班的同學的sname
select sname from student where ssex = (select ssex from student where sname = '李軍') and sclass = (select sclass from student where sname = '李軍');
42、查詢所有選修計算機導論課程的男同學的成績表
select * from score where sno in (select sno from student where ssex = '男') and cno = (select cno from course where cname = '計算機導論');
43、假設使用如下命令建立了一個grade表:
create table grade(
low int(3),
upp int(3),
grade 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(50,59,'E');
查詢所有同學的sno、cno和grade列
select sno,cno,grade from score,grade where degree between low and upp;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.