-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay01.sql
More file actions
143 lines (111 loc) · 3.34 KB
/
Day01.sql
File metadata and controls
143 lines (111 loc) · 3.34 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
-- Amazon Interview Practice 21/02/2024
/* 1. You have two tables: Product and Supplier.
- Product Table Columns: Product_id, Product_Name, Supplier_id, Price
- Supplier Table Columns: Supplier_id, Supplier_Name, Country
*/
-- Write an SQL query to find the name of the product with the highest
-- price in each country.
-- creating the product table
-- creating supplier table
CREATE TABLE suppliers(supplier_id int PRIMARY KEY,
supplier_name varchar(25),
country VARCHAR(25)
);
-- let's insert some values
INSERT INTO suppliers
VALUES(501, 'alan', 'India'),
(502, 'rex', 'US'),
(503, 'dodo', 'India'),
(504, 'rahul', 'US'),
(505, 'zara', 'Canda'),
(506, 'max', 'Canada')
;
CREATE TABLE products(
product_id int PRIMARY KEY,
product_name VARCHAR(25),
supplier_id int,
price float,
FOREIGN KEY (supplier_id) REFERENCES suppliers(supplier_id)
);
INSERT INTO products
VALUES (201, 'iPhone 14', '501', 1299),
(202, 'iPhone 8', '502', 999),
(204, 'iPhone 13', '502', 1199),
(203, 'iPhone 11', '503', 1199),
(205, 'iPhone 12', '502', 1199),
(206, 'iPhone 14', '501', 1399),
(214, 'iPhone 15', '503', 1499),
(207, 'iPhone 15', '505', 1499),
(208, 'iPhone 15', '504', 1499),
(209, 'iPhone 12', '502', 1299),
(210, 'iPhone 13', '502', 1199),
(211, 'iPhone 11', '501', 1099),
(212, 'iPhone 14', '503', 1399),
(213, 'iPhone 8', '502', 1099)
;
-- adding more products
INSERT INTO products
VALUES (222, 'Samsung Galaxy S21', '504', 1699),
(223, 'Samsung Galaxy S20', '505', 1899),
(224, 'Google Pixel 6', '501', 899),
(225, 'Google Pixel 5', '502', 799),
(226, 'OnePlus 9 Pro', '503', 1699),
(227, 'OnePlus 9', '502', 1999),
(228, 'Xiaomi Mi 11', '501', 899),
(229, 'Xiaomi Mi 10', '504', 699),
(230, 'Huawei P40 Pro', '505', 1099),
(231, 'Huawei P30', '502', 1299),
(232, 'Sony Xperia 1 III', '503', 1199),
(233, 'Sony Xperia 5 III', '501', 999),
(234, 'LG Velvet', '505', 1899),
(235, 'LG G8 ThinQ', '504', 799),
(236, 'Motorola Edge Plus', '502', 1099),
(237, 'Motorola One 5G', '501', 799),
(238, 'ASUS ROG Phone 5', '503', 1999),
(239, 'ASUS ZenFone 8', '504', 999),
(240, 'Nokia 8.3 5G', '502', 899),
(241, 'Nokia 7.2', '501', 699),
(242, 'BlackBerry Key2', '504', 1899),
(243, 'BlackBerry Motion', '502', 799),
(244, 'HTC U12 Plus', '501', 899),
(245, 'HTC Desire 20 Pro', '505', 699),
(246, 'Lenovo Legion Phone Duel', '503', 1499),
(247, 'Lenovo K12 Note', '504', 1499),
(248, 'ZTE Axon 30 Ultra', '501', 1299),
(249, 'ZTE Blade 20', '502', 1599),
(250, 'Oppo Find X3 Pro', '503', 1999);
SELECT * FROM suppliers;
SELECT * FROM products;
-- ----------------------------------------------
-- My solution
-- ----------------------------------------------
WITH CTE
AS (SELECT
s.country,
p.product_name,
p.price,
ROW_NUMBER() OVER(PARTITION BY s.country ORDER BY p.price DESC) as rn
FROM products as p
JOIN suppliers as s
ON s.supplier_id = p.supplier_id
)
SELECT
product_name, price,
country
FROM CTE
WHERE rn =1;
-- another approach!
SELECT
product_name,
price,
country
FROM
(SELECT
s.country,
p.product_name,
p.price,
ROW_NUMBER() OVER(PARTITION BY s.country ORDER BY p.price DESC) as rn
FROM products as p
JOIN suppliers as s
ON s.supplier_id = p.supplier_id) x1 -- using alias for the query
WHERE rn = 1;