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;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.