常用的資料庫查詢語言
要求:
1)查出“計算機系”的所有學生資訊。
2)查出“韓順平”所在的院系資訊。
3)查出在“行政樓”辦公的院系名稱。
4)查出男生女生各多少人。
5)查出人數最多的院系資訊。
6)查出人數最多的院系的男女生各多少人。
7)查出跟“羅弟華”同籍貫的所有人。
8)查出有“河北”人就讀的院系資訊。
9)查出跟“河北女生”同院系的所有學生的資訊。
參考答案:
1)select 學生.* from 學生 where 院系ID=(select 院系ID from 院系 where 院系名稱='計算機系')
1) select 學生.* from 學生 inner join 院系 on 學生.院系ID=院系.院系ID where 院系名稱='計算機系'
2)select 院系.* from 院系 where 院系ID=(select 院系ID from 學生 where 姓名='韓順平')
2)select 院系.* from 學生 inner join 院系 on 學生.院系ID=院系.院系ID where 姓名='韓順平'
3) select 院系名稱 from 院系 where 系辦地址 like '%行政樓%'
4) select count(*) as c from 學生 group by 性別
5) select * from 院系 where 院系ID = (select 院系ID from 學生表 group by 院系ID order by count(*) desc limit 0,1)
5) select * from 院系 inner join (select count(*) as 數量, 院系ID from 學生表 group by 院系ID ) as t on t.院系ID=院系.院系ID order by 數量 desc limit 0,1
5)select * from 院系 where 院系ID in(select 院系ID from 學生表 group by 院系ID having count(*) >= all(select count(*) as c from 學生表 group by 院系ID));
6) select 性別, count(*) from 學生表 where 院系ID = (select 院系ID from 學生表 group by 院系ID order by count(*) desc limit 0,1) group by 性別
7)select * from 學生表 where 籍貫=(select 籍貫 from 學生表 where 學生='羅弟華') and 姓名<>'羅弟華'
8)select * from 院系 where 院系ID in (select 院系ID from 學生表 where 籍貫='河北')
9)select * from 學生表 where 院系ID in (select 院系ID from 學生表 where 籍貫='河北' and 性別='女')
〉〉〉
資料結構大致如下:
注意:以上資料並不意味著全部資料,自己可根據題目要求構建(插入)適當數量的資料。
要求:
1)查詢選修了 MySQL 的學生姓名;
2)查詢 張三 同學選修了的課程名字;
3)查詢只選修了1門課程的學生學號和姓名;
4)查詢選修了至少3門課程的學生資訊;
5)查詢選修了所有課程的學生;
6)查詢選修課程的學生人數;
7)查詢所學課程至少有一門跟 張三 所學課程相同的學生資訊。
8)查詢兩門及兩門以上不及格同學的平均分
參考答案(噓,不許告訴別人):
1)select name from stu inner join stu_kecheng on stu.id=stu_kecheng.stu_id inner join kecheng on kecheng.id=stu_kecheng.kecheng_id where kecheng_name='mysql';
2)select kecheng_name from stu inner join stu_kecheng on stu.id=stu_kecheng.stu_id inner join kecheng on kecheng.id=stu_kecheng.kecheng_id where stu.name='張三';
3)select id, name from stu where id in(select stu_id from stu_kecheng group by stu_id having count(kecheng_id)=3 );
4)select * from stu where id in( select stu_id from stu_kecheng group by stu_id having count(kecheng_id) = (select count(id) as c from kecheng));
5)select * from stu where id in( select stu_id from stu_kecheng group by stu_id having count(kecheng_id)>=3);
6)select count(distinct stu_id) as c from stu_kecheng;
7)select id from stu where id in( select stu_id from stu_kecheng where kecheng_id in ( select kecheng_id from stu_kecheng where stu_id=(select id from stu where name = '張三')) );
8)select stu_id,avg(score) from stu_kecheng group by stu_id having sum(score<60)>=2;
8)select avg(score) from stu_kecheng where stu_id in(select stu_id from stu_kecheng group by stu_id having sum(score<60)>=2)