Database_07_connection query

Advanced 6: Connect query

Meaning: also known as multi-table query, when the query field comes from multiple tables, it will use
Cartesian product phenomenon: Table 1 has m rows, Table 2 has n rows, and the result = m*n rows
. Reason: No Effective link conditions
How to avoid: Add effective link conditions

Classification: Classified
by age:
sql192 standard: only supports inner connection
sql199 standard (recommended): supports all but all
by function classification:
inner connection: equal value connection non-equivalent connection, self connection
, outer connection: left outer connection, right outer connection Full external connection
Cross connection

#--------------------------------------
This article only talks about the sql199 standard

Syntax:
select query list
from table 1 alias [connection type]
join table 2 alias
on connection condition
[where filter condition]
[group by]
[having]
[order by]

Category:
inner connection: inner
outer connection
left outer: left [outer]
right outer: right [outer]
all outer: full[outer]
cross connection: cross

#--------------------------------------
#一, internal connection
Features:
1. Can be added Sorting, grouping, and filtering
2. inner can be omitted
3. The filter condition is placed after where, and the connection condition is placed after on, which improves the separation and is easy to read.
4. The inner join has the same effect as the equivalent join in the sql92 syntax, both of which are queries Intersection of multiple tables

#1.等值连接
# Case 1: Query employee name, department name

SELECT last_name,department_name
FROM employees e
INNER JOIN departments d
ON e.`department_id`=d.`department_id`;

#Case 2: Query the name of the employee and type of work that contains e (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 number of departments "3 city name and number of departments (group + filter)

SELECT COUNT(*),city
FROM departments d
INNER JOIN locations l
ON d.`location_id`=l.`location_id`
GROUP BY city
HAVING COUNT(*)>3; 

#Case 4: Query the number of employees in which department "3 Department name and number of employees, and in descending order by number (add sort)

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 type name, and descending order by department name

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.非等值连接
#Query employee salary level

SELECT salary,grade_level
FROM employees e
INNER JOIN job_grades j
ON e.salary BETWEEN j.lowest_sal AND j.highest_sal;

#Query the number of each salary level, and in descending order by salary level

SELECT COUNT(*),grade_level
FROM employees e
INNER JOIN job_grades j
ON e.salary BETWEEN j.lowest_sal AND j.highest_sal
GROUP BY grade_level
ORDER BY grade_level DESC;

#3.自连接
#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
INNER JOIN employees m
ON e.manager_id=m.employee_id
WHERE e.`last_name` LIKE '%k%';

#-------------------------------------- #Two
, external connection
application scenario: for query Records in one table but not in the other table

Features:
1. The query result of the outer join is all records in the main table.
If there is a match from the table, the matching value will be displayed.
If there is no match from the table, the NULL
outer join query result = inner Join query result + records in the main table but not in the secondary table
2. Left outer join: left join is the main table on the left,
right outer join: right join is the main table on the right
3. The left and right outer exchange order, the effect is the same
4 .Full outer join = result of inner join + table 1 has but table 2 does not + table 2 has but table 1 does not

#Introduction: Query the name of the goddess whose boyfriend is not in the male god table

SELECT be.`name`
FROM beauty be
LEFT JOIN boys bo
ON be.`boyfriend_id`=bo.`id`
WHERE bo.`id` IS NULL;

#Case 1: Query which department has no employees

SELECT DISTINCT(department_name)
FROM departments d
LEFT JOIN employees e
ON d.`department_id`=e.`department_id`
WHERE e.`employee_id` IS NULL;

#Three, all external connection SQL does not support

SELECT b.*,bo.*
FROM beauty b
FULL JOIN boys bo
ON b.boyfriend_id=bo.id;

#Cross connection is equivalent to Cartesian product

SELECT be.*,bo.*
FROM beauty be
CROSS JOIN boys bo;

#--------------------------------------
#测试
#1. Query number "3 goddess The boyfriend's information, if it is listed in detail, it is not filled with NULL

SELECT bo.*
FROM beauty be
LEFT JOIN boys bo
ON be.`boyfriend_id`=bo.`id`
WHERE be.`id`>3;

#2. Query which city has no department

SELECT l.`city`
FROM locations l
LEFT JOIN departments d
ON l.`location_id`=d.`location_id`
WHERE d.`department_id` IS NULL;

#3. Query the information of employees whose department name is SAL or IT

SELECT e.*,d.`department_name`
FROM employees e
RIGHT JOIN departments d
ON e.`department_id`=d.`department_id`
WHERE d.`department_name`IN('SAL','IT');

Guess you like

Origin blog.csdn.net/Yungang_Young/article/details/104583764