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;

練習:導入hellodb.sql,完成以下題目:

1、顯示前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;

2、顯示其成績高於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;

3、求前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;

4、顯示每門課程課程名稱及學習了這門課的同學的個數;

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;

思考:

1、如何顯示其年齡大於平均年齡的同學的名字?

MariaDB [hellodb]> select name from students where age > (select avg(age) from students);

2、如何顯示其學習的課程爲第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;

3、如何顯示其成員數最少爲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;

4、統計各班級中年齡大於全校同學平均年齡的同學。

select name,age,classid from students where age > (select avg(age) as a from students);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章