日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

mysql 多表查询练习题_mysql多表查询练习

發布時間:2025/3/21 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql 多表查询练习题_mysql多表查询练习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

導入hellodb.sql生成數據庫,并進入到數據庫之中

mysql -uroot < hellodb.sql

use mysql

(1)以ClassID分組,顯示每班的同學的人數

`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的學員的年齡之和

select gender,sum(age) from students group by gender

(5)顯示前5位同學的姓名、課程及成績

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的同學的名稱及課程

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位同學每位同學自己兩門課的平均成績,并按降序排列

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)取每位同學各門課的平均成績,顯示成績前三名的同學的姓名和平均成績

select name,avg(score) as 平均分數 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 平均分數 desc limit 3

(9)顯示每門課程課程名稱及學習了這門課的同學的個數

select courseid,count(CourseID) from students inner join coc on coc.classid=students.classid group by courseid

(10)顯示其年齡大于平均年齡的同學的名字

select * from students where age>(select avg(age) from students)

(11)顯示其學習的課程為第1、2,4或第7門課的同學的名字

select name,courseid from (select * from coc where CourseIDin (‘1′,’2′,’4′,’7’)) as new inner join students on students.classid=new.classid

(12)顯示其成員數最少為3個的班級的同學中年齡大于同班同學平均年齡的同學

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

總結

以上是生活随笔為你收集整理的mysql 多表查询练习题_mysql多表查询练习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。