上一篇只是一些基础的SQL查询语句,这篇是关于子查询,多表查询,聚合函数,视图的基础。
子查询。
1. 查找在财务部工作的雇员的情况。
SELECT *FROM Employees WHERE DepartmentID
IN (SELECT DepartmentID FROM Departments WHERE Departmentname ='财务部')
子查询先产生结果集,然后主查询再去结果集里去找符合要求的字段列表去.符合要求的输出,反之则不输出.。
先找到财务部的DepartmentID,再到父(主)查询去寻找符合DepartmentID=‘1’的字段。
关于IN,EXISTS的具体用法会再写一遍博客
2. 查找财务部年龄不低于研发部雇员年龄的雇员的姓名 。
(我实在不知道用哪个子查询才能让财务部所以有的值和研发部的一名进行比较,所以就把财务部的弄了个视图,有别的方法请务必告诉我!!)
创建视图:CREATE VIEW <视图名> AS <语句>
创建一个符合语句的虚表(视图)就可以用这个视图名对虚表数据进行操作
CREATE VIEW cai_wu AS
SELECT Birthday FROM Employees WHERE DepartmentID='1'
SELECT Name FROM cai_wu
WHERE Birthday < (SELECT MIN(Birthday) FROM Employees WHERE DepartmentID='4')
3.查找比所有财务部的雇员收入都高的雇员的姓名
SELECT *FROM Employees
JOIN Salary ON Employees.EmployeeID=Salary.EmployeeID
WHERE Income>=(SELECT MAX(Income) FROM Salary ,Employees
WHERE Salary.EmployeeID=Employees.EmployeeID AND DepartmentID='1')
这里有个小点可以证明是先执行子查询,再执行主查询
因为当写成
SELECT *FROM Employees
JOIN Salary ON Employees.EmployeeID=Salary.EmployeeID
WHERE Income>=(SELECT MAX(Income) FROM Salary WHERE DepartmentID='1')
时,子查询里的数据并不会因为主查询已得到的表(含有两个表信息)去筛选条件
多表查询。
1.查询每个雇员的情况及其薪水的情况(内连接)
SELECT * FROM Salary
JOIN Employees ON Employees.EmployeeID=Salary.EmployeeID
2.使用内连接的方法查询名字为“王林”的员工所在的部门
这是把三个表都连接起来,显然当时用WHERE时是对于前面写的所有已连接的表
SELECT Departmentname,Name FROM Employees
JOIN Salary ON Employees.EmployeeID=Salary.EmployeeID
JOIN Departments ON Departments.DepartmentID=Employees.DepartmentID
WHERE Name='王林'
3. 查找财务部收人在 2000 元以上的雇员姓名及其薪水详情。
SELECT Name,Income,OutCome FROM Employees
JOIN Salary ON Salary.EmployeeID=Employees.EmployeeID
WHERE DepartmentID='1' AND Income>2000
4. 查询研发部在 1976 年以前出生的雇员姓名及其薪水详情。
记住年份越小年龄越大
SELECT Name Salary.* FROM Salary
JOIN Employees ON Employees.EmployeeID=Salary.EmployeeID
WHERE DepartmentID='4' AND Birthday < '1976-01-01'
聚合函数。
1. 求财务部雇员的平均收人。
求平均数AVG(<列名>),返回值是一个值为该列的平均值
SELECT AVG(Income) AS 平均收入 FROM Salary,Employees
WHERE Salary.EmployeeID=Employees.EmployeeID AND DepartmenID='1'
2. 求财务部雇员的平均实际收入。
SELECT (Income-OutCome) AS 平均实际收入 FROM Salary,Employees
WHERE Salary.EmployeeID=Employees.EmployeeID AND DepartmenID='1'
3. 统计财务部收人在 2500 元以上的雇员人数。
SELECT COUNT(*) AS '财务部里工资大于2500的人数' FROM Employees
JOIN Salary ON Salary.EmployeeID=Employees.EmployeeID
WHERE Income>2500 AND DepartmentID='1'
(5) 视图。
1. 创建 yggl1 数据库上的视图 DS_VIEW,视图包含 Departments 表的全部列。
创建视图:CREATE VIEW <视图名> AS <语句>
创建一个符合语句的虚表(视图)就可以用这个视图名对虚表数据进行操作
CREATE VIEW DS_VIEW
AS SELECT * FROM Departments
SELECT * FROM DS_VIEW
2.创建 yggl1 数据库上的视图 Employees_view,视图包含员工号码、姓名和实际收入
CREATE VIEW Employees_view
AS SELECT Employees.EmployeeID,Name,(Income-OutCome) AS 实际收入
FROM Employees,Salary
WHERE Employees.EmployeeID=Salary .EmployeeID
3. 从视图 DS_VIEW 中查询出部门号为 3 的部门名称。
SELECT DepartmentName FROM DS_VIEW WHERE DepartmentID=
4. 从视图 Employees_view中查询姓名为“王林”的员工的实际收人。
SELECT 实际收入 FROM Employees_view WHERE Name='王林'
5. 向视图 DS_VIEW 中插人一行数据“6, 广告部, 广告业务”
INSERT INTO DS_VIEW VALUES ('6','广告部','广告业务')
SELECT *FROM DS_VIEW
数据库数据
USE yggl1 INSERT INTO Departments
VALUES('1','财务部',NULL),
('2','人力资源部',NULL),
('3','经理办公室',NULL),
('4','研发部',NULL),
('5','市场部',NULL);
INSERT INTO Employees
VALUES
('000001','王林','大专','1966-1-23',1,'8','中山路 32-1-508','83355668','2'),
('010008','吴荣华','本科','1976-03-28',1,'3','北京东路100-2','83321321','1'),
('020010','王向荣','硕士','1982-12-09',1,'2','四牌楼40-0108','8792361','1'),
('020018','李丽','大专','1960-07-30',0,'6','中山东路102-2','83413301','1'),
('102201','刘明','本科','1972-10-18',1,'3','虎踞路100-2','83606608','5'),
('102208','朱俊','硕士','1965-09-28',1,'2','牌楼巷5-3-106','84708817','5'),
('108991','钟敏','硕士','1979-08-10',0,'4','中山路10-3-105','83346722','3'),
('111006','张石兵','本科','1974-10-01',1,'1','解放路34-1-203','84563418','5'),
('210678','林涛','大专','1977-04-02',1,'2','中山北路24-35','83467336','3'),
('302566','李玉明','本科','1968-09-20',1,'3','热河路209-3','58765991','4'),
('308759','叶凡','本科','1978-11-18',1,'2','北京西路3-7352','83308901','4'),
('504209','陈林琳','大专','1969-09-03',0,'6','汉中路120-4012','84468158','4')
INSERT INTO Salary
VALUES
('000001','2100.80','123.09'),
('010008','1582.62','88.03'),
('102201','2569.88','185.65'),
('111006','1987.01','79.58'),
('504209','2066.15','108.00'),
('302566','2980.70','210.20'),
('108991','3259.98','281.52'),
('020010','2860.00','198.00'),
('020018','2347.68','180.00'),
('308759','2531.98','199.08'),
('210678','2240.00','121.00'),
('102208','1980.00','100.00')