Advanced 6: join query two, sql99 grammar

# Two, sql99 syntax
/ *
Syntax:
the SELECT list of queries
from Table 1 alias [Connection Type]
join in Table 2 aliases
on the connection conditions
[where] Filters
[group] group by
[having] Filters
[order by sorting the list]

Classification:
the connector (★): inner
outer connecting
left outer (★): left [outer]
right outer (★): right [outer]
Full outer: full] [outer
cross-connect: cross

*/


# A) en
/ *
Syntax:

select a list of queries
from Table 1 Alias
inner join Alias Table 2
on the connection condition;

Category:
Equivalent
non-equivalent
self-join

Features:
① adding sorting, grouping, screening
②inner may be omitted
③ filters on the back where, on the back connection conditions on, to improve the separation, easy to read
④inner join connections and equivalent connections syntax sql92 effect is the same, are It is the intersection of a multi-table query

 

 

*/


# 1, the equivalent connection
# Case 1. queries employee name, department name

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

 

Case # 2 employees names and types of queries name (Add Filter) contained in e name of
the SELECT last_name, JOB_TITLE
the FROM the Employees e
the INNER JOIN Jobs J
ON e.`job_id` = j.`job_id`
the WHERE e.`last_name` the LIKE '% e% ';

 

# 3. The number of inquiries department> City and department number 3 (+ add packet filtering)

# ① query each city department number
# ② ① screened to meet the conditions on the results of the
SELECT city, COUNT (*) The number of sectors
the FROM the Departments d
the INNER JOIN locations L
ON d.`location_id` = l.`location_id`
the GROUP City BY
the HAVING COUNT (*)>. 3;

 


4. Case # query the number of employees which department> department name and employee number 3, press the number descending (add a sort)

# ① query number of employees in each department of
the SELECT COUNT (*), department_name
the FROM the Employees E
the INNER JOIN the Departments d
ON e.`department_id` = d.`department_id`
the GROUP BY department_name

# ② ① screening the number of employees on the results of> 3 record and 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;

# 5. Queries staff name, department name, the name of jobs, according to the department name in descending (add three tables connection)

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;

# B) non-equivalent connection

# Query wage level employees

The salary the SELECT, grade_level
the FROM E the Employees
the JOIN G job_grades
the ON e.`salary` the BETWEEN g.`lowest_sal` the AND g.`highest_sal`;


# pay level inquiry number> number of 20, and the wage level in descending order according
SELECT COUNT ( *), grade_level
the fROM E the Employees
the JOIN G job_grades
the ON e.`salary` the BETWEEN g.`lowest_sal` the AND g.`highest_sal`
the GROUP BY grade_level
the HAVING COUNT (*)> 20 is
the ORDER BY grade_level DESC;


# c) connected from

# query employee's name, the name of the higher
SELECT e.LAST_NAME, m.last_name
the FROM E the employees
the JOIN m the employees
the ON e.`manager_id` = m.`employee_id`;

# query character name contains the name of the employee k, the higher name
SELECT e.LAST_NAME, m.last_name
the FROM E the Employees
the JOIN the Employees m
E.`manager_id` = m.`employee_id` the ON
the WHERE e.`last_name` the LIKE '% K%';


# two, external connection

/ *
scenarios: a query for a table has, there is no record of another table

characteristics :
1, the outer connector main query results in the table all records
if it matches and, the display matches the value from the table
if the table and it does not match, then the null display
connection outer join query result = + the results from the primary table, there is no record of the table
2, connected to the left outer, left join the left is the main table
right outer join, right join right is the main table
3, the left and right outer two outer switching order table, you can achieve the same effect
4, all connected to the outer connection results within + = table 1 table 2, but there is no + in table 2, but not in table 1
* /
# introduced: query table boyfriend not god goddess the name of

the SELECT * the FROM Beauty;
the SELECT * the FROM Boys;

# left outer connecting
the SELECT B *, BO *..
the FROM BO Boys
the lEFT oUTER the JOIN Beauty B
the ON b.`boyfriend_id` = bo.`id`
the WHERE b.`id` the IS NULL;


# case 1: query which department employees have not
# left outside
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;


#全外


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

# Cross-connect

the SELECT b *, BO *..
The FROM Beauty b
the CROSS JOIN Boys BO;



# SQL92 and sql99pk
/ *
Function: sql99 support more
readable: sql99 separation connection conditions and filter conditions, than readability high
* /

Guess you like

Origin www.cnblogs.com/Diyo/p/11358952.html