use pay
select distinct professor from person;
–男员工信息
select no,name,sex,birthday,professor,deptno from person where sex = ‘男’;
–00102女员工信息
select no,name,sex,birthday,professor,deptno from person where deptno=00102 and sex = ‘女’;
–1.5倍工资后实际收入
select base*1.5+bonus-deduct as real_fact from pay where no = ‘000001’;
–00102基本信息
select person.no,name,sex,birthday,professor,deptno,base,bonus,deduct,fact
from person join pay on person.no = pay.no
where person.deptno = 00102
order by fact desc;
select deptno as ‘部门’,sum(fact) as ‘总额’
from pay,person
where pay.no = person.no and year = 2005
group by deptno;
–平均奖金升序排序
select deptname,avg(bonus) as 平均工资 from dept,pay,person
where dept.deptno = person.DeptNo and person.no = pay.no and year = 2005 and month = 1
group by dept.deptno,deptname
having avg(bonus) >= 400
order by avg(bonus);
/查询市场所有员工姓名和2005年1月工资明细:以下提供两种方法/
select name,base,bonus,deduct,fact from (
select no,name,deptname from person left join dept on dept.Deptno=person.DeptNo) as f,pay
where f.no=pay.No and f.DeptName = ‘市场部门’ and pay.year=2005 and pay.Month = 1;
select name,base,bonus,deduct,fact
from person,pay,dept
where person.no = pay.no and year = 2005 and month = 1 and dept.deptname = ‘市场部门’ and dept.DeptNo = person.DeptNo
select * from person
where no in(select no from pay where year = 2005 and month = 1 and fact >(select fact from pay where year = 2005 and month = 1 and no=‘000001’));
–比实发工资都高的员工基本信息
select no,name,sex,birthday,professor,deptno
from person
where no in
(select no from pay
where year = 2005 and month = 1 and
fact>(select max(fact)
from pay,person
where year = 2005 and month = 1 and deptno = ‘00102’ and person.no = pay.no));
–比实发工资都高的员工基本信息和工资明细
select person.No,name,sex,birthday,professor,deptno,base,bonus,deduct,fact
from person,pay
where person.no=pay.no and fact >
(select avg(fact) from pay) and year = 2005 and month = 1;
create view dbo.view_person(No,Name,Sex,professor,deptno)
as
select no,name,sex,professor,deptno
from dbo.person;
–创建视图
create view dbo.view_pay(year,month,no,name,sex,prefessor,base,bonus,deduct,fact) as
select year,month,person.no,name,sex,person.professor,base,bonus,deduct,fact
from person,pay,dept;
use education
–查询学分=4的课程号,课程名
select Cno,Cname from Course where Ccredit=4;
–20~23之间的学生
select Sno,Sname from Student where Sage between 20 and 23;
–刘晨的课程号
select Cno from Course where Cno not in
(select Cno from SC where Sno in
(select Sno from Student where Sname = ‘刘晨’));
–姓刘同学的基本信息
select Sno,Sname,Ssex,Sage,Sdept from Student where Sname like ‘刘%’;
–查询所有选修了课程的学生基本信息和选课情况
select distinct(sname),course.Cname,course.Ccredit,Grade
from student,sc,course
where sc.Sno=student.Sno and sc.Cno=course.Cno and sc.Cno in
(select cno from course);
–查询学生基本信息
select student.sno,sname,ssex,sage,sdept,cname,ccredit from student,course,sc
where course.cno=sc.cno and student.sno=sc.sno;
–成绩为0的学生
–注意空值与null的区别
select Sname,Cname
from Student S,SC,Course
where SC.Sno = S.Sno and SC.Cno = Course.Cno and SC.Grade=0;
–有学生有选修的课程门数
select count(distinct C.Cno) as 课程门数 from Course C left join SC on C.Cno = SC.Cno;
–至少选修了三门课程的学生和姓名
select Sname,count(CNO) 选修门数 from SC join student on sc.Sno=student.Sno
group by sname
having count(CNO)>=3;
–选修数据库原理的学生的平均年龄
select avg(Sage) 平均年龄 from Student S,SC
where SC.Cno = (select Cno from Course where Cname = ‘数据库原理’) and SC.Sno = S.Sno;
–创建视图
create view CS_S(sno,sname,cname,grade)
as select student.sno,sname,cname,grade from student,course,sc;
/检索至少选修3门课程的学生学号详细说明:/
/1、先选择出大于等于三的人数2、与course表关联/
–select cno,count(sno) from sc group by cno;
select cname,num from
(select cno,count(sno) as num from sc group by cno) f,course
where course.cno = f.Cno and num >= 3
order by num desc;
–成绩在2~3名的学生1、先排序2、连接表筛选
select * from (select sc.sno,grade,sc.Cno,ROW_NUMBER() over (partition by sc.sno order by grade desc) m
from sc join student on sc.Sno=student.sno) a
where m in(2,3) and a.cno = ‘809023501’;