mysql查询学生表的总人数_mysql数据库查询练习

创建四张数据表

学生表student:学号,姓名,性别,出生年月日,所在班级

教师表teacher:教师编号,教师名,教师性别,出生年月日,职称,所在部门

课程表course:课程号,课程名,教师编号

成绩表score:学号,课程号,成绩

添加信息:

学生表:

insert into student values ('1001','老大','男','1999-08-01','2000');

insert into student values ('1002','老二','女','1999-08-02','2000');

insert into student values ('1003','老三','男','1999-08-03','2001');

insert into student values ('1004','老四','女','1999-08-04','2001');

insert into student values ('1005','老五','男','1999-08-05','2002');

insert into student values ('1006','老六','男','1999-08-06','2002');

insert into student values ('1007','老七','男','1999-08-07','2003');

insert into student values ('1008','老八','女','1999-08-08','2004');

教师表:

insert into teacher values('201','王菊','女','1890-10-11','副教授','计算机系');

insert into teacher values('202','李平','男','1890-10-11','教授','电子工程系');

insert into teacher values('203','张利','男','1890-10-11','副教授','自动化系');

insert into teacher values('204','王霞','女','1890-10-11','讲师','电子工程系');

insert into teacher values('205','刘杰','男','1890-10-11','助教','计算机系');

课程表:

insert into course values ('3-101','计算机基础','201');

insert into course values ('3-102','高等数学','202');

insert into course values ('3-103','操作系统','203');

insert into course values ('3-104','概率论','204');

insert into course values ('3-105','概率论','205');

成绩表:

insert into score values ('1001','3-101','98');

insert into score values ('1001','3-103','97');

insert into score values ('1002','3-101','88');

insert into score values ('1003','3-104','78');

insert into score values ('1004','3-103','98');

insert into score values ('1006','3-101','77');

insert into score values ('1008','3-104','55');

insert into score values ('1001','3-102','98');

insert into score values ('1007','3-105','67')

查询练习:

1.查询student表的所有记录;

select * from student

dd2fad3a1ddddf3462788265b31faede.png

2.查询student表的sname,ssex,class列;

select sname,ssex,class from student

7524d91038c7aac4841353b793ad416b.png

3.查询教师所有的部门,不重复显示

select distinct depart from teacher

157435bea71307e0376a7ad483e72cf1.png

4.查询成绩表中成绩在77-88之间的所有记录

select * from score where degree between '77' and '88'

62e14293a091dc4b38ad61810c50aa57.png

由查询的数据可知,between...and查询的结果包括两端的值

select * from score where degree>77 and degree<88

e4600aaef80e5576b3a28b8fcb0b50bb.png

由查询数据可知,直接使用运算符比较不包含两端数据

5.查询成绩为98,88,或77的记录

select *  from score  where degree = '77' or degree = '88' or degree = '98'

471745d5ea9e0c5b6dc4afad67486ab1.png

select * from score where degree in (77,88,98)  (in表示或者的关系)

64c299e075a6963e8178d240795c5659.png

6.查询学生表中1001班或性别为女的学生记录

select * from student where class='2000' or ssex = '女'

e57823ea9943eebc6330d79f17659b4b.png

7.以class降序查询学生表的所有记录

select * from student order by class desc

1a327c0aaf34c507ba5ea5523ab85bfd.png

8.以cno升序,degree降序查询成绩表的所有记录

select * from score order by cno asc , degree desc

6121db25bb5769cc6eef06a589b1cc62.png

9.查询2000班的所有学生人数

select count(*) from student where class = 2000

0a48a5b623aa2cca01d74116d7f0646d.png

10.查询成绩表中的最高分的学生号和课程号(子查询)

select sno,cno from score where degree=(select max(degree) from score)

48c5693940a5693b6d63f9c4b52f9727.png

11.查询每门课的平均成绩(分组查询group by)

select cno,avg(degree)from score group by cno

2c2efffd7b2d49035e239c39daac72b9.png

12.查询score 表中至少有2名学生选修的并以3开头的课程的平均成绩(具有条件的分组查询group by....having,模糊查询)

select cno,count(cno),avg(degree) from score group by cnohaving count(cno)>=2 and cno like'3%'

13.查询分数大于70,小于90的sno列

select sno,degree from score where degree between 70 and 90

select sno,degree from score where degree>70 and degree<90

131cdffd0d27a831f14e526169fa58f8.png

14.查询所有学生的sname,cno,degree列(多表查询)

select sname,cno,degree from student,score where student.sno = score.sno

9567b09932aadb76cc73bd61ebe8e09e.png

15.查询所有学生的sno,cname,degree的列(多表联查)

select sno,cname,degree from course,score where course.cno = score.cno

e4cd2082c05367fcc2773eafad5daa14.png

