mysql 多表查询练习题_mysql多表查询练习
導(dǎo)入hellodb.sql生成數(shù)據(jù)庫,并進(jìn)入到數(shù)據(jù)庫之中
mysql -uroot < hellodb.sql
use mysql
(1)以ClassID分組,顯示每班的同學(xué)的人數(shù)
`select classid,count(stuid) from students group by classid“
(2)以Gender分組,顯示其年齡之和
select gender,sum(age) from students group by gender
(3)以ClassID分組,顯示其平均年齡大于25的班級
select classid,avg(age) as new_age from students group by classid having new_age>25
(4)以Gender分組,顯示各組中年齡大于25的學(xué)員的年齡之和
select gender,sum(age) from students group by gender
(5)顯示前5位同學(xué)的姓名、課程及成績
select name,course,score from (select name,score,courseid from (select * from students where stuid<=5) as s inner join scores on scores.stuid=s.stuid)as t inner join courses on courses.courseid=t.courseid
(6)顯示其成績高于80的同學(xué)的名稱及課程
select name,course from (select name,score,courseid from (select from scores where Score>80) as t inner join students on students.stuid=t.stuid) as t inner join courses on courses.courseid=t.courseid
(7)求前8位同學(xué)每位同學(xué)自己兩門課的平均成績,并按降序排列
select t.stuid,avg(score) from (select stuid,courseid from (select from students where stuid<=8) as s inner join coc on s.classid=coc.classid) as t inner join scores on s cores.stuid=t.stuid group by t.stuid
(8)取每位同學(xué)各門課的平均成績,顯示成績前三名的同學(xué)的姓名和平均成績
select name,avg(score) as 平均分?jǐn)?shù) from (select name,courseid from students inner join coc on students.classid=coc.classid) as s inner join scores on s.courseid=scores.courseid group by stuid order by 平均分?jǐn)?shù) desc limit 3
(9)顯示每門課程課程名稱及學(xué)習(xí)了這門課的同學(xué)的個數(shù)
select courseid,count(CourseID) from students inner join coc on coc.classid=students.classid group by courseid
(10)顯示其年齡大于平均年齡的同學(xué)的名字
select * from students where age>(select avg(age) from students)
(11)顯示其學(xué)習(xí)的課程為第1、2,4或第7門課的同學(xué)的名字
select name,courseid from (select * from coc where CourseIDin (‘1′,’2′,’4′,’7’)) as new inner join students on students.classid=new.classid
(12)顯示其成員數(shù)最少為3個的班級的同學(xué)中年齡大于同班同學(xué)平均年齡的同學(xué)
select * from (select name,classid,age from students) as s inner join (select new.classid,avg(age) as cc from (select classid from students group by ClassID having count(stuid) >= 3) as new inner join students on students.classid=new.classid group by new.classid) as a on a.classid=s.classid where cc
總結(jié)
以上是生活随笔為你收集整理的mysql 多表查询练习题_mysql多表查询练习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql parameters_MyS
- 下一篇: mysql主备数据库配置文档_MySQL