学生-课程数据库练习

  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

 

posted @ 2014-01-05 20:13  thatday  阅读(3016)  评论(0编辑  收藏  举报