16.查询所有学生的sname,cname,degree(三表联查,找到一个中间表,另两个表与中间表产生联系)

select sname,cname,degree from student,course,score where student.sno= score.sno and course.cno = score.cno

2692082eb9a1f923b1b122a3bf6c2204.png

17.查询“2000”班学生每门课的平均成绩

(1)2000班的学生的学号

select* from student where class='2000'select sno from student whereclass='2000'(2)根据(1)学生号查询2000班的学生成绩表

select* from score where sno in (select sno from student where class='2000')

select cno,degree from score where sno in (select sno from student whereclass='2000')

(3)根据(2)中的cno对学科进行分组求平均分

select cno,avg(degree) from score where sno in (select sno from student whereclass='2000') group by cno

64a06bedc27134aceecf509c3d57b758.png

18.查询选修“3-101”课程的成绩高于“1006”号同学选修“3-101”成绩的所有同学的记录

(1)查选“1006”号同学选修3-101课的成绩

select* from score where cno='3-101' and sno='1006'(2)查询选修“3-101”课程的成绩高于“1006”号同学选修“3-101”成绩的所有同学的记录

select* from score where degree>(select degree from score where cno='3-101' and sno='1006')and cno='3-101'

0ffe3a0afbfc569e1f2c0dee994c04fa.png

19.查询成绩高于学号“1006”、课程号为“3-101”的成绩的所有记录

(1)查选“1006”号同学选修3-101课的成绩

select* from score where cno='3-101' and sno='1006'(2)查询成绩高于学号“1006”、课程号为“3-101”的成绩的所有记录

select* from score where degree>(select degree from score where cno='3-101' and sno='1006')

605ed2ad33c696e3ad90a08e86218fd8.png

20.查询学号为1008、1001的同学的同年出生的所有学生的sno\sname\sbrithday列

(1)查询学号为1008、1001的同学的同年出生

select year(sbrithday) from student where sno in ('1001','1008')

(2)查询学号为1008、1001的同学的同年出生的所有学生的sno\sname\sbrithday列

select sno,sname,sbrithday from student where year(sbrithday)=(select year(sbrithday) from student where sno in ('1001','1008'))

3f45552241960a74139b88e139b5f3cd.png

21.查询王菊老师任课的学生成绩

(1)查询王菊老师的课程号

select cno from teacher as t,course as c where t.tno=c.tno

(2)查询王菊老师任课的学生成绩

select sno,degree from score where cno=(select cno from teacher as t,course as c where t.tno=c.tno and tname='王菊')

e45fbd126e5fa6482c2ec9ea6423caa9.png

22.查询选修某课程的学生人数多于2人的教师姓名

(1)根据课程号查询选修人数大于2人的课程号(group by的条件分组查询)

select cno from score group by cno having count(cno)>2(2)根据(1)查询出课程表中的教师号

select tno from course where cno=(select cno from score group by cno having count(cno)>2)

(3)根据教师号查询教师名字

select tname from teacher where tno=(select tno from course where cno =(select cno from score group by cno having count(cno)>2))

7a90483f2145ae20cbf60e46d12a78fa.png

23.查询2000班和2001班的学生记录(或的使用,In,or)

select * from student where class in('2000','2001')

select * from student where class ='2000' or class='2001'

24.查询计算机系老师所教课程的成绩

(1)查询计算机系老师的教师号

select tno from teacher where depart='计算机系'(2)根据教师号查询老师对应的课程号

select cno from course where tno in (select tno from teacher where depart='计算机系')

(3)根据课程号查询成绩

select cno,degree from score where cno in (select cno from course where tno in (select tno from teacher where depart='计算机系'))

d15563a97d6608fd1f78a7dd3f96ee73.png

25.查询计算机系和电子工程系不同职称的教师姓名和职称prof(这两个系中职称相同的舍去)

(1)查询电子工程系的所有老师的职称

select prof from teacher where depart= '计算机系'查询计算机系的所有老师的职称

select prof from teacher where depart= '电子工程系'(2)查询计算机系中与电子工程系中不同的职称

select prof from teacher where depart= '计算机系' and prof not in (select prof from teacher where depart = '电子工程系')

查询电子工程系中与计算机系中不同的职称

select prof from teacher where depart= '电子工程系' and prof not in (select prof from teacher where depart = '计算机系')

(3)使用union将(2)中的语句进行联合(求并集),得出两个系中不同的职称

select prof from teacher where depart= '电子工程系' and prof not in (select prof from teacher where depart = '计算机系')

union

select prof from teacher where depart= '计算机系' and prof not in (select prof from teacher where depart = '电子工程系')

3f2fcb2f91d2375804e15164594d408c.png

26.查询选修编号为“3-101”课程且成绩至少高于选修编号为“3-102”的同学的cno,sno,degree,并按成绩由高到底排列

(1)查询出选修“3-102”学生的成绩

