-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoins. & Aggregation.sql
More file actions
80 lines (75 loc) · 1.8 KB
/
Joins. & Aggregation.sql
File metadata and controls
80 lines (75 loc) · 1.8 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
--Sample Data:
Customers [-]
customer_id [int]
first_name [varchar(100)]
last_name [varchar(100)] age[int]
country [varchar(100)]
Orders [-]
order_id[integer]
item[varchar(100)]
amount[integer]
customer_id [integer]
Shippings [-]
shipping_id[integer]
status[integer]
customer[integer]
-- To identify the orders which is pending
select
cus.customer_id,
cus.first_name,
cus.last_name,
ord.item,
sum(ord.amount) as Order_amt,
ship.shipping_id
from
Customers cus
join Orders ord on cus.customer_id = ord.customer_id
join Shippings ship on cus.customer_id = ship.customer
where
ship.status = 'Pending'
group by
cus.customer_id,
cus.first_name,
ord.item
order by
cus.customer_id --To identify those customers from USA whose items delivered were Keyboard and Mouse
select
cus.customer_id,
cus.first_name,
cus.last_name,
ord.item,
ship.status,
sum(ord.amount) as order_amt
from
Customers cus
join Orders ord on cus.customer_id = ord.customer_id
join Shippings ship on cus.customer_id = ship.customer
where
ord.item IN ('Keyboard', 'Mouse')
and cus.country = 'USA'
and ship.status = 'Delivered'
group by
cus.customer_id,
cus.first_name,
ord.item --To identify the customers who has made the expensive delivered purchase with the item
select
cus.customer_id,
cus.first_name,
cus.last_name,
ord.item,
ship.status,
ord.amount as order_amt
from
Customers cus
join Orders ord on cus.customer_id = ord.customer_id
join Shippings ship on cus.customer_id = ship.customer
where
ord.amount = (
select
max(ord.amount)
from
Customers cus
join Orders ord on cus.customer_id = ord.customer_id
join Shippings ship on cus.customer_id = ship.customer
)
and ship.status = 'Delivered'