目录
连接查询(sq99语法)
内连接
等值连接
非等值连接
自链接
外连接
案例
子查询
where或having后面
标量子查询
列子查询
行子查询(结果集一行多列或多行多列)
select 后面
from后面
exists后面
语法:
select 查询列表
from 表1 别名(连接类型)
join 表2 别名
on 连接条件
...
分类:
1.内连接:inner
2.外连接:
左外连接:left(outer)
右外连接:right(outer)
全外连接:full(outer)
3.交叉连接:cross
语法:
select 查询列表
from 表1 别名
inner join 表2 别名
on 连接条件
分类:等值,非等值,自链接
#等值连接
#案例:查询员工名,部门名
SELECT last_name,department_name
FROM employees e
INNER JOIN departments d
ON e.`department_id`=d.`department_id`;
#案例:查询名字中包含e的员工名和工种名
#(包含筛选)
SELECT last_name,job_title
FROM employees e
INNER JOIN jobs j
ON e.`job_id`=j.`job_id`
WHERE last_name LIKE '%e%';
#案例:查询部门个数>3的城市名和部门个数
#(添加分组+筛选)
SELECT city,COUNT(*)
FROM departments d
INNER JOIN locations l
ON d.`location_id`=l.`location_id`
GROUP BY city
HAVING COUNT(*)>3;
#案例:查询员工个数>3的部门的部门名和员工个数,并按个数降序
#(添加排序)
SELECT department_name,COUNT(*) 员工个数
FROM employees e
INNER JOIN departments d
ON d.`department_id`=e.`department_id`
GROUP BY department_name
HAVING COUNT(*)>3
ORDER BY COUNT(*) DESC;
#案例:查询员工名,部门名,工种名,并按部门名降序
#(三表连接)
SELECT last_name,department_name,job_title
FROM employees e
INNER JOIN departments d
ON e.`department_id`=d.`department_id`
INNER JOIN jobs j
ON j.`job_id`=e.`job_id`
ORDER BY department_name DESC;
#非等值连接
#案例:查询员工的工资级别
SELECT salary,grade_level
FROM employees e
JOIN job_grades j
ON e.`salary`
BETWEEN j.`lowest_sal`AND j.`highest_sal`;
#自连接
#查询员工的名字,上级的名字
SELECT e.last_name 员工名,m.last_name 上级名
FROM employees e
INNER JOIN employees m
ON e.`manager_id`=m.`employee_id`;
应用场景:用于查询一个表中有,另一个表中没有的记录
特点:
1.外连接的查询结果为主表中的所有记录:
如果从表中有和它匹配的,则显示匹配的值,如果没有和它匹配的,则显示null
外连接查询结果=内连接结果+主表中有而从表中没有的记录
2.左外连接:left join左边的是主表
3.右外连接:right join右边的是主表
#外连接
#查询哪个部门没有员工
SELECT d.*,e.last_name
FROM departments d
LEFT JOIN employees e
ON d.`department_id`=e.`department_id`
WHERE e.`last_name`IS NULL;
#案例:查询哪个城市没有部门
SELECT l.*,d.department_name
FROM locations l
LEFT JOIN departments d
ON l.`location_id`=d.`location_id`
WHERE d.`department_name`IS NULL;
#案例:查询部门名为SAL或IT的员工信息
SELECT d.department_name,e.*
FROM departments d
LEFT JOIN employees e
ON d.`department_id`=e.`department_id`
WHERE d.`department_name`IN('SAL','IT');
含义:出现在其他语句中的select语句,称为子查询或内查询
分类:
按子查询出现的位置:
1.select后面:仅仅支持标量子查询
2.from后面:支持表子查询
3.where或having后面:标量子查询,列子查询,行子查询
4.exists后面:相关子查询
按结果集的行列数不同:
1.标量子查询:结果集只有一行一列
2.列子查询:结果集只有一列多行
3.行子查询:结果集只有一行多列
4.表子查询:结果集有多行多列
特点:
1.子查询放在小括号内
2.子查询一般放在条件的右侧
3.标量子查询一般搭配着单行操作符使用> < >= <= <> =
4.列子查询一般搭配着多行操作符使用in any/some all
#案例:谁的工资比Abel的高
SELECT *
FROM employees
WHERE salary>(
SELECT salary
FROM employees
WHERE last_name LIKE 'Abel'
);
#案例:返回员工的员工姓名,工种号和工资
#要求工种号与141号员工相同,工资比143号员工多
SELECT last_name,job_id,salary
FROM employees
WHERE job_id=(
SELECT job_id
FROM employees
WHERE employee_id=141
)
AND salary>(
SELECT salary
FROM employees
WHERE employee_id=143
);
#案例:返回员工的员工姓名,工种号和工资
#要求在公司中工资最少
SELECT last_name,job_id,salary
FROM employees
WHERE salary=(
SELECT MIN(salary)
FROM employees
);
#案例:查询部门的部门id和其最低工资
#要求最低工资大于50号部门的最低工资
SELECT MIN(salary),department_id
FROM employees
GROUP BY department_id
HAVING MIN(salary)>(
SELECT MIN(salary)
FROM employees
WHERE department_id=50
);
操作符 | 含义 |
in/not in | 等于列表中的任意一个 |
any/some | 和子查询返回的某一个值比较 |
all | 和子查询返回的所有值比较 |
#列子查询
#案例:返回员工姓名
#要求部门的location——id是1400或1700
SELECT last_name
FROM employees
WHERE department_id IN(
SELECT DISTINCT department_id
FROM departments
WHERE location_id IN(1400,1700)
);
#案例:返回其他工种中员工的工号,姓名,工资
#要求要求工资比工号为IT——PROG工种任一工资低
SELECT job_id,last_name,salary
FROM employees
WHERE salary'IT_PROG'
#案例:返回其他工种中员工的工号,姓名,工资
#要求要求工资比工号为IT——PROG工种所有工资低
SELECT job_id,last_name,salary
FROM employees
WHERE salary'IT_PROG'
#行子查询
#案例:查询员工信息
#要求员工编号最小并且工资最高(普通方法)
SELECT *
FROM employees e
WHERE employee_id=(
SELECT MIN(employee_id)
FROM employees
)
AND salary=(
SELECT MAX(salary)
FROM employees
);
#行子查询
SELECT *
FROM employees
WHERE (employee_id,salary)=(
SELECT MIN(employee_id),MAX(salary)
FROM employees);
#select后面
#案例:查询每个部门的员工个数(子查询)
SELECT d.`department_id`,(
SELECT COUNT(*)
FROM employees e
WHERE e.department_id=d.`department_id`
)员工个数
FROM departments d;
#普通方法
SELECT COUNT(*),e.department_id
FROM employees e
RIGHT JOIN departments d
ON e.`department_id`=d.`department_id`
GROUP BY d.`department_id`;
#案例:查询员工号=102的部门名
SELECT (
SELECT department_name
FROM departments d
JOIN employees e
ON d.department_id=e.department_id
WHERE e.employee_id=102
)部门名
#from后面
#查询每个部门的平均工资的工资等级
SELECT avgsalary.ag,j.grade_level
FROM (
SELECT AVG(salary) ag,department_id
FROM employees e
GROUP BY department_id
)avgsalary
JOIN job_grades j
ON avgsalary.ag BETWEEN j.lowest_sal AND j.highest_sal;
语法:
exists(完整的查询语句)
结果为1或0
#exists后面
#案例:查询有员工的部门名(子查询)
SELECT department_name
FROM departments d
WHERE EXISTS(
SELECT last_name
FROM employees e
WHERE e.`department_id`=d.`department_id`
);
#普通查询
SELECT department_name
FROM departments d
LEFT JOIN employees e
ON e.`department_id`=d.`department_id`
WHERE e.`employee_id`IS NOT NULL
GROUP BY department_name;
#in
SELECT department_name
FROM departments d
WHERE d.`department_id` IN(
SELECT department_id
FROM employees
);