-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoins.sql
More file actions
105 lines (83 loc) · 1.78 KB
/
joins.sql
File metadata and controls
105 lines (83 loc) · 1.78 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
select
order_id,
o.customer_id,
first_name,
last_name
from
orders o
join
customers c
on o.customer_id = c.customer_id;
-- self join
USE sql_hr;
select
e.employee_id,
e.first_name as employee,
m.first_name as manager
from employees e
join employees m
on e.reports_to = m.employee_id
limit 10;
-- Self Outer join
USE sql_hr;
select
e.employee_id,
e.first_name as employee,
m.first_name as manager
from employees e
left join employees m
on e.reports_to = m.employee_id
limit 10;
-- Joining multiple tables
select
o.order_id,
o.order_date,
c.first_name,
c.last_name,
os.name as status
from orders o
join customers c
on o.customer_id = c.customer_id
join order_statuses os
on o.status = os.order_status_id;
-- Implicit join syntax
select * from customers c, orders o where c.customer_id = o.customer_id;
-- Outer Joins
select
c.customer_id,
c.first_name,
o.order_id,
sh.name as name
from customers c
left join orders o
on c.customer_id = o.customer_id
left join shippers sh
on o.shipper_id = sh.shipper_id
order by c.customer_id;
-- Using clause
select *
from order_items oi
left join order_item_notes oin
using (order_id, product_id);
-- Natural Join (sql engine identifies the common column and joins the tables.)
select * from orders o natural join customers c;
-- Cross Join (every record from first table joins with every record from second table [n*m].)
select * from orders o cross join customers c;
-- Union(used to combine records from multiple queries)
select
order_id,
order_date,
'active' as status
from
orders
where
order_date >='2018-01-01'
union
select
order_id,
order_date,
'archived' as status
from
orders
where
order_date <'2018-01-01';