├── 1.sql ├── 10.sql ├── 11.sql ├── 12.sql ├── 13.sql ├── 14.sql ├── 15.sql ├── 16.sql ├── 17.sql ├── 18.sql ├── 19.sql ├── 2.sql ├── 20.sql ├── 21.sql ├── 22.sql ├── 23.sql ├── 24.sql ├── 25.sql ├── 26 of 30.sql ├── 27 of 30 .sql ├── 28 of 30.sql ├── 29 of 30 days.sql ├── 3.sql ├── 4.sql ├── 5.sql ├── 6.sql ├── 7.sql ├── 8.sql ├── 9.sql ├── Day 30 of 30.sql ├── README.md └── SQL Challenge 0130.png /1.sql: -------------------------------------------------------------------------------- 1 | -- Amazon Interview Practice 21/02/2024 2 | 3 | 4 | /* 1. You have two tables: Product and Supplier. 5 | - Product Table Columns: Product_id, Product_Name, Supplier_id, Price 6 | - Supplier Table Columns: Supplier_id, Supplier_Name, Country 7 | */ 8 | 9 | 10 | 11 | -- Write an SQL query to find the name of the product with the highest 12 | -- price in each country. 13 | 14 | -- creating the product table 15 | 16 | -- creating supplier table 17 | 18 | CREATE TABLE suppliers(supplier_id int PRIMARY KEY, 19 | supplier_name varchar(25), 20 | country VARCHAR(25) 21 | ); 22 | -- let's insert some values 23 | 24 | INSERT INTO suppliers 25 | VALUES(501, 'alan', 'India'), 26 | (502, 'rex', 'US'), 27 | (503, 'dodo', 'India'), 28 | (504, 'rahul', 'US'), 29 | (505, 'zara', 'Canda'), 30 | (506, 'max', 'Canada') 31 | ; 32 | 33 | CREATE TABLE products( 34 | 35 | product_id int PRIMARY KEY, 36 | product_name VARCHAR(25), 37 | supplier_id int, 38 | price float, 39 | FOREIGN KEY (supplier_id) REFERENCES suppliers(supplier_id) 40 | ); 41 | 42 | INSERT INTO products 43 | VALUES (201, 'iPhone 14', '501', 1299), 44 | (202, 'iPhone 8', '502', 999), 45 | (204, 'iPhone 13', '502', 1199), 46 | (203, 'iPhone 11', '503', 1199), 47 | (205, 'iPhone 12', '502', 1199), 48 | (206, 'iPhone 14', '501', 1399), 49 | (214, 'iPhone 15', '503', 1499), 50 | (207, 'iPhone 15', '505', 1499), 51 | (208, 'iPhone 15', '504', 1499), 52 | (209, 'iPhone 12', '502', 1299), 53 | (210, 'iPhone 13', '502', 1199), 54 | (211, 'iPhone 11', '501', 1099), 55 | (212, 'iPhone 14', '503', 1399), 56 | (213, 'iPhone 8', '502', 1099) 57 | ; 58 | 59 | -- adding more products 60 | 61 | INSERT INTO products 62 | VALUES (222, 'Samsung Galaxy S21', '504', 1699), 63 | (223, 'Samsung Galaxy S20', '505', 1899), 64 | (224, 'Google Pixel 6', '501', 899), 65 | (225, 'Google Pixel 5', '502', 799), 66 | (226, 'OnePlus 9 Pro', '503', 1699), 67 | (227, 'OnePlus 9', '502', 1999), 68 | (228, 'Xiaomi Mi 11', '501', 899), 69 | (229, 'Xiaomi Mi 10', '504', 699), 70 | (230, 'Huawei P40 Pro', '505', 1099), 71 | (231, 'Huawei P30', '502', 1299), 72 | (232, 'Sony Xperia 1 III', '503', 1199), 73 | (233, 'Sony Xperia 5 III', '501', 999), 74 | (234, 'LG Velvet', '505', 1899), 75 | (235, 'LG G8 ThinQ', '504', 799), 76 | (236, 'Motorola Edge Plus', '502', 1099), 77 | (237, 'Motorola One 5G', '501', 799), 78 | (238, 'ASUS ROG Phone 5', '503', 1999), 79 | (239, 'ASUS ZenFone 8', '504', 999), 80 | (240, 'Nokia 8.3 5G', '502', 899), 81 | (241, 'Nokia 7.2', '501', 699), 82 | (242, 'BlackBerry Key2', '504', 1899), 83 | (243, 'BlackBerry Motion', '502', 799), 84 | (244, 'HTC U12 Plus', '501', 899), 85 | (245, 'HTC Desire 20 Pro', '505', 699), 86 | (246, 'Lenovo Legion Phone Duel', '503', 1499), 87 | (247, 'Lenovo K12 Note', '504', 1499), 88 | (248, 'ZTE Axon 30 Ultra', '501', 1299), 89 | (249, 'ZTE Blade 20', '502', 1599), 90 | (250, 'Oppo Find X3 Pro', '503', 1999); 91 | 92 | 93 | 94 | SELECT * FROM suppliers; 95 | SELECT * FROM products; 96 | 97 | 98 | 99 | 100 | 101 | 102 | -- ---------------------------------------------- 103 | -- My solution 104 | -- ---------------------------------------------- 105 | 106 | 107 | 108 | 109 | WITH CTE 110 | AS (SELECT 111 | s.country, 112 | p.product_name, 113 | p.price, 114 | ROW_NUMBER() OVER(PARTITION BY s.country ORDER BY p.price DESC) as rn 115 | FROM products as p 116 | JOIN suppliers as s 117 | ON s.supplier_id = p.supplier_id 118 | ) 119 | SELECT 120 | product_name, price, 121 | country 122 | FROM CTE 123 | WHERE rn =1; 124 | 125 | -- another approach! 126 | SELECT 127 | product_name, 128 | price, 129 | country 130 | FROM 131 | (SELECT 132 | s.country, 133 | p.product_name, 134 | p.price, 135 | ROW_NUMBER() OVER(PARTITION BY s.country ORDER BY p.price DESC) as rn 136 | FROM products as p 137 | JOIN suppliers as s 138 | ON s.supplier_id = p.supplier_id) x1 -- using alias for the query 139 | WHERE rn = 1; 140 | 141 | 142 | -- Q.2 143 | 144 | /* 145 | You have two tables: Customer and Transaction. 146 | - Customer Table Columns: Customer_id, Customer_Name, Registration_Date 147 | - Transaction Table Columns: Transaction_id, Customer_id, Transaction_Date, Amount 148 | 149 | -- Write an SQL query to calculate the total transaction amount for each customer for the current year. 150 | The output should contain Customer_Name and the total amount. 151 | */ 152 | 153 | -- 1. find total transaction amt group by each customer filter with current year 154 | -- put where condition to check if the transaction are current year year 1 155 | 156 | 157 | 158 | -- Create Customer table 159 | CREATE TABLE Customers ( 160 | Customer_id INT PRIMARY KEY, 161 | Customer_Name VARCHAR(100), 162 | Registration_Date DATE 163 | ); 164 | 165 | -- Create Transaction table 166 | CREATE TABLE Transaction ( 167 | Transaction_id INT PRIMARY KEY, 168 | Customer_id INT, 169 | Transaction_Date DATE, 170 | Amount DECIMAL(10, 2), 171 | FOREIGN KEY (Customer_id) REFERENCES Customers(Customer_id) 172 | ); 173 | 174 | -- Insert records into Customer table 175 | INSERT INTO Customers (Customer_id, Customer_Name, Registration_Date) 176 | VALUES 177 | (1, 'John Doe', '2023-01-15'), 178 | (2, 'Jane Smith', '2023-02-20'), 179 | (3, 'Michael Johnson', '2023-03-10'); 180 | 181 | -- Insert records into Transaction table 182 | INSERT INTO Transaction (Transaction_id, Customer_id, Transaction_Date, Amount) 183 | VALUES 184 | (201, 1, '2024-01-20', 50.00), 185 | (202, 1, '2024-02-05', 75.50), 186 | (203, 2, '2023-02-22', 100.00), 187 | (204, 3, '2022-03-15', 200.00), 188 | (205, 2, '2024-03-20', 120.75), 189 | (301, 1, '2024-01-20', 50.00), 190 | (302, 1, '2024-02-05', 75.50), 191 | (403, 2, '2023-02-22', 100.00), 192 | (304, 3, '2022-03-15', 200.00), 193 | (505, 2, '2024-03-20', 120.75); 194 | 195 | 196 | 197 | SELECT * FROM customers; 198 | SELECT * FROM transaction; 199 | 200 | 201 | 202 | 203 | -- ---------------------------------------------- 204 | -- My solution 205 | -- ---------------------------------------------- 206 | 207 | 208 | 209 | SELECT 210 | c.customer_name, 211 | c.customer_id, 212 | SUM(t.amount) total_amt 213 | FROM customers as c 214 | JOIN transaction as t 215 | ON c.customer_id = t.customer_id 216 | WHERE EXTRACT(YEAR FROM t.Transaction_Date) = EXTRACT(YEAR FROM current_date) 217 | GROUP BY 1, 2 218 | 219 | -- verifying it 220 | SELECT 221 | SUM(amount) 222 | FROM Transaction 223 | WHERE customer_id = 1 AND EXTRACT(YEAR FROM Transaction_Date) = '2024' 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | /* 232 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 233 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 234 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 235 | */ 236 | 237 | -------------------------------------------------------------------------------- /10.sql: -------------------------------------------------------------------------------- 1 | -- Day 10/30 SQL Interview Question - Medium 2 | 3 | CREATE TABLE Transactions ( 4 | id INT PRIMARY KEY, 5 | country VARCHAR(255), 6 | state VARCHAR, 7 | amount INT, 8 | trans_date DATE 9 | ); 10 | 11 | INSERT INTO Transactions (id, country, state, amount, trans_date) VALUES 12 | (121, 'US', 'approved', 1000, '2018-12-18'), 13 | (122, 'US', 'declined', 2000, '2018-12-19'), 14 | (123, 'US', 'approved', 2000, '2019-01-01'), 15 | (124, 'DE', 'approved', 2000, '2019-01-07'); 16 | 17 | 18 | /* 19 | Write an SQL query to find for each month and country, 20 | the number of transactions and their total amount, 21 | the number of approved transactions and their total amount. 22 | 23 | Return the result table in in below order.RANGE 24 | 25 | 26 | Output: 27 | +----------+---------+-------------+----------------+--------------------+-----------------------+ 28 | | month | country | trans_count | approved_count | trans_total_amount | approved_total_amount | 29 | +----------+---------+-------------+----------------+--------------------+-----------------------+ 30 | | 2018-12 | US | 2 | 1 | 3000 | 1000 | 31 | | 2019-01 | US | 1 | 1 | 2000 | 2000 | 32 | | 2019-01 | DE | 1 | 1 | 2000 | 2000 | 33 | +----------+---------+-------------+----------------+--------------------+-----------------------+ 34 | */ 35 | 36 | -- ------------------------------------------------------------- 37 | -- My Solution 38 | -- ------------------------------------------------------------- 39 | 40 | SELECT 41 | TO_CHAR(trans_date, 'YYYY-MM') as month, 42 | country, 43 | COUNT(1) as trans_count, 44 | SUM(CASE WHEN state='approved' THEN 1 ELSE 0 END) as approved_count, 45 | SUM(amount) as trans_total_amount, 46 | SUM(CASE WHEN state= 'approved' THEN amount ELSE 0 END) as approved_total_amount 47 | FROM transactions 48 | GROUP BY 1, 2; 49 | 50 | 51 | -- ------------------------------------------------------------- 52 | -- Follow me in LinkedIn www.linkedin.com/in/najirr 53 | -- ------------------------------------------------------------- 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /11.sql: -------------------------------------------------------------------------------- 1 | -- SQL 11/30 Days 2 | 3 | CREATE TABLE reviews ( 4 | review_id INTEGER, 5 | user_id INTEGER, 6 | submit_date TIMESTAMP, 7 | product_id INTEGER, 8 | stars INTEGER 9 | ); 10 | 11 | INSERT INTO reviews (review_id, user_id, submit_date, product_id, stars) VALUES 12 | (6171, 123, '2022-06-08 00:00:00', 50001, 4), 13 | (7802, 265, '2022-06-10 00:00:00', 69852, 4), 14 | (5293, 362, '2022-06-18 00:00:00', 50001, 3), 15 | (6352, 192, '2022-07-26 00:00:00', 69852, 3), 16 | (4517, 981, '2022-07-05 00:00:00', 69852, 2), 17 | (8654, 753, '2022-08-15 00:00:00', 50001, 5), 18 | (9743, 642, '2022-08-22 00:00:00', 69852, 3), 19 | (1025, 874, '2022-08-05 00:00:00', 50001, 4), 20 | (2089, 512, '2022-09-10 00:00:00', 69852, 2), 21 | (3078, 369, '2022-09-18 00:00:00', 50001, 5), 22 | (4056, 785, '2022-09-25 00:00:00', 69852, 4), 23 | (5034, 641, '2022-10-12 00:00:00', 50001, 3), 24 | (6023, 829, '2022-10-18 00:00:00', 69852, 5), 25 | (7012, 957, '2022-10-25 00:00:00', 50001, 2), 26 | (8001, 413, '2022-11-05 00:00:00', 69852, 4), 27 | (8990, 268, '2022-11-15 00:00:00', 50001, 3), 28 | (9967, 518, '2022-11-22 00:00:00', 69852, 3), 29 | (1086, 753, '2022-12-10 00:00:00', 50001, 5), 30 | (1175, 642, '2022-12-18 00:00:00', 69852, 4), 31 | (1264, 874, '2022-12-25 00:00:00', 50001, 3), 32 | (1353, 512, '2022-12-31 00:00:00', 69852, 2), 33 | (1442, 369, '2023-01-05 00:00:00', 50001, 4), 34 | (1531, 785, '2023-01-15 00:00:00', 69852, 5), 35 | (1620, 641, '2023-01-22 00:00:00', 50001, 3), 36 | (1709, 829, '2023-01-30 00:00:00', 69852, 4); 37 | 38 | 39 | 40 | -- --------------------------------------------- 41 | -- Amazon Interview Question for enty Business Analyst! 42 | -- --------------------------------------------- 43 | /* 44 | Question:: Given the reviews table, write a query to retrieve 45 | the average star rating for each product, grouped by month. 46 | The output should display the month as a numerical value, 47 | product ID, and average star rating rounded to two decimal places. 48 | Sort the output first by month and then by product ID. 49 | */ 50 | 51 | -- --------------------------------- 52 | -- My Solution 53 | -- --------------------------------- 54 | 55 | 56 | SELECT * FROM reviews; 57 | -- month by each product and their avg rating 58 | 59 | SELECT 60 | EXTRACT(MONTH FROM submit_date) as month, 61 | product_id, 62 | ROUND(AVG(stars), 2) as avg_rating 63 | FROM reviews 64 | GROUP BY month, product_id 65 | ORDER BY month, product_id; 66 | 67 | 68 | 69 | -- --------------------------------- 70 | -- Thank you! 71 | -- --------------------------------- 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /12.sql: -------------------------------------------------------------------------------- 1 | -- Day 12/30 SQL Challenge 2 | -- IBM Interview Questions 3 | 4 | -- --------------------------------------------------- 5 | -- 2 IBM SQL Interview Questions 6 | ---------------------------------------------------- 7 | 8 | 9 | CREATE TABLE purchases ( 10 | purchase_id INT PRIMARY KEY, 11 | user_id INT, 12 | date_of_purchase TIMESTAMP, 13 | product_id INT, 14 | amount_spent DECIMAL(10, 2) 15 | ); 16 | 17 | INSERT INTO purchases (purchase_id, user_id, date_of_purchase, product_id, amount_spent) VALUES 18 | (2171, 145, '2024-02-22 00:00:00', 43001, 1000), 19 | (3022, 578, '2024-02-24 00:00:00', 25852, 4000), 20 | (4933, 145, '2024-02-28 00:00:00', 43001, 7000), 21 | (6322, 248, '2024-02-19 00:00:00', 25852, 2000), 22 | (4717, 578, '2024-02-12 00:00:00', 25852, 7000), 23 | (2172, 145, '2024-01-15 00:00:00', 43001, 8000), 24 | (3023, 578, '2024-01-18 00:00:00', 25852, 3000), 25 | (4934, 145, '2024-01-28 00:00:00', 43001, 9000), 26 | (6323, 248, '2024-02-20 00:00:00', 25852, 1500), 27 | (4718, 578, '2024-02-25 00:00:00', 25852, 6000); 28 | 29 | 30 | 31 | 32 | /* 33 | SQL Question 1: Identify IBM's High Capacity Users 34 | 35 | SQL Question: 36 | Identify users who have made purchases 37 | totaling more than $10,000 in the last month 38 | from the purchases table. 39 | The table contains information about purchases, 40 | including the user ID, date of purchase, product ID, 41 | and amount spent. 42 | */ 43 | 44 | 45 | SELECT * FROM purchases; 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -- ----------------------------------------- 60 | Question 2 61 | -- ----------------------------------------- 62 | 63 | CREATE TABLE employee_service ( 64 | employee_id INT PRIMARY KEY, 65 | name VARCHAR(50), 66 | start_date DATE, 67 | end_date DATE, 68 | department VARCHAR(50) 69 | ); 70 | 71 | INSERT INTO employee_service (employee_id, name, start_date, end_date, department) VALUES 72 | (101, 'John', '2015-01-15', '2020-06-30', 'Technology'), 73 | (102, 'Emma', '2016-08-01', NULL, 'Management'), 74 | (103, 'Ava', '2017-05-30', '2019-08-01', 'Strategy'), 75 | (104, 'Oliver', '2018-11-11', NULL, 'Technology'), 76 | (105, 'Sophia', '2020-01-17', NULL, 'Management'), 77 | (106, 'William', '2019-03-20', NULL, 'Strategy'), 78 | (107, 'James', '2018-09-10', NULL, 'Technology'), 79 | (108, 'Charlotte', '2017-12-05', NULL, 'Management'), 80 | (109, 'Michael', '2016-06-15', '2021-02-28', 'Technology'), 81 | (110, 'Amelia', '2019-11-25', NULL, 'Strategy'), 82 | (111, 'Ethan', '2018-04-08', '2022-01-10', 'Management'), 83 | (112, 'Mia', '2020-07-15', NULL, 'Technology'), 84 | (113, 'Alexander', '2017-10-30', '2020-09-15', 'Strategy'), 85 | (114, 'Isabella', '2016-05-22', '2021-08-20', 'Management'), 86 | (115, 'Liam', '2019-02-12', '2023-04-05', 'Technology'), 87 | (116, 'Ella', '2018-08-05', '2022-11-28', 'Strategy'), 88 | (117, 'Noah', '2020-09-18', NULL, 'Management'), 89 | (118, 'Avery', '2017-11-10', NULL, 'Technology'), 90 | (119, 'Benjamin', '2016-04-04', NULL, 'Strategy'), 91 | (120, 'Abigail', '2019-08-30', NULL, 'Management'); 92 | 93 | 94 | /* 95 | SQL Question 4: Average Duration of Employee's Service 96 | Given the data on IBM employees, can you find the average duration 97 | of service for employees across different departments? 98 | The Duration of service is represented as end_date - start_date. 99 | If end_date is NULL, consider it as the current date. 100 | */ 101 | 102 | 103 | SELECT * FROM employee_service 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | -------------------------------------------------------------------------------- /13.sql: -------------------------------------------------------------------------------- 1 | -- 13/30 SQL Challenge 2 | 3 | -- Facebook medium level SQL question for Data Analyst 4 | ----------------------------------------------------- 5 | !If you get stuck my solution at the end of the query 6 | ----------------------------------------------------- 7 | 8 | -- Q.1 SCHEMAS 9 | 10 | CREATE TABLE fb_posts ( 11 | post_id INT PRIMARY KEY, 12 | user_id INT, 13 | likes INT, 14 | comments INT, 15 | post_date DATE 16 | ); 17 | 18 | 19 | INSERT INTO fb_posts (post_id, user_id, likes, comments, post_date) VALUES 20 | (1, 101, 50, 20, '2024-02-27'), 21 | (2, 102, 30, 15, '2024-02-28'), 22 | (3, 103, 70, 25, '2024-02-29'), 23 | (4, 101, 80, 30, '2024-03-01'), 24 | (5, 102, 40, 10, '2024-03-02'), 25 | (6, 103, 60, 20, '2024-03-03'), 26 | (7, 101, 90, 35, '2024-03-04'), 27 | (8, 101, 90, 35, '2024-03-05'), 28 | (9, 102, 50, 15, '2024-03-06'), 29 | (10, 103, 30, 10, '2024-03-07'), 30 | (11, 101, 60, 25, '2024-03-08'), 31 | (12, 102, 70, 30, '2024-03-09'), 32 | (13, 103, 80, 35, '2024-03-10'), 33 | (14, 101, 40, 20, '2024-03-11'), 34 | (15, 102, 90, 40, '2024-03-12'), 35 | (16, 103, 20, 5, '2024-03-13'), 36 | (17, 101, 70, 25, '2024-03-14'), 37 | (18, 102, 50, 15, '2024-03-15'), 38 | (19, 103, 30, 10, '2024-03-16'), 39 | (20, 101, 60, 20, '2024-03-17'); 40 | 41 | /* 42 | -- Q.1 43 | Question: Identify the top 3 posts with the highest engagement 44 | (likes + comments) for each user on a Facebook page. Display 45 | the user ID, post ID, engagement count, and rank for each post. 46 | */ 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | -- Schemas for Q.2 62 | 63 | CREATE TABLE posts ( 64 | post_id INT PRIMARY KEY, 65 | user_id INT, 66 | likes INT, 67 | post_date DATE 68 | ); 69 | 70 | INSERT INTO posts (post_id, user_id, likes, post_date) VALUES 71 | (1, 101, 50, '2024-02-27'), 72 | (2, 102, 30, '2024-02-28'), 73 | (3, 103, 70, '2024-02-29'), 74 | (4, 101, 80, '2024-02-01'), 75 | (5, 102, 40, '2024-02-02'), 76 | (6, 103, 60, '2024-02-29'), 77 | (7, 101, 90, '2024-01-29'), 78 | (8, 101, 20, '2024-02-05'), 79 | (9, 102, 50, '2024-01-29'), 80 | (10, 103, 30, '2024-02-29'), 81 | (11, 101, 60, '2024-01-08'), 82 | (12, 102, 70, '2024-01-09'), 83 | (13, 103, 80, '2024-01-10'), 84 | (14, 101, 40, '2024-01-29'), 85 | (15, 102, 90, '2024-01-29'), 86 | (16, 103, 20, '2024-01-13'), 87 | (17, 101, 70, '2024-01-14'), 88 | (18, 102, 50, '2024-02-29'), 89 | (19, 103, 30, '2024-02-16'), 90 | (20, 101, 60, '2024-02-17'); 91 | 92 | /* 93 | -- Q.2 94 | 95 | Determine the users who have posted more than 2 times 96 | in the past week and calculate the total number of likes 97 | they have received. Return user_id and number of post and no of likes 98 | */ 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | -- ------------------------------------------------------------------------ 117 | -- My Solution for Q1 118 | -- top 3 post by each user 119 | -- SUM( likes + comments) 120 | -- post ranks 121 | ------------------------------------------------------------------------ 122 | 123 | WITH rank_posts 124 | AS ( 125 | SELECT 126 | user_id, 127 | post_id, 128 | SUM(likes + comments) as engagement_count, 129 | ROW_NUMBER() OVER(PARTITION BY user_id ORDER BY SUM(likes + comments) DESC) rn, 130 | DENSE_RANK() OVER(PARTITION BY user_id ORDER BY SUM(likes + comments) DESC) ranks 131 | FROM fb_posts 132 | GROUP BY user_id, post_id 133 | ORDER BY user_id, engagement_count DESC 134 | ) 135 | SELECT 136 | user_id, 137 | post_id, 138 | engagement_count, 139 | ranks 140 | FROM rank_posts 141 | WHERE rn <=3 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | -- ------------------------------------------------------------------------ 161 | -- My Solution for Q2 162 | ------------------------------------------------------------------------ 163 | -- user posted > 2 164 | -- sum(likes) 165 | -- COUNT(post) 166 | -- past week 167 | 168 | SELECT 169 | user_id, 170 | SUM(likes) as total_likes, 171 | COUNT(post_id) as cnt_post 172 | FROM posts 173 | WHERE post_date >= CURRENT_DATE - 7 AND 174 | post_date < CURRENT_DATE 175 | GROUP BY user_id 176 | HAVING COUNT(post_id) 177 | 178 | 179 | 180 | /* 181 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 182 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 183 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 184 | */ 185 | -------------------------------------------------------------------------------- /14.sql: -------------------------------------------------------------------------------- 1 | -- SQL Challenge 14/30 Days 2 | 3 | CREATE TABLE job_listings ( 4 | job_id INTEGER PRIMARY KEY, 5 | company_id INTEGER, 6 | title TEXT, 7 | description TEXT 8 | ); 9 | 10 | INSERT INTO job_listings (job_id, company_id, title, description) 11 | VALUES 12 | (248, 827, 'Business Analyst', 'Business analyst evaluates past and current business data with the primary goal of improving decision-making processes within organizations.'), 13 | (149, 845, 'Business Analyst', 'Business analyst evaluates past and current business data with the primary goal of improving decision-making processes within organizations.'), 14 | (945, 345, 'Data Analyst', 'Data analyst reviews data to identify key insights into a business''s customers and ways the data can be used to solve problems.'), 15 | (164, 345, 'Data Analyst', 'Data analyst reviews data to identify key insights into a business''s customers and ways the data can be used to solve problems.'), 16 | (172, 244, 'Data Engineer', 'Data engineer works in a variety of settings to build systems that collect, manage, and convert raw data into usable information for data scientists and business analysts to interpret.'), 17 | (573, 456, 'Software Engineer', 'Software engineer designs, develops, tests, and maintains software applications.'), 18 | (324, 789, 'Software Engineer', 'Software engineer designs, develops, tests, and maintains software applications.'), 19 | (890, 123, 'Data Scientist', 'Data scientist analyzes and interprets complex data to help organizations make informed decisions.'), 20 | (753, 123, 'Data Scientist', 'Data scientist analyzes and interprets complex data to help organizations make informed decisions.'); 21 | 22 | 23 | /* 24 | -- Q.1 LinkedIn Data Analyst Interview question 25 | 26 | Assume you're given a table containing job postings 27 | from various companies on the LinkedIn platform. 28 | Write a query to retrieve the count of companies 29 | that have posted duplicate job listings. 30 | 31 | Definition: 32 | 33 | Duplicate job listings are defined as two job listings 34 | within the same company that share identical titles and descriptions. 35 | */ 36 | 37 | 38 | 39 | -- ----------------------------------------------------------------- 40 | -- My Solution 41 | -- ----------------------------------------------------------------- 42 | 43 | -- count of company who posted more 1 jobs wiht same title and desc 44 | 45 | SELECT 46 | COUNT(1) as cnt_company 47 | FROM 48 | (SELECT 49 | company_id, 50 | title, 51 | description, 52 | COUNT(1) as total_job 53 | FROM job_listings 54 | GROUP BY 1, 2, 3 55 | HAVING COUNT(1) > 1 56 | )x1; 57 | 58 | 59 | 60 | 61 | 62 | /* 63 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 64 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 65 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 66 | */ 67 | 68 | 69 | -------------------------------------------------------------------------------- /15.sql: -------------------------------------------------------------------------------- 1 | -- SQL Challenge 15/30 Days 2 | 3 | CREATE TABLE Sales ( 4 | SaleID SERIAL PRIMARY KEY, 5 | Region VARCHAR(50), 6 | Amount DECIMAL(10, 2), 7 | SaleDate DATE 8 | ); 9 | 10 | INSERT INTO Sales (Region, Amount, SaleDate) VALUES 11 | ('North', 5000.00, '2024-02-01'), 12 | ('South', 6000.00, '2024-02-02'), 13 | ('East', 4500.00, '2024-02-03'), 14 | ('West', 7000.00, '2024-02-04'), 15 | ('North', 5500.00, '2024-02-05'), 16 | ('South', 6500.00, '2024-02-06'), 17 | ('East', 4800.00, '2024-02-07'), 18 | ('West', 7200.00, '2024-02-08'), 19 | ('North', 5200.00, '2024-02-09'), 20 | ('South', 6200.00, '2024-02-10'), 21 | ('East', 4700.00, '2024-02-11'), 22 | ('West', 7100.00, '2024-02-12'), 23 | ('North', 5300.00, '2024-02-13'), 24 | ('South', 6300.00, '2024-02-14'), 25 | ('East', 4600.00, '2024-02-15'), 26 | ('West', 7300.00, '2024-02-16'), 27 | ('North', 5400.00, '2024-02-17'), 28 | ('South', 6400.00, '2024-02-18'), 29 | ('East', 4900.00, '2024-02-19'), 30 | ('West', 7400.00, '2024-02-20'), 31 | ('North', 5600.00, '2024-02-21'), 32 | ('South', 6600.00, '2024-02-22'), 33 | ('East', 5000.00, '2024-02-23'), 34 | ('West', 7500.00, '2024-02-24'), 35 | ('North', 5700.00, '2024-02-25'), 36 | ('South', 6700.00, '2024-02-26'), 37 | ('East', 5100.00, '2024-02-27'), 38 | ('West', 7600.00, '2024-02-28'); 39 | 40 | 41 | 42 | -- Flipkart Business Analyst entry level SQL question 43 | 44 | /* 45 | 46 | Identify the region with the lowest sales amount for the previous month. 47 | return region name and total_sale amount. 48 | 49 | */ 50 | -- region and sum sale 51 | -- filter last month 52 | -- lowest sale region 53 | 54 | -- ----------------------------------------------------- 55 | -- My Solution 56 | -- ----------------------------------------------------- 57 | 58 | SELECT 59 | region, 60 | SUM(amount) as total_sales 61 | FROM sales 62 | WHERE EXTRACT(MONTH FROM saledate) = EXTRACT(MONTH FROM CURRENT_DATE - INTERVAL '1 month') 63 | AND EXTRACT(YEAR FROM saledate) = EXTRACT(YEAR FROM CURRENT_DATE) 64 | GROUP BY region 65 | ORDER BY total_sales ASC 66 | LIMIT 1; 67 | 68 | 69 | 70 | 71 | /* 72 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 73 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 74 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 75 | */ -------------------------------------------------------------------------------- /16.sql: -------------------------------------------------------------------------------- 1 | -- 16/30 Days SQL Challenge 2 | 3 | -- TikTok Interview Question for data analyst 4 | 5 | -- Create TikTok table 6 | CREATE TABLE tiktok ( 7 | views INT 8 | ); 9 | 10 | 11 | -- Insert records 12 | -- Insert records into the tiktok table 13 | INSERT INTO tiktok (views) 14 | VALUES 15 | (100), (800), (350), 16 | (150), (600), 17 | (700), (700), (950); 18 | 19 | /* 20 | -- Tik Tok Data Analyst Interview question 21 | 22 | Q.1 Find the median within a series of numbers in SQL ; 23 | 24 | 1 8 3 4 5 odd 25 | 26 | 1 3 4 5 8 9 even 27 | 28 | */ 29 | 30 | 31 | 32 | -- My Solution 33 | 34 | 35 | WITH CTE 36 | AS ( 37 | SELECT 38 | views, 39 | ROW_NUMBER() OVER( ORDER BY views ASC) rn_asc, 40 | ROW_NUMBER() OVER( ORDER BY views DESC) rn_desc 41 | FROM tiktok 42 | WHERE views < 900 43 | ) 44 | SELECT 45 | AVG(views) as median 46 | FROM CTE 47 | WHERE ABS(rn_asc - rn_desc) <= 1 -- 0 or 1 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -- ORDER BY views; 56 | 57 | n + 1/2 58 | 59 | 7+1/2 = 4th 60 | 61 | 62 | 200/1 = 200 63 | 64 | 200+300/2 = 250 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /17.sql: -------------------------------------------------------------------------------- 1 | -- 17/30 SQL Challenge 2 | 3 | -- Zomato Business Analyst interview question 4 | 5 | CREATE TABLE order_details ( 6 | order_id INT, 7 | del_partner VARCHAR(255), 8 | predicted_time TIMESTAMP, 9 | delivery_time TIMESTAMP 10 | ); 11 | 12 | 13 | INSERT INTO order_details (order_id, del_partner, predicted_time, delivery_time) 14 | VALUES 15 | (11, 'Partner C', '2024-02-29 11:30:00', '2024-02-29 12:00:00'), 16 | (12, 'Partner A', '2024-02-29 10:45:00', '2024-02-29 11:30:00'), 17 | (13, 'Partner B', '2024-02-29 09:00:00', '2024-02-29 09:45:00'), 18 | (14, 'Partner A', '2024-02-29 12:15:00', '2024-02-29 13:00:00'), 19 | (15, 'Partner C', '2024-02-29 13:30:00', '2024-02-29 14:15:00'), 20 | (16, 'Partner B', '2024-02-29 14:45:00', '2024-02-29 15:30:00'), 21 | (17, 'Partner A', '2024-02-29 16:00:00', '2024-02-29 16:45:00'), 22 | (18, 'Partner B', '2024-02-29 17:15:00', '2024-02-29 18:00:00'), 23 | (19, 'Partner C', '2024-02-29 18:30:00', '2024-02-29 19:15:00'); 24 | 25 | 26 | /* 27 | -- How many delayed orders does each delivery partner have, 28 | considering the predicted delivery time and the actual delivery time? 29 | */ 30 | 31 | 32 | 33 | 34 | -- My solution 35 | 36 | 37 | -- del_partner delayed orders cnt 38 | -- delayed order means del_time > pred_del_time 39 | 40 | 41 | 42 | SELECT 43 | del_partner, 44 | COUNT(order_id) as cnt_delayed_orders 45 | FROM order_details 46 | WHERE 47 | predicted_time < delivery_time 48 | GROUP BY del_partner; 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /18.sql: -------------------------------------------------------------------------------- 1 | -- 18/30 SQL Challenge 2 | 3 | -- SWIGGY BA Interview questions 4 | 5 | 6 | -- Create the Table 7 | CREATE TABLE restaurant_orders ( 8 | city VARCHAR(50), 9 | restaurant_id INT, 10 | order_id INT, 11 | order_date DATE 12 | ); 13 | 14 | 15 | -- Insert Records 16 | INSERT INTO restaurant_orders (city, restaurant_id, order_id, order_date) 17 | VALUES 18 | ('Delhi', 101, 1, '2021-09-05'), 19 | ('Bangalore', 102, 12, '2021-09-08'), 20 | ('Bangalore', 102, 13, '2021-09-08'), 21 | ('Bangalore', 102, 14, '2021-09-08'), 22 | ('Mumbai', 103, 3, '2021-09-10'), 23 | ('Mumbai', 103, 30, '2021-09-10'), 24 | ('Chennai', 104, 4, '2021-09-15'), 25 | ('Delhi', 105, 5, '2021-09-20'), 26 | ('Bangalore', 106, 6, '2021-09-25'), 27 | ('Mumbai', 107, 7, '2021-09-28'), 28 | ('Chennai', 108, 8, '2021-09-30'), 29 | ('Delhi', 109, 9, '2021-10-05'), 30 | ('Bangalore', 110, 10, '2021-10-08'), 31 | ('Mumbai', 111, 11, '2021-10-10'), 32 | ('Chennai', 112, 12, '2021-10-15'), 33 | ('Kolkata', 113, 13, '2021-10-20'), 34 | ('Hyderabad', 114, 14, '2021-10-25'), 35 | ('Pune', 115, 15, '2021-10-28'), 36 | ('Jaipur', 116, 16, '2021-10-30'); 37 | 38 | /* 39 | Question: 40 | 41 | Which metro city had the highest number of restaurant orders in September 2021? 42 | 43 | Write the SQL query to retrieve the city name and the total count of orders, 44 | ordered by the total count of orders in descending order. 45 | 46 | -- Note metro cites are 'Delhi', 'Mumbai', 'Bangalore', 'Hyderabad' 47 | */ 48 | 49 | 50 | __________________________ 51 | -- My solution 52 | __________________________ 53 | 54 | -- city name 55 | -- total orders 56 | -- filter metro 57 | -- grouyp by city 58 | -- 1 59 | 60 | 61 | SELECT 62 | city, 63 | count(order_id) as total_orders 64 | FROM restaurant_orders 65 | WHERE city IN ('Delhi', 'Mumbai', 'Bangalore', 'Hyderabad') 66 | AND order_date BETWEEN '2021-09-01' AND '2021-09-30' 67 | GROUP BY city 68 | ORDER BY total_orders DESC 69 | LIMIT 1; 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | -- SELECT 93 | -- city, 94 | -- COUNT(order_id) as total_orders 95 | -- FROM restaurant_orders 96 | -- WHERE city IN ('Delhi', 'Mumbai', 'Bangalore', 'Hyderabad') 97 | -- AND order_date BETWEEN '2021-09-01' AND '2021-09-30' 98 | -- GROUP BY city 99 | -- ORDER BY total_orders DESC 100 | -- LIMIT 1; 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /19.sql: -------------------------------------------------------------------------------- 1 | -- -- Day 19/30 SQL Challenge 2 | 3 | -- Google Interview Question for DA 4 | 5 | CREATE TABLE student_names( 6 | student_id INT, 7 | name VARCHAR(50) 8 | ); 9 | 10 | -- Insert the records 11 | INSERT INTO student_names (student_id, name) VALUES 12 | (1, 'RAM'), 13 | (2, 'ROBERT'), 14 | (3, 'ROHIM'), 15 | (4, 'RAM'), 16 | (5, 'ROBERT'); 17 | 18 | 19 | -- Question 20 | 21 | 22 | -- Get the count of distint student that are not unique 23 | 24 | 25 | 26 | 27 | SELECT 28 | COUNT(*) as distint_student_cnt 29 | FROM 30 | ( 31 | SELECT name, 32 | COUNT(name) 33 | FROM student_names 34 | GROUP BY name 35 | HAVING COUNT(name) = 1 36 | ) as subquery 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | SELECT 60 | COUNT(name) 61 | FROM 62 | (SELECT name, 63 | COUNT(name) 64 | FROM student_names 65 | GROUP BY name 66 | HAVING COUNT(name) = 1 67 | ) subquery 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | -- SELECT 89 | -- COUNT(*) as non_unique 90 | -- FROM 91 | -- ( 92 | -- SELECT 93 | -- name, 94 | -- COUNT(name) as cnt 95 | -- FROM student_names 96 | -- GROUP BY name 97 | -- HAVING COUNT(name) = 1 98 | -- ) subquery 99 | 100 | -------------------------------------------------------------------------------- /2.sql: -------------------------------------------------------------------------------- 1 | -- Day 02/30 days SQL challenge 2 | 3 | 4 | -- SCHEMA 5 | 6 | 7 | -- Create pages table 8 | CREATE TABLE pages ( 9 | page_id INTEGER PRIMARY KEY, 10 | page_name VARCHAR(255) 11 | ); 12 | 13 | -- Insert data into pages table 14 | INSERT INTO pages (page_id, page_name) VALUES 15 | (20001, 'SQL Solutions'), 16 | (20045, 'Brain Exercises'), 17 | (20701, 'Tips for Data Analysts'); 18 | 19 | -- Create page_likes table 20 | CREATE TABLE page_likes ( 21 | user_id INTEGER, 22 | page_id INTEGER, 23 | liked_date DATETIME, 24 | FOREIGN KEY (page_id) REFERENCES pages(page_id) 25 | ); 26 | 27 | -- Insert data into page_likes table 28 | INSERT INTO page_likes (user_id, page_id, liked_date) VALUES 29 | (111, 20001, '2022-04-08 00:00:00'), 30 | (121, 20045, '2022-03-12 00:00:00'), 31 | (156, 20001, '2022-07-25 00:00:00'); 32 | 33 | 34 | 35 | /* 36 | Question 1: 37 | Write a SQL query to retrieve the IDs of the Facebook pages that have zero likes. 38 | The output should be sorted in ascending order based on the page IDs. 39 | */ 40 | 41 | -- Question 1 link :: https://datalemur.com/questions/sql-page-with-no-likes 42 | 43 | -- ------------------------------------------------------------------------------------------------------------------------------------ 44 | -- My Solution 45 | -- ------------------------------------------------------------------------------------------------------------------------------------ 46 | 47 | 48 | SELECT p.page_id 49 | FROM pages p 50 | LEFT JOIN page_likes pl ON p.page_id = pl.page_id 51 | GROUP BY p.page_id 52 | HAVING COUNT(pl.page_id) = 0 53 | ORDER BY p.page_id ASC; 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -- ------------------------------------------------------------------------------------------------------------------------------------ 67 | -- Question 2 App Click-through Rate (CTR) [Facebook SQL Interview Question] 68 | -- ------------------------------------------------------------------------------------------------------------------------------------ 69 | 70 | 71 | -- Create the events table 72 | CREATE TABLE events ( 73 | app_id INTEGER, 74 | event_type VARCHAR(10), 75 | timestamp DATETIME 76 | ); 77 | 78 | -- Insert records into the events table 79 | INSERT INTO events (app_id, event_type, timestamp) VALUES 80 | (123, 'impression', '2022-07-18 11:36:12'), 81 | (123, 'impression', '2022-07-18 11:37:12'), 82 | (123, 'click', '2022-07-18 11:37:42'), 83 | (234, 'impression', '2022-07-18 14:15:12'), 84 | (234, 'click', '2022-07-18 14:16:12'); 85 | 86 | /* 87 | 88 | Question 2: 89 | Write a query to calculate the click-through rate (CTR) for the app in 2022 and round the results to 2 decimal places. 90 | Definition and note: 91 | Percentage of click-through rate (CTR) = 100.0 * Number of clicks / Number of impressions 92 | To avoid integer division, multiply the CTR by 100.0, not 100. 93 | Expected Output Columns: app_id, ctr 94 | 95 | Question 2 Link :: https://datalemur.com/questions/click-through-rate 96 | 97 | */ 98 | 99 | 100 | -- ------------------------------------------------------------------------------------------------------------------------------------ 101 | -- My Solution 102 | -- ------------------------------------------------------------------------------------------------------------------------------------ 103 | 104 | -- SQL query to calculate the click-through rate (CTR) 105 | SELECT 106 | app_id, 107 | ROUND((100.0 * SUM(CASE WHEN event_type = 'click' THEN 1 ELSE 0 END) / COUNT(*)), 2) AS ctr 108 | FROM 109 | events 110 | WHERE 111 | YEAR(timestamp) = 2022 112 | GROUP BY 113 | app_id; 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | /* 122 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 123 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 124 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 125 | */ 126 | -------------------------------------------------------------------------------- /20.sql: -------------------------------------------------------------------------------- 1 | -- Day 20/30 days sql challenge 2 | 3 | -- Schemas 4 | 5 | 6 | CREATE TABLE zomato_orders( 7 | order_id INT PRIMARY KEY, 8 | customer_id INT, 9 | order_date DATE, 10 | price FLOAT, 11 | city VARCHAR(25) 12 | ); 13 | 14 | 15 | -- Insert sample records into the zomato_orders table 16 | INSERT INTO zomato_orders (order_id, customer_id, order_date, price, city) VALUES 17 | (1, 101, '2023-11-01', 150.50, 'Mumbai'), 18 | (2, 102, '2023-11-05', 200.75, 'Delhi'), 19 | (3, 103, '2023-11-10', 180.25, 'Mumbai'), 20 | (4, 104, '2023-11-15', 120.90, 'Delhi'), 21 | (5, 105, '2023-11-20', 250.00, 'Mumbai'), 22 | (6, 108, '2023-11-25', 180.75, 'Gurgoan'), 23 | (7, 107, '2023-12-30', 300.25, 'Delhi'), 24 | (8, 108, '2023-12-02', 220.50, 'Gurgoan'), 25 | (9, 109, '2023-11-08', 170.00, 'Mumbai'), 26 | (10, 110, '2023-10-12', 190.75, 'Delhi'), 27 | (11, 108, '2023-10-18', 210.25, 'Gurgoan'), 28 | (12, 112, '2023-11-24', 280.50, 'Mumbai'), 29 | (13, 113, '2023-10-29', 150.00, 'Mumbai'), 30 | (14, 103, '2023-11-03', 200.75, 'Mumbai'), 31 | (15, 115, '2023-10-07', 230.90, 'Delhi'), 32 | (16, 116, '2023-11-11', 260.00, 'Mumbai'), 33 | (17, 117, '2023-11-16', 180.75, 'Mumbai'), 34 | (18, 102, '2023-11-22', 320.25, 'Delhi'), 35 | (19, 103, '2023-11-27', 170.50, 'Mumbai'), 36 | (20, 102, '2023-11-05', 220.75, 'Delhi'), 37 | (21, 103, '2023-11-09', 300.25, 'Mumbai'), 38 | (22, 101, '2023-11-15', 180.50, 'Mumbai'), 39 | (23, 104, '2023-11-18', 250.75, 'Delhi'), 40 | (24, 102, '2023-11-20', 280.25, 'Delhi'), 41 | (25, 117, '2023-11-16', 180.75, 'Mumbai'), 42 | (26, 117, '2023-11-16', 180.75, 'Mumbai'), 43 | (27, 117, '2023-11-16', 180.75, 'Mumbai'), 44 | (28, 117, '2023-11-16', 180.75, 'Mumbai'); 45 | 46 | /* 47 | zomato business analyst interview question 48 | 49 | Find city wise customers count who have placed 50 | more than three orders in November 2023. 51 | */ 52 | 53 | 54 | 55 | 56 | -- ----------------------- 57 | -- My Solution 58 | -- ----------------------- 59 | 60 | SELECT * FROM zomato_orders; 61 | 62 | -- city, cust_wise total orders 63 | -- filter nov-23 64 | -- having count of orders > 3 65 | -- group by city 66 | 67 | 68 | SELECT 69 | city, 70 | COUNT(1) total_customer_count 71 | FROM ( 72 | SELECT 73 | city, 74 | customer_id as customer, 75 | COUNT(1) as total_orders 76 | FROM zomato_orders 77 | WHERE order_date BETWEEN '2023-11-01' AND '2023-11-30' 78 | GROUP BY city, customer_id 79 | HAVING COUNT(1)> 3 80 | ) sub_query 81 | GROUP BY city 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | /* 91 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 92 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 93 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 94 | */ 95 | -------------------------------------------------------------------------------- /21.sql: -------------------------------------------------------------------------------- 1 | -- 21/30 days sql challenge 2 | 3 | -- SCHEMA 4 | 5 | -- Create the hotel_revenue table 6 | CREATE TABLE hotel_revenue ( 7 | hotel_id INT, 8 | month VARCHAR(10), 9 | year INT, 10 | revenue DECIMAL(10, 2) 11 | ); 12 | 13 | 14 | -- Insert sample records 15 | INSERT INTO hotel_revenue (hotel_id, month, year, revenue) VALUES 16 | (101, 'January', 2022, 15000.50), 17 | (101, 'February', 2022, 18000.75), 18 | (101, 'March', 2022, 20000.00), 19 | (101, 'April', 2022, 20000.00), 20 | (101, 'May', 2022, 20000.00), 21 | (101, 'June', 2022, 20000.00), 22 | (101, 'July', 2022, 26000.00), 23 | (101, 'August', 2022, 28000.00), 24 | (102, 'January', 2022, 12000.25), 25 | (102, 'February', 2022, 14000.50), 26 | (102, 'March', 2022, 16000.75), 27 | (101, 'January', 2023, 17000.25), 28 | (101, 'February', 2023, 19000.50), 29 | (101, 'March', 2023, 21000.75), 30 | (102, 'January', 2023, 13000.50), 31 | (102, 'February', 2023, 15000.75), 32 | (102, 'March', 2023, 17000.25), 33 | (103, 'January', 2022, 11000.25), 34 | (103, 'February', 2022, 13000.50), 35 | (103, 'March', 2022, 15000.75), 36 | (104, 'January', 2022, 14000.50), 37 | (108, 'May', 2022, 31000.75), 38 | (108, 'April', 2022, 28000.75), 39 | (108, 'June', 2022, 16000.75), 40 | (108, 'August', 2022, 16000.75), 41 | (104, 'March', 2022, 18000.25), 42 | (103, 'January', 2023, 12000.50), 43 | (103, 'February', 2023, 14000.75), 44 | (103, 'March', 2023, 16000.25), 45 | (104, 'January', 2023, 15000.75), 46 | (107, 'February', 2023, 17000.25), 47 | (106, 'March', 2023, 19000.50); 48 | 49 | 50 | -- Booking.com Data Analyst interview question 51 | 52 | /* 53 | Find the top-performing two months 54 | by revenue for each hotel for each year. 55 | return hotel_id, year, month, revenue 56 | */ 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | -- ----------------------- 75 | -- My Solution 76 | -- ----------------------- 77 | 78 | 79 | 80 | 81 | -- hotel_id, year, month, revenue 82 | -- ranking based on revenue 83 | -- filter top 2 month for each hotel in each year 84 | 85 | 86 | WITH CTE1 87 | AS 88 | ( 89 | SELECT 90 | hotel_id, 91 | year, 92 | month, 93 | revenue, 94 | DENSE_RANK() OVER(PARTITION BY hotel_id, year ORDER BY revenue DESC) drn 95 | FROM hotel_revenue 96 | ) 97 | 98 | SELECT 99 | hotel_id, 100 | year, 101 | month, 102 | revenue 103 | FROM CTE1 104 | WHERE drn <= 2 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | /* 113 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 114 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 115 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 116 | */ 117 | 118 | 119 | -------------------------------------------------------------------------------- /22.sql: -------------------------------------------------------------------------------- 1 | -- 22/30 days sql challenge 2 | 3 | 4 | CREATE TABLE employees ( 5 | emp_id INT PRIMARY KEY, 6 | emp_name VARCHAR(255), 7 | manager_id INT, 8 | FOREIGN KEY (manager_id) REFERENCES employees(emp_id) 9 | ); 10 | 11 | 12 | -- Inserting records into the employees table 13 | INSERT INTO employees (emp_id, emp_name, manager_id) VALUES 14 | (1, 'John Doe', NULL), -- John Doe is the manager 15 | (2, 'Jane Smith', 1), -- Jane Smith reports to John Doe 16 | (3, 'Alice Johnson', 1), -- Alice Johnson reports to John Doe 17 | (4, 'Bob Williams', 2), -- Bob Williams reports to Jane Smith 18 | (5, 'Charlie Brown', 2), -- Charlie Brown reports to Jane Smith 19 | (6, 'David Lee', 3), -- David Lee reports to Alice Johnson 20 | (7, 'Emily Davis', 3), -- Emily Davis reports to Alice Johnson 21 | (8, 'Fiona Clark', 4), -- Fiona Clark reports to Bob Williams 22 | (9, 'George Turner', 4), -- George Turner reports to Bob Williams 23 | (10, 'Hannah Baker', 5), -- Hannah Baker reports to Charlie Brown 24 | (11, 'Isaac White', 5), -- Isaac White reports to Charlie Brown 25 | (12, 'Jessica Adams', 6), -- Jessica Adams reports to David Lee 26 | (13, 'Kevin Harris', 6); -- Kevin Harris reports to David Lee 27 | 28 | 29 | -- 30 | /* 31 | 32 | -- TCS Data Analyst Interview question 33 | 34 | Question 35 | Write a SQL query to retrieve the emp_id, emp_name, and manager_name 36 | from the given employee table. 37 | It's important to note that managers are also employees in the table. 38 | 39 | Employees table has 3 COLUMNS 40 | -- emp_id, emp_name, maneger_id 41 | */ 42 | 43 | 44 | 45 | 46 | 47 | 48 | -- ----------------------- 49 | -- My Solution 50 | -- ----------------------- 51 | 52 | 53 | 54 | -- emp_id, 55 | -- emp_name, 56 | -- manager_name based on manager id 57 | 58 | SELECT 59 | e1.emp_id, 60 | e1.emp_name, 61 | e1.manager_id, 62 | e2.emp_name as manager_name 63 | FROM employees as e1 64 | CROSS JOIN 65 | employees as e2 66 | WHERE e1.manager_id = e2.emp_id 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | -- approach 2 75 | 76 | 77 | SELECT 78 | e1.emp_id, 79 | e1.emp_name, 80 | e1.manager_id, 81 | e2.emp_name as manager_name 82 | FROM employees as e1 83 | LEFT JOIN 84 | employees as e2 85 | ON e1.manager_id = e2.emp_id 86 | WHERE e1.manager_id IS NOT NULL 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | /* 96 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 97 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 98 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 99 | */ 100 | 101 | -------------------------------------------------------------------------------- /23.sql: -------------------------------------------------------------------------------- 1 | -- 23/30 Days SQL Challenge 2 | 3 | -- Dropping the table if it exists and then recreating it 4 | DROP TABLE IF EXISTS employee; 5 | 6 | -- Creating the employee table 7 | CREATE TABLE employee ( 8 | EMP_ID INT PRIMARY KEY, 9 | SALARY DECIMAL(10, 2) 10 | ); 11 | 12 | -- Inserting sample data into the employee table 13 | INSERT INTO employee (EMP_ID, SALARY) VALUES 14 | (1, 50000), 15 | (2, 60000), 16 | (3, 70000), 17 | (4, 45000), 18 | (5, 80000), 19 | (6, 55000), 20 | (7, 75000), 21 | (8, 62000), 22 | (9, 48000), 23 | (10, 85000); 24 | 25 | /* 26 | Question 1 27 | 28 | Given the employee table with columns EMP_ID and SALARY, 29 | write an SQL query to find all salaries greater than the average salary. 30 | return emp_id and salary 31 | */ 32 | 33 | 34 | -- ---------------------------------------------- 35 | -- My solution 36 | -- ---------------------------------------------- 37 | 38 | -- avg salary 39 | -- salary > avg salary 40 | 41 | -- SELECT AVG(salary) FROM employee; 63000 42 | 43 | SELECT 44 | emp_id, 45 | salary 46 | FROM employee 47 | WHERE salary < (SELECT AVG(salary) FROM employee) 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | /* 57 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 58 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 59 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 60 | */ 61 | -------------------------------------------------------------------------------- /24.sql: -------------------------------------------------------------------------------- 1 | -- 24/30 SQL Challenge 2 | 3 | -- ---------------------------------------------------------------------------------- 4 | -- Questions 1 5 | -- ---------------------------------------------------------------------------------- 6 | 7 | -- SCHEMA 8 | 9 | -- Dropping the table if it exists and then recreating it 10 | DROP TABLE IF EXISTS customers; 11 | 12 | -- Creating the customers table 13 | CREATE TABLE customers ( 14 | customer_id INT PRIMARY KEY, 15 | first_name VARCHAR(50), 16 | last_name VARCHAR(50), 17 | email VARCHAR(100) 18 | ); 19 | 20 | -- Inserting sample data into the customers table 21 | INSERT INTO customers (customer_id, first_name, last_name, email) VALUES 22 | (1, 'John', 'Doe', 'john.doe@example.com'), 23 | (2, 'Jane', 'Smith', 'jane.smith@example.com'), 24 | (3, 'Alice', 'Johnson', 'alice.johnson@example.com'), 25 | (4, 'Bob', 'Brown', 'bob.brown@example.com'), 26 | (5, 'Emily', 'Davis', 'john.doe@example.com'), 27 | (6, 'Michael', 'Williams', 'michael.w@example.com'), 28 | (7, 'David', 'Wilson', 'jane.smith@example.com'), 29 | (8, 'Sarah', 'Taylor', 'sarah.t@example.com'), 30 | (9, 'James', 'Anderson', 'james.a@example.com'), 31 | (10, 'Laura', 'Martinez', 'laura.m@example.com'); 32 | 33 | 34 | /* 35 | 36 | 37 | Consider a table named customers with the following columns: 38 | customer_id, first_name, last_name, and email. 39 | Write an SQL query to find all the duplicate email addresses 40 | in the customers table. 41 | 42 | */ 43 | 44 | -- email 45 | -- GROUP BY email 46 | -- HAVING COUNT(email ) > 1 47 | 48 | SELECT 49 | email 50 | -- COUNT(email) as cnt_frequency 51 | FROM customers 52 | GROUP BY email 53 | HAVING COUNT(email) > 1 54 | 55 | SELECT * FROM CUSTOMERS; 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | /* 70 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 71 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 72 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 73 | */ -------------------------------------------------------------------------------- /25.sql: -------------------------------------------------------------------------------- 1 | -- 25/30 2 | 3 | -- Drop the orders table if it exists 4 | DROP TABLE IF EXISTS orders; 5 | 6 | -- Create the orders table with columns: date, product_id, product_name, and revenue 7 | CREATE TABLE orders ( 8 | date DATE, 9 | product_id INT, 10 | product_name VARCHAR(255), 11 | revenue DECIMAL(10, 2) 12 | ); 13 | 14 | -- Insert sample data into the orders table representing orders of iPhones 15 | INSERT INTO orders (date, product_id, product_name, revenue) VALUES 16 | ('2024-01-01', 101, 'iPhone 13 Pro', 1000.00), 17 | ('2024-01-01', 102, 'iPhone 13 Pro Max', 1200.00), 18 | ('2024-01-02', 101, 'iPhone 13 Pro', 950.00), 19 | ('2024-01-02', 103, 'iPhone 12 Pro', 1100.00), 20 | ('2024-01-03', 102, 'iPhone 13 Pro Max', 1250.00), 21 | ('2024-01-03', 104, 'iPhone 11', 1400.00), 22 | ('2024-01-04', 101, 'iPhone 13 Pro', 800.00), 23 | ('2024-01-04', 102, 'iPhone 13 Pro Max', 1350.00), 24 | ('2024-01-05', 103, 'iPhone 12 Pro', 1000.00), 25 | ('2024-01-05', 104, 'iPhone 11', 700.00), 26 | ('2024-01-06', 101, 'iPhone 13 Pro', 600.00), 27 | ('2024-01-06', 102, 'iPhone 13 Pro Max', 550.00), 28 | ('2024-01-07', 101, 'iPhone 13 Pro', 400.00), 29 | ('2024-01-07', 103, 'iPhone 12 Pro', 250.00), 30 | ('2024-01-08', 102, 'iPhone 13 Pro Max', 200.00), 31 | ('2024-01-08', 104, 'iPhone 11', 150.00), 32 | ('2024-01-09', 101, 'iPhone 13 Pro', 100.00), 33 | ('2024-01-09', 102, 'iPhone 13 Pro Max', 50.00), 34 | ('2024-01-10', 101, 'iPhone 13 Pro', 1000.00), 35 | ('2024-01-10', 102, 'iPhone 13 Pro Max', 1200.00), 36 | ('2024-01-11', 101, 'iPhone 13 Pro', 950.00), 37 | ('2024-01-11', 103, 'iPhone 12 Pro', 1100.00), 38 | ('2024-01-12', 102, 'iPhone 13 Pro Max', 1250.00), 39 | ('2024-01-12', 104, 'iPhone 11', 1400.00); 40 | 41 | /* 42 | -- Flipkart Business Analyst Interview Question 43 | 44 | Question: 45 | Write a SQL query to calculate the running 46 | total revenue for each combination of date and product ID. 47 | 48 | Expected Output Columns: 49 | date, product_id, product_name, revenue, running_total 50 | ORDER BY product_id, date ascending 51 | 52 | */ 53 | 54 | -- date, prod_id, prod_name, revenue, running_total 55 | 56 | 57 | 58 | -- -------------------- 59 | -- My Solution 60 | -- -------------------- 61 | 62 | 63 | SELECT 64 | o1.date, 65 | o1.product_id, 66 | o1.product_name, 67 | o1.revenue, 68 | SUM(o2.revenue) as running_total 69 | FROM orders as o1 70 | JOIN 71 | orders as o2 72 | ON 73 | o1.product_id = o2.product_id 74 | AND 75 | o1.date >= o2.date 76 | GROUP BY 77 | o1.date, 78 | o1.product_id, 79 | o1.product_name, 80 | o1.revenue 81 | ORDER BY 82 | o1.product_id, o1.date 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | /* 100 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 101 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 102 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 103 | */ 104 | 105 | -------------------------------------------------------------------------------- /26 of 30.sql: -------------------------------------------------------------------------------- 1 | -- 26/30 Days SQL Challenge 2 | 3 | -- SCHEMA 4 | 5 | DROP TABLE IF EXISTS orders; 6 | 7 | CREATE TABLE orders ( 8 | order_id INT, 9 | customer_id INT, 10 | order_date DATE, 11 | total_items_ordered INT 12 | ); 13 | 14 | INSERT INTO orders VALUES 15 | (1, 101, '2022-01-01', 5), 16 | (2, 102, '2022-01-02', 10), 17 | (3, 103, '2022-01-03', 8), 18 | (4, 104, '2022-01-04', 12), 19 | (5, 105, '2022-01-05', 15), 20 | (6, 106, '2022-01-06', 20), 21 | (7, 107, '2022-01-07', 25), 22 | (8, 108, '2022-01-08', 30), 23 | (9, 109, '2022-01-09', 35), 24 | (10, 110, '2022-01-10', 40), 25 | (11, 111, '2022-01-11', 45), 26 | (12, 112, '2022-01-12', 50), 27 | (13, 113, '2022-01-13', 55), 28 | (14, 114, '2022-01-14', 60), 29 | (15, 115, '2022-01-15', 65); 30 | 31 | 32 | DROP TABLE IF EXISTS returns; 33 | 34 | CREATE TABLE returns ( 35 | return_id INT, 36 | order_id INT, 37 | return_date DATE, 38 | returned_items INT 39 | ); 40 | 41 | INSERT INTO returns VALUES 42 | (1, 1, '2022-01-03', 2), 43 | (2, 2, '2022-01-05', 3), 44 | (3, 3, '2022-01-07', 1), 45 | (4, 5, '2022-01-08', 4), 46 | (5, 6, '2022-01-08', 6), 47 | (6, 7, '2022-01-09', 7), 48 | (7, 8, '2022-01-10', 8), 49 | (8, 9, '2022-01-11', 9), 50 | (9, 10, '2022-01-12', 10), 51 | (10, 11, '2022-01-13', 11), 52 | (11, 12, '2022-01-14', 12), 53 | (12, 13, '2022-01-15', 13), 54 | (13, 14, '2022-01-16', 14), 55 | (14, 15, '2022-01-17', 15); 56 | 57 | 58 | 59 | 60 | /* 61 | -- Amazon Data Analyst Interview 62 | Hard Category Questions Time 15min 63 | 64 | Question: 65 | 66 | Suppose you are given two tables - Orders and Returns. 67 | The Orders table contains information about orders placed by customers, 68 | and the Returns table contains information about returned items. 69 | 70 | Design a SQL query to 71 | find the top 5 ustomer with the highest percentage 72 | of returned items out of their total orders. 73 | 74 | Return the customer ID 75 | and the percentage of returned items rounded to two decimal places. 76 | 77 | */ 78 | 79 | -- customer_id, 80 | -- total_items_ordered by each cx 81 | -- total_items_returned by each cx 82 | -- 2/4*100 50% total_items_returned/total_items_ordered*100 83 | 84 | 85 | 86 | SELECT * FROM orders; 87 | SELECT * FROM returns; 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | -- -------------------- 102 | -- My Solution 103 | -- -------------------- 104 | 105 | 106 | 107 | 108 | 109 | WITH orders_cte 110 | AS 111 | ( 112 | SELECT 113 | customer_id, 114 | SUM(total_items_ordered) as total_items_ordered 115 | FROM orders 116 | GROUP BY customer_id 117 | ), 118 | return_cte 119 | As 120 | ( 121 | SELECT 122 | o.customer_id, 123 | SUM(r.returned_items) as total_items_returned 124 | FROM returns as r 125 | JOIN 126 | orders as o 127 | ON r.order_id = o.order_id 128 | GROUP BY 129 | o.customer_id 130 | ) 131 | 132 | SELECT 133 | oc.customer_id, 134 | oc.total_items_ordered, 135 | rc.total_items_returned, 136 | ROUND(CASE 137 | WHEN oc.total_items_ordered > 0 THEN (rc.total_items_returned::float/oc.total_items_ordered::float)*100 138 | ELSE 0 END::numeric ,2) as return_percentage 139 | 140 | FROM orders_cte as oc 141 | JOIN 142 | return_cte rc 143 | ON oc.customer_id = rc.customer_id 144 | ORDER BY return_percentage DESC 145 | LIMIT 5; 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | /* 154 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 155 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 156 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 157 | */ 158 | -------------------------------------------------------------------------------- /27 of 30 .sql: -------------------------------------------------------------------------------- 1 | -- 27/30 2 | 3 | -- SCHEMAS 4 | 5 | DROP TABLE IF EXISTS orders; 6 | 7 | -- Create the orders table 8 | CREATE TABLE orders ( 9 | user_id INT, 10 | item_ordered VARCHAR(512) 11 | ); 12 | 13 | -- Insert sample data into the orders table 14 | INSERT INTO orders VALUES 15 | ('1', 'Pizza'), 16 | ('1', 'Burger'), 17 | ('2', 'Cold Drink'), 18 | ('2', 'Burger'), 19 | ('3', 'Burger'), 20 | ('3', 'Cold Drink'), 21 | ('4', 'Pizza'), 22 | ('4', 'Cold Drink'), 23 | ('5', 'Cold Drink'), 24 | ('6', 'Burger'), 25 | ('6', 'Cold Drink'), 26 | ('7', 'Pizza'), 27 | ('8', 'Burger'); 28 | 29 | -- Flipkart Data Analyst Interview Questions 30 | -- Question: Write an SQL query to fetch user IDs that have only bought both 'Burger' and 'Cold Drink' items. 31 | 32 | -- Expected Output Columns: user_id 33 | 34 | 35 | -- ----------------------- 36 | -- My Solution 37 | -- ----------------------- 38 | 39 | 40 | 41 | -- user_id 42 | -- COUNT(orders) = 2 43 | -- DISTINCT 44 | 45 | SELECT 46 | user_id 47 | -- COUNT(DISTINCT item_ordered) 48 | FROM orders 49 | GROUP BY user_id 50 | HAVING COUNT(DISTINCT item_ordered) = 2 51 | AND 52 | SUM(CASE WHEN item_ordered IN ('Burger', 'Cold Drink') 53 | THEN 1 54 | ELSE 0 55 | END 56 | ) = 2 -- 2, 3, 6 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | /* 66 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 67 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 68 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 69 | */ 70 | 71 | -------------------------------------------------------------------------------- /28 of 30.sql: -------------------------------------------------------------------------------- 1 | -- 28/30 Days SQL Challenge! 2 | 3 | DROP TABLE IF EXISTS orders; 4 | 5 | CREATE TABLE orders ( 6 | order_id INT PRIMARY KEY, 7 | seller_id INT, 8 | sale_amount DECIMAL(10, 2) 9 | ); 10 | 11 | 12 | DROP TABLE IF EXISTS returns; 13 | 14 | CREATE TABLE returns ( 15 | return_id INT PRIMARY KEY, 16 | seller_id INT, 17 | return_quantity INT 18 | ); 19 | 20 | 21 | INSERT INTO orders (order_id, seller_id, sale_amount) VALUES 22 | (1, 101, 1500.00), 23 | (2, 102, 2200.00), 24 | (3, 103, 1800.00), 25 | (4, 104, 2500.00), 26 | (5, 107, 1900.00), 27 | (6, 106, 2100.00), 28 | (7, 107, 2400.00), 29 | (8, 107, 1700.00), 30 | (9, 108, 2000.00), 31 | (10, 107, 2300.00), 32 | (11, 103, 2600.00), 33 | (12, 102, 2900.00), 34 | (13, 101, 3100.00), 35 | (14, 101, 2800.00), 36 | (15, 101, 3300.00), 37 | (16, 106, 2700.00), 38 | (17, 101, 3000.00), 39 | (18, 108, 3200.00), 40 | (19, 107, 3400.00), 41 | (20, 106, 3500.00), 42 | (21, 101, 3600.00), 43 | (22, 102, 3700.00), 44 | (23, 103, 3800.00), 45 | (24, 102, 3900.00), 46 | (25, 105, 4000.00); 47 | 48 | INSERT INTO returns (return_id, seller_id, return_quantity) VALUES 49 | (1, 101, 10), 50 | (2, 102, 5), 51 | (3, 103, 8), 52 | (4, 104, 3), 53 | (5, 105, 12), 54 | (6, 106, 6), 55 | (7, 107, 4), 56 | (8, 108, 9); 57 | 58 | 59 | 60 | /* 61 | -- Amazon DS Interview Question? 62 | 63 | -- Given two tables, orders and return, containing sales and returns data for Amazon's 64 | 65 | 66 | write a SQL query to find the top 3 sellers with the highest sales amount 67 | but the lowest lowest return qty. 68 | 69 | */ 70 | 71 | 72 | -- ------------------ 73 | -- My Solution 74 | -- ------------------ 75 | 76 | 77 | -- total_sales by each seller 78 | -- total_qty return by each seller 79 | 80 | 81 | SELECT * FROM orders; 82 | SELECT * FROM returns; 83 | 84 | 85 | WITH orders_cte 86 | AS 87 | ( 88 | SELECT 89 | seller_id, 90 | SUM(sale_amount) as total_sales 91 | FROM orders 92 | GROUP BY seller_id 93 | ), 94 | 95 | returns_cte 96 | AS 97 | ( 98 | SELECT 99 | seller_id, 100 | SUM(return_quantity) as total_return_qty 101 | FROM returns 102 | GROUP BY seller_id 103 | ) 104 | 105 | SELECT 106 | orders_cte.seller_id as seller_id, 107 | orders_cte.total_sales as total_sale_amt, 108 | COALESCE(returns_cte.total_return_qty, 0) as total_return_qty 109 | FROM orders_cte 110 | LEFT JOIN 111 | returns_cte 112 | ON orders_cte.seller_id = returns_cte.seller_id 113 | ORDER BY total_sale_amt DESC, total_return_qty ASC 114 | LIMIT 3 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | /* 131 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 132 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 133 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 134 | */ 135 | 136 | 137 | 138 | 139 | 140 | 141 | -------------------------------------------------------------------------------- /29 of 30 days.sql: -------------------------------------------------------------------------------- 1 | -- 29/30 Days SQL Challenge 2 | 3 | DROP TABLE IF EXISTS Sales; 4 | -- Create Sales table 5 | CREATE TABLE Sales ( 6 | sale_id INT, 7 | product_id INT, 8 | year INT, 9 | quantity INT, 10 | price INT 11 | ); 12 | -- Insert sample records into Sales table 13 | INSERT INTO Sales (sale_id, product_id, year, quantity, price) 14 | VALUES 15 | (1, 100, 2008, 10, 5000), 16 | (2, 100, 2009, 12, 5000), 17 | (7, 200, 2011, 15, 9000), 18 | (3, 100, 2010, 8, 5500), 19 | (4, 100, 2011, 15, 4800), 20 | (5, 200, 2013, 20, 8500), 21 | (6, 200, 2006, 18, 9200), 22 | (8, 300, 2012, 25, 7500), 23 | (9, 300, 2009, 30, 7200), 24 | (10, 300, 2013, 22, 6800); 25 | 26 | 27 | 28 | /* 29 | -- Walmart Data Analyst Interview Question 30 | 31 | Write a solution to select the product id, year, quantity, 32 | and price for the first year of every product sold. 33 | */ 34 | 35 | 36 | -- -------------------------- 37 | -- My Solutions 38 | -- -------------------------- 39 | 40 | 41 | -- first year product sale details 42 | -- product_id, year (MIN), qty and price 43 | -- rank 44 | 45 | SELECT 46 | product_id, 47 | first_year, 48 | quantity, 49 | price 50 | 51 | FROM ( 52 | SELECT 53 | product_id, 54 | year as first_year, 55 | quantity, 56 | price, 57 | RANK() OVER(PARTITION BY product_id ORDER BY year) as rn 58 | FROM sales 59 | ) subquery 60 | WHERE rn = 1 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | /* 69 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 70 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 71 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 72 | */ 73 | -------------------------------------------------------------------------------- /3.sql: -------------------------------------------------------------------------------- 1 | 2 | 3 | -- Leetcode problem LeetCode SQL Premium Problem 2853: 'Highest Salary Difference' 4 | 5 | 6 | -- DDL for Salaries table 7 | CREATE TABLE Salaries ( 8 | emp_name VARCHAR(50), 9 | department VARCHAR(50), 10 | salary INT, 11 | PRIMARY KEY (emp_name, department) 12 | ); 13 | 14 | -- DML for Salaries table 15 | INSERT INTO Salaries (emp_name, 16 | department, salary) VALUES 17 | ('Kathy', 'Engineering', 50000), 18 | ('Roy', 'Marketing', 30000), 19 | ('Charles', 'Engineering', 45000), 20 | ('Jack', 'Engineering', 85000), 21 | ('Benjamin', 'Marketing', 34000), 22 | ('Anthony', 'Marketing', 42000), 23 | ('Edward', 'Engineering', 102000), 24 | ('Terry', 'Engineering', 44000), 25 | ('Evelyn', 'Marketing', 53000), 26 | ('Arthur', 'Engineering', 32000); 27 | 28 | 29 | 30 | /* 31 | Write an SQL query to calculate the difference 32 | between the highest salaries 33 | in the marketing and engineering department. 34 | Output the absolute difference in salaries. 35 | */ 36 | 37 | 38 | 39 | 40 | 41 | SELECT * FROM Salaries; 42 | 43 | 44 | 45 | 46 | -- ---------------------------------------------- 47 | -- My solution 48 | -- ---------------------------------------------- 49 | 50 | 51 | 52 | 53 | 54 | SELECT 55 | ABS(MAX(CASE WHEN department = 'Marketing' THEN salary END) - 56 | MAX(CASE WHEN department = 'Engineering' THEN salary END)) as salary_diff 57 | FROM Salaries; 58 | 59 | -- Solving the same problem using CTE and Window Function 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | /* 69 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 70 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 71 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 72 | */ 73 | 74 | -------------------------------------------------------------------------------- /4.sql: -------------------------------------------------------------------------------- 1 | 2 | CREATE TABLE customers ( 3 | customer_id INT PRIMARY KEY, 4 | customer_name VARCHAR(255), 5 | age INT, 6 | gender VARCHAR(10) 7 | ); 8 | 9 | CREATE TABLE orders ( 10 | order_id INT PRIMARY KEY, 11 | customer_id INT, 12 | order_date DATE, 13 | total_amount DECIMAL(10, 2), 14 | FOREIGN KEY (customer_id) REFERENCES customers(customer_id) 15 | ); 16 | 17 | 18 | -- Insert values into the customers table 19 | INSERT INTO customers (customer_id, customer_name, age, gender) 20 | VALUES 21 | (1, 'John Doe', 30, 'Male'), 22 | (2, 'Jane Smith', 25, 'Female'), 23 | (3, 'Alice Johnson', 35, 'Female'), 24 | (4, 'Bob Brown', 40, 'Male'); 25 | 26 | -- Insert values into the orders table 27 | INSERT INTO orders (order_id, customer_id, order_date, total_amount) 28 | VALUES 29 | (101, 1, '2023-01-15', 150.50), 30 | (102, 2, '2022-02-20', 200.25), 31 | (103, 3, '2023-03-10', 180.75), 32 | (104, 4, '2023-04-05', 300.00), 33 | (105, 1, '2022-05-12', 175.80), 34 | (106, 2, '2021-06-18', 220.40), 35 | (107, 3, '2023-07-22', 190.30), 36 | (108, 4, '2023-08-30', 250.60), 37 | (109, 4, '2021-08-30', 250.60), 38 | (110, 4, '2024-01-30', 250.60), 39 | (111, 4, '2023-08-30', 250.60);; 40 | 41 | 42 | /* IBM Data Analyst SQL Question 43 | Q.1 44 | Customer Segmentation Problem: 45 | You have two tables: customers and orders. 46 | 47 | customers table has columns: 48 | customer_id, customer_name, age, gender. 49 | orders table has columns: 50 | order_id, customer_id, order_date, total_amount. 51 | 52 | 53 | Write an SQL query to find the average order amount 54 | for male and female customers separately 55 | return the results with 2 DECIMAL. 56 | */ 57 | 58 | 59 | 60 | 61 | 62 | -- ---------------------------------------------- 63 | -- My solution 64 | -- ---------------------------------------------- 65 | 66 | 67 | 68 | 69 | 70 | SELECT * FROM customers 71 | SELECT * FROM orders 72 | 73 | -- avg spent 74 | -- by gender 75 | -- join both table 76 | 77 | SELECT 78 | c.gender as gender_name, 79 | ROUND(avg(o.total_amount), 2) as avg_spent 80 | FROM customers as c 81 | JOIN orders as o 82 | ON c.customer_id = o.customer_id 83 | GROUP BY gender_name 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | /* 99 | Q.2 You have been given a table named sales with the following columns: 100 | 101 | order_id (unique identifier for each order) 102 | order_date (date when the order was placed) 103 | product_id (unique identifier for each product) 104 | quantity (the quantity of the product ordered in that particular order) 105 | price_per_unit (the price per unit of the product) 106 | 107 | Write an SQL query to find out the total sales revenue generated for each month in the year 2023. 108 | */ 109 | 110 | 111 | 112 | 113 | -- Create the sales table 114 | CREATE TABLE sales ( 115 | order_id INT PRIMARY KEY, 116 | order_date DATE, 117 | product_id INT, 118 | quantity INT, 119 | price_per_unit DECIMAL(10, 2) 120 | ); 121 | 122 | -- Insert sample values into the sales table 123 | INSERT INTO sales (order_id, order_date, product_id, quantity, price_per_unit) 124 | VALUES 125 | (1, '2022-03-10', 101, 2, 15.00), 126 | (2, '2022-04-05', 102, 1, 25.50), 127 | (3, '2023-01-15', 103, 3, 10.75), 128 | (4, '2023-02-20', 104, 2, 30.20), 129 | (5, '2022-05-12', 105, 4, 12.80), 130 | (6, '2023-06-18', 106, 2, 22.40), 131 | (7, '2023-07-22', 107, 1, 45.30), 132 | (8, '2021-08-30', 108, 3, 20.60); 133 | 134 | 135 | /* 136 | Q.2 You have been given a table named sales with the following columns: 137 | 138 | order_id (unique identifier for each order) 139 | order_date (date when the order was placed) 140 | product_id (unique identifier for each product) 141 | quantity (the quantity of the product ordered in that particular order) 142 | price_per_unit (the price per unit of the product) 143 | 144 | Write an SQL query to find out the total sales revenue generated for each month in the year 2023. 145 | */ 146 | 147 | 148 | 149 | 150 | 151 | -- ---------------------------------------------- 152 | -- My solution 153 | -- ---------------------------------------------- 154 | 155 | 156 | 157 | 158 | -- total_sales 159 | -- by month (group by) 160 | -- filter 2023 161 | 162 | 163 | SELECT 164 | TO_CHAR(order_date, 'month') as month_name, 165 | SUM(price_per_unit * quantity) as revene 166 | FROM sales 167 | WHERE EXTRACT(YEAR FROM order_date) = '2023' 168 | GROUP BY month_name 169 | ORDER BY 170 | EXTRACT(MONTH FROM MIN(order_date)) 171 | ; 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | /* 181 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 182 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 183 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 184 | */ 185 | 186 | 187 | 188 | 189 | -------------------------------------------------------------------------------- /5.sql: -------------------------------------------------------------------------------- 1 | -- Day 05/30 days SQL challenge 2 | 3 | -- SCHEMAS 4 | 5 | 6 | -- Create transactions table 7 | CREATE TABLE transactions ( 8 | user_id INTEGER, 9 | spend DECIMAL(10, 2), 10 | transaction_date TIMESTAMP 11 | ); 12 | 13 | -- Insert data into transactions table 14 | INSERT INTO transactions (user_id, spend, transaction_date) VALUES 15 | (111, 100.50, '2022-01-08 12:00:00'), 16 | (111, 55.00, '2022-01-10 12:00:00'), 17 | (121, 36.00, '2022-01-18 12:00:00'), 18 | (145, 24.99, '2022-01-26 12:00:00'), 19 | (111, 89.60, '2022-02-05 12:00:00'); 20 | 21 | /* 22 | -- UBER DATA ANALYST INTERVIEW QUESTION 23 | Question: 24 | Write a SQL query to obtain the third transaction of every user. 25 | Output the user id, spend, and transaction date. 26 | 27 | */ 28 | 29 | 30 | 31 | 32 | 33 | -- ---------------------------------------------- 34 | -- My solution 35 | -- ---------------------------------------------- 36 | 37 | 38 | 39 | 40 | 41 | 42 | SELECT 43 | user_id, 44 | spend, 45 | transaction_date 46 | FROM 47 | ( 48 | SELECT 49 | user_id, 50 | spend, 51 | transaction_date, 52 | ROW_NUMBER() OVER(PARTITION BY user_id ORDER BY transaction_date) as rn 53 | FROM transactions) x1 54 | WHERE rn = 3 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | -- Question 2 66 | 67 | -- Create product_revenue table 68 | CREATE TABLE product_revenue ( 69 | product_name VARCHAR(255), 70 | year INTEGER, 71 | revenue DECIMAL(10, 2) 72 | ); 73 | 74 | -- Insert sample records 75 | INSERT INTO product_revenue (product_name, year, revenue) VALUES 76 | ('Product A', 2022, 10000.00), 77 | ('Product A', 2023, 9500.00), 78 | ('Product B', 2022, 15000.00), 79 | ('Product B', 2023, 14500.00), 80 | ('Product C', 2022, 8000.00), 81 | ('Product C', 2023, 8500.00), 82 | ('Product D', 2022, 12000.00), 83 | ('Product D', 2023, 12500.00), 84 | ('Product E', 2022, 20000.00), 85 | ('Product E', 2023, 19000.00), 86 | ('Product F', 2022, 7000.00), 87 | ('Product F', 2023, 7200.00), 88 | ('Product G', 2022, 18000.00), 89 | ('Product G', 2023, 17000.00), 90 | ('Product H', 2022, 3000.00), 91 | ('Product H', 2023, 3200.00), 92 | ('Product I', 2022, 9000.00), 93 | ('Product I', 2023, 9200.00), 94 | ('Product J', 2022, 6000.00), 95 | ('Product J', 2023, 5900.00); 96 | 97 | -- Formulate the question 98 | 99 | 100 | /* 101 | Question: 102 | Find the top 5 products whose revenue has decreased in comparison to the 103 | previous year (both 2022 and 2023). Return the product name, 104 | revenue for the previous year, revenue for the current year, 105 | revenue decreased, and the decreased ratio (percentage). 106 | */ 107 | 108 | 109 | 110 | 111 | 112 | -- ---------------------------------------------- 113 | -- My solution 114 | -- ---------------------------------------------- 115 | 116 | 117 | 118 | 119 | WITH revenue_comparison AS ( 120 | SELECT 121 | product_name, 122 | year, 123 | revenue, 124 | LAG(revenue) OVER (PARTITION BY product_name ORDER BY year) AS prev_year_revenue 125 | FROM product_revenue 126 | ) 127 | SELECT 128 | product_name, 129 | prev_year_revenue AS previous_year_revenue, 130 | revenue AS current_year_revenue, 131 | (prev_year_revenue - revenue) AS revenue_decreased, 132 | ((prev_year_revenue - revenue) / prev_year_revenue) * 100 AS decreased_ratio_percentage 133 | FROM revenue_comparison 134 | WHERE prev_year_revenue IS NOT NULL 135 | ORDER BY revenue_decreased DESC 136 | LIMIT 5; 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | -- Question 1 link :: https://datalemur.com/questions/sql-page-with-no-likes 147 | 148 | 149 | /* 150 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 151 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 152 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 153 | */ 154 | -------------------------------------------------------------------------------- /6.sql: -------------------------------------------------------------------------------- 1 | -- Day 06/30 days sql challenge 2 | 3 | 4 | -- SCHEMAS 5 | 6 | 7 | -- Create viewership table 8 | CREATE TABLE viewership ( 9 | device_type VARCHAR(255), 10 | viewership_count INTEGER 11 | ); 12 | 13 | -- Insert sample records 14 | INSERT INTO viewership (device_type, viewership_count) VALUES 15 | ('laptop', 5000), 16 | ('tablet', 3000), 17 | ('phone', 7000), 18 | ('laptop', 6000), 19 | ('tablet', 4000), 20 | ('phone', 8000), 21 | ('laptop', 5500), 22 | ('tablet', 3500), 23 | ('phone', 7500); 24 | 25 | -- Formulate the question 26 | 27 | 28 | /* 29 | 30 | Question: 31 | Write a query that calculates the total viewership for laptops and mobile devices, 32 | where mobile is defined as the sum of tablet and phone viewership. Output the total 33 | viewership for laptops as laptop_views and the total viewership for mobile devices as mobile_views. 34 | 35 | */ 36 | 37 | 38 | 39 | -- ---------------------------------------------- 40 | -- My solution 41 | -- ---------------------------------------------- 42 | 43 | 44 | 45 | 46 | SELECT 47 | SUM(CASE WHEN device_type = 'laptop' THEN viewership_count ELSE 0 END) AS laptop_views, 48 | SUM(CASE WHEN device_type IN ('tablet', 'phone') THEN viewership_count ELSE 0 END) AS mobile_views 49 | FROM viewership; 50 | 51 | 52 | 53 | -- Question link https://datalemur.com/questions/laptop-mobile-viewership 54 | 55 | /* 56 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 57 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 58 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 59 | */ 60 | 61 | -------------------------------------------------------------------------------- /7.sql: -------------------------------------------------------------------------------- 1 | -- Day 07/30 SQL Challenge 2 | 3 | 4 | -- Create product_spend table 5 | CREATE TABLE product_spend ( 6 | category VARCHAR(255), 7 | product VARCHAR(255), 8 | user_id INTEGER, 9 | spend DECIMAL(10, 2), 10 | transaction_date TIMESTAMP 11 | ); 12 | 13 | -- Insert sample records 14 | INSERT INTO product_spend (category, product, user_id, spend, transaction_date) VALUES 15 | ('appliance', 'refrigerator', 165, 246.00, '2021-12-26 12:00:00'), 16 | ('appliance', 'refrigerator', 123, 299.99, '2022-03-02 12:00:00'), 17 | ('appliance', 'washing machine', 123, 219.80, '2022-03-02 12:00:00'), 18 | ('electronics', 'vacuum', 178, 152.00, '2022-04-05 12:00:00'), 19 | ('electronics', 'wireless headset', 156, 249.90, '2022-07-08 12:00:00'), 20 | ('electronics', 'vacuum', 145, 189.00, '2022-07-15 12:00:00'); 21 | 22 | -- Formulate the question 23 | 24 | 25 | /* 26 | -- Amazon Interview question 27 | 28 | Question: 29 | Write a query to identify the top two highest-grossing products 30 | within each category in the year 2022. Output should include the category, 31 | product, and total spend. 32 | 33 | */ 34 | 35 | 36 | 37 | -- ---------------------------------------------- 38 | -- My solution 39 | -- ---------------------------------------------- 40 | 41 | 42 | 43 | 44 | SELECT 45 | category, 46 | product, 47 | total_spend 48 | FROM ( 49 | SELECT 50 | category, 51 | product, 52 | SUM(spend) as total_spend, 53 | RANK() OVER(PARTITION BY category ORDER BY SUM(spend) DESC) rn 54 | FROM product_spend 55 | WHERE EXTRACT(YEAR FROM transaction_date) = '2022' 56 | GROUP BY 1, 2 ) x1 57 | WHERE rn <= 2 58 | 59 | 60 | 61 | 62 | 63 | 64 | -- Question link https://datalemur.com/questions/sql-highest-grossing 65 | 66 | /* 67 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 68 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 69 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 70 | */ 71 | 72 | 73 | -------------------------------------------------------------------------------- /8.sql: -------------------------------------------------------------------------------- 1 | -- Day 08/30 days SQL Challenge 2 | 3 | 4 | 5 | -- Create tweets table 6 | CREATE TABLE tweets ( 7 | tweet_id INTEGER, 8 | user_id INTEGER, 9 | msg VARCHAR(255), 10 | tweet_date TIMESTAMP 11 | ); 12 | 13 | -- Insert sample records 14 | INSERT INTO tweets (tweet_id, user_id, msg, tweet_date) VALUES 15 | (214252, 111, 'Am considering taking Tesla private at $420. Funding secured.', '2021-12-30 00:00:00'), 16 | (739252, 111, 'Despite the constant negative press covfefe', '2022-01-01 00:00:00'), 17 | (846402, 111, 'Following @NickSinghTech on Twitter changed my life!', '2022-02-14 00:00:00'), 18 | (241425, 254, 'If the salary is so competitive why won’t you tell me what it is?', '2022-03-01 00:00:00'), 19 | (231574, 148, 'I no longer have a manager. I can\'t be managed', '2022-03-23 00:00:00'); 20 | 21 | -- Formulate the question 22 | 23 | /* 24 | 25 | Question: 26 | Write a query to obtain a histogram of tweets posted per user in 2022. 27 | Output the tweet count per user as the bucket and the number of Twitter users who fall into that bucket. 28 | 29 | */ 30 | 31 | 32 | 33 | 34 | 35 | -- ---------------------------------------------- 36 | -- My solution 37 | -- ---------------------------------------------- 38 | 39 | 40 | 41 | 42 | SELECT 43 | num_post, 44 | COUNT(user_id) as num_user 45 | FROM 46 | ( 47 | SELECT 48 | user_id, 49 | COUNT(tweet_id) as num_post 50 | FROM tweets 51 | WHERE EXTRACT(YEAR FROM tweet_date) = '2022' 52 | GROUP BY user_id 53 | )x1 54 | GROUP BY num_post 55 | 56 | 57 | 58 | 59 | 60 | 61 | -- Question link https://datalemur.com/questions/sql-histogram-tweets 62 | 63 | /* 64 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 65 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 66 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 67 | */ 68 | 69 | -------------------------------------------------------------------------------- /9.sql: -------------------------------------------------------------------------------- 1 | -- Day 09 SQL 2 | 3 | 4 | -- Create Department table 5 | CREATE TABLE Department ( 6 | id INT PRIMARY KEY, 7 | name VARCHAR(50) 8 | ); 9 | 10 | -- Insert values into Department table 11 | INSERT INTO Department (id, name) VALUES 12 | (1, 'IT'), 13 | (2, 'Sales'); 14 | 15 | -- Create Employee table 16 | CREATE TABLE Employee ( 17 | id INT PRIMARY KEY, 18 | name VARCHAR(50), 19 | salary INT, 20 | departmentId INT, 21 | FOREIGN KEY (departmentId) REFERENCES Department(id) 22 | ); 23 | 24 | -- Insert additional records into Employee table 25 | INSERT INTO Employee (id, name, salary, departmentId) VALUES 26 | (8, 'Alice', 75000, 2), 27 | (9, 'Bob', 82000, 2), 28 | (10, 'Carol', 78000, 1), 29 | (11, 'David', 70000, 1), 30 | (12, 'Eva', 85000, 2), 31 | (13, 'Frank', 72000, 1), 32 | (14, 'Gina', 83000, 1), 33 | (15, 'Hank', 68000, 1), 34 | (16, 'Irene', 76000, 2), 35 | (17, 'Jack', 74000, 2), 36 | (18, 'Kelly', 79000, 1), 37 | (19, 'Liam', 71000, 1), 38 | (20, 'Molly', 77000, 2), 39 | (21, 'Nathan', 81000, 1), 40 | (22, 'Olivia', 73000, 2), 41 | (23, 'Peter', 78000, 1), 42 | (24, 'Quinn', 72000, 1), 43 | (25, 'Rachel', 80000, 2), 44 | (26, 'Steve', 75000, 2), 45 | (27, 'Tina', 79000, 1); 46 | 47 | 48 | 49 | 50 | 51 | 52 | /* 53 | -- Question Day-09/30 Find Department's Top three 54 | salaries in each department. 55 | 56 | Table: department 57 | 58 | +-------------+---------+ 59 | | Column Name | Type | 60 | +-------------+---------+ 61 | | id | int | id is primary key 62 | | name | varchar | department_name 63 | +-------------+---------+ 64 | 65 | Table: employee 66 | +--------------+---------+ 67 | | Column Name | Type | 68 | +--------------+---------+ 69 | | id | int | id is the primary key 70 | | name | varchar | 71 | | salary | int | 72 | | departmentId | int | departmentId is the foreign key 73 | +--------------+---------+ 74 | */ 75 | A company's executives are interested in seeing who earns the most 76 | money in each of the company's departments. 77 | A high earner in a department is an employee 78 | who has a salary in the top three unique salaries 79 | for that department. 80 | 81 | -- Write a solution to find the employees 82 | who are high earners in each of the departments. 83 | 84 | SELECT * FROM department; 85 | SELECT * FROM employee; 86 | 87 | 88 | -- ------------------------------------------------------------------------ 89 | -- My Solution using Window Function Dense Rank and Sub queries 90 | -- ------------------------------------------------------------------------ 91 | 92 | SELECT 93 | department_name, 94 | emp_name, 95 | salary 96 | FROM ( 97 | SELECT 98 | d.name as department_name, 99 | e.name as emp_name, 100 | e.salary as salary, 101 | DENSE_RANK() OVER(PARTITION BY d.name ORDER BY e.salary DESC) drn 102 | FROM employee as e 103 | JOIN 104 | department as d 105 | ON e.departmentid = d.id) x1 106 | WHERE drn <= 3 107 | 108 | -- ------------------------------------------------------------------------ 109 | -- For any doubts contact me in Linedin https://www.linkedin.com/in/najirr/ 110 | -- ------------------------------------------------------------------------ 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | -------------------------------------------------------------------------------- /Day 30 of 30.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Songs ( 2 | song_id INT PRIMARY KEY, 3 | song_name VARCHAR(255), 4 | artist_name VARCHAR(255) 5 | ); 6 | 7 | CREATE TABLE Listens ( 8 | listen_id INT PRIMARY KEY, 9 | user_id INT, 10 | song_id INT, 11 | listen_date DATE, 12 | FOREIGN KEY (song_id) REFERENCES Songs(song_id) 13 | ); 14 | 15 | 16 | -- Inserting records into the Songs table 17 | INSERT INTO Songs (song_id, song_name, artist_name) VALUES 18 | (1, 'Song A', 'Artist X'), 19 | (2, 'Song B', 'Artist Y'), 20 | (3, 'Song C', 'Artist Z'), 21 | (4, 'Song D', 'Artist X'), 22 | (5, 'Song E', 'Artist Y'), 23 | (6, 'Song F', 'Artist Z'), 24 | (7, 'Song G', 'Artist X'), 25 | (8, 'Song H', 'Artist Y'), 26 | (9, 'Song I', 'Artist Z'), 27 | (10, 'Song J', 'Artist X'); 28 | 29 | -- Inserting records into the Listens table 30 | INSERT INTO Listens (listen_id, user_id, song_id, listen_date) VALUES 31 | (1, 101, 1, '2024-03-22'), 32 | (2, 102, 1, '2024-03-22'), 33 | (3, 103, 2, '2024-03-22'), 34 | (4, 104, 2, '2024-03-22'), 35 | (5, 105, 2, '2024-03-22'), 36 | (6, 106, 3, '2024-03-22'), 37 | (7, 107, 4, '2024-03-22'), 38 | (8, 108, 5, '2024-03-22'), 39 | (9, 109, 6, '2024-03-22'), 40 | (10, 110, 7, '2024-03-22'), 41 | (11, 111, 7, '2024-03-22'), 42 | (12, 112, 8, '2024-03-22'), 43 | (13, 113, 8, '2024-03-22'), 44 | (14, 114, 8, '2024-03-22'), 45 | (15, 115, 9, '2024-03-22'), 46 | (16, 116, 9, '2024-03-22'), 47 | (17, 117, 10, '2024-03-22'), 48 | (18, 118, 10, '2024-03-22'), 49 | (19, 119, 10, '2024-03-22'), 50 | (20, 120, 10, '2024-03-22'); 51 | 52 | 53 | 54 | 55 | 56 | /* 57 | 58 | Spotify Data Analyst Interview Questions 59 | Question: 60 | Write a SQL query to find the top 10 most popular songs by total number of listens. 61 | You have two tables: Songs (containing song_id, song_name, 62 | and artist_name) and Listens (containing listen_id, user_id, song_id, and listen_date). 63 | 64 | */ 65 | 66 | 67 | -- --------------------------- 68 | -- My Solution 69 | -- --------------------------- 70 | 71 | 72 | SELECT 73 | song_name, 74 | times_of_listens, 75 | DENSE_RANK() OVER (ORDER BY times_of_listens DESC) AS rank 76 | FROM 77 | (SELECT 78 | s.song_name, 79 | COUNT(l.listen_id) AS times_of_listens 80 | FROM 81 | Songs s 82 | JOIN 83 | Listens l ON s.song_id = l.song_id 84 | GROUP BY 85 | s.song_name) AS sub 86 | ORDER BY 87 | times_of_listens DESC 88 | LIMIT 10; 89 | 90 | 91 | 92 | 93 | 94 | /* 95 | Follow me in LinkedIn :: https://www.linkedin.com/in/najirr/ 96 | Follow me in insta :: https://www.instagram.com/zero_analyst/ 97 | Subscribe to our youtube channel :: https://www.youtube.com/@zero_analyst 98 | */ 99 | 100 | 101 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SQL Interview Question Solve Challenge 2 | 3 | Welcome to the SQL Interview Question Solve Challenge repository! In this repository, you'll find solutions to common SQL interview questions tailored for Data Analysts and Business Analysts. This repository is part of our YouTube series where we tackle one SQL interview question per day for 30 days. 4 | 5 | ![SQL Interview Question Solve Challenge](https://github.com/najirh/30-Days-SQL-Challenge/blob/main/SQL%20Challenge%200130.png) 6 | 7 | ## About the Series 8 | 9 | Each day, we'll solve a different SQL interview question commonly asked by top tech companies like Amazon, Google, Facebook, and more. Our goal is to help aspiring data analysts and business analysts prepare for their interviews by providing detailed explanations and solutions to these questions. 10 | 11 | ## Getting Started 12 | 13 | To get started with this repository, simply browse through the folders to find solutions to specific interview questions. Each solution is accompanied by detailed explanations and SQL code snippets to help you understand the problem-solving process. 14 | 15 | ## Contributions 16 | 17 | Contributions to this repository are welcome! If you have a solution to a SQL interview question that you'd like to share, feel free to submit a pull request. Make sure to follow the contribution guidelines outlined in the `CONTRIBUTING.md` file. 18 | 19 | ## Connect with Us 20 | 21 | - Subscribe to our YouTube channel: [Your YouTube Channel](https://youtu.be/aKmDHTaYS6w) 22 | - Follow us on LinkedIn: [@LinkedIn](https://www.linkedin.com/in/najirr/) 23 | 24 | ## Support Us 25 | 26 | If you find this repository helpful, consider supporting us by giving it a star ⭐️ and sharing it with others who might benefit from it. 27 | 28 | ## License 29 | 30 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. 31 | -------------------------------------------------------------------------------- /SQL Challenge 0130.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/najirh/30-Days-SQL-Challenge/2efab59af8cba54b0a6e5056beb8847fbad73109/SQL Challenge 0130.png --------------------------------------------------------------------------------