-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoin.sql
More file actions
56 lines (35 loc) · 1.07 KB
/
Copy pathjoin.sql
File metadata and controls
56 lines (35 loc) · 1.07 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
-- Create users table with primary key
create table users(
id serial primary key,
username varchar(255) not null
)
-- Create orders table with foreign key referencing users(id)
create table orders(
id serial primary key,
product varchar(255) not null,
user_id int references users(id) on delete cascade
)
-- Insert sample data into users & orders table
insert into users(username) values
('Alice'),('Robert'),('Sorker'),('Epick'),('Emma')
insert into orders(product,user_id) values
('Laptop',1),('Mouse',1),('Keyboard',3),('Phone',4),('Calculator',2)
select * from users
select * from orders
-- INNER JOIN = Join
select * from users
inner join orders on orders.user_id = users.id
select username,count(orders.id) from users
inner join orders on orders.user_id = users.id group by username
-- LEFT JOIN
select * from users
left join orders on orders.user_id = users.id
-- RIGHT JOIN
select * from orders
right join users on orders.user_id = users.id
-- CROSS JOIN
select * from users
cross join orders
-- NATURAL JOIN
select * from users
natural join orders