SQL server 实验二 数据查询

本文通过一系列实际案例,深入浅出地介绍了SQL语言的基础及高级用法,包括基本的SELECT语句、条件筛选、聚合函数、子查询、联表查询等,并展示了如何使用SQL解决复杂的数据分析问题。
/* 1 */
select *
from Student;

/* 2 */
select *
from Student
where Sdept='CS';

/* 3 */
select *
from Student
where Sage not between 19 and 21;

/* 4 */
select MAX(Sage)
from Student;

/* 5 */
select Sno,Sname
from Student
where Sage=(select MAX(Sage)
			from Student
			where Sdept='IS');

/* 6 */
select Sno,Sname,Sdept,Sage
from Student x
where x.Sage=(select MAX(Sage)
			from Student y
			where x.Sdept=y.Sdept);

/* 7 */
select Sdept,COUNT(*) People
from Student
where Sdept='MA'
group by Sdept;

/* 8 */
select Sdept,COUNT(*) People
from Student
group by Sdept
order by People asc;

/* 9 */
select Sdept,AVG(Sage) avg_sage
from Student
group by Sdept
order by avg_sage desc;

/* 10 */
select Cname
from Course;

/* 11 */
select Cname,Ccredit,Cterm
from Course
where Cterm='1';

/* 12 */
select Cterm,SUM(Ccredit)
from Course
where Cterm='2'
group by Cterm;

/* 13 */
select Student.Sno,Sname,COUNT(SC.Cno) as class,SUM(Course.Ccredit) as sum_credit,AVG(Grade) avg_grade
from Student,Course,SC
where Student.Sno=SC.Sno and Course.Cno=SC.Cno
group by Student.Sno,Sname;

/* 14 */
select Sno,Cno
from SC
where Cno='1'
union
select Sno,Cno
from SC
where Cno='2';

/* 15 */
select Student.Sno,Sname,Grade
from Student,SC,Course
where Student.Sno=SC.Sno and Course.Cno=SC.Cno and Cname='数据库' and Grade<60;

/* 16 */
select Student.Sno,Sname,SC.Cno,Course.Cname,Grade
from Student,Course,SC
where Student.Sno=SC.Sno and Course.Cno=SC.Cno;

/* 17 */
select *
from Student
where  Sno not in (	select Sno
					from SC);

/* 18 */
select Sno
from SC
group by Sno
having COUNT(*/*Sno 也可以*/)>=3;

/* 19 */
select distinct Sno
from SC
where Grade>80;

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值