基本的SQL語句練習(學生課程表)

創建表

           
1、查出全體同學的學號、姓名
select sno,sname from student;
2、查詢全體同學的姓名學號班級(按順序輸出)
select sname,sno,sclass from student;
3、查詢全體同學的記錄(顯示所有行)
select * from student;
4、查詢全體同學的姓名及出生年份
select sname,(2010-sage) as '出生年份' from student ;
5、查詢全體同學姓名出生年份班級(班級要用小寫字母lower函數)
select sname,2010-sage as '出生年份',lower(sclass) from student;
6、查詢全體同學的姓名/出生年份/所在班級列爲YearOfBirth
select sname,2010-sage YearOfBirth,sclass from student;
7、查詢選中學員的所在班級並且去掉重複行用distinct
select distinct sclass from student;
8、查詢Y02班全體同學名單
select * from student where sclass='y02';
9、查詢所有年齡在20歲一下的同學姓名及年齡
select sname,sage from student where sage<20;
10、查詢考試不及格的同學的姓名及成績
select s.sname,sc.grade from student s,sc where s.sno = sc.sno and sc.grade <60;
11、查詢年齡在19-20歲(包括19、20)之間的同學姓名、班級、年齡
select sname,sclass,sage from student where sage in (19,20);(19、20 比較特殊選項少可以使用 in)
select sname,sclass,sage from student where sage between 19 and 20;
select sname,sclass,sage from student where sage>=19 and sage<=20;
12、查詢年齡不在19-20歲之間的同學姓名、班級、年齡
select sname,sclass,sage from student where sage not in (19,20);
select sname,sclass,sage from student where sage not between 19 and 20;
select sname,sclass,sage from student where sage<19 or sage>20;
13、查詢y02班級和y05班的同學的姓名、性別
select sname,sclass from student where sclass in ('y02','y05');
select sname,sclass from student where sclass='y02' or sclass='y05';
14、查詢不是y02或者y05班的同學的姓名、性別
select sname,sage from student where sclass not in('y02','y05');
select sname,sage from student where sclass!='y02' and sclass!='y05';
select sname,sage from student where not sclass='y02' and not sclass='y05';
15、查所有姓劉的同學的姓名、學號、性別( " % "表示一個或者多個," _ "表示只佔一個字符)
select sname,sno,ssex from student where sname like'劉%';
16、查所有姓張且全名只有2個漢子的同學的所有信息
select * from student where sname like'張_';
17、某些學生未考試查缺成績的同學的學號和課程號
select sno,cno,grade from sc where grade is null;
18、查詢所有成績的同學的學號、課程號和成績
select sno,cno,grade from sc where grade is not null;
19、查y02班年齡在20歲一下的姓名和年齡
select sname,sage from student where sclass='y02' and sage<20;
20、查選修1號課程的同學的學號和成績,按成績降序排序
select sno,grade from sc where cno=1 order by grade desc;
21、查全體同學信息查詢結果按所在班級的班級名稱按降序排列,同班同學按年齡升序排列
select * from student order by sclass desc,sage asc;
22、查詢學員的總人數
select count(sno) from student;
23、查選修課程學院人數
select count(*) from sc;
24、統計1號課的學院平均成績
select avg(grade) 平均成績 from sc where cno=1;
25、查選修1號課和同學最高成績
select max(grade) from sc where cno=1;
26、求各個課程號及相應選課人數
select cno,count(*) 選課人數 from sc group by cno;
27、查選取1門以上課程的同學學號和課程個數
select sno,count(cno) from sc group by sno having count(cno)>1;
28、查每個學員及其選修課程情況
select sno,cno from sc;
29、查每個學員及其選修課程情況對沒有選課的也要輸出其姓名、學號、性別、班級(注意:是作外連接student是主表)
select st.sname,st.sno,st.ssex,st.sclass,sc.cno,sc.grade
from student st left join sc on
st.sno = sc.sno order by st.sname;
30、查選取2號課程且成績在90分以上的同學
select * from sc where cno=2 and grade>90;
31、查詢每個同學學號姓名,選課程名稱及其成績
select stu.sno,stu.sname,c.cname,sc.grade
from student stu join sc on stu.sno = sc.sno
join course c on c.cno = sc.cno;
或者
select stu.sno,stu.sname,c.cname,sc.grade
from student stu,course c,sc
where stu.sno=sc.sno and c.cno= sc.cno
order by sc.cno  desc;
32、查與劉晨在一個班的同學
select * from student where sclass=(select sclass from student where sname='劉晨');
33、選取C語言的同學學號和姓名
select s.sno,s.sname
from student s join sc on s.sno=sc.sno
join course c on sc.cno=c.cno
where c.cname='C語言';
或者
select sno,sname from student where sno in
(select sno from sc where cno in
(select cno from course where cname='C語言'));
34、查其他班級中比y02班某一同學大的同學姓名和年齡
select sname,sage from student
where sclass<>'y02' and
sage > (select min(sage) from student where sclass='y02');
或者(加入 any 關鍵字)
select sname,sage from student
where sclass<>'y02' and
sage >any (select sage from student where sclass='y02');
35、查其他班中比y02班同學全部都大的同學姓名和年齡
select sname,sage from student
where sclass!='y02' and
sage > (select max(sage) from student where sclass='y02');
或者(加入 all 關鍵字)
select sname,sage from student
where sclass!='y02' and
sage >all (select sage from student where sclass='y02');
36、查選取1號課程的學員的姓名
(在與查詢的集合的包含關係時,最好使用 in 、any、all)
select s.sname from student s,sc
where s.sno = sc.sno and sc.cno=1;
或者(子查詢)
select sname from student where sno in
(select sno from sc where cno=1);
37、查沒有選取1號課程的學員的姓名(注意:要過濾已經選取過1號的同學,因爲可能某同學多選修課程)
說明:如果是過濾集合,就只能用 in/all/any
select sname from student where sno not in
(select sno from sc where cno=1);
38、查y02班同學及年齡不大於19歲的學員(union)
select * from student where sclass='y02' union
select * from student where sage<=19;
解析:y02班級的同學與全年紀年齡不大於19歲的學生,而(sage<=19 and sclass='y02')表示爲
y02班級同學“且”年齡不大於19歲的同學,與題意不符。所以下面的語句是錯誤的,因爲範圍沒有確定好
select * from student where sclass='y02' and sage<=19;
說明:union 用來兩個集合求並集(合併兩個集合,並且除去重複的記錄)
39、查詢選取1號課程或者2號課程的同學學號
select distinct sno from sc where  cno=1 or cno=2;
或者
select distinct sno from sc where cno in (1,2);
40、將4號學員的年齡改爲23歲
update student set sage=23 where sno=4;
41、將所有同學的年齡增加1歲
update student set sage=sage+1;
42、y02班的同學的成績改爲100
update sc set grade=100 where sno in (select sno from student where sclass='y02');
43、刪除學號爲1的同學記錄
delete from student where sno=1;

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