Repository navigation
Expand file tree
/
Copy pathLeetCodeSQLPractice-Session2.sql
More file actions
166 lines (126 loc) · 3.21 KB
/
Copy pathLeetCodeSQLPractice-Session2.sql
File metadata and controls
166 lines (126 loc) · 3.21 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#LEETCODE SESSION2 JUN21-2025
#SALES AND PRODUCT TABLEs
CREATE TABLE IF NOT EXISTS Sales (
sale_id INT,
product_id INT,
year INT,
quantity INT,
price INT
);
CREATE TABLE IF NOT EXISTS Product (
product_id INT,
product_name VARCHAR(10)
);
TRUNCATE TABLE Sales;
INSERT INTO Sales (sale_id, product_id, year, quantity, price) VALUES
(1, 100, 2008, 10, 5000),
(2, 100, 2009, 12, 5000),
(7, 200, 2011, 15, 9000);
TRUNCATE TABLE Product;
INSERT INTO Product (product_id, product_name) VALUES
(100, 'Nokia'),
(200, 'Apple'),
(300, 'Samsung');
#1068. Product Sales Analysis I
/*
Write a solution to report the product_name, year, and price for each sale_id in the Sales table.
*/
SELECT Product.product_name, year, price
FROM Sales
JOIN Product on Product.Product_id=Sales.Product_id;
#VISITS AND TRANSACTIONS TABLE
CREATE TABLE IF NOT EXISTS Visits (
visit_id INT,
customer_id INT
);
CREATE TABLE IF NOT EXISTS Transactions (
transaction_id INT,
visit_id INT,
amount INT
);
TRUNCATE TABLE Visits;
INSERT INTO Visits (visit_id, customer_id) VALUES
(1, 23),
(2, 9),
(4, 30),
(5, 54),
(6, 96),
(7, 54),
(8, 54);
TRUNCATE TABLE Transactions;
INSERT INTO Transactions (transaction_id, visit_id, amount) VALUES
(2, 5, 310),
(3, 5, 300),
(9, 5, 200),
(12, 1, 910),
(13, 2, 970);
/*
1581. Customer Who Visited but Did Not Make Any Transactions
Write a solution to find the IDs of the users who visited without making any transactions and the number of times they made these types of visits.
*/
# Write your MySQL query statement below
SELECT
v.customer_id,
COUNT(*) AS count_no_trans
FROM
Visits v
LEFT JOIN
Transactions t
ON
v.visit_id = t.visit_id
WHERE
t.transaction_id IS NULL
GROUP BY
v.customer_id;
#WEATHER TABLE
-- Ensure you're using the correct database
USE health;
-- Create the Weather table
CREATE TABLE IF NOT EXISTS Weather (
id INT,
recordDate DATE,
temperature INT
);
-- Clear previous data
TRUNCATE TABLE Weather;
-- Insert records
INSERT INTO Weather (id, recordDate, temperature) VALUES
(1, '2015-01-01', 10),
(2, '2015-01-02', 25),
(3, '2015-01-03', 20),
(4, '2015-01-04', 30);
#Write a solution to find all dates' id with higher temperatures compared to its previous dates (yesterday).
SELECT w1.id
FROM Weather w1
JOIN Weather w2
ON DATEDIFF(w1.recordDate, w2.recordDate) = 1
WHERE w1.temperature > w2.temperature;
#BONUS and EMPLOYEE TABLEs
CREATE TABLE IF NOT EXISTS Employee (
empId INT,
name VARCHAR(255),
supervisor INT,
salary INT
);
CREATE TABLE IF NOT EXISTS Bonus (
empId INT,
bonus INT
);
TRUNCATE TABLE Employee;
INSERT INTO Employee (empId, name, supervisor, salary) VALUES
(3, 'Brad', NULL, 4000),
(1, 'John', 3, 1000),
(2, 'Dan', 3, 2000),
(4, 'Thomas', 3, 4000);
TRUNCATE TABLE Bonus;
INSERT INTO Bonus (empId, bonus) VALUES
(2, 500),
(4, 2000);
/*
Write a solution to report the name and bonus amount of each employee with a bonus less than 1000.
*/
SELECT e.name, b.bonus AS bonus
FROM Employee e
LEFT JOIN Bonus b ON e.empId = b.empId
WHERE b.bonus < 1000 or bonus is null
;