-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathself_outer_join.sql
36 lines (32 loc) · 905 Bytes
/
self_outer_join.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* "use sql_hr" from MYSQL tables employees*/
-- use sql_hr
SELECT
e.employee_id,
e.first_name,
m.first_name AS head
FROM employees e
LEFT JOIN employees m
ON e.reports_to = m.employee_id
-- USING(customer_id) identical to above
LEFT JOIN shippers sh
ON o.shipper_id = sh.shipper_id
-- USING(shipper_id) identical to above
/* USING() is used when we join two tables with same column*/
/*----------------*/
SELECT *
FROM order_items oi
JOIN order_item_notes oin
ON oi.order_id = oin.order_id AND oi.product_id = oin.product_id
-- USING(order_id,product_id) for multiple columns
/* "use sql_invoicing" from MYSQL tables */
-- use sql_invoicing
SELECT
p.date,
c.name AS client,
p.amount,
pm.name AS payment_method
FROM payments p
JOIN clients c
USING (client_id)
JOIN payments_methods pm
ON p.payment_method = pm.payment_method_id