-
Notifications
You must be signed in to change notification settings - Fork 0
/
joins.sql
79 lines (64 loc) · 1.57 KB
/
joins.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
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
use db_oct_2024;
select * from books;
select * from author;
-- cross join
select * from books cross join author;
-- this is also cross join
select * from books join author;
#### insert some books without authors ###
-- insert into books(title, genre)
-- values ("Bible", "mystery"),
-- ("Book A", "horror"),
-- ("Book B", "Thriller"),
-- ("Book C", "horror");
#### add some new authors #################
-- insert into author
-- values (1004, "William", "Shakespeare"),
-- (1005, "Jules", "Verne");
###########################################
-- inner join
select * from books
inner join author
on books.author_id = author.id;
-- inner join again
select * from books
join author
on books.author_id = author.id;
-- writing in a long format
select
books.title,
author.first_name,
author.last_name
from books
join author
on books.author_id = author.id;
-- writing in professional format
select
b.title,
a.first_name,
a.last_name
from books b
join author a
on b.author_id = a.id;
-- left join
select * from
books left join author
on books.author_id = author.id;
-- swap tables in left join
select * from
author left join books
on books.author_id = author.id;
-- find missing entries using left join (left excluding join)
select * from
books left join author
on books.author_id = author.id
where author.id is null;
-- count the missing entries using left join
select count(*) from
books left join author
on books.author_id = author.id
where author.id is null;
-- right join
select * from
books right join author
on books.author_id = author.id;