-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoins.sql
More file actions
83 lines (72 loc) · 1.71 KB
/
Joins.sql
File metadata and controls
83 lines (72 loc) · 1.71 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
--gell all customer along with order, including orders without matching customer do with right join and same from left
Select
c.id,
c.first_name,
o.customer_id,
o.sales
from customers as c
right join orders as o
on c.id=o.customer_id
Select
c.id,
c.first_name,
o.customer_id,
o.sales
from orders as o
left join customers as c
on c.id=o.customer_id
--get all customers and all orders, even if there is no match
Select *
from customers as c
full join orders as o
on c.id=o.customer_id
--get all customer who does not order anything
--left anti join - return rows from left that has no match in right
Select
c.id,
c.first_name,
o.order_id,
o.sales
from customers as c
left join orders as o
on c.id=o.customer_id
where o.customer_id is null
--right anti join - return rows from right that has no match in left
--solve this from left anti join
Select
c.id,
c.first_name,
o.order_id,
o.sales
from customers as c
right join orders as o
on c.id=o.customer_id
where c.id is null
Select
c.id,
c.first_name,
o.order_id,
o.sales
from orders as o
left join customers as c
on c.id=o.customer_id
where c.id is null
-- full anti join - only unmatching rows from both side
--find custo without order and orders without customer
select *
from orders as o
full join customers as c
on c.id = o.customer_id
where
c.id is null or o.customer_id is null;
-- advanced inner join - return only matching from left and right
--get all custo with their orders but only customer who placedd the order - without using inner joins
select *
from customers as c
left join orders as o
on c.id = o.customer_id
where o.customer_id is not null
-- generate all possible combinations of custo and order
select *
from customers
cross join orders