select degree from score where cno='3-102'(2)查询出选修3-101课程的成绩至少大于3-102的学生cno,sno,degree的信息(至少any:只需要满足3-101中的成绩大于3-102中的最低成绩即可)

select cno,sno,degree from score where degree>any(select degree from score where cno='3-102') and cno ='3-101'(3)根据(2)的成绩降序排列(order by ... desc)

select cno,sno,degree from score where degree>any(select degree from score where cno='3-102') and cno ='3-101' order by degree desc

3b11eaebbbb994a568df845813131094.png

27.查询选修编号为“3-101”课程且成绩高于选修编号为“3-102”的同学的cno,sno,degree

(1)查询出选修“3-102”学生的成绩

select degree from score where cno='3-102'(2)查询出选修3-101课程的成绩大于3-102的学生cno,sno,degree的信息(且all:高于3-102班的最高成绩才满足)

select cno,sno,degree from score where degree>all(select degree from score where cno='3-102') and cno ='3-101'

2b3542a69c4985275934ea8c46446b83.png

28.查询所有教师和学生的name,sex,brithday(求并集union)

select sname as name,ssex as sex,sbrithday as brithday from student

union

select tname as name,tsex as sex,tbrithday as brithday from teacher

29.查询成绩比该成绩平均分低的同学的成绩表

(1)查询每一门课的平均成绩

select avg(degree) from score b group by cno

(2)通过采用复制表结构的方法做条件查询(保证使用的cno是相同的)

select* from score a where degree

21dee05e4d622f4d4d82e3b7deeffca3.png

30.查询至少有两名男生的班级

select count(ssex),class from student where ssex='男' group by class having count(ssex)>=2

eae0aaac5ecd8f5d2260964a188b78b9.png

31.查询student表中不含“老”字的同学记录

select * from student where sname not like'老%'

32.查询student表中每个学生的姓名和年龄

年龄=当前年份-出生年份

(1)当前年份

select year(now())

(2)学生出生年份

select year(sbrithday) from student

(3)每个学生的姓名和年龄

select sname,year(now())-year(sbrithday) as age from student

3ee57f388745bceeb3aac1c83c7505dc.png

33.以班号和年龄从大到小查询student表中所有同学的信息

select * from student order by class desc,sbrithday

34.按等级查询

创建一个等级表

create table grade(lowint(3),upp int(3),grade char(4))

插入数据

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');

查询所有学生的sno,cno,grade

select sno,cno,grade from score,grade where score.degree between grade.low and grade.upp

07fe192a265c06891d9ebcae93265b1d.png

35.sql的四种连接查询

内连接:inner join 或 join

外连接:

左外连接:left join ...on或 left outer join ...on

右外连接:right join...on 或 right outer join...on

完全外连接:full join...on 或 full outer join...on

创建两张表:

person 表:id,name,cardId

card 表 :id,name

create table person (id int (10) primary key ,name varchar(20),cardid int(10) );

create table card (id int(10) primary key ,name varchar (20) );

向两张表中分别添加数据

insert into card values(1,'饭卡');

insert into card values(2,'地铁卡');

insert into card values(3,'工商卡');

insert into card values(4,'邮政卡');

insert into card values(5,'农行卡');

insert into person values(1,'老大',1);

insert into person values(2,'老二',4);

insert into person values(3,'老三',5);

insert into person values(4,'老四',2);

insert into person values(5,'老五',2);

insert into person values(6,'老六',6);

注意到person表中并没有创建外键

(1)内连接(jion ...on),查询出两张表有关联的信息(即两表共有的部分)

726f6c41db02f07f498a92a3b0267b4a.png

select * from person join card on person.cardId=card.id

select * from person inner join card on person.cardId=card.id

8d6a02efeba43b72c364f492856e4863.png

(2)左外连接:会把左边表里的所有数据取出来,而右边表中的数据,如果有相等的就显示出来,若没有就显示为空

cef8e8ea746828d7fd159c79cf3c8de6.png

select * from person left join card on person.cardId=card.id

f7725820695ba7be941b8e9ba91bf173.png

(3)右外连接:会把右边表里的所有数据取出来,而左边表中的数据,如果有相等的就显示出来,若没有就显示为空

e78033a31ee112bde1f4815cc0a4c6af.png

select * from person right join card on person.cardId=card.id

79b658b2652112739c212d97cd02f7bd.png

(4)全外连接(full join )

e6ade63aa22a769c2d692d5a692b496d.png,注:Mysql 默认不支持此种写法 Oracle支持(select * from person full join card on person.cardId=card.id) ,可以使用将左连接与右连接结合起来作为全连接

select * from person right join card on person.cardId=card.id

union

select * from person left join card on person.cardId=card.id

5cfb10e9f2f4bcf7e7e25ca3d6617978.png

  • 5
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值