實驗四 資料庫查詢--2
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.課程號
)
);