1、 查询“王林”的基本情况和所工作的部门名称;
Use yggl
Select name,sex,address,departmentname
From employees,departments
Where employees.departmentid=departmentname.departmentid
And name=’王林’;
2、 查询财务部、研发部、市场部的员工信息(要求使用三种语法分别实现);
(1) Select *
From employees,salary,departments
Where employees.departmentid=departmentname.departmentid
And employees.employeeid=salary.employeeid
And (departments.departmentname=’研发部’
Or departments.departmentname=’财务部’
Or departments.departmentname=’市场部’);
(2)
Select *
From employees join departments
on employees.departmentid=departmentname.departmentid
where departments.departmentname=’研发部’
Or departments.departmentname=’财务部’
Or departments.departmentname=’市场部’;
(3) Select *
From employees join departments
Using (departmentid)
where departments.departmentname=’研发部’
Or departments.departmentname=’财务部’
Or departments.departmentname=’市场部’;
3、 查询每个雇员的基本情况和薪水情况(要求使用三种语法分别实现);
(1)
Select *
From employees join salary
Using(employeeid);
2)
Select *
From employees, salary
Where employees.employeeid=salary.employeeid;
(3)
Select *
From employees join salary
On employees.employeeid=salary.employeeid;
4、 查询研发部在1970年以前出生的员工姓名和薪水情况;
Select name,income,outcome
From employees, salary,departments
Where employees.employeeid=salary.employeeid
And employees.departmentid=departments.departmentid
And departments.departmentname=’研发部’
And birthday < ‘1997-01-01’;
5、 查询Employees表中员工的姓名、住址和收入水平,要求2000元以下显示为“低收入”,2000-3000显示为“中等收入”,3000以上显示为“高收入”;
Select name,address,
Case
When income<2000 then ‘低收入’
When income>2000 and income<3000 then ‘中等收入’
When income > 3000 then ‘高收入’
End as 收入水平
From employees,salary
Where employees.employeeid=salary.employeeid;
6、 查询选修了“计算机基础”这门课的学生的学号、姓名、课程名及成绩(要求使用三种语法分别实现);
(1)
Select xs.学号,xs.姓名,kc.课程名,xs_kc.成绩
From kc,xs,xs_kc
And xs_kc.课程号=kc.课程号
And 课程名=’计算机基础’;
(2)
Select xs.学号,xs.姓名,kc.课程名,xs_kc.成绩
From xs join xs_kc
On xs.学号=xs_kc.学号
Join kc
on xs_kc.课程号=kc.课程号
where 课程名=’计算机基础’;
(3)
Select xs.学号,xs.姓名,kc.课程名,xs_kc.成绩
From xs join xs_kc
Using(学号)
Join kc
Using(课程号)
where 课程名=’计算机基础’;
7、 查询kc表中所有学生修过的课程名(使用两种方法:一种连接查询,一种使用group_concat)
(1)
Select distinct kc.课程名,xs_kc.课程号
From kc,xs_kc
Where kc.课程号=xs_kc.课程号;
(2)
Select distinct group_concat(课程名)
From kc
Where课程号 in
( select 课程号
From xs_kc
);
8、 查询所有学生的情况及他们选修的课程号,若学生未选修任何课,也要包括其情况;
Select xs.*,课程号
From xs left outer join xs_kc
On xs.学号=xs_kc.学号;
9、 查询被选修的课程号的选修情况和所有开设的课程名;
Select xs_kc.*,课程名
From xs_kc right join kc on xs_kc.课程号=kc.课程号;
10、 (子查询)查询选修了102课程的学生学号、姓名;
Select 学号,姓名
From xs
Where 学号 in
(select 学号
From xs_kc
Where 课程号=’102’
);
11、 (子查询)查询选修了“程序设计与语言”这门课的学生学号;
Select 学号
From xs_kc
Where 课程号 in
(select 课程号
From kc
Where 课程名=’程序设计与语言’
);
12、 (子查询)查询选修了“程序设计与语言”这门课的学生姓名、学号;
Select 姓名,学号
From xs
Where 学号 in
(select 学号
From xs_kc
Where 课程号 in
(select 课程号
From kc
Where 课程名=’ 程序设计与语言’
)
);
13、 (子查询)查询未选修“程序设计与语言”这门课的学生姓名、学号;
Select 姓名,学号
From xs
Where 学号 not in
(select 学号
From xs_kc
Where 课程号 in
(select 课程号
From kc
Where 课程名=’ 程序设计与语言’
)
);
14、 (子查询)查询xs表中比所有“通信工程”专业学生年龄都小的学生学号、姓名、出生时间;
Select 学号,姓名
From xs
Where 出生时间 >all
(select 出生时间
From xs
Where 专业名=’通信工程’
);
15、 (子查询)查询xs表中总学分不低于女生的最低学分的男生的学号、姓名、总学分;
Select 学号,姓名,总学分
From xs
Where 性别=’1’ and 总学分>any
(select 总学分
From xs
Where 性别=’0’
);
16、 (子查询)查询选修了全部课程的学生姓名。
Select 姓名
From xs
Where not exists
(select *
From kc
Where not exists
(select *
From xs_kc
Where 学号=xs.学号 and 课程号=kc.课程号
)
);