1 select Sno,Sname from Student where Ssex='女'and Sage>25; 2 --查詢選修了“數據庫”課程的學生姓名。
3 select Sname from Student where Sno in(select Sno from SC where Cno in(select Cno 4 from Course where Cname='數據庫')); 5
6 select Sname from Student,Course,SC Where
7 Student.Sno=SC.Sno and Course.Cno=SC.Cno and Cname='數據庫'; 8
9 --統計選修2號課程的人數。
10 select count(*) from Student where Sno in(select Sno from SC where Cno='2'); 11
12 --查詢平均成績大於80分的學生的學號。
13 select Sno,Sname from Student where Sno in
14 (select Sno from SC group by Sno having avg(grade)>80); 15
16 --統計每個系的學生人數。
17 select Count(*)from Student group by Sdept; 18
19 --查詢選修數據庫課程並且成績不及格的學生學號和姓名
20 select Student.Sno,Student.Sname 21 from Student,SC,Course 22 where Course.Cname='數據庫' and SC.Grade<60 and SC.Cno=Course.Cno and Student.Sno=SC.Sno; 23
24 --自身鏈接查詢
25 --查詢每門課程先修課的學分。
26 select first.Cno,second.Ccredit from Course first,Course second 27 where first.Cpno=second.Cno; 28
29 --查詢成績在60到80之間的所有記錄。
30 select * from SC where Grade between 60 and 80; 31 --查詢成績為85,86或88的記錄
32 select * from SC where Grade='85'or Grade='86' or Grade='88'; 33 -- 查詢所有不姓“王”的學生記錄。
34 select * from Student where Sname not like '王%'; 35 --以系別和年齡從大到小的順序查詢Student表中的全部記錄。
36 select * from Student order by Sdept,Sage Desc; 37 --- 統計男女生分別有多少人。
38 select Ssex,count(*) from Student group by Ssex; 39 -- 查詢姓名的第二個字為“小”字的女生信息。
40 select *from Student where Sname like '_小%' and Ssex='女'; 41 select *from Student where Sname like '%小%'; 42 -- 查詢成績最高的三個學生的學號和成績。
43 select top(3) Sno,SUM(Grade)from SC group by Sno order by SUM(Grade) desc; 44 -- 查詢學生的成績信息,先按學號升序排序,再按成績降序排序。
45 Select Student.Sno,Student.Sname,Grade from Student,SC 46 where Student.Sno=SC.Sno order by Sno asc, Grade desc; 47
48 --查詢至少選修了兩門課的學生的學號,選修課程數和選修的平均分數
49 select Sno,COUNT(*),AVG(Grade) from sc group by Sno having count(*)>=2; 50 -- 查詢所有比劉晨大的學生的學號,姓名,年齡。
51 select Sno,Sname,Sage from Student where Sage>(select Sage from Student where Sname='劉晨'); 52 -- 求出總分大於150的學生的學號、姓名。
53 select Sno,Sname from Student where sno in( 54 select Sno from SC group by Sno having SUM(Grade)>150); 55
56 select Student.Sno,Student.Sname from Student,SC where
57 Student.Sno=SC.Sno group by SC.Sno,Student.Sno,Student.Sname having sum(Grade)>150
58
59 --實驗數據----select SUM(Grade)from SC group by Sno;
60 --列出那些專業相同的學生相應的姓名及專業信息
61 --實驗數據----select * from Student group by Sdept having COUNT(*)>=2;
62 select * from Student where Sdept in
63 (select Sdept from Student group by Sdept having COUNT(*)>=2) 64
65 --求至少選修1號課和2號課的學生的學號。
66 select Sno from SC where Sno in (select Sno from SC where Cno=1) and Cno=2; 67
68 --求出所有學生的總成績 SELECT SUM(成績) AS 總成績 FROM 選課
69 select SUM(Grade) as g from SC group by Sno; 70
71 ---列出非電院的所有學生
72 select * from Student where Sdept!='IS'; 73 select * from Student where Sdept<>'IS'; 74 select * from Student where not Sdept='IS'; 75 --列出那些專業相同的學生相應的姓名及專業信息。
76 select a.Sname,b.Sname,a.Sdept 77 from Student a,Student b 78 where a.Sno<>b.Sno and a.Sdept=b.Sdept; 79
80 --、求至少選修1號課和2號課的學生的學號
81 --SELECT X.學號 FROM 選課 X,選課 Y WHERE
82 -- X.學號=Y.學號AND X.課號="1" AND Y.課號="2"
83 select a.Sno from SC a,SC b 84 where a.Sno=b.Sno and a.Cno='1' and b.Cno='2'; 85 --、求選修2號課的學生中,成績比選修1號課的
86 --最低成績要高的學生的學號和成績。
87 select sno,sname from Student where sno in
88 (select Sno from SC where Cno='2' and Grade>( 89 select MIN(Grade)from SC where Cno='1')); 90
91
92 --打開查詢分析器用sql語句給u1授予student的查詢權限。
93 grant select on Student to u1; 94 -- 把對表SC的查詢授予所有用戶
95 grant select on SC to public ; 96
97 --給用戶u1授予對sc表的查詢權限並具有給其他用戶授權的權利。
98
99 grant select on SC to u1 with grant option; 100
101 ---讓用戶u1對用戶u2授予對sc表的查詢權限。
102
103 grant select on SC to u2; 104
105 --分別回收u2和u1所擁有的權限。
106 revoke all privileges to u2,u1 107 grant select on Student to r1