简单查询
学生选课数据库查询语句实例:
- select form where
use 学生选课;
SELECT sno,sname FROM student
WHERE sdept = '数计学院'
SELECT DISTINCT sno FROM SC
SELECT sno,grade FROM SC
WHERE cno = 10001
ORDER BY grade DESC,sno ASC
SELECT sno,grade*0.8 FROM SC
WHERE cno = 10001 AND (grade < 70 OR grade >60)
SELECT* FROM student
WHERE sdept IN ('数计学院','生工学院')
ORDER BY sdept DESC
SELECT sno,cno FROM SC
WHERE grade IS NULL
SELECT SC.sno,sname,cname,grade FROM SC,Course,student
WHERE SC.sno = student.sno AND SC.cno =Course.cno
ORDER BY grade DESC
SELECT* FROM Course
WHERE cno IN (SELECT cno FROM SC WHERE sno IN(SELECT sno FROM student WHERE sdept = '数计学院' )
AND ccredit > 2)
SELECT student.*,SC.* FROM student,Sc
WHERE sc.sno=student.sno
SELECT X.sno,sname,grade FROM student X,SC Y
WHERE X.sno=Y.sno AND Y.cno = '10001'
SElECT FIRST.*,SECOND.cpno 先行课的先行课 FROM Course FIRST,Course SECOND
WHERE (FIRST.cpno=SECOND.cname AND SECOND.cpno IS NOT NULL)
嵌套查询
- left join 左连接
--嵌套
/*
1.选修课里没有UML故修改为java
查询了选修了java的课程的学生学号和姓名
*/
--method1.
select sno ,sname from student where sno in
(select sno from SC where cno in (select cno from Course where cname = 'java'))
--method2.
select student.sno ,sname from student ,SC,Course where student.sno=SC.sno and SC.cno = Course.cno and Course.cname = 'java'
/*
2.查询比王华年龄大的学生的学号和姓名。
*/
select sno,sname from student where sage >
(select sage from student where sname ='王华')
/*
3.查询c1课程的成绩低于张三的学生学号和成绩
*/
select SC.sno ,grade from SC where grade <
(select grade from SC,student where (SC.sno=student.sno and student.sname='zeze' and SC.cno='c1'))
/*
4.查询其他学院中比数计学院学生年龄都小的学生
*/
select sname from student where sage >all (select sage from student where sdept ='数计学院') AND sdept!='数计学院'
/*
5查询选修了C2的课程的学生姓名
*/
--method1
select student.sname from student,sc where student.sno=SC.sno and SC.cno= 'c1'
--method2
select student.sname from student left join SC on student.sno=SC.sno left join Course on Course.cno=SC.cno where Course.cno='c1 '
/*
6.查询了选修了全部课程的学生姓名
*/
select sname
from student
where not exists (select *
from Course
where not exists( select *
from SC
where SC.sno = student.sno and SC.cno = Course.cno))
/*
7.查询至少选修了学生为"S2"的学生所选秀的全部课程的学生学号的姓名
*/
select sname
from student
where not exists(select *
from SC
where SC.sno = 'S2' and not exists(select *
from Course
where SC.sno = student.sno and SC.cno = Course.cno))
/*
9.查询 既选修了数据结构又选修了数据库原理与应用的课程的学生姓名(ps:因为表中没有,所以没有结果)
*/
select sname from student
left join SC sc
on sc.sno = student.sno
left join Course c1
on sc.cno = c1.cno
where c1.cname = '数据结构'
intersect
select sname from student
left join SC sc
on sc.sno = student.sno
left join Course c1
on sc.cno = c1.cno
where c1.cname = '数据库原理'
组合查询
- 交集 intersect
- 并集 union
- 除去 expcept
- 分组 group by
- 过滤 聚合having 与group by 一起使用
--组合
/*
1.使用集合运算查询既选修了数据结构课程又选修了数据库原理及应用课程的学生姓名。
*/
select sname
from student
where sno in( select sno
from SC
where cno in (select cno
from Course
where cname = '数据结构'))
intersect
select sname
from student
where sno in( select sno
from SC
where cno in (select cno
from Course
where cname = '数据库原理'))
/*
2.使用集合运算查询选修了数据结构课程或选修了数据库原理及应用课程的学生学号。
*/
select sno
from SC
where cno in (select cno
from Course
where cname = '数据结构')
union
select sno
from SC
where cno in (select cno
from Course
where cname = '数据库原理')
/*
3.使用集合运算查询选修了数据结构课程而没有选修了数据库原理及应用课程的学生学号。
*/
select sno
from SC
where cno in (select cno
from Course
where cname = '数据结构')
except
select sno
from SC
where sno in (select cno
from Course
where cname = '数据库原理')
/*
4.统计选修了课程的学生人数。
*/
select count(distinct sno) as status
from SC
/*
5.查询选修成绩合格的课程超过 4 门以上学生的学生学号、总成绩。
(ps:having 也是一种过滤,是聚合过滤。
where 为过滤,在结果集产生前过滤。
having 在 where对源输入过滤之后聚合的结果再行过滤。
HAVING子句可以让我们筛选成组后的各组数据.
WHERE子句在聚合前先筛选记录.也就是说作用在GROUP BY 子句和HAVING子句前.
而 HAVING子句在聚合后对组记录进行筛选。 )
*/
select SC.sno,SUM(grade) as '总成绩'
from SC
where SC.grade > =60
group by sno
having count(cno)>4
/*
6.统计各院系的学生人数。
*/
select sdept ,count(sdept) as total from student
group by sdept
/*
7.统计各年龄的学生的人数
*/
select sage ,count(sage) as everyone from student
group by sage
/*
8.统计每个学生的选修课数目和平均成绩
*/
select sno,count(cno) as '课程数目',AVG(grade) as '平均成绩'
from SC
group by sno
/*
9.查询每门课程的详细信息及选课人数。
*/
select Course.*,count(SC.cno) as '选课人数'
from Course,SC
where SC.cno = Course.cno
group by Course.cno,ccredit,cname,cpno,ctech
持续补充中…