-
Notifications
You must be signed in to change notification settings - Fork 0
/
left_join.sql
68 lines (64 loc) · 1.11 KB
/
left_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
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
CREATE TABLE IF NOT EXISTS film (
film_id SERIAL PRIMARY KEY,
title TEXT NOT NULL
);
-- inventory jadvali
CREATE TABLE IF NOT EXISTS inventory (
inventory_id SERIAL PRIMARY KEY,
film_id INT,
-- Qo'shimcha ustunlar uchun kerakli ma'lumotlarni qo'shing
FOREIGN KEY (film_id) REFERENCES film(film_id)
);
-- film jadviga ma'lumot qo'shish
INSERT INTO film (title)
VALUES
('Film1'),
('Film2'),
('Film3');
-- inventory jadviga ma'lumot qo'shish
INSERT INTO inventory (film_id)
VALUES
(1),
(2);
-- LEFT JOIN
SELECT
film.film_id,
title,
inventory_id
FROM
film
LEFT JOIN inventory
ON inventory.film_id = film.film_id
ORDER BY title;
--
SELECT
film.film_id,
film.title,
inventory_id
FROM
film
LEFT JOIN inventory
ON inventory.film_id = film.film_id
WHERE inventory.film_id IS NULL
ORDER BY title;
--
SELECT
f.film_id,
title,
inventory_id
FROM
film f
LEFT JOIN inventory i
ON i.film_id = f.film_id
WHERE i.film_id IS NULL
ORDER BY title;
--
SELECT
f.film_id,
title,
inventory_id
FROM
film f
LEFT JOIN inventory i USING (film_id)
WHERE i.film_id IS NULL
ORDER BY title;