mysql練習題

?導入hellodb.sql生成數據庫
?(1) 在students表中,查詢年齡大于25歲,且為男性的同學的名字和年齡
MariaDB [hellodb]> select Name,Age from students where Age>25 and Gender=’M’;
?(2) 以ClassID為分組依據,顯示每組的平均年齡
MariaDB [hellodb]> select ClassID,avg(age) as avg_age from students group by ClassID;
?(3) 顯示第2題中平均年齡大于30的分組及平均年齡
MariaDB [hellodb]> select ClassID,avg(age) as avg_age from students group by ClassID having avg_age>30;
?(4) 顯示以L開頭的名字的同學的信息
MariaDB [hellodb]> select * from students where Name like ‘L%’;
MariaDB [hellodb]> select * from students where Name rlike ‘^L’;
?(5) 顯示TeacherID非空的同學的相關信息
MariaDB [hellodb]> select * from students where TeacherID is NOT NULL;
?(6) 以年齡排序后,顯示年齡最大的前10位同學的信息
MariaDB [hellodb]> select * from students order by age desc limit 10;
?(7) 查詢年齡大于等于20歲,小于等于25歲的同學的信息
MariaDB [hellodb]> select * from students where age>=20 and age<=25;
MariaDB [hellodb]> select * from students where age between 20 and 25;

 

?導入hellodb.sql,以下操作在students表上執行
?1、以ClassID分組,顯示每班的同學的人數
MariaDB [hellodb]> select classid,count(name) from students group by classid;
?2、以Gender分組,顯示其年齡之和
MariaDB [hellodb]> select gender,sum(age) from students group by gender;
?3、以ClassID分組,顯示其平均年齡大于25的班級
select classid from students group by classid having avg(age) > 25;
?4、以Gender分組,顯示各組中年齡大于25的學員的年齡之和
MariaDB [hellodb]> select gender,sum(age) from (select age,gender from students where age>25) as t group by gender;
?5、顯示前5位同學的姓名、課程及成績
MariaDB [hellodb]> select name,course,score from (select * from students limit 5) as t,courses,scores where t.stuid=scores.stuid and scores.courseid=courses.courseid;
?6、顯示其成績高于80的同學的名稱及課程;
select name,course,score from students,(select * from scores where score > 80) as s,courses where students.stuid=s.stuid and s.courseid=courses.courseid;
?7、求前8位同學每位同學自己兩門課的平均成績,并按降序排列
select name,a from (select * from students limit 8) as s,(select stuid,avg(score) as a from scores group by stuid) as c where s.stuid=c.stuid order by a desc;
?8、顯示每門課程課程名稱及學習了這門課的同學的個數
select course,count(name) from (select name,course from students,courses,scores where students.stuid=scores.stuid and scores.courseid=courses.courseid) as a group by course;
select courses.course,count(stuid) from scores left join courses on scores.courseid=courses.courseid group by scores.courseid;
?9、顯示其年齡大于平均年齡的同學的名字
MariaDB [hellodb]> select name from students where age > (select avg(age) from students);
?10、顯示其學習的課程為第1、2,4或第7門課的同學的名字
MariaDB [hellodb]> select name from students,(select distinct stuid from (select * from scores where courseid in (1,2,4,7)) as a) as b where b.stuid=students.stuid;
select students.name from students,(select distinct stuid from scores where courseid in (1,2,4,7))as s where s.stuid=students.stuid;
?11、顯示其成員數最少為3個的班級的同學中年齡大于同班同學平均年齡的同學
MariaDB [hellodb]> select student.name,student.age,student.classid,second.avg_age from (select students.name as name ,students.age as age,students.classid as classid from students left join (select count(name) as num,classid as classid from students group by classid having num>=3) as first on first.classid=students.classid) as student,(select avg(age) as avg_age,classid as classid from students group by classid) as second where student.age>second.avg_age and student.classid=second.classid;
?12、統計各班級中年齡大于全校同學平均年齡的同學
select name,age,classid from students where age > (select avg(age) as a from students);

本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/100759

(2)
會飛的蝸牛會飛的蝸牛
上一篇 2018-06-09 16:16
下一篇 2018-06-10 01:41

相關推薦

  • Linux系統命令格式及常用命令

    一.Linux命令使用格式 ? ? ?#COMMAND OPTIONS ARGUMENTS 1.發起一命令:請求內核將某個二進制程序運行進一個程序; ? ? ? ? ? ? ? ? ? ?程序——>進程 靜態——>動態(有生命周期) ? ? ? ? ? 命令本身是一個可執行的程序文件:二進制格式的文件,有可能會調用共享庫文件; 2.多系統程序文件…

    2018-05-12
  • linux 學習5

    第五周(7.16-7.30)

    2018-07-30
  • 計算機組成及其功能

    計算機主要有兩個組成,硬件和軟件; 硬件主要分為主機和外部設備; 主機: 主板:主板是電腦中各個部件工作的一個平臺,它把電腦的各個部件緊密連接在一起,各個部件通過主板進行數據傳輸。也就是說,電腦中重要的“交通樞紐”都在主板上,它工作的穩定性影響著整機工作的穩定性。 CPU:CPU即中央處理器,是一臺計算機的運算核心和控制核心。其功能主要是解釋計算機指令以及處…

    Linux筆記 2018-05-10
  • linux學習-2周

    文本處理工具、cat/rev/more/less/head/tail/cut/paste/wc/sort/uniq/diff/grep

    2018-04-08
  • 測試

    測試

    Linux筆記 2018-07-22
  • CentOS系統Shell編程語言基礎之Bash的基礎特性

    bash的基礎特性之命令歷史: 命令歷史:shell進程會在其會話中保存此前用戶提交執行過的命令~]#history定制history的功能,可通過環境變量實現HISTSIZE:shell進程可保留的命令歷史的條數HISTFILE:持久保存命令歷史的文件, “.bash_history”HISTFILESIZE:命令歷史文件的大小 命令用法: history…

    Linux筆記 2018-06-27
欧美性久久久久