sql進階語句例項
1 多表查詢 通過where 將表進行關聯
表human,student資料如下圖:
將兩表通過id欄位進行連線 輸出
select t1.id,t1.name,t1.age,t1.sex,t2.chinese,t2.math,t2.english from human as t1,student as t2 where t1.id = t2.id;
2 檢視 view 資料只可檢視,不可操作
create view view1 as select t1.id,t1.name,t1.age,t1.sex,t2.chinese,t2.math,t2.english from human as t1,student as t2 where t1.id = t2.id;
3 sql語句綜合練習(表結構如下圖所示(學生表,課程表,分數表))
3.1 分別查詢學生表和課程表中的全部資料;
select *from student;
select * from course;
3.2 查詢成績在70到80之間的學生學號,課程號和成績;
select id,cno,grade from sroce where grade >=70 and grade <= 80;
or: select id,cno,grade from sroce where grade between 70 and 80;
3.3 查詢291號課程成績最高分數;
select max(grade) from sroce where cno =291;
3.4 查詢學生都選修了哪些課程,要求列出課程號;
select distinct(cno) from course;
3.5 查詢291號課程所有學生的平均成績,最高成績,最低成績;
select avg(grade),max(grade),min(grade) from score where cno = 291;
3.6統計每個系的人數;
select dept,count(dept) from student group by dept;
3.7統計每門課程的修課人數和考試最高分;
select count(id),max(grade) from score group by cno;
3.8統計每個學生的選課門數,並按選課門數遞增顯示;
select count(id) from score group by id;
3.9 統計選修課的學生總數和考試的平均成績;
select count(id) ,avg(grade) from score;
3.10 查閱選修課數超過2門的學生的平均成績;
select avg(grade) ,count(cno) as count from score group by cno having count >2 ;
3.11列出總成績超過200分的學生,要求列出學號,總成績;
select id,sum(grade) as sum_grade from score group by id having sum_grade >200;
3.12 查詢選修了291課程學生的姓名和所在系;
select t1.name,t1.dept from student as t1,score as t2 where t1.id =t2.id and cno = 291;
3.13 查詢成績80分以上的學生姓名,課程號和成績,並按成績降序排列;
select t1.name ,t2.cno,t2.grade from student as t1,sorce as t2 where t1.id = t2.id group by t2.id having grade > 80 order by grade desc;
3.13.1 查詢80分以上的學生;
select id ,cno,grade from score where grade > 80;
3.13.2 連線student 表,得到姓名;
select t1.name ,t2.cno,t2.grade from student as t1, (select id, cno,grade from score where grade > 80) as t2 where t1.id =t2.id;
3.14 查詢計算機系男生修了‘數學’的學生姓名,成績;
select t1.name,t2.grade from student as t1,score as t2 where t1.id = t2.id and t1.dept = ‘computer’ and t1.sex = ‘man’;
3.15 查詢哪些學生的年齡相同,要求列出年齡相同的學生姓名和成績;
select t1.name ,t2.grade from student as t1 ,score as t2 where t1.id = t2.id group by t1.age;
3.16 分別查詢計算機系和數學系的學生姓名,性別,修課名稱,修課成績;
並要求將這兩個查詢結果合併成一個結果集;
並以系名,姓名,修課名稱,修課成績的順序顯示各列;
第一步:在student 表中獲得計算機系和數學系的學號,姓名,以及性別;
select id,name,sex from student where dept in(‘computer’,’math’);
第二步:與分數表相關聯,得到成績和課程號;
select grade ,cno,t1.* from score as t1,( select id,name,sex from student where dept in(‘computer’,’math’) ) as t2 where t1.id = t2.id;
第三步:與course關聯得到修課名稱;
select cname, t2.* from course as t1, (select grade ,cno,t1.* from score as t1,( select id,name,sex from student where dept in(‘computer’,’math’) ) as t2 where t1.id = t2.id ) as t2 where t1.cno=t2.cno;
3.17 將計算機系成績高於80分的學生的修課情況插入到另一張表中,建立一個新表,然後插入資料。
create table temp as select t1.name,t3.cname from student as t1, score as t2,course as t3 where t1.id =t2.id and t1.dept = ‘computer’ and t2.cno = t3.cno and t2.grade > 80 ;
3.18 刪除修課成績小於60分的學生的修課記錄;
delete from score where grade < 60;
3.19 將所有選修了“c01”課程的學生的成績加10分;
update score set grade = grade + 10 where cno =291;