-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLtricks.sql
More file actions
75 lines (46 loc) · 1.54 KB
/
SQLtricks.sql
File metadata and controls
75 lines (46 loc) · 1.54 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
Create Database sql_practise
use sql_practise
---Creating table product
Create table Product
(
Product_id INT PRIMARY KEY,
Product_name VARCHAR(50),
quantity INT ,
sales_unit INT ,
price INT ,
city VARCHAR(30)
);
---INSERT DATA'S
INSERT INTO PRODUCT VALUES
(1, 'Laptop', 15, 40, 60000, 'Mumbai'),
(2, 'Smartphone', 20, 50, 25000, 'Ahmedabad'),
(3, 'Tablet', 0, 18, 18000, 'Pune'),
(4, 'Headphones', 5, 12, 3000, 'Vadodara'),
(5, 'Keyboard', 8, 25, 2000, 'Vadodara'),
(6, 'Monitor', 6, 42, 12000, 'Surat'),
(7, 'Mouse', 10, 38, 1500, 'Delhi'),
(8, 'Printer', 9, 56, 9000, 'Rajkot'),
(9, 'Smartwatch', 12, 40, 15000, 'Mumbai'),
(10,'Camera', 18, 30, 45000, 'Surat');
--TASK SET 1 — SUBQUERIES (YOU PRACTICE ALL HERE)
---Use BOTTOM → TOP for every question.
--Basic Subqueries
--Quantity > average quantity
Products in same city as Laptop
Product with maximum quantity
Quantity < minimum quantity of Vadodara
Sales_unit > average sales_unit of Mumbai
🔹 IN / ANY / ALL
Products in cities where quantity = 0 exists
Sales_unit > ALL products in Surat
Quantity > ANY product in Pune
Product names same as any sold in Ahmedabad
🔹 Group + Subquery
Cities with total quantity > 20
Products from cities with avg sales_unit > 30
City having highest product count
🔹 Correlated Subquery
Products whose quantity > average of their own city
🔹 Calculation
Product name + quantity difference from global average
👉 All 14 are solvable using only Product table