//----------Connection query summary----------//
1. Meaning
When fields of multiple tables are involved in the query, multi-table joins are required;
SELECT 字段1,字段2
FROM 表1,表2,...;
Cartesian product: when multiple tables are queried, no effective connection conditions are added, resulting in full connection of all rows in multiple tables;
How to fix: add a valid join condition;
2. Classification
Classified by age:
sql92:
equivalence
non-equivalence
self-join
Also supports some external joins (for oracle, sqlserver, mysql does not support)
sql99 [recommended use]
inner join
equivalent
non-equivalent
self join
outer join
left outer
right outer
all outer (not supported by mysql)
cross join
Three, SQL92 syntax
1. Equivalent connection
(1) Grammar
SELECT 查询列表
FROM 表1 别名,表2 别名
WHERE 表1.key=表2.key
【AND 筛选条件】
【GROUP BY 分组字段】
【HAVING 分组后的筛选】
【ORDER BY 排序字段】
(2) Features
① Generally, an alias is given to the table;
② The order of multiple tables can be exchanged;
③ At least n-1 connection conditions are required for the connection of n tables;
④ The result of equivalent connection is the intersection of multiple tables;
(3) Case
Case 1: Query the goddess name and the corresponding male god name
SELECT NAME,boyName
FROM boys,beauty
WHERE beauty.boyfriend_id = boys.id;
Case 2: query employee name and corresponding department name
SELECT last_name,department_name
FROM employees,departments
WHERE employees.department_id=departments.department_id;
Case 3: Query employee name, job number, job name
#起别名的好处
/*
①提高语句的简洁度
②区分多个重名的字段
注意:如果为表起了别名,则查询的字段就不能使用原来的表名去限定
*/
#方式一
SELECT e.last_name,e.job_id,j.job_title
FROM employees e,jobs j
WHERE e.`job_id`=j.`job_id`;
#方式二
SELECT e.last_name,e.job_id,j.job_title
FROM jobs j,employees e
WHERE e.`job_id`=j.`job_id`;
Case 4: Query the employee name and department name with bonus --- plus filter
SELECT last_name,department_name,commission_pct
FROM employees e,departments d
WHERE e.`department_id`=d.`department_id`
AND e.`commission_pct` IS NOT NULL;
Case 5: Query the department name and city name whose second character is o in the city name ------ plus filter
SELECT department_name,city
FROM departments d,locations l
WHERE d.`location_id` = l.`location_id`
AND city LIKE '_o%';
Case 6: Query the number of departments in each city--- add grouping
SELECT COUNT(*) 个数,city
FROM departments d,locations l
WHERE d.`location_id`=l.`location_id`
GROUP BY city;
Case 7: Query the department name and department leader number of each department with bonuses and the minimum salary of the department ------ plus grouping
SELECT department_name,d.`manager_id`,MIN(salary)
FROM departments d,employees e
WHERE d.`department_id`=e.`department_id`
AND commission_pct IS NOT NULL
GROUP BY department_name,d.`manager_id`;
Case 8: Query the name of each type of work and the number of employees, and sort by the number of employees in descending order ------ plus sorting
SELECT job_title,COUNT(*)
FROM employees e,jobs j
WHERE e.`job_id`=j.`job_id`
GROUP BY job_title
ORDER BY COUNT(*) DESC;
Case 9: Query employee name, department name and city --- three table connection
SELECT last_name,department_name,city
FROM employees e,departments d,locations l
WHERE e.`department_id`=d.`department_id`
AND d.`location_id`=l.`location_id`
AND city LIKE 's%'
ORDER BY department_name DESC;
2. Non-equivalent connection
(1) Grammar
SELECT 查询列表
FROM 表1 别名,表2 别名
WHERE 非等值的连接条件
【AND 筛选条件】
【GROUP BY 分组字段】
【HAVING 分组后的筛选】
【ORDER BY 排序字段】
(2) Case
Case 1: Query the employee's salary and salary level
SELECT salary,grade_level
FROM employees e,job_grades g
WHERE salary BETWEEN g.`lowest_sal` AND g.`highest_sal`
AND g.`grade_level`='A';
3. Self-connection
(1) Grammar
SELECT 查询列表
FROM 表 别名1,表 别名2
WHERE 等值的连接条件
【AND 筛选条件】
【GROUP BY 分组字段】
【HAVING 分组后的筛选】
【ORDER BY 排序字段】
(2) Case
Case 1: Query the name of the employee and the name of the superior
SELECT e.employee_id,e.last_name,m.employee_id,m.last_name
FROM employees e,employees m
WHERE e.`manager_id`=m.`employee_id`;
Four, SQL99 syntax
1. Inner connection
(1) Grammar
SELECT 查询列表
FROM 表1 别名
【INNER】 JOIN 表2 别名 ON 连接条件
WHERE 筛选条件
GROUP BY 分组列表
HAVING 分组后的筛选
ORDER BY 排序列表
LIMIT 子句;
(2) Features
① The order of the tables can be reversed
② The result of the inner join = the intersection of multiple tables
③ The n-table connection requires at least n-1 connection conditions
(3) classification
Equijoin
Non-equijoin
Self-join
(4) Case
1) Equivalent connection
Case 1: query employee name, department name
SELECT last_name,department_name
FROM departments d
JOIN employees e
ON e.`department_id` = d.`department_id`;
Case 2: Query the employee name and job type name containing e in the name (add filter)
SELECT last_name,job_title
FROM employees e
INNER JOIN jobs j
ON e.`job_id`= j.`job_id`
WHERE e.`last_name` LIKE '%e%';
Case 3: Query the city name and the number of departments with the number of departments > 3 (add group + filter)
#①查询每个城市的部门个数
#②在①结果上筛选满足条件的
SELECT city,COUNT(*) 部门个数
FROM departments d
INNER JOIN locations l
ON d.`location_id`=l.`location_id`
GROUP BY city
HAVING COUNT(*)>3;
Case 4: Query which department has the number of employees > 3 department names and the number of employees, and sort them in descending order (add sorting)
#①查询每个部门的员工个数
SELECT COUNT(*),department_name
FROM employees e
JOIN departments d
ON e.`department_id`=d.`department_id`
GROUP BY department_name
#②在①结果上筛选员工个数>3的记录,并排序
SELECT COUNT(*) 个数,department_name
FROM employees e
INNER JOIN departments d
ON e.`department_id`=d.`department_id`
GROUP BY department_name
HAVING COUNT(*)>3
ORDER BY COUNT(*) DESC;
Case 5: Query employee name, department name, job name, and descending order by department name (add three table connections)
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 e.`job_id` = j.`job_id`
ORDER BY department_name DESC;
2) Non-equivalent connection
Case 1: Query the salary level of an employee
SELECT salary,grade_level
FROM employees e
JOIN job_grades g
ON e.`salary` BETWEEN g.`lowest_sal` AND g.`highest_sal`;
Case 2: Query the number of salary levels > 20, and sort by salary level in descending order
SELECT COUNT(*),grade_level
FROM employees e
JOIN job_grades g
ON e.`salary` BETWEEN g.`lowest_sal` AND g.`highest_sal`
GROUP BY grade_level
HAVING COUNT(*)>20
ORDER BY grade_level DESC;
3) Self-join
Case 1: Query the name of the employee and the name of the superior
SELECT e.last_name,m.last_name
FROM employees e
JOIN employees m
ON e.`manager_id`= m.`employee_id`;
Case 2: Query the name of the employee whose name contains the character k, the name of the superior
SELECT e.last_name,m.last_name
FROM employees e
JOIN employees m
ON e.`manager_id`= m.`employee_id`
WHERE e.`last_name` LIKE '%k%';
2. Outer connection
(1) Grammar
SELECT 查询列表
FROM 表1 别名
LEFT|RIGHT|FULL【OUTER】 JOIN 表2 别名 ON 连接条件
WHERE 筛选条件
GROUP BY 分组列表
HAVING 分组后的筛选
ORDER BY 排序列表
LIMIT 子句;
(2) Features
①Query result = all the rows in the main table, if the slave table matches it, it will display the matching row, if there is no match from the slave table, it will display NULL;
②The left side of LEFT JOIN is the main table, and the right side of RIGHT JOIN is the main table;
both sides of FULL JOIN are the main table;
③ It is generally used to query the remaining unmatched rows except for the intersection part;
(3) Case
Case 1: Query which department has no employees
#左外
SELECT d.*,e.employee_id
FROM departments d
LEFT OUTER JOIN employees e
ON d.`department_id` = e.`department_id`
WHERE e.`employee_id` IS NULL;
#右外
SELECT d.*,e.employee_id
FROM employees e
RIGHT OUTER JOIN departments d
ON d.`department_id` = e.`department_id`
WHERE e.`employee_id` IS NULL;
3. Cross connection
(1) Grammar
SELECT 查询列表
FROM 表1 别名
CROSS JOIN 表2 别名;
(1) Features
Similar to Cartesian product;