├── #8Weeksqlchallange ├── Case Study # 1 - Danny's Diner │ ├── Danny's Diner Solution.md │ ├── Danny's Diner Solution.sql │ ├── Diner_Schema.sql │ ├── ERD.png │ └── README.md ├── Case Study # 2 - Pizza Runner │ ├── 0. Data Cleaning.md │ ├── A. Pizza Metrics.md │ ├── B. Runner and Customer Experience.md │ ├── C. Ingredient Optimisation.md │ ├── D. Pricing and Ratings.md │ ├── Pizza Runner Schema.sql │ └── Readme.md ├── Case Study # 3 - Foodie Fi │ └── Readme.md └── README.md ├── Analyze Incredible 120 Years of Olympic Games ├── Olympics_data.zip └── README.md ├── Credit card spending habits in India ├── Credit Card Image.jpg ├── CreditCard_Sql_EDA_01June.pdf ├── Readme.md └── SqlCode_Creditcard.sql ├── Cryptocurrency ├── Cryptocurrency Solutions.md ├── PostgresCode.sql ├── README.MD ├── Schema_Postgres.sql ├── crypto-erd.png ├── members.csv ├── prices.csv └── transactions.csv ├── Indian Bank Case Study ├── IndianBank_Queries.sql └── README.md ├── Murder Mystery Case Study ├── Crime Scene.csv ├── Drivers license.csv ├── GetFitNow check in.csv ├── GetFitNow members.csv ├── Interviews.csv ├── Person.csv ├── Readme.md ├── SQL Murder Mystery.sql └── Schema.png ├── OYO business case study ├── Oyo Business Room Sales Analysis_Sql_18June.pdf ├── Oyo_City.xlsx ├── Oyo_Sales.xlsx ├── Readme.md └── SqlCode_OYO_business.sql ├── README.md ├── School Case Study ├── README.md └── School_Queries.sql ├── Sql_case_studies(Data In Motion, LLC) ├── 01Sql_Challenge(The Tiny Shop) │ ├── PostgreSqlcode.sql │ └── Readme.md └── 02Sql_Challenge(Human Resources) │ ├── PostgreSqlCode.sql │ └── Readme.md ├── Sql_case_studies(Steel data challenge) ├── 01Sql_challenge (Steve's Car Showroom) │ ├── 01Sql_challenge (Steve's Car Showroom).jpeg │ └── Schema.sql ├── 02Sql_challenge (Esports Tournament) │ ├── 02Sql_challenge (Esports Tournament).jpeg │ └── Schema.sql ├── 03Sql_challenge (Customer Insights) │ ├── 03Sql_challenge (Customer Insights).jpeg │ └── Schema.sql ├── 04Sql_challenge (Finance Analysis) │ ├── 04Sql_challenge (Finance Analysis)Part 1.jpeg │ ├── 04Sql_challenge (Finance Analysis)Part 2.jpeg │ ├── Schema.sql │ └── Sqlcode(Finance analysis).sql ├── 05Sql_Challenge (Marketing Analysis) │ ├── 06 Sql_Challenge (Marketing Analysis).sql │ ├── Schema.sql │ └── Sql Challenge_6_Steeldata.pdf ├── 05Sql_Challenge (Pub chain) │ ├── Schema.sql │ └── Sqlcode(PubPricinganalysis).sql └── 05Sql_Challenge (Pub chain) │ └── Sql Challenge_5_Steeldata.pdf └── Supplier Case Study ├── README.md └── Supplier_Queries.sql /#8Weeksqlchallange/Case Study # 1 - Danny's Diner/Danny's Diner Solution.md: -------------------------------------------------------------------------------- 1 | # :ramen: :curry: :sushi: Case Study #1: Danny's Diner 2 | 3 | ## Case Study Questions 4 | 5 | 1. What is the total amount each customer spent at the restaurant? 6 | 2. How many days has each customer visited the restaurant? 7 | 3. What was the first item from the menu purchased by each customer? 8 | 4. What is the most purchased item on the menu and how many times was it purchased by all customers? 9 | 5. Which item was the most popular for each customer? 10 | 6. Which item was purchased first by the customer after they became a member? 11 | 7. Which item was purchased just before the customer became a member? 12 | 8. What is the total items and amount spent for each member before they became a member? 13 | 9. If each $1 spent equates to 10 points and sushi has a 2x points multiplier - how many points would each customer have? 14 | 10. In the first week after a customer joins the program (including their join date) they earn 2x points on all items, not just sushi - how many points do customer A and B have at the end of January? 15 | *** 16 | 17 | ### 1. What is the total amount each customer spent at the restaurant? 18 |
19 | Click here for the solution 20 | 21 | ```sql 22 | SELECT s.customer_id AS customer, SUM(m.price) AS spentamount_cust 23 | FROM sales s 24 | JOIN menu m 25 | ON s.product_id = m.product_id 26 | GROUP BY s.customer_id; 27 | ``` 28 |
29 | 30 | #### Output: 31 | |customer|spentamount_cust| 32 | |--------|----------------| 33 | |A| 76 34 | |B |74 35 | |C |36 36 | 37 | *** 38 | 39 | ### 2. How many days has each customer visited the restaurant? 40 |
41 | Click here for the solution 42 | 43 | ```sql 44 | SELECT customer_id, COUNT(DISTINCT order_date) AS Visit_frequency 45 | FROM sales 46 | GROUP BY customer_id; 47 | ``` 48 |
49 | 50 | #### Output: 51 | |customer_id| Visit_frequency| 52 | |-----------|----------------| 53 | |A| 4 54 | |B |6 55 | |C |2 56 | 57 | *** 58 | 59 | ### 3. What was the first item from the menu purchased by each customer? 60 | -- Asssumption: Since the timestamp is missing, all items bought on the first day is considered as the first item(provided multiple items were purchased on the first day) 61 |
62 | Click here for the solution 63 | 64 | ```sql 65 | SELECT s.customer_id AS customer, m.product_name AS food_item 66 | FROM sales s 67 | JOIN menu m 68 | ON s.product_id = m.product_id 69 | WHERE s.order_date = (SELECT MIN(order_date) FROM sales WHERE customer_id = s.customer_id) 70 | ORDER BY s.product_id; 71 | ``` 72 |
73 | 74 | #### Output: 75 | |customer |food_item| 76 | |--------|----------| 77 | |A| sushi 78 | |B |curry 79 | |C |ramen 80 | 81 | *** 82 | 83 | ### 4. What is the most purchased item on the menu and how many times was it purchased by all customers? 84 |
85 | Click here for the solution 86 | 87 | ```sql 88 | SELECT TOP 1 m.product_name, COUNT(s.product_id) AS order_count 89 | FROM sales s 90 | JOIN menu m ON s.product_id=m.product_id 91 | GROUP BY m.product_name 92 | ORDER BY order_count DESC; 93 | ``` 94 |
95 | 96 | #### Output: 97 | |product_name| order_count| 98 | |------------|-------------| 99 | |ramen |8| 100 | *** 101 | 102 | ### 5. Which item was the most popular for each customer? 103 | -- Asssumption: Products with the highest purchase counts are all considered to be popular for each customer 104 |
105 | Click here for the solution 106 | 107 | ```sql 108 | with cte_order_count as 109 | ( 110 | select s.customer_id, 111 | m.product_name, 112 | count(*) as order_count 113 | from sales as s 114 | inner join menu as m 115 | on s.product_id = m.product_id 116 | group by s.customer_id,m.product_name 117 | ), 118 | cte_popular_rank AS ( 119 | SELECT *, 120 | rank() over(partition by customer_id order by order_count desc) as rn 121 | from cte_order_count ) 122 | 123 | SELECT customer_id as customer, product_name as food_item,order_count 124 | FROM cte_popular_rank 125 | WHERE rn = 1; 126 | ``` 127 |
128 | 129 | #### Output: 130 | 131 | | customer_id | product_name | order_count | rank | 132 | |--------------|---------------|--------------|-------| 133 | | A | ramen | 3 | 1 | 134 | | B | ramen | 2 | 1 | 135 | | B | curry | 2 | 1 | 136 | | B | sushi | 2 | 1 | 137 | | C | ramen | 3 | 1 | 138 | 139 | *** 140 | 141 | ### 6. Which item was purchased first by the customer after they became a member? 142 | -- Before answering question 6, I created a membership_validation table to validate only those customers joining in the membership program: 143 |
144 | Click here for the solution 145 | 146 | ```sql 147 | DROP TABLE #Membership_validation; 148 | CREATE TABLE #Membership_validation 149 | ( 150 | customer_id varchar(1), 151 | order_date date, 152 | product_name varchar(5), 153 | price int, 154 | join_date date, 155 | membership varchar(5) 156 | ) 157 | 158 | INSERT INTO #Membership_validation 159 | SELECT s.customer_id, s.order_date, m.product_name, m.price, mem.join_date, 160 | CASE WHEN s.order_date >= mem.join_date THEN 'X' ELSE '' END AS membership 161 | FROM sales s 162 | INNER JOIN menu m ON s.product_id = m.product_id 163 | LEFT JOIN members mem ON mem.customer_id = s.customer_id 164 | WHERE join_date IS NOT NULL 165 | ORDER BY customer_id, order_date; 166 | 167 | select * from #Membership_validation; 168 | ``` 169 |
170 | 171 | #### Temporary Table {#Membership_validation} Output: 172 | 173 | | customer_id| order_date | product_name | price | join_date |membership | 174 | |:------------:|:---------------:|:--------------:|:-------:|:--------------:|:-----------:| 175 | | A |2021-01-01| sushi| 10| 2021-01-07| | 176 | | A |2021-01-01 |curry | 15 |2021-01-07 | | 177 | | A |2021-01-07 |curry | 15 |2021-01-07 | X | 178 | | A |2021-01-10 |ramen | 12 |2021-01-07 | X 179 | | A |2021-01-11 | ramen | 12 |2021-01-07 | X 180 | | A |2021-01-11 | ramen |12 | 2021-01-07 | X 181 | | B |2021-01-01 | curry | 15 | 2021-01-09 | | 182 | | B | 2021-01-02 | curry | 15 | 2021-01-09 | | 183 | | B |2021-01-04 | sushi |10 | 2021-01-09 | | 184 | | B |2021-01-11 | sushi |10 | 2021-01-09 | X | 185 | | B |2021-01-16 | ramen | 12 | 2021-01-09| X| 186 | | B |2021-02-01 | ramen |12 |2021-01-09 | X| 187 | 188 |
189 | Click here for the solution 190 | 191 | ```sql 192 | WITH cte_first_after_mem AS ( 193 | SELECT 194 | customer_id, 195 | product_name, 196 | order_date, 197 | RANK() OVER(PARTITION BY customer_id ORDER BY order_date) AS purchase_order 198 | FROM #Membership_validation 199 | WHERE membership = 'X' 200 | ) 201 | SELECT * FROM cte_first_after_mem 202 | WHERE purchase_order = 1; 203 | ``` 204 |
205 | 206 | #### Output: 207 | |customer_id| product_name| order_date| purchase_order| 208 | |:---------:|:-----------:|:--------:|:-------------:| 209 | |A| curry| 2021-01-07| 1| 210 | |B |sushi| 2021-01-11 |1| 211 | 212 | *** 213 | 214 | ### 7. Which item was purchased just before the customer became a member? 215 |
216 | Click here for the solution 217 | 218 | ```sql 219 | WITH cte_last_before_mem AS ( 220 | SELECT 221 | customer_id, 222 | product_name, 223 | order_date, 224 | RANK() OVER( PARTITION BY customer_id ORDER BY order_date DESC) AS purchase_order 225 | FROM #Membership_validation 226 | WHERE membership = '' 227 | ) 228 | SELECT * FROM cte_last_before_mem 229 | WHERE purchase_order = 1; 230 | ``` 231 |
232 | 233 | #### Output: 234 | |customer_id |product_name| order_date |purchase_order| 235 | |:---------:|:------------:|:----------:|:-------------:| 236 | |A |sushi| 2021-01-01| 1| 237 | |A |curry| 2021-01-01| 1| 238 | |B| sushi| 2021-01-04| 1| 239 | 240 | *** 241 | 242 | ### 8. What is the total items and amount spent for each member before they became a member? 243 |
244 | Click here for the solution 245 | 246 | ```sql 247 | WITH cte_spent_before_mem AS ( 248 | SELECT 249 | customer_id, 250 | product_name, 251 | price 252 | FROM #Membership_validation 253 | WHERE membership = '' 254 | ) 255 | SELECT 256 | customer_id, 257 | SUM(price) AS total_spent, 258 | COUNT(*) AS total_items 259 | FROM cte_spent_before_mem 260 | GROUP BY customer_id 261 | ORDER BY customer_id; 262 | ``` 263 |
264 | 265 | #### Output: 266 | |customer_id |total_spent |total_items| 267 | |:---------:|:------------:|:---------:| 268 | |A| 25| 2| 269 | |B |40| 3| 270 | 271 | *** 272 | 273 | ### 9. If each $1 spent equates to 10 points and sushi has a 2x points multiplier - how many points would each customer have? 274 |
275 | Click here for the solution 276 | 277 | ```sql 278 | SELECT 279 | customer_id, 280 | SUM( CASE 281 | WHEN product_name = 'sushi' THEN (price * 20) ELSE (price * 10) 282 | END 283 | ) AS total_points 284 | FROM #Membership_validation 285 | WHERE order_date >= join_date 286 | GROUP BY customer_id 287 | ORDER BY customer_id; 288 | ``` 289 |
290 | 291 | #### Output: 292 | |customer_id |total_points| 293 | |:----------:|:-----------:| 294 | |A| 510| 295 | |B| 440| 296 | 297 | ### 10. In the first week after a customer joins the program (including their join date) they earn 2x points on all items, not just sushi - how many points do customer A and B have at the end of January 298 | Asssumption: Points is rewarded only after the customer joins in the membership program 299 | #### Steps 300 | 1. Find the program_last_date which is 7 days after a customer joins the program (including their join date) 301 | 2. Determine the customer points for each transaction and for members with a membership 302 | a. During the first week of the membership -> points = price*20 irrespective of the purchase item 303 | b. Product = Sushi -> and order_date is not within a week of membership -> points = price*20 304 | c. Product = Not Sushi -> and order_date is not within a week of membership -> points = price*10 305 | 3. Conditions in WHERE clause 306 | a. order_date <= '2021-01-31' -> Order must be placed before 31st January 2021 307 | b. order_date >= join_date -> Points awarded to only customers with a membership 308 | 309 |
310 | Click here for the solution 311 | 312 | ```sql 313 | with program_last_day_cte as 314 | ( 315 | select join_date , 316 | Dateadd(dd,7,join_date) as program_last_date, 317 | customer_id 318 | from members 319 | ) 320 | SELECT s.customer_id, 321 | SUM(CASE 322 | WHEN order_date BETWEEN join_date AND program_last_date THEN price*10*2 323 | WHEN order_date NOT BETWEEN join_date AND program_last_date 324 | AND product_name = 'sushi' THEN price*10*2 325 | WHEN order_date NOT BETWEEN join_date AND program_last_date 326 | AND product_name != 'sushi' THEN price*10 327 | END) AS customer_points 328 | FROM MENU as m 329 | INNER JOIN sales as s on m.product_id = s.product_id 330 | INNER JOIN program_last_day_cte as k on k.customer_id = s.customer_id 331 | AND order_date <='2021-01-31' 332 | AND order_date >=join_date 333 | GROUP BY s.customer_id 334 | ORDER BY s.customer_id; 335 | ``` 336 |
337 | 338 | #### Output: 339 | |customer_id |customer_points| 340 | |:----------:|:-------------:| 341 | |A| 1020| 342 | |B| 440| 343 | 344 | *** 345 | 346 | 347 | Click [here](https://github.com/AmitPatel-analyst/SQL-Case-Study/tree/main/%238Weeksqlchallange) to move back to the 8-Week-SQL-Challenge repository! 348 | 349 | 350 | -------------------------------------------------------------------------------- /#8Weeksqlchallange/Case Study # 1 - Danny's Diner/Danny's Diner Solution.sql: -------------------------------------------------------------------------------- 1 | ---------------------------------- 2 | -- CASE STUDY #1: DANNY'S DINER -- 3 | ---------------------------------- 4 | 5 | -- Author: Amit Patel 6 | -- Date: 25/04/2023 7 | -- Tool used: MSSQL Server 8 | 9 | -------------------------- 10 | -- CASE STUDY QUESTIONS -- 11 | -------------------------- 12 | 13 | -- 1. What is the total amount each customer spent at the restaurant? 14 | 15 | SELECT s.customer_id AS customer, SUM(m.price) AS spentamount_cust 16 | FROM sales s 17 | JOIN menu m 18 | ON s.product_id = m.product_id 19 | GROUP BY s.customer_id; 20 | 21 | 22 | -- 2. How many days has each customer visited the restaurant? 23 | 24 | SELECT customer_id, COUNT(DISTINCT order_date) AS Visit_frequency 25 | FROM sales 26 | GROUP BY customer_id; 27 | 28 | -- 3. What was the first item from the menu purchased by each customer? 29 | -- -- Asssumption: Since the timestamp is missing, all items bought on the first day is considered as the first item(provided multiple items were purchased on the first day) 30 | 31 | SELECT s.customer_id AS customer, m.product_name AS food_item 32 | FROM sales s 33 | JOIN menu m 34 | ON s.product_id = m.product_id 35 | WHERE s.order_date = (SELECT MIN(order_date) FROM sales WHERE customer_id = s.customer_id) 36 | ORDER BY s.product_id; 37 | 38 | -- 4. What is the most purchased item on the menu and how many times was it purchased by all customers? 39 | 40 | SELECT TOP 1 m.product_name, COUNT(s.product_id) AS order_count 41 | FROM sales s 42 | JOIN menu m ON s.product_id=m.product_id 43 | GROUP BY m.product_name 44 | ORDER BY order_count DESC; 45 | 46 | -- 5. Which item was the most popular for each customer? 47 | -- Asssumption: Products with the highest purchase counts are all considered to be popular for each customer 48 | 49 | with cte_order_count as 50 | ( 51 | select s.customer_id, 52 | m.product_name, 53 | count(*) as order_count 54 | from sales as s 55 | inner join menu as m 56 | on s.product_id = m.product_id 57 | group by s.customer_id,m.product_name 58 | ), 59 | cte_popular_rank AS ( 60 | SELECT *, 61 | rank() over(partition by customer_id order by order_count desc) as rn 62 | from cte_order_count ) 63 | 64 | SELECT customer_id as customer, product_name as food_item,order_count 65 | FROM cte_popular_rank 66 | WHERE rn = 1; 67 | 68 | -- 6. Which item was purchased first by the customer after they became a member? 69 | -- Before answering question 6, I created a membership_validation table to validate only those customers joining in the membership program: 70 | 71 | DROP TABLE #Membership_validation; 72 | CREATE TABLE #Membership_validation 73 | ( 74 | customer_id varchar(1), 75 | order_date date, 76 | product_name varchar(5), 77 | price int, 78 | join_date date, 79 | membership varchar(5) 80 | ) 81 | 82 | INSERT INTO #Membership_validation 83 | SELECT s.customer_id, s.order_date, m.product_name, m.price, mem.join_date, 84 | CASE WHEN s.order_date >= mem.join_date THEN 'X' ELSE '' END AS membership 85 | FROM sales s 86 | INNER JOIN menu m ON s.product_id = m.product_id 87 | LEFT JOIN members mem ON mem.customer_id = s.customer_id 88 | WHERE join_date IS NOT NULL 89 | ORDER BY customer_id, order_date; 90 | 91 | select * from #Membership_validation; 92 | Soln No. 6 93 | WITH cte_first_after_mem AS ( 94 | SELECT 95 | customer_id, 96 | product_name, 97 | order_date, 98 | RANK() OVER(PARTITION BY customer_id ORDER BY order_date) AS purchase_order 99 | FROM #Membership_validation 100 | WHERE membership = 'X' 101 | ) 102 | SELECT * FROM cte_first_after_mem 103 | WHERE purchase_order = 1; 104 | 105 | -- 7. Which item was purchased just before the customer became a member? 106 | 107 | WITH cte_last_before_mem AS ( 108 | SELECT 109 | customer_id, 110 | product_name, 111 | order_date, 112 | RANK() OVER( PARTITION BY customer_id ORDER BY order_date DESC) AS purchase_order 113 | FROM #Membership_validation 114 | WHERE membership = '' 115 | ) 116 | SELECT * FROM cte_last_before_mem 117 | WHERE purchase_order = 1; 118 | 119 | -- 8. What is the total items and amount spent for each member before they became a member? 120 | WITH cte_spent_before_mem AS ( 121 | SELECT 122 | customer_id, 123 | product_name, 124 | price 125 | FROM #Membership_validation 126 | WHERE membership = '' 127 | ) 128 | SELECT 129 | customer_id, 130 | SUM(price) AS total_spent, 131 | COUNT(*) AS total_items 132 | FROM cte_spent_before_mem 133 | GROUP BY customer_id 134 | ORDER BY customer_id; 135 | 136 | -- 9. If each $1 spent equates to 10 points and sushi has a 2x points multiplier - how many points would each customer have? 137 | 138 | SELECT 139 | customer_id, 140 | SUM( CASE 141 | WHEN product_name = 'sushi' THEN (price * 20) ELSE (price * 10) 142 | END 143 | ) AS total_points 144 | FROM #Membership_validation 145 | WHERE order_date >= join_date 146 | GROUP BY customer_id 147 | ORDER BY customer_id; 148 | 149 | -- 10. In the first week after a customer joins the program (including their join date) they earn 2x points on all items, not just sushi - how many points do customer A and B have at the end of January 150 | Asssumption: Points is rewarded only after the customer joins in the membership program 151 | 152 | Steps 153 | Find the program_last_date which is 7 days after a customer joins the program (including their join date) 154 | Determine the customer points for each transaction and for members with a membership a. During the first week of the membership -> points = price20 irrespective of the purchase item b. Product = Sushi -> and order_date is not within a week of membership -> points = price20 c. Product = Not Sushi -> and order_date is not within a week of membership -> points = price*10 155 | Conditions in WHERE clause a. order_date <= '2021-01-31' -> Order must be placed before 31st January 2021 b. order_date >= join_date -> Points awarded to only customers with a membership 156 | 157 | with program_last_day_cte as 158 | ( 159 | select join_date , 160 | Dateadd(dd,7,join_date) as program_last_date, 161 | customer_id 162 | from members 163 | ) 164 | SELECT s.customer_id, 165 | SUM(CASE 166 | WHEN order_date BETWEEN join_date AND program_last_date THEN price*10*2 167 | WHEN order_date NOT BETWEEN join_date AND program_last_date 168 | AND product_name = 'sushi' THEN price*10*2 169 | WHEN order_date NOT BETWEEN join_date AND program_last_date 170 | AND product_name != 'sushi' THEN price*10 171 | END) AS customer_points 172 | FROM MENU as m 173 | INNER JOIN sales as s on m.product_id = s.product_id 174 | INNER JOIN program_last_day_cte as k on k.customer_id = s.customer_id 175 | AND order_date <='2021-01-31' 176 | AND order_date >=join_date 177 | GROUP BY s.customer_id 178 | ORDER BY s.customer_id; 179 | -------------------------------------------------------------------------------- /#8Weeksqlchallange/Case Study # 1 - Danny's Diner/Diner_Schema.sql: -------------------------------------------------------------------------------- 1 | ---------------------------------- 2 | -- CASE STUDY #1: DANNY'S DINER -- 3 | ---------------------------------- 4 | 5 | -- Author: Amit Patel 6 | -- Date: 25/04/2023 7 | -- Tool used: MSSQL Server 8 | 9 | 10 | 11 | CREATE SCHEMA dannys_diner; 12 | use dannys_diner; 13 | 14 | CREATE TABLE sales ( 15 | customer_id VARCHAR(1), 16 | order_date DATE, 17 | product_id INTEGER 18 | ); 19 | 20 | INSERT INTO sales ( 21 | customer_id, order_date, product_id 22 | ) 23 | VALUES 24 | ('A', '2021-01-01', '1'), 25 | ('A', '2021-01-01', '2'), 26 | ('A', '2021-01-07', '2'), 27 | ('A', '2021-01-10', '3'), 28 | ('A', '2021-01-11', '3'), 29 | ('A', '2021-01-11', '3'), 30 | ('B', '2021-01-01', '2'), 31 | ('B', '2021-01-02', '2'), 32 | ('B', '2021-01-04', '1'), 33 | ('B', '2021-01-11', '1'), 34 | ('B', '2021-01-16', '3'), 35 | ('B', '2021-02-01', '3'), 36 | ('C', '2021-01-01', '3'), 37 | ('C', '2021-01-01', '3'), 38 | ('C', '2021-01-07', '3'); 39 | 40 | CREATE TABLE menu ( 41 | product_id INTEGER, 42 | product_name VARCHAR(5), 43 | price INTEGER 44 | ); 45 | 46 | INSERT INTO menu (product_id, product_name, price) 47 | VALUES 48 | (1, 'sushi', 10), 49 | (2, 'curry', 15), 50 | (3, 'ramen', 12); 51 | 52 | 53 | CREATE TABLE members ( 54 | customer_id VARCHAR(1), 55 | join_date DATE 56 | ); 57 | 58 | INSERT INTO members (customer_id, join_date) 59 | VALUES 60 | ('A', '2021-01-07'), 61 | ('B', '2021-01-09'); 62 | -------------------------------------------------------------------------------- /#8Weeksqlchallange/Case Study # 1 - Danny's Diner/ERD.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmitPatel-analyst/SQL-Case-Study/4dae8f080f537149fb3f2a7233519d9d17903659/#8Weeksqlchallange/Case Study # 1 - Danny's Diner/ERD.png -------------------------------------------------------------------------------- /#8Weeksqlchallange/Case Study # 1 - Danny's Diner/README.md: -------------------------------------------------------------------------------- 1 | # :ramen: :curry: :sushi: Case Study #1: Danny's Diner 2 |

3 | Image 4 | 5 | You can find the complete challenge here: https://8weeksqlchallenge.com/case-study-1/ 6 | 7 | ## Table Of Contents 8 | - [Introduction](#introduction) 9 | - [Problem Statement](#problem-statement) 10 | - [Datasets used](#datasets-used) 11 | - [Entity Relationship Diagram](#entity-relationship-diagram) 12 | - [Case Study Questions](#case-study-questions) 13 | 14 | ## Introduction 15 | Danny seriously loves Japanese food so in the beginning of 2021, he decides to embark upon a risky venture and opens up a cute little restaurant that sells his 3 favourite foods: sushi, curry and ramen. 16 | 17 | Danny’s Diner is in need of your assistance to help the restaurant stay afloat - the restaurant has captured some very basic data from their few months of operation but have no idea how to use their data to help them run the business. 18 | 19 | ## Problem Statement 20 | Danny wants to use the data to answer a few simple questions about his customers, especially about their visiting patterns, how much money they’ve spent and also which menu items are their favourite. Having this deeper connection with his customers will help him deliver a better and more personalised experience for his loyal customers. 21 | He plans on using these insights to help him decide whether he should expand the existing customer loyalty program. 22 | 23 | ## Datasets used 24 | Three key datasets for this case study 25 | - sales: The sales table captures all customer_id level purchases with an corresponding order_date and product_id information for when and what menu items were ordered. 26 | - menu: The menu table maps the product_id to the actual product_name and price of each menu item. 27 | - members: The members table captures the join_date when a customer_id joined the beta version of the Danny’s Diner loyalty program. 28 | 29 | ## Entity Relationship Diagram 30 | 31 | ![ERD](https://user-images.githubusercontent.com/120770473/234551667-d04ba731-3950-406b-96fc-4eedbbed924c.png) 32 | 33 | ## Case Study Questions 34 | 1. What is the total amount each customer spent at the restaurant? 35 | 2. How many days has each customer visited the restaurant? 36 | 3. What was the first item from the menu purchased by each customer? 37 | 4. What is the most purchased item on the menu and how many times was it purchased by all customers? 38 | 5. Which item was the most popular for each customer? 39 | 6. Which item was purchased first by the customer after they became a member? 40 | 7. Which item was purchased just before the customer became a member? 41 | 10. What is the total items and amount spent for each member before they became a member? 42 | 11. If each $1 spent equates to 10 points and sushi has a 2x points multiplier - how many points would each customer have? 43 | 12. In the first week after a customer joins the program (including their join date) they earn 2x points on all items, not just sushi - how many points do customer A and B have at the end of January? 44 | 45 | ## Insights 46 | ◼Out of the three Customers A, B and C, 2 customers A & B have joined the membership and there are 3 products offered at Danny’s Diner: sushi, curry and ramen. 47 | ◼Customer A has spent the highest amount of $76 at Danny’s Diner, followed by B and C 48 | ◼Frequently visited Customers are A and B with 6 number of times and the Customer C has done the lowest no of visits. 49 | ◼The First item opted by most of the customers to taste from Danny’s Diner is curry and then it was ramen. 50 | ◼The most popular item in Danny’s diner is found as Ramen, followed by curry and sushi. 51 | ◼The first item bought By Customers A and B after taking membership are Ramen and sushi respectively. 52 | ◼Before taking membership the last item chosen by Customer A were sushi and curry and for B it was sushi 53 | ◼Before taking the membership, Customer A has bought 2 items with a Total amount of $25 and B has bought 3 items with a total amount of $40 54 | ◼Customers A, B and C have earned Total points of 860, 940 and 360 based on their purchases without their membership. 55 | ◼As Customers A and B took membership and can earn 20 points for every $1 spent irrespective of the items, they earned total Points of 1010 and 820 by the end of January 2021. 56 | 57 | Click [here](https://github.com/AmitPatel-analyst/SQL-Case-Study/blob/main/%238Weeksqlchallange/Case%20Study%20%23%201%20-%20Danny's%20Diner/Danny's%20Diner%20Solution.md) to view the solution of the case study! 58 | -------------------------------------------------------------------------------- /#8Weeksqlchallange/Case Study # 2 - Pizza Runner/0. Data Cleaning.md: -------------------------------------------------------------------------------- 1 | 2 | # Data Cleaning 3 | 4 | ## customer_orders table 5 | - The exclusions and extras columns in customer_orders table will need to be cleaned up before using them in the queries 6 | - In the exclusions and extras columns, there are blank spaces and null values. 7 | 8 | ```sql 9 | SELECT 10 | [order_id], 11 | [customer_id], 12 | [pizza_id], 13 | CASE 14 | WHEN exclusions IS NULL OR 15 | exclusions LIKE 'null' THEN ' ' 16 | ELSE exclusions 17 | END AS exclusions, 18 | CASE 19 | WHEN extras IS NULL OR 20 | extras LIKE 'null' THEN ' ' 21 | ELSE extras 22 | END AS extras, 23 | [order_time] INTO Updated_customer_orders 24 | FROM [pizza_runner].[customer_orders]; 25 | 26 | select * from Updated_customer_orders; 27 | ``` 28 | #### Result set: 29 | ![image](https://user-images.githubusercontent.com/120770473/236623265-7f5928df-4851-4d81-a2e6-0002a05c6e81.png) 30 | 31 | *** 32 | 33 | ## runner_orders table 34 | - The pickup_time, distance, duration and cancellation columns in runner_orders table will need to be cleaned up before using them in the queries 35 | - In the pickup_time column, there are null values. 36 | - In the distance column, there are null values. It contains unit - km. The 'km' must also be stripped 37 | - In the duration column, there are null values. The 'minutes', 'mins' 'minute' must be stripped 38 | - In the cancellation column, there are blank spaces and null values. 39 | 40 | ```sql 41 | create table updated_runner_orders 42 | ( 43 | [order_id] int, 44 | [runner_id] int, 45 | [pickup_time] datetime, 46 | [distance] float, 47 | [duration] int, 48 | [cancellation] varchar(23) 49 | ); 50 | 51 | insert into updated_runner_orders 52 | select 53 | order_id, 54 | runner_id, 55 | case when pickup_time LIKE 'null' then null else pickup_time end as pickup_time, 56 | case when distance LIKE 'null' then null else trim( 57 | REPLACE(distance, 'km', '') 58 | ) end as distance, 59 | case when duration LIKE 'null' then null else trim( 60 | REPLACE( 61 | duration, 62 | substring(duration, 3, 10), 63 | '' 64 | ) 65 | ) end as duration, 66 | CASE WHEN cancellation IN ('null', 'NaN', '') THEN null ELSE cancellation END AS cancellation 67 | from 68 | [pizza_runner].[runner_orders]; 69 | ``` 70 | #### Result set: 71 | ![image](https://user-images.githubusercontent.com/120770473/236623387-298bc300-2269-4952-9248-5c79611d9b06.png) 72 | 73 | 74 | *** 75 | -------------------------------------------------------------------------------- /#8Weeksqlchallange/Case Study # 2 - Pizza Runner/A. Pizza Metrics.md: -------------------------------------------------------------------------------- 1 | # :pizza: Case Study #2: Pizza runner - Pizza Metrics 2 | 3 | ## Case Study Questions 4 | 5 | 1. How many pizzas were ordered? 6 | 2. How many unique customer orders were made? 7 | 3. How many successful orders were delivered by each runner? 8 | 4. How many of each type of pizza was delivered? 9 | 5. How many Vegetarian and Meatlovers were ordered by each customer? 10 | 6. What was the maximum number of pizzas delivered in a single order? 11 | 7. For each customer, how many delivered pizzas had at least 1 change and how many had no changes? 12 | 8. How many pizzas were delivered that had both exclusions and extras? 13 | 9. What was the total volume of pizzas ordered for each hour of the day? 14 | 10. What was the volume of orders for each day of the week? 15 | 16 | *** 17 | Total Tables - 18 | SELECT * FROM updated_runner_orders; 19 | SELECT * FROM Updated_customer_orders; 20 | SELECT * FROM pizza_runner.pizza_toppings; 21 | SELECT * FROM pizza_runner.pizza_recipes; 22 | SELECT * FROM pizza_runner.pizza_names; 23 | SELECT * FROM pizza_runner.runners; 24 | 25 | ### 1. How many pizzas were ordered? 26 |

27 | Click here for solution 28 | 29 | ```sql 30 | select count(pizza_id) as pizza_count from Updated_customer_orders; 31 | ``` 32 |
33 | 34 | #### Output: 35 | ![image](https://user-images.githubusercontent.com/120770473/236623764-037c342f-f9ec-433b-bef9-c79426f880de.png) 36 | 37 | ### 2. How many unique customer orders were made? 38 |
39 | Click here for solution 40 | 41 | ```sql 42 | select count(distinct order_id) as order_count from Updated_customer_orders; 43 | ``` 44 |
45 | 46 | #### Output: 47 | ![image](https://user-images.githubusercontent.com/120770473/236623790-e401b7c5-c956-4b20-90d3-74649db8738d.png) 48 | 49 | ### 3. How many successful orders were delivered by each runner? 50 |
51 | Click here for solution 52 | 53 | ```sql 54 | SELECT 55 | runner_id, 56 | COUNT(order_id) [Successful Order] 57 | FROM updated_runner_orders 58 | WHERE cancellation IS NULL 59 | OR cancellation NOT IN ('Restaurant Cancellation', 'Customer Cancellation') 60 | GROUP BY runner_id 61 | ORDER BY 2 DESC; 62 | ``` 63 |
64 | 65 | #### Output: 66 | ![image](https://user-images.githubusercontent.com/120770473/236623849-11c3fa59-dd6e-47be-b8e4-d44425eb0421.png) 67 | 68 | #### 4. How many of each type of pizza was delivered? 69 |
70 | Click here for solution 71 | 72 | ```sql 73 | SELECT 74 | p.pizza_name, 75 | Pizza_count 76 | FROM (SELECT 77 | c.pizza_id, 78 | COUNT(r.order_id) AS Pizza_count 79 | FROM updated_runner_orders r 80 | JOIN Updated_customer_orders c 81 | ON r.order_id = c.order_id 82 | WHERE cancellation IS NULL 83 | OR cancellation NOT IN ('Restaurant Cancellation', 'Customer Cancellation') 84 | GROUP BY c.pizza_id) k 85 | INNER JOIN pizza_runner.[pizza_names] p 86 | ON k.pizza_id = p.pizza_id; 87 | ``` 88 |
89 | 90 | #### Output: 91 | ![image](https://user-images.githubusercontent.com/120770473/236623909-7dc585d2-23d1-4bfe-818c-5a0b25c33cf0.png) 92 | 93 | #### 5. How many Vegetarian and Meatlovers were ordered by each customer? 94 |
95 | Click here for solution 96 | 97 | ```sql 98 | SELECT 99 | customer_id, 100 | SUM(CASE 101 | WHEN pizza_id = 1 THEN 1 102 | ELSE 0 103 | END) AS Orderby_Meatlovers, 104 | SUM(CASE 105 | WHEN pizza_id = 2 THEN 1 106 | ELSE 0 107 | END) AS Orderby_Vegetarian 108 | FROM Updated_customer_orders 109 | GROUP BY customer_id; 110 | ``` 111 |
112 | 113 | #### Output: 114 | ![image](https://user-images.githubusercontent.com/120770473/236624294-e6ea617b-8705-49ac-9586-e50dad1edf1f.png) 115 | 116 | #### 6. What was the maximum number of pizzas delivered in a single order? 117 |
118 | Click here for solution 119 | 120 | ```sql 121 | SELECT 122 | order_id, pizza_count AS max_count_delivered_pizza 123 | FROM (SELECT top 1 124 | r.order_id, 125 | COUNT(c.pizza_id) AS pizza_count 126 | FROM updated_runner_orders r 127 | JOIN Updated_customer_orders c 128 | ON r.order_id = c.order_id 129 | WHERE cancellation IS NULL 130 | OR cancellation NOT IN ('Restaurant Cancellation', 'Customer Cancellation') 131 | GROUP BY r.order_id 132 | ORDER BY pizza_count desc) k; 133 | ``` 134 |
135 | 136 | #### Output: 137 | ![image](https://user-images.githubusercontent.com/120770473/236624380-5db1af42-d49f-4fac-b7d8-c4de70fb75ae.png) 138 | 139 | #### 7. For each customer, how many delivered pizzas had at least 1 change and how many had no changes? 140 | - at least 1 change -> either exclusion or extras 141 | - no changes -> exclusion and extras are NULL 142 |
143 | Click here for solution 144 | 145 | ```sql 146 | SELECT 147 | c.customer_id, 148 | SUM(CASE WHEN c.exclusions <> ' ' OR 149 | c.extras <> ' ' THEN 1 ELSE 0 END) AS Changes, 150 | SUM(CASE WHEN c.exclusions = ' ' AND 151 | c.extras = ' ' THEN 1 ELSE 0 END) AS No_changes 152 | FROM updated_runner_orders r 153 | INNER JOIN Updated_customer_orders c 154 | ON r.order_id = c.order_id 155 | WHERE r.cancellation IS NULL 156 | OR r.cancellation NOT IN ('Restaurant Cancellation', 'Customer Cancellation') 157 | GROUP BY c.customer_id 158 | ORDER BY c.customer_id; 159 | ``` 160 |
161 | 162 | #### Output: 163 | ![image](https://github.com/AmitPatel-analyst/SQL-Case-Study/assets/120770473/087d505a-68be-4e01-8e06-7410a8b52db4) 164 | 165 | 166 | #### 8. How many pizzas were delivered that had both exclusions and extras? 167 |
168 | Click here for solution 169 | 170 | ```sql 171 | SELECT 172 | count(r.order_id) as Order_had_bothexclusions_and_extras 173 | FROM updated_runner_orders r 174 | inner JOIN Updated_customer_orders c 175 | ON r.order_id = c.order_id 176 | WHERE r.cancellation IS NULL and c.exclusions <> ' ' AND 177 | c.extras <> ' '; 178 | ``` 179 |
180 | 181 | #### Output: 182 | ![image](https://github.com/AmitPatel-analyst/SQL-Case-Study/assets/120770473/8cb4cdba-2f0a-4628-8e2f-b6b4bc588d04) 183 | 184 | 185 | #### 9. What was the total volume of pizzas ORDERED for each hour of the day? 186 |
187 | Click here for solution 188 | 189 | ```sql 190 | 191 | SELECT 192 | DATEPART(HOUR, order_time) AS Hour, 193 | COUNT(1) AS Pizza_Ordered_Count 194 | FROM Updated_customer_orders 195 | WHERE order_time IS NOT NULL 196 | GROUP BY DATEPART(HOUR, order_time) 197 | ORDER BY 1; 198 | ``` 199 |
200 | 201 | #### Output: 202 | ![image](https://github.com/AmitPatel-analyst/SQL-Case-Study/assets/120770473/a15e261f-a199-4f54-9c6f-04550d2b9985) 203 | 204 | 205 | #### 10. What was the volume of orders for each day of the week? 206 |
207 | Click here for solution 208 | 209 | ```sql 210 | SELECT 211 | DATENAME(dw, order_time) AS Day_of_Week, 212 | COUNT(1) AS Pizza_Ordered_Count 213 | FROM Updated_customer_orders 214 | GROUP BY DATENAME(dw, order_time) 215 | ORDER BY 2 DESC; 216 | 217 | ``` 218 |
219 | 220 | #### Output: 221 | ![image](https://github.com/AmitPatel-analyst/SQL-Case-Study/assets/120770473/7ef0d3d5-41f4-4cce-acf4-7418ea0c5d01) 222 | 223 | ## Insights 224 | 225 | ◼ Total Unique orders customers made were 10 and total 14 pizzas were ordered. 226 | ◼ Runner 1 had Delivered highest number of pizzas , whereas Runner 3 had delivered Least. 227 | ◼ Meatlovers pizza was delivered 9 times , whereas Vegetarian pizza was delivered 3 times only. 228 | ◼ Customer 101,102,103,105 had ordered Vegetarian pizza only 1 time, whereas Customer 104 had ordered Meatlovers pizza 3 times. 229 | ◼ Customer 101,102 liked his/her pizza as per the original recipe, whereas customer 103,104,105 had their own 230 | preference for pizza toppings and requested atleast 1 change on their pizza. 231 | ◼ Only One customer had ordered 1 pizza having both exclusions and extra toppings. 232 | ◼ Highest number of pizza ordered were at 1:00 pm , 6:00 pm , 9:00 pm & 11:00 pm, whereas Least number of pizza were ordered at 11:00 am & 7:00 pm. 233 | ◼ On Saturday and Wednesday highest number of pizza were ordered , whereas on Friday least number of pizza were ordered 234 | 235 | 236 | *** 237 | Click [here](https://github.com/AmitPatel-analyst/SQL-Case-Study/blob/main/%238Weeksqlchallange/Case%20Study%20%23%202%20-%20Pizza%20Runner/B.%20Runner%20and%20Customer%20Experience.md) to view the solution of B. Runner and Customer Experience 238 | -------------------------------------------------------------------------------- /#8Weeksqlchallange/Case Study # 2 - Pizza Runner/B. Runner and Customer Experience.md: -------------------------------------------------------------------------------- 1 | # :pizza: Case Study #2: Pizza runner - Runner and Customer Experience 2 | 3 | ## Case Study Questions 4 | 5 | 1. How many runners signed up for each 1 week period? (i.e. week starts 2021-01-01) 6 | 2. What was the average time in minutes it took for each runner to arrive at the Pizza Runner HQ to pickup the order? 7 | 3. Is there any relationship between the number of pizzas and how long the order takes to prepare? 8 | 4. What was the average distance travelled for each customer? 9 | 5. What was the difference between the longest and shortest delivery times for all orders? 10 | 6. What was the average speed for each runner for each delivery and do you notice any trend for these values? 11 | 7. What is the successful delivery percentage for each runner? 12 | 13 | *** 14 | Total Tables are following: 15 | select * from updated_runner_orders; 16 | select * from Updated_customer_orders; 17 | select * from pizza_runner.runners; 18 | 19 | ### 1. How many runners signed up for each 1 week period? (i.e. week starts 2021-01-01) 20 |
21 | Click here for solution 22 | 23 | ```sql 24 | SELECT 25 | CASE 26 | WHEN registration_date BETWEEN '2021-01-01' AND '2021-01-07' THEN '2021-01-01' 27 | WHEN registration_date BETWEEN '2021-01-08' AND '2021-01-14' THEN '2021-01-08' 28 | WHEN registration_date BETWEEN '2021-01-15' AND '2021-01-21' THEN '2021-01-15' 29 | END AS [Week Start_Period],count(runner_id) as cnt 30 | from pizza_runner.runners 31 | group by CASE 32 | WHEN registration_date BETWEEN '2021-01-01' AND '2021-01-07' THEN '2021-01-01' 33 | WHEN registration_date BETWEEN '2021-01-08' AND '2021-01-14' THEN '2021-01-08' 34 | WHEN registration_date BETWEEN '2021-01-15' AND '2021-01-21' THEN '2021-01-15' 35 | END ; 36 | ``` 37 |
38 | 39 | #### Output: 40 | ![image](https://github.com/AmitPatel-analyst/SQL-Case-Study/assets/120770473/a11f65b6-6b0b-433c-9f84-8bff4e6fd44f) 41 | 42 | ### 2.What was the AVERAGE TIME IN MINUTES it took for EACH RUNNER to arrive at the Pizza Runner HQ to PICKUP the order? 43 |
44 | Click here for solution 45 | 46 | ```sql 47 | SELECT r.runner_id 48 | ,Avg_Arrival_minutes = avg(datepart(minute, (pickup_time - order_time))) 49 | FROM updated_runner_orders r 50 | INNER JOIN Updated_customer_orders c ON r.order_id = c.order_id 51 | WHERE r.cancellation IS NULL 52 | OR r.cancellation NOT IN ( 53 | 'Restaurant Cancellation' 54 | ,'Customer Cancellation' 55 | ) 56 | GROUP BY r.runner_id; 57 | ``` 58 |
59 | 60 | #### Output: 61 | ![image](https://github.com/AmitPatel-analyst/SQL-Case-Study/assets/120770473/27c8c42f-dcca-492a-89cf-6c118dc7832b) 62 | 63 | ### 3. Is there any relationship between the number of pizzas and how long the order takes to prepare? 64 |
65 | Click here for solution 66 | 67 | ```sql 68 | with order_count as 69 | ( 70 | select order_id,order_time,count(pizza_id) as pizza_order_count 71 | from Updated_customer_orders 72 | group by order_id,order_time 73 | ), 74 | prepare_time as 75 | ( 76 | select c.*,r.pickup_time 77 | ,datepart(minute,(r.pickup_time-c.order_time)) as prepare_time 78 | from updated_runner_orders r 79 | join order_count c 80 | on r.order_id=c.order_id 81 | where r.pickup_time is not null 82 | ) 83 | select pizza_order_count,avg(prepare_time) as avg_prepare_time from prepare_time 84 | group by pizza_order_count 85 | order by pizza_order_count; 86 | ``` 87 |
88 | 89 | #### Output: 90 | ![image](https://github.com/AmitPatel-analyst/SQL-Case-Study/assets/120770473/5bc3bf1c-e6ab-44ee-a15f-6f6c2659b1f2) 91 | 92 | 93 | ### 4. What was the AVERAGE DISTANCE travelled for EACH RUNNER? 94 |
95 | Click here for solution 96 | 97 | ```sql 98 | select runner_id, 99 | Avg_distance_travel = round(avg(distance),2) 100 | from updated_runner_orders 101 | where cancellation is null 102 | or cancellation not in ('Restaurant Cancellation','Customer Cancellation') 103 | group by runner_id 104 | order by runner_id; 105 | ``` 106 |
107 | 108 | #### Output: 109 | ![image](https://github.com/AmitPatel-analyst/SQL-Case-Study/assets/120770473/62ee549d-7e6b-4f54-88fa-6548fbc00393) 110 | 111 | ### 5. What was the difference between the longest and shortest delivery times for all orders? 112 |
113 | Click here for solution 114 | 115 | ```sql 116 | SELECT 117 | MAX(duration) - MIN(duration) AS Time_span 118 | FROM updated_runner_orders; 119 | ``` 120 |
121 | 122 | #### Output: 123 | ![image](https://github.com/AmitPatel-analyst/SQL-Case-Study/assets/120770473/4d75bc2c-b69a-48bb-8bc0-3d1ee1aa4502) 124 | 125 | ### 6.What was the average speed for each runner for each delivery and do you notice any trend for these values? 126 |
127 | Click here for solution 128 | 129 | ```sql 130 | with order_count as 131 | ( 132 | select order_id,order_time,count(pizza_id) as pizza_order_count 133 | from Updated_customer_orders 134 | group by order_id,order_time 135 | ), speed as 136 | ( select 137 | c.order_id 138 | ,r.runner_id 139 | ,c.pizza_order_count 140 | ,round((60* r.distance/ r.duration),2) as speed_kmph 141 | from updated_runner_orders r 142 | join order_count c 143 | on r.order_id = c.order_id 144 | where cancellation is null 145 | 146 | ) 147 | select runner_id,pizza_order_count,speed_kmph,avg(speed_kmph) over(partition by runner_id) as speed_avg 148 | from speed 149 | order by runner_id; 150 | ``` 151 |
152 | 153 | #### Output: 154 | ![image](https://github.com/AmitPatel-analyst/SQL-Case-Study/assets/120770473/d955008b-47ae-4b57-a2db-dc10b924e4b4) 155 | 156 | ### 7. What is the successful delivery percentage for each runner? 157 |
158 | Click here for solution 159 | 160 | ```sql 161 | select runner_id, 162 | count(pickup_time) as delivered_orders, 163 | count(order_id) as total_orders, 164 | round(100*count(pickup_time)/count(order_id),0) as delivery_pct 165 | from updated_runner_orders 166 | group by runner_id; 167 | ``` 168 |
169 | 170 | #### Output: 171 | ![image](https://github.com/AmitPatel-analyst/SQL-Case-Study/assets/120770473/001c40cd-5400-4fc3-a4a0-75d90754304d) 172 | 173 | ## Insights 174 | 1. On average , An order with single pizza took 12 minutes to prepare. whereas an order of 2 pizzas took total 18 minutes ,so 9 minutes per pizza is an ultimate efficiency rate. 175 | 2. On average , A runner_id 3 had least distance travelled, whereas A runner_id 2 had highest distance travelled. 176 | 3. The difference between longest (40 minutes) and shortest ( 10 minutes) delivery time for all orders is 30 minutes. 177 | 4. While delivering pizza , an speed for runner_id 1 was varied from 37.5 kmph to 60 kmph. 178 | 5. An speed for runner_id 2 has varied from 35.1 km h to 93.6 mph which is abnormal , so danny has to lookat the matter seriously on runner_id 2. 179 | 6. An speed for runner_id 3 is 40 kmph. 180 | 181 | 182 | Click [here](https://github.com/AmitPatel-analyst/SQL-Case-Study/blob/main/%238Weeksqlchallange/Case%20Study%20%23%202%20-%20Pizza%20Runner/C.%20Ingredient%20Optimisation.md) to view the solution of C. Ingredient Optimisation 183 | -------------------------------------------------------------------------------- /#8Weeksqlchallange/Case Study # 2 - Pizza Runner/C. Ingredient Optimisation.md: -------------------------------------------------------------------------------- 1 | # :pizza: Case Study #2: Pizza runner - Ingredient Optimisation 2 | 3 | ## Case Study Questions 4 | 5 | 1. What are the standard ingredients for each pizza? 6 | 2. What was the most commonly added extra? 7 | 3. What was the most common exclusion? 8 | 4. Generate an order item for each record in the customers_orders table in the format of one of the following: 9 | ◼ Meat Lovers 10 | ◼ Meat Lovers - Exclude Beef 11 | ◼ Meat Lovers - Extra Bacon 12 | ◼ Meat Lovers - Exclude Cheese, Bacon - Extra Mushroom, Peppers 13 | 14 | *** 15 | Total Tables are following: 16 | select * from updated_runner_orders; 17 | select * from Updated_customer_orders; 18 | SELECT * FROM pizza_runner.[pizza_toppings]; 19 | SELECT * FROM pizza_runner.[pizza_recipes]; 20 | SELECT * FROM pizza_runner.[pizza_names]; 21 | select * from pizza_runner.runners 22 | select * from customer_orders_new; 23 | 24 | 25 | ### 1. What are the standard ingredients for each pizza? 26 |
27 | Click here for solution 28 | 29 | ```sql 30 | with cte as 31 | ( select pizza_id,value as topping_id 32 | from pizza_runner.[pizza_recipes] 33 | cross apply string_split (toppings,',') 34 | ), 35 | final as 36 | ( 37 | SELECT pizza_id,topping_name 38 | FROM pizza_runner.[pizza_toppings] as t 39 | inner join cte 40 | on cte.topping_id=t.topping_id 41 | ) 42 | select c.pizza_name,STRING_AGG(topping_name,',') as Ingredients 43 | from final inner join pizza_runner.[pizza_names] as c on c.pizza_id=final.pizza_id 44 | group by c.pizza_name; 45 | ``` 46 |
47 | 48 | #### Output: 49 | ![image](https://github.com/AmitPatel-analyst/SQL-Case-Study/assets/120770473/0a12be59-6573-405b-8e94-e6cf3595eab1) 50 | 51 | ### 2. What was the most commonly added extra? 52 |
53 | Click here for solution 54 | 55 | ```sql 56 | with Get_toppingid as 57 | (select value as topping_id 58 | from Updated_customer_orders 59 | cross apply string_split (extras,',') 60 | ) 61 | select t2.topping_name ,count(1) as added_extras_count 62 | from Get_toppingid as t1 inner join pizza_runner.[pizza_toppings] as t2 63 | on t1.topping_id=t2.topping_id 64 | where t2.topping_name is not null 65 | group by t2.topping_name 66 | order by 2 desc; 67 | ``` 68 |
69 | 70 | #### Output: 71 | ![image](https://github.com/AmitPatel-analyst/SQL-Case-Study/assets/120770473/e9f3aa75-6d7e-48f0-a648-3666e2624a46) 72 | 73 | ### 3. What was the most common exclusion? 74 |
75 | Click here for solution 76 | 77 | ```sql 78 | with Get_toppingid as 79 | (select value as topping_id 80 | from Updated_customer_orders 81 | cross apply string_split (exclusions,',') 82 | ) 83 | select t2.topping_name ,count(1) as added_exclusions_count 84 | from Get_toppingid as t1 inner join pizza_runner.[pizza_toppings] as t2 85 | on t1.topping_id=t2.topping_id 86 | where t2.topping_name is not null 87 | group by t2.topping_name 88 | order by 2 desc; 89 | ``` 90 |
91 | 92 | #### Output: 93 | ![image](https://github.com/AmitPatel-analyst/SQL-Case-Study/assets/120770473/d2de817c-4bfd-4545-9d1f-90b1fdd01c64) 94 | 95 | ### 4. Generate an order item for each record in the customers_orders table 96 |
97 | Click here for solution 98 | 99 | ```sql 100 | with cte as 101 | ( 102 | select c.order_id , p.pizza_name , c.exclusions , c.extras 103 | ,topp1.topping_name as exclude , topp2.topping_name as extra 104 | from customer_orders_new as c 105 | inner join pizza_runner.[pizza_names] as p 106 | on c.pizza_id = p.pizza_id 107 | left join pizza_runner.[pizza_toppings] as topp1 108 | on topp1.topping_id = c.exclusions 109 | left join pizza_runner.[pizza_toppings] as topp2 110 | on topp2.topping_id = c.extras 111 | ) 112 | 113 | select order_id , pizza_name, 114 | case when pizza_name is not null and exclude is null and extra is null then pizza_name 115 | when pizza_name is not null and exclude is not null and extra is not null then concat(pizza_name,' - ','Exclude ',exclude,' - ','Extra ',extra) 116 | when pizza_name is not null and exclude is null and extra is not null then concat(pizza_name,' - ','Extra ',extra) 117 | when pizza_name is not null and exclude is not null and extra is null then concat(pizza_name,' - ','Exclude ',exclude) 118 | end as order_item 119 | from cte 120 | order by order_id; 121 | ``` 122 |
123 | 124 | #### Output: 125 | ![image](https://github.com/AmitPatel-analyst/SQL-Case-Study/assets/120770473/ef04e7a2-5294-4c9f-af47-7db5878dbfcb) 126 | 127 | ### Insights Gathered 128 | 1. The most commonly added extra was "Bacon". 129 | 2. The most commonly added exclusion was "Cheese". 130 | 131 | -------------------------------------------------------------------------------- /#8Weeksqlchallange/Case Study # 2 - Pizza Runner/D. Pricing and Ratings.md: -------------------------------------------------------------------------------- 1 | # :pizza: Case Study #2: Pizza runner - Pricing and Ratings 2 | 3 | ## Case Study Questions 4 | 1. If a Meat Lovers pizza costs $12 and Vegetarian costs $10 and there were no charges for changes - how much money has Pizza Runner made so far if there are no delivery fees? 5 | 2. What if there was an additional $1 charge for any pizza extras? 6 | - Add cheese is $1 extra 7 | 3. The Pizza Runner team now wants to add an additional ratings system that allows customers to rate their runner, how would you design an additional table for this new dataset - generate a schema for this new table and insert your own data for ratings for each successful customer order between 1 to 5. 8 | 4. Using your newly generated table - can you join all of the information together to form a table which has the following information for successful deliveries? 9 | ◼ customer_id 10 | ◼ order_id 11 | ◼ runner_id 12 | ◼ rating 13 | ◼ order_time 14 | ◼ pickup_time 15 | ◼ Time between order and pickup 16 | ◼ Delivery duration 17 | ◼ Average speed 18 | ◼ Total number of pizzas 19 | 5. If a Meat Lovers pizza was $12 and Vegetarian $10 fixed prices with no cost for extras and each runner is paid $0.30 per kilometre traveled - how much money does Pizza Runner have left over after these deliveries? 20 | *** 21 | Total Tables are following: 22 | select * from updated_runner_orders; 23 | select * from Updated_customer_orders; 24 | SELECT * FROM pizza_runner.[pizza_toppings]; 25 | SELECT * FROM pizza_runner.[pizza_recipes]; 26 | SELECT * FROM pizza_runner.[pizza_names]; 27 | select * from pizza_runner.runners 28 | select * from customer_orders_new; 29 | 30 | 31 | -------------------------------------------------------------------------------- /#8Weeksqlchallange/Case Study # 2 - Pizza Runner/Pizza Runner Schema.sql: -------------------------------------------------------------------------------- 1 | /******************************************************************************************** 2 | #CASE STUDY - 2 'PIZZA RUNNER' 3 | *******************************************************************************************/ 4 | 5 | CREATE SCHEMA pizza_runner 6 | go 7 | 8 | CREATE TABLE pizza_runner.runners ( 9 | "runner_id" INTEGER, 10 | "registration_date" DATE 11 | ); 12 | INSERT INTO pizza_runner.runners 13 | ("runner_id", "registration_date") 14 | VALUES 15 | (1, '2021-01-01'), 16 | (2, '2021-01-03'), 17 | (3, '2021-01-08'), 18 | (4, '2021-01-15'); 19 | 20 | CREATE TABLE pizza_runner.customer_orders ( 21 | "order_id" INTEGER, 22 | "customer_id" INTEGER, 23 | "pizza_id" INTEGER, 24 | "exclusions" VARCHAR(4), 25 | "extras" VARCHAR(4), 26 | "order_time" datetime 27 | ); 28 | INSERT INTO pizza_runner.customer_orders 29 | ("order_id", "customer_id", "pizza_id", "exclusions", "extras", "order_time") 30 | VALUES 31 | ('1', '101', '1', '', '', '2020-01-01 18:05:02'), 32 | ('2', '101', '1', '', '', '2020-01-01 19:00:52'), 33 | ('3', '102', '1', '', '', '2020-01-02 23:51:23'), 34 | ('3', '102', '2', '', NULL, '2020-01-02 23:51:23'), 35 | ('4', '103', '1', '4', '', '2020-01-04 13:23:46'), 36 | ('4', '103', '1', '4', '', '2020-01-04 13:23:46'), 37 | ('4', '103', '2', '4', '', '2020-01-04 13:23:46'), 38 | ('5', '104', '1', 'null', '1', '2020-01-08 21:00:29'), 39 | ('6', '101', '2', 'null', 'null', '2020-01-08 21:03:13'), 40 | ('7', '105', '2', 'null', '1', '2020-01-08 21:20:29'), 41 | ('8', '102', '1', 'null', 'null', '2020-01-09 23:54:33'), 42 | ('9', '103', '1', '4', '1, 5', '2020-01-10 11:22:59'), 43 | ('10', '104', '1', 'null', 'null', '2020-01-11 18:34:49'), 44 | ('10', '104', '1', '2, 6', '1, 4', '2020-01-11 18:34:49'); 45 | 46 | 47 | 48 | CREATE TABLE pizza_runner.runner_orders ( 49 | "order_id" INTEGER, 50 | "runner_id" INTEGER, 51 | "pickup_time" VARCHAR(19), 52 | "distance" VARCHAR(7), 53 | "duration" VARCHAR(10), 54 | "cancellation" VARCHAR(23) 55 | ); 56 | 57 | INSERT INTO pizza_runner.runner_orders 58 | ("order_id", "runner_id", "pickup_time", "distance", "duration", "cancellation") 59 | VALUES 60 | ('1', '1', '2020-01-01 18:15:34', '20km', '32 minutes', ''), 61 | ('2', '1', '2020-01-01 19:10:54', '20km', '27 minutes', ''), 62 | ('3', '1', '2020-01-03 00:12:37', '13.4km', '20 mins', NULL), 63 | ('4', '2', '2020-01-04 13:53:03', '23.4', '40', NULL), 64 | ('5', '3', '2020-01-08 21:10:57', '10', '15', NULL), 65 | ('6', '3', 'null', 'null', 'null', 'Restaurant Cancellation'), 66 | ('7', '2', '2020-01-08 21:30:45', '25km', '25mins', 'null'), 67 | ('8', '2', '2020-01-10 00:15:02', '23.4 km', '15 minute', 'null'), 68 | ('9', '2', 'null', 'null', 'null', 'Customer Cancellation'), 69 | ('10', '1', '2020-01-11 18:50:20', '10km', '10minutes', 'null'); 70 | 71 | CREATE TABLE pizza_runner.pizza_names ( 72 | "pizza_id" INTEGER, 73 | "pizza_name" varchar(50) 74 | ); 75 | INSERT INTO pizza_runner.pizza_names 76 | ("pizza_id", "pizza_name") 77 | VALUES 78 | (1, 'Meatlovers'), 79 | (2, 'Vegetarian'); 80 | 81 | CREATE TABLE pizza_runner.pizza_recipes ( 82 | "pizza_id" INTEGER, 83 | "toppings" varchar(50) 84 | ); 85 | INSERT INTO pizza_runner.pizza_recipes 86 | ("pizza_id", "toppings") 87 | VALUES 88 | (1, '1, 2, 3, 4, 5, 6, 8, 10'), 89 | (2, '4, 6, 7, 9, 11, 12'); 90 | 91 | CREATE TABLE pizza_runner.pizza_toppings ( 92 | "topping_id" INTEGER, 93 | "topping_name" varchar(50) 94 | ); 95 | INSERT INTO pizza_runner.pizza_toppings 96 | ("topping_id", "topping_name") 97 | VALUES 98 | (1, 'Bacon'), 99 | (2, 'BBQ Sauce'), 100 | (3, 'Beef'), 101 | (4, 'Cheese'), 102 | (5, 'Chicken'), 103 | (6, 'Mushrooms'), 104 | (7, 'Onions'), 105 | (8, 'Pepperoni'), 106 | (9, 'Peppers'), 107 | (10, 'Salami'), 108 | (11, 'Tomatoes'), 109 | (12, 'Tomato Sauce'); 110 | 111 | select * from pizza_runner.runners; 112 | 113 | select * from pizza_runner.customer_orders; 114 | 115 | select * from pizza_runner.runner_orders; 116 | 117 | SELECT * FROM pizza_runner.[pizza_toppings]; 118 | SELECT * FROM pizza_runner.[pizza_recipes]; 119 | SELECT * FROM pizza_runner.[pizza_names]; 120 | -------------------------------------------------------------------------------- /#8Weeksqlchallange/Case Study # 2 - Pizza Runner/Readme.md: -------------------------------------------------------------------------------- 1 | # :pizza: :runner: Case Study #2: Pizza Runner 2 |

3 | Image 4 | 5 | You can find the complete challenge here:https://8weeksqlchallenge.com/case-study-2/ 6 | ## Table Of Contents 7 | - [Introduction](#introduction) 8 | - [Datasets used](#datasets-used) 9 | - [Entity Relationship Diagram](#entity-relationship-diagram) 10 | - [Data Cleaning](#data-cleaning) 11 | - [Case Study Solutions](#case-study-solutions) 12 | 13 | ## Introduction 14 | Danny was scrolling through his Instagram feed when something really caught his eye - “80s Retro Styling and Pizza Is The Future!” 15 | Danny was sold on the idea, but he knew that pizza alone was not going to help him get seed funding to expand his new Pizza Empire - so he had one more genius idea to combine with it - he was going to Uberize it - and so Pizza Runner was launched! 16 | Danny started by recruiting “runners” to deliver fresh pizza from Pizza Runner Headquarters (otherwise known as Danny’s house) and also maxed out his credit card to pay freelance developers to build a mobile app to accept orders from customers. 17 | 18 | 19 | ## Datasets used 20 | Key datasets for this case study 21 | - **runners** : The table shows the registration_date for each new runner 22 | - **customer_orders** : Customer pizza orders are captured in the customer_orders table with 1 row for each individual pizza that is part of the order. The pizza_id relates to the type of pizza which was ordered whilst the exclusions are the ingredient_id values which should be removed from the pizza and the extras are the ingredient_id values which need to be added to the pizza. 23 | - **runner_orders** : After each orders are received through the system - they are assigned to a runner - however not all orders are fully completed and can be cancelled by the restaurant or the customer. The pickup_time is the timestamp at which the runner arrives at the Pizza Runner headquarters to pick up the freshly cooked pizzas. The distance and duration fields are related to how far and long the runner had to travel to deliver the order to the respective customer. 24 | - **pizza_names** : Pizza Runner only has 2 pizzas available the Meat Lovers or Vegetarian! 25 | - **pizza_recipes** : Each pizza_id has a standard set of toppings which are used as part of the pizza recipe. 26 | - **pizza_toppings** : The table contains all of the topping_name values with their corresponding topping_id value 27 | 28 | ## Entity Relationship Diagram 29 |

30 | Image 31 | 32 | ## Case Study Question: 33 | A. Pizza Metrics 34 | 1. How many pizzas were ordered? 35 | 2. How many unique customer orders were made? 36 | 3. How many successful orders were delivered by each runner? 37 | 4. How many of each type of pizza was delivered? 38 | 5. How many Vegetarian and Meatlovers were ordered by each customer? 39 | 6. What was the maximum number of pizzas delivered in a single order? 40 | 7. For each customer, how many delivered pizzas had at least 1 change and how many had no changes? 41 | 8. How many pizzas were delivered that had both exclusions and extras? 42 | 9. What was the total volume of pizzas ordered for each hour of the day? 43 | 10. What was the volume of orders for each day of the week? 44 | 45 | B. Runner and Customer Experience 46 | 1. How many runners signed up for each 1 week period? (i.e. week starts 2021-01-01) 47 | 2. What was the average time in minutes it took for each runner to arrive at the Pizza Runner HQ to pickup the order? 48 | 3. Is there any relationship between the number of pizzas and how long the order takes to prepare? 49 | 4. What was the average distance travelled for each customer? 50 | 5. What was the difference between the longest and shortest delivery times for all orders? 51 | 6. What was the average speed for each runner for each delivery and do you notice any trend for these values? 52 | 7. What is the successful delivery percentage for each runner? 53 | 54 | C. Ingredient Optimisation 55 | 1. What are the standard ingredients for each pizza? 56 | 2. What was the most commonly added extra? 57 | 3. What was the most common exclusion? 58 | 4. Generate an order item for each record in the customers_orders table in the format of one of the following: 59 | ◼ Meat Lovers 60 | ◼ Meat Lovers - Exclude Beef 61 | ◼ Meat Lovers - Extra Bacon 62 | ◼ Meat Lovers - Exclude Cheese, Bacon - Extra Mushroom, Peppers 63 | 64 | D. Pricing and Ratings 65 | 1. If a Meat Lovers pizza costs $12 and Vegetarian costs $10 and there were no charges for changes - how much money has Pizza Runner made so far if there are no delivery fees? 66 | 2. What if there was an additional $1 charge for any pizza extras? 67 | ◼ Add cheese is $1 extra 68 | 3. The Pizza Runner team now wants to add an additional ratings system that allows customers to rate their runner, how would you design an additional table for this new dataset - generate a schema for this new table and insert your own data for ratings for each successful customer order between 1 to 5. 69 | 4. Using your newly generated table - can you join all of the information together to form a table which has the following information for successful deliveries? 70 | ◼ customer_id 71 | ◼ order_id 72 | ◼ runner_id 73 | ◼ rating 74 | ◼ order_time 75 | ◼ pickup_time 76 | ◼ Time between order and pickup 77 | ◼ Delivery duration 78 | ◼ Average speed 79 | ◼ Total number of pizzas 80 | 5. If a Meat Lovers pizza was $12 and Vegetarian $10 fixed prices with no cost for extras and each runner is paid $0.30 per kilometre traveled - how much money does Pizza Runner have left over after these deliveries? 81 | 82 | ## Data Cleaning 83 | There are some known data issues with few tables. Data cleaning was performed and saved in temporary tables before attempting the case study. 84 | 85 | **customer_orders table** 86 | - The exclusions and extras columns in customer_orders table will need to be cleaned up before using them in the queries 87 | - In the exclusions and extras columns, there are blank spaces and null values. 88 | 89 | **runner_orders table** 90 | - The pickup_time, distance, duration and cancellation columns in runner_orders table will need to be cleaned up before using them in the queries 91 | - In the pickup_time column, there are null values. 92 | - In the distance column, there are null values. It contains unit - km. The 'km' must also be stripped 93 | - In the duration column, there are null values. The 'minutes', 'mins' 'minute' must be stripped 94 | - In the cancellation column, there are blank spaces and null values. 95 | 96 | Click [here](https://github.com/AmitPatel-analyst/SQL-Case-Study/blob/main/%238Weeksqlchallange/Case%20Study%20%23%202%20-%20Pizza%20Runner/0.%20Data%20Cleaning.md) to view the code of data cleaning task. 97 | 98 | ## Case Study Solutions 99 | - [A. Pizza Metrics](https://github.com/AmitPatel-analyst/SQL-Case-Study/blob/main/%238Weeksqlchallange/Case%20Study%20%23%202%20-%20Pizza%20Runner/A.%20Pizza%20Metrics.md) 100 | - [B. Runner and Customer Experience](https://github.com/AmitPatel-analyst/SQL-Case-Study/blob/main/%238Weeksqlchallange/Case%20Study%20%23%202%20-%20Pizza%20Runner/B.%20Runner%20and%20Customer%20Experience.md) 101 | - [C. Ingredient Optimisation](https://github.com/AmitPatel-analyst/SQL-Case-Study/blob/main/%238Weeksqlchallange/Case%20Study%20%23%202%20-%20Pizza%20Runner/C.%20Ingredient%20Optimisation.md) 102 | - [D. Pricing and Ratings](https://github.com/AmitPatel-analyst/SQL-Case-Study/blob/main/%238Weeksqlchallange/Case%20Study%20%23%202%20-%20Pizza%20Runner/D.%20Pricing%20and%20Ratings.md) 103 | -------------------------------------------------------------------------------- /#8Weeksqlchallange/Case Study # 3 - Foodie Fi/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /#8Weeksqlchallange/README.md: -------------------------------------------------------------------------------- 1 | # 8-Week-SQL-Challenge 2 | 3 | This repository contains the solutions for the case studies in **[8WeekSQLChallenge](https://8weeksqlchallenge.com)**. 4 | The 8 Week SQL Challenge is started by Danny Ma through Data With Danny virtual data apprenticeship program, which consists of 8 different SQL challenges. 5 | 6 | Each case-study folder contains the following files 7 | - A readme file explaining the problem statement and datasets used 8 | - SQL file to create the schema, tables and loading data 9 | - MD files which contains SQL queries answering the respective challenge questions 10 | 11 | **Note**: 12 | - Solutions are coded in **Microsoft SQL Server** 13 | 14 | ## Challenge case studies 15 | * 🍜[Case Study #1 - Danny's Diner](https://github.com/AmitPatel-analyst/SQL-Case-Study/tree/main/%238Weeksqlchallange/Case%20Study%20%23%201%20-%20Danny's%20Diner) 16 | * 🍕[Case Study #2 - Pizza Runner](https://github.com/AmitPatel-analyst/SQL-Case-Study/tree/main/%238Weeksqlchallange/Case%20Study%20%23%202%20-%20Pizza%20Runner) 17 | 18 | 19 | 20 | ## Support 21 | Give a ⭐️ if you like this project! 22 | -------------------------------------------------------------------------------- /Analyze Incredible 120 Years of Olympic Games/Olympics_data.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmitPatel-analyst/SQL-Case-Study/4dae8f080f537149fb3f2a7233519d9d17903659/Analyze Incredible 120 Years of Olympic Games/Olympics_data.zip -------------------------------------------------------------------------------- /Analyze Incredible 120 Years of Olympic Games/README.md: -------------------------------------------------------------------------------- 1 | ### Incredible 120 Years of Olympic Games 2 | Introduction 3 | 4 | The modern Olympics represents an evolution of all games from 1986 to 2016. Because the Olympics is an international sports event. It is affected by global issues such as the COVID-19 pandemic, and world wars. Nevertheless, there are positive movements such as women's empowerment and the development of the game itself. 5 | 6 | In this project, I used a 120-year-old dataset containing a massive collection of data. I downloaded the data from Kaggle Website. https://www.kaggle.com/datasets/heesoo37/120-years-of-olympic-history-athletes-and-results 7 | There are total 2 Csv files "athlete_events.csv" and "noc_regions.csv" and I imported the table into MS-SQL Server and created a database in my local machine and saved these 2 tables in the following names. 8 | 1) olympics_history 9 | 2) Olympics_history_noc_regions 10 | 11 | I performed intermediate to advanced SQL queries (Subqueries, CTE, Joins) for better analysis. 12 | First and foremost, I wanted to study the dataset to get answers to the following questions: How many columns and rows are in the dataset? Ans:- ~271K Rows and 15 Columns 13 | 14 | How many total datasets are there? Ans:- 2 datasets 15 | 16 | ```sql 17 | select count(1) as Total_rows from olympics_history; 18 | ``` 19 | ![image](https://user-images.githubusercontent.com/120770473/220054252-f442e35a-c2ff-4c3f-b073-429ee015c327.png) 20 | ```sql 21 | select COUNT(*) as Total_Columns 22 | from sys.all_columns 23 | where object_id = ( 24 | select object_id 25 | from sys.tables 26 | where name = 'olympics_history'); 27 | ``` 28 | ![image](https://user-images.githubusercontent.com/120770473/220054633-fc379b63-483b-4aa1-b0aa-96821aabac74.png) 29 | -------------------------------------------------------------------------------------------------- 30 | Derived answers of some questions by writing sql queries. 31 | ### Ques-1 How many olympics games have been held? 32 | : For this query I used 2 commands ie.distinct Command to get rid of any duplicate values and then Count of That result to show the number of Total Olympics games. 33 | ```sql 34 | select count(distinct games) as total_olympics_games 35 | from olympics_history; 36 | ``` 37 | ![image](https://user-images.githubusercontent.com/120770473/220057463-efee6918-e4c8-4ca5-a756-5af0542cfc43.png) 38 | ### Ques-2 Mention the total no of nations who participated in each olympics game 39 | : Use a JOIN to display a table showing Countries who participated olympics game and then Count All countries with respect to each Olympic games By Grouping . 40 | ```sql 41 | WITH ALL_COUNTRIES AS 42 | ( 43 | SELECT GAMES,NR.REGION AS COUNTRY 44 | FROM OLYMPICS_HISTORY AS OH 45 | JOIN OLYMPICS_HISTORY_NOC_REGIONS NR ON OH.NOC=NR.NOC 46 | GROUP BY GAMES,NR.REGION 47 | ) 48 | SELECT GAMES,COUNT(1) AS TOTAL_NATIONS_PARTICIPATED 49 | FROM ALL_COUNTRIES 50 | GROUP BY GAMES 51 | ORDER BY GAMES ; 52 | ``` 53 | ![image](https://user-images.githubusercontent.com/120770473/220058410-39520caa-ba15-4cdc-b392-a041728ef93c.png) 54 | ![image](https://user-images.githubusercontent.com/120770473/220058581-06ba4173-30aa-4ac8-aa12-66ca6178bf22.png) 55 | ### Ques-3 Which year saw the highest and lowest no of countries participating in olympics? 56 | : For this query First I need to know the Total Countries Participated for each Olympics Games , here I used CTE to create a Temporary result sets. and then I used Window function "First_value" and Over() function to fetch both Lowest Participation Country name and Highest Participation Country name by using total_nations_participated Column and also fetch their respective Olympic games name and last I merge the Olympic games name with Number of Participation using "Concat" Command. 57 | ```sql 58 | with all_countries as 59 | ( 60 | select Games,nr.region 61 | from olympics_history as oh 62 | join Olympics_history_noc_regions nr on oh.NOC=nr.NOC 63 | group by games,nr.region 64 | ), 65 | tot_countries as( 66 | select Games,count(1) as total_nations_participated 67 | from all_countries 68 | group by Games 69 | ) 70 | select distinct 71 | concat(first_value(games) over(order by total_nations_participated) 72 | , ' - ' 73 | , first_value(total_nations_participated) over(order by total_nations_participated)) as Lowest_Countries, 74 | concat(first_value(games) over(order by total_nations_participated desc) 75 | , ' - ' 76 | , first_value(total_nations_participated) over(order by total_nations_participated desc)) as Highest_Countries 77 | from tot_countries 78 | order by 1; 79 | ``` 80 | ![image](https://user-images.githubusercontent.com/120770473/220059898-a315859a-269d-4315-8f97-709f1863b373.png) 81 | ### Ques-4 Which country has participated in all of the olympic games? 82 | : For this query , I used a CTE to create multiple temporary result sets like 1st find a Total Olymics games held till last date . 2nd find the all Countries who participated olympics game and last use a JOIN to display only those countries name who have played all Olympics games till last date by matching the values of total_olympics_games column of Total Games Table and total_participated_games column of countries_participated. 83 | ```sql 84 | with tot_games as 85 | ( 86 | select count(distinct games) as total_olympics_games 87 | from olympics_history), 88 | all_countries as 89 | ( 90 | select Games,nr.region as country 91 | from olympics_history as oh 92 | join Olympics_history_noc_regions nr on oh.NOC=nr.NOC 93 | group by games,nr.region 94 | ), 95 | countries_participated as 96 | (select country,count(1) as all_participated_games 97 | from all_countries 98 | group by country 99 | ) 100 | 101 | select cp.* 102 | from countries_participated as cp 103 | join tot_games as tg 104 | on cp.all_participated_games=tg.total_olympics_games; 105 | ``` 106 | ![image](https://user-images.githubusercontent.com/120770473/223629512-34da0b38-1db0-482b-a695-a7b09f1a264d.png) 107 | 108 | ### Ques-5 Identify the sports which were played in all summer olympics. 109 | : With the help of CTE I find total no of distinct summer olympics games(Ans. Total 29 Games). 110 | then after count the total Summer olympics games played for each Sports by Grouping. 111 | and last Join both the result sets on matching of Total_summer_games column and no_of_games column to fetch only that Sports who has Played total 29 summer olympics games . 112 | ```sql 113 | with t1 as 114 | (select count(distinct Games) as Total_summer_games 115 | from olympics_history 116 | where Season = 'Summer'), 117 | t2 as 118 | (select Sport,count(distinct Games) as no_of_games 119 | from olympics_history 120 | where Season = 'Summer' 121 | group by Sport 122 | ) 123 | select * 124 | from t2 join t1 125 | on t2.no_of_games=t1.Total_summer_games; 126 | ``` 127 | Another approach to solve this query is by Subquery 128 | ```sql 129 | SELECT A.* 130 | FROM 131 | ( 132 | select Sport,count(distinct Games) as Total_summergames 133 | from olympics_history 134 | where Season = 'Summer' 135 | group by Sport 136 | ) A 137 | WHERE A.Total_summergames = (select count(distinct Games) as Total_summer_games 138 | from olympics_history 139 | where Season = 'Summer'); 140 | ``` 141 | ![image](https://user-images.githubusercontent.com/120770473/223631994-196f1580-a764-46b8-863e-f42e05a6cd70.png) 142 | ### Ques-6 Which Sports were just played only once in the entire olympics? 143 | 144 | ```sql 145 | with t2 as 146 | (select distinct Sport,Games 147 | from olympics_history 148 | ), 149 | t3 as 150 | (select Sport,count(Games) as Games_Played_Once 151 | from t2 152 | group by Sport 153 | having count(Games) = 1) 154 | 155 | select t3.Sport,t2.Games as Olympics_games ,Games_Played_Once 156 | from t3 join t2 157 | on t3.Sport=t2.Sport; 158 | ``` 159 | ![image](https://user-images.githubusercontent.com/120770473/223632810-30f05a56-8ba8-4463-b16e-2253ddfc97f2.png) 160 | ### Ques-7 Fetch the total no of sports played in each olympic games. 161 | : The goal is a table that shows how many Sports have played on each Olympic games. and sort the rows by latest Games played . 162 | ```sql 163 | SELECT Games,count(distinct Sport) as no_of_sports 164 | FROM olympics_history 165 | group by Games 166 | order by 1 desc; 167 | ``` 168 | ![image](https://user-images.githubusercontent.com/120770473/223651858-99d5c229-cc2b-4f72-90f4-e3a66bdff1f3.png) 169 | ![image](https://user-images.githubusercontent.com/120770473/223651937-7db7e3f2-8a8e-4355-9d7d-11e065b8f64d.png) 170 | ![image](https://user-images.githubusercontent.com/120770473/223652132-e0ea78f3-5db4-4ddd-b54f-73d6a827688b.png) 171 | 172 | ### Ques-8 Fetch oldest athletes to win a gold medal. 173 | :To display the Oldest athletes who won the gold medal on any Olympics games or any Sports . 174 | First I Replaced NA Value with "0" on that records where Data is missing on Age Column by using CASE with the age column 175 | and then filter the records by Gold Medal and then Store in Temporary result sets named "temp". then using Ranking Functions Rank() 176 | ,I fetched only those records whose age is highest among the Athletes. 177 | ```sql 178 | with temp as 179 | (select name,sex, 180 | cast(case when age = 'NA' then '0' else age end as int) as age 181 | ,team,games,city,sport 182 | ,event,medal 183 | from olympics_history 184 | where Medal = 'gold'), 185 | ranking as 186 | (select * 187 | ,rank() over(order by age desc) as rnk 188 | from temp 189 | ) 190 | select name,sex,age,team,games,city,sport,event,medal 191 | from ranking 192 | where rnk = 1; 193 | ``` 194 | ![image](https://user-images.githubusercontent.com/120770473/223654078-3ff5d303-90b9-4b35-9843-4d9247ee091e.png) 195 | ### Ques-9 Find the Total Athletes participated in the olympic games? 196 | : Using Distinct Command and Count function we can display the total athletes Participated. 197 | ```sql 198 | select count(distinct Name) 199 | from olympics_history; 200 | ``` 201 | ![image](https://user-images.githubusercontent.com/120770473/224466671-41f2050c-8490-4c4f-a7ff-ff89f45be4c4.png) 202 | ### Ques-10 Find the Ratio of male and female athletes participated in all olympic games? 203 | ```sql 204 | with t1 as 205 | (select sex, count(1) as cnt 206 | from olympics_history 207 | group by sex), 208 | t2 as 209 | (select *, row_number() over(order by cnt) as rn 210 | from t1), 211 | female_cnt as 212 | (select cnt from t2 where rn = 1), 213 | male_count as 214 | (select cnt from t2 where rn = 2) 215 | 216 | select format(round(male_count.cnt*1.0/female_cnt.cnt,2),'f2') as ratio 217 | from female_cnt cross join male_count; 218 | ``` 219 | ![image](https://user-images.githubusercontent.com/120770473/224466748-89da22a8-7f99-46e9-8c5a-aa315f6b719f.png) 220 | ### Ques-11 Find the top 5 athletes who have won the most gold medals? (we also consider those athletes who have same number of Gold Medals ) 221 | ```sql 222 | WITH T1 AS 223 | (SELECT Name,COUNT(1) AS TOTALMEDALS 224 | FROM olympics_history 225 | WHERE Medal = 'GOLD' 226 | GROUP BY Name 227 | ), 228 | T2 AS 229 | (SELECT *,DENSE_RANK() OVER(ORDER BY TOTALMEDALS DESC) AS RNK 230 | FROM T1 ) 231 | SELECT * FROM T2 WHERE RNK <=5; 232 | ``` 233 | ![image](https://user-images.githubusercontent.com/120770473/224773345-c5cdba53-2353-43db-902a-927c66758e19.png) 234 | ### Ques-12 Fetch the top 5 athletes who have won the most medals (gold/silver/bronze). 235 | ```sql 236 | with medals as 237 | ( SELECT Name,COUNT(1) AS TOTALMEDALS 238 | FROM olympics_history 239 | WHERE Medal in ('GOLD','silver','bronze') 240 | group by Name), 241 | ranking as 242 | ( select *,DENSE_RANK() over (order by TOTALMEDALS desc) as rnk 243 | from medals) 244 | select Name,TOTALMEDALS 245 | from ranking 246 | where rnk<=5; 247 | ``` 248 | ![image](https://user-images.githubusercontent.com/120770473/224774137-2de72cda-36d9-4750-9961-c3c271503594.png) 249 | ### Ques-13 Fetch the top 5 most successful countries in olympics. (Success is defined by Highest no of medals won.) 250 | : Here in this Query , I Joined both tables using Common columns "NOC" and filter only those records where Medals are NA. 251 | Then group the final result set into Region Field ,count the total Medals and Fetch Only Top 5 Countries who has earned Highest Medals. 252 | ```sql 253 | select TOP 5 nr.region as country 254 | ,count(Medal) as Total_Medals 255 | from olympics_history as oh 256 | inner join Olympics_history_noc_regions as nr 257 | on oh.NOC=nr.NOC 258 | where Medal <> 'NA' 259 | group by nr.region 260 | order by Total_Medals desc; 261 | ``` 262 | ![image](https://user-images.githubusercontent.com/120770473/224774795-81d336f9-ce6e-45fb-a128-4ac9383442c9.png) 263 | 264 | ### Ques-14 In which Sport/event, India has won highest medals. 265 | ```sql 266 | SELECT A.Sport AS SPORT,COUNT(1) AS TOTAL_MEDALS 267 | FROM olympics_history AS A 268 | INNER JOIN(SELECT NOC,region 269 | FROM Olympics_history_noc_regions 270 | WHERE NOC = 'IND') AS K 271 | ON K.NOC = A.NOC 272 | WHERE Medal <> 'NA' 273 | GROUP BY A.Sport 274 | ORDER BY 2 DESC; 275 | ``` 276 | ![image](https://user-images.githubusercontent.com/120770473/224779725-fd978fb5-2f06-4f7f-8f4c-8a5ee5913b03.png) 277 | ### Ques-15 Break down all olympic games where India won medal for Hockey and how many medals in each olympic games. 278 | ```SQL 279 | SELECT TEAM,SPORT,Games,Event,COUNT(1) AS TOTAL 280 | FROM olympics_history 281 | WHERE Medal <> 'NA' 282 | AND Team = 'INDIA' 283 | AND Sport = 'HOCKEY' 284 | GROUP BY TEAM,SPORT,Games,Event 285 | ORDER BY Games ; 286 | ``` 287 | ![image](https://user-images.githubusercontent.com/120770473/224780942-3c62504d-2c93-422b-a57d-368b0a75bd40.png) 288 | 289 | CLick [Here](https://github.com/AmitPatel-analyst/SQL-Case-Study/tree/main) to move back to other case studies repository! 290 | 291 | 292 | -------------------------------------------------------------------------------- /Credit card spending habits in India/Credit Card Image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmitPatel-analyst/SQL-Case-Study/4dae8f080f537149fb3f2a7233519d9d17903659/Credit card spending habits in India/Credit Card Image.jpg -------------------------------------------------------------------------------- /Credit card spending habits in India/CreditCard_Sql_EDA_01June.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmitPatel-analyst/SQL-Case-Study/4dae8f080f537149fb3f2a7233519d9d17903659/Credit card spending habits in India/CreditCard_Sql_EDA_01June.pdf -------------------------------------------------------------------------------- /Credit card spending habits in India/Readme.md: -------------------------------------------------------------------------------- 1 | ![custom](https://github.com/AmitPatel-analyst/SQL-Case-Study/blob/main/Credit%20card%20spending%20habits%20in%20India/Credit%20Card%20Image.jpg) 2 | I recently came across a case study on the Kaggle website. As a data analyst, I found this challenge to be a great way to showcase my SQL skills and improve my problem-solving abilities. 3 | 4 | Imported the dataset in SQL server with table name: credit_card_transcations 5 | Here is the link to download the credit card transactions [dataset](https://lnkd.in/dBkkCw2n) 6 | 7 | 8 | I used various SQL functions such as : 9 | ✅ Case When Statements 10 | ✅ CTE 11 | ✅ Joins 12 | ✅ Logical Operator 13 | ✅ Group by, Order by, Where 14 | ✅ Aggregate functions Sum, Count 15 | ✅ Date functions Datepart , Datename, Datediff 16 | ✅ Window functions(DENSE_RANK, LAG) 17 | 18 | Some of the key insights are : 19 | 1) Allocate additional marketing resources and promotional campaigns to the top 5 cities to capitalize on their high spending patterns. 20 | 2) Plan targeted promotional offers or campaigns during the highest spending months for each card type to encourage increased spending. 21 | 3) Investigate the reasons behind the low spending in the identified city and consider targeted marketing strategies or partnerships to increase spending in that location. 22 | 4) Allocate additional staffing or resources in the city with the highest spend-to-transaction ratio during weekends to capitalize on increased spending opportunities. 23 | 5) Identify market potential and consider targeted marketing efforts in the city with the fastest transaction growth to capture new customers and increase business growth. 24 | 6) Develop specific product or service offerings targeted towards females based on their significant contribution to spending in specific expense categories. 25 | 26 | ## Order of execution of SQL queries 27 | 28 | ``` 29 | FROM – the database gets the data from tables in FROM clause and if necessary performs the JOINs, 30 | WHERE – the data are filtered with conditions specified in WHERE clause, 31 | GROUP BY – the data are grouped by with conditions specified in WHERE clause, 32 | aggregate functions – the aggregate functions are applied to the groups created in the GROUP BY phase, 33 | HAVING – the groups are filtered with the given condition, 34 | window functions, 35 | SELECT – the database selects the given columns, 36 | DISTINCT – repeated values are removed, 37 | UNION/INTERSECT/EXCEPT – the database applies set operations, 38 | ORDER BY – the results are sorted, 39 | OFFSET – the first rows are skipped, 40 | LIMIT/FETCH/TOP – only the first rows are selected 41 | ``` 42 | -------------------------------------------------------------------------------- /Credit card spending habits in India/SqlCode_Creditcard.sql: -------------------------------------------------------------------------------- 1 | -- Date : 01-June-2023 2 | use Creditcard 3 | go 4 | 5 | --Questions: 6 | 7 | --1. write a query to print top 5 cities with highest spends and their percentage contribution of total credit card spends 8 | --2. write a query to print highest spend month and amount spent in that month for each card type 9 | --3. write a query to print the transaction details(all columns from the table) for each card type when 10 | --it reaches a cumulative of 1000000 total spends(We should have 4 rows in the o/p one for each card type) 11 | --4. write a query to find city which had lowest percentage spend for gold card type 12 | --5. write a query to print 3 columns: city, highest_expense_type , lowest_expense_type (example format : Delhi , bills, Fuel) 13 | --6. write a query to find percentage contribution of spends by females for each expense type 14 | --7. which card and expense type combination saw highest month over month growth in Jan-2014 15 | --8. during weekends which city has highest total spend to total no of transcations ratio 16 | --9. which city took least number of days to reach its 500th transaction after first transaction in that city 17 | 18 | select 19 | SUM(case when "Index" IS NULL THEN 1 ELSE 0 END) AS index_null_count 20 | ,sum(case when city IS NULL THEN 1 ELSE 0 END) AS city_null_count 21 | ,sum(case when "Date" IS NULL THEN 1 ELSE 0 END) AS date_null_count 22 | ,sum(case when Card_Type IS NULL THEN 1 ELSE 0 END) AS card_null_count 23 | ,sum(case when Exp_Type IS NULL THEN 1 ELSE 0 END) AS exp_null_count 24 | ,sum(case when Gender IS NULL THEN 1 ELSE 0 END) AS gender_null_count 25 | ,sum(case when Amount IS NULL THEN 1 ELSE 0 END) AS Amt_null_count 26 | from credit_card_transactions; 27 | 28 | select top 1 Date 29 | from credit_card_transactions 30 | order by Date desc; -- 26-may-2015 is the last date of dataset 31 | 32 | select top 1 Date 33 | from credit_card_transactions 34 | order by Date ; -- 04-oct-2013 is the first date of dataset 35 | 36 | 37 | select count(1) from credit_card_transactions; 38 | -- total 26052 records are present in the dataset 39 | 40 | select City , count(1) as Frequency_Usage 41 | from credit_card_transactions 42 | group by City 43 | order by 2 desc; -- Show me the total frequency usage by city 44 | 45 | select Gender,Card_Type,count(1) as Frequency_Usage 46 | from credit_card_transactions 47 | group by Gender,Card_Type 48 | order by 3 desc; -- Female do more credit card transactions as compared to male . 49 | 50 | 51 | select Gender,Card_Type,count(1) as Total_users 52 | from credit_card_transactions 53 | group by Gender,Card_Type 54 | order by 3 desc; -- Show me the transaction frequency by Gender and credit card type 55 | 56 | 57 | select gender,Exp_Type ,count(1) as Frequency_Usage 58 | from credit_card_transactions 59 | group by gender,Exp_Type 60 | order by 3 desc; --Show me the transactions frequency by Expense type and Gender 61 | 62 | select gender,Card_Type , sum(Amount) as SpendingAmt_byExptype 63 | from credit_card_transactions 64 | group by gender,Card_Type 65 | order by 3 desc; -- Show me the Amount Spent by Gender and credit card type 66 | 67 | 68 | --1. write a query to print top 5 cities with highest spends 69 | -- and their percentage contribution of total credit card spends? 70 | 71 | With cte1 as ( 72 | select top 5 city ,sum(Amount) as Citywise_Spent_Amount 73 | from credit_card_transactions 74 | group by city 75 | order by Citywise_Spent_Amount desc 76 | ), 77 | cte2 as ( 78 | select sum(Amount) as total_amt 79 | from credit_card_transactions 80 | ) 81 | select c1.City 82 | ,c1.Citywise_Spent_Amount 83 | ,100.0*c1.Citywise_Spent_Amount / c2.total_amt as Percentage_contribution 84 | from cte1 as c1 85 | inner join cte2 as c2 86 | on 1=1; 87 | 88 | --2. write a query to print highest spend month and amount spent in that month for each card type 89 | 90 | 91 | with spent_amt_datewise as( 92 | select DATEPART(year,Date) as trans_year 93 | ,DATENAME(month,date) as trans_month 94 | ,Card_Type 95 | ,sum(amount) as spent_amt 96 | from credit_card_transactions 97 | group by DATEPART(year,Date) 98 | ,DATENAME(month,date) 99 | ,Card_Type 100 | ) 101 | ,ranking as ( 102 | select * 103 | ,DENSE_RANK() over(partition by card_type order by spent_amt desc) as drank 104 | from spent_amt_datewise 105 | ) 106 | select trans_year,trans_month,Card_Type,spent_amt 107 | from ranking 108 | where drank = 1; 109 | 110 | --3. write a query to print the transaction details(all columns from the table) for each card type 111 | -- when it reaches a cumulative of 10,00,000 total spends(We should have 4 rows in the o/p one for each card type) 112 | 113 | select City,Date,Card_Type,Exp_Type,Gender,Amount,cumulative_sum 114 | from ( 115 | select * 116 | ,DENSE_RANK() over(Partition by card_type order by k.cumulative_sum) as drank 117 | from ( 118 | select * 119 | ,sum(Amount) over(partition by card_type order by Date,Amount) as cumulative_sum 120 | from credit_card_transactions 121 | ) k 122 | where k.cumulative_sum>=1000000 123 | ) m 124 | where m.drank = 1 ; 125 | 126 | --4. write a query to find city which had lowest percentage spend for gold card type 127 | 128 | 129 | with cte1 as ( 130 | select City, sum(Amount) as spend_amt_ingold_citywise 131 | from credit_card_transactions 132 | where Card_Type = 'Gold' 133 | group by City,Card_Type 134 | ) 135 | , cte2 as ( 136 | select City, Sum(Amount) as spent_amt_citywise 137 | from credit_card_transactions 138 | group by City 139 | ) 140 | ,cte3 as ( 141 | select c1.City 142 | ,c1.spend_amt_ingold_citywise as Citywise_Spent_money_ongold 143 | ,c2.spent_amt_citywise as Citywise_Spent_money 144 | ,100.0 * c1.spend_amt_ingold_citywise / c2.spent_amt_citywise as perc_contribution 145 | from cte1 c1 146 | join cte2 c2 147 | on c1.City = c2.City 148 | ) 149 | select top 1 * 150 | from cte3 151 | order by perc_contribution; -- Dhamtari, India has spent least amount in gold. 152 | 153 | --5. write a query to print 3 columns: city, highest_expense_type , lowest_expense_type 154 | -- (example format : Delhi , bills, Fuel) 155 | 156 | 157 | with cte_1 as ( 158 | select City ,Exp_Type, sum(Amount) as spent_amt 159 | from credit_card_transactions 160 | group by City , Exp_Type ) 161 | , cte_2 as ( 162 | select city, 163 | min(spent_amt) as lowest_spent_amount, 164 | MAX (spent_amt) as highest_spent_amount 165 | from cte_1 166 | group by City 167 | ) 168 | select c1.City, 169 | max(case when c2.highest_spent_amount = c1.spent_amt then Exp_Type end) as highest_expense_type, 170 | max(case when c2.lowest_spent_amount = c1.spent_amt then Exp_Type end ) as lowest_expense_type 171 | from cte_1 as c1 172 | inner join cte_2 as c2 173 | on c1.City=c2.City 174 | group by c1.City 175 | order by c1.City; 176 | 177 | -- 6. Write a query to find percentage contribution of spends by females for each expense type. 178 | 179 | with cte_1 as ( 180 | select Exp_Type , sum(Amount) as Exp_type_spent_amount 181 | from credit_card_transactions 182 | where Gender = 'F' 183 | group by Exp_Type 184 | ), cte_2 as ( 185 | select sum(Amount) as total_spent 186 | from credit_card_transactions 187 | where Gender = 'F' 188 | ) 189 | select 190 | Exp_Type, 191 | format(100.0* Exp_type_spent_amount / total_spent ,'f2') as perc_contribution_spent_female 192 | from cte_1 inner join cte_2 on 1=1; 193 | 194 | --7. which card and expense type combination saw highest month over month growth in january 2014? 195 | 196 | with cte_1 as( 197 | select Card_Type,Exp_Type, 198 | DATEPART(year,Date) as Trans_Year, 199 | DATEPART(month,Date) as Trans_Month, 200 | sum(Amount) as total_amount 201 | from credit_card_transactions 202 | group by Card_Type,Exp_Type,DATEPART(year,Date),DATEPART(month,Date) 203 | ) 204 | ,cte_2 as( 205 | select *, 206 | LAG(total_amount,1) over(partition by Card_Type,Exp_Type order by Trans_Year,Trans_Month) as prev_month_trans_amount 207 | from cte_1 208 | ) 209 | ,cte_3 as( 210 | select *, 211 | 100.0*(total_amount - prev_month_trans_amount)/ prev_month_trans_amount as growth_per_month 212 | from cte_2 213 | where Trans_Year = 2014 and Trans_Month = 1 214 | ) 215 | select top 1 * 216 | from cte_3 217 | order by growth_per_month desc; 218 | 219 | --8. during weekends which city has highest total spend to total no of transcations ratio 220 | 221 | 222 | select top 1 city, 223 | sum(Amount) as total_spent 224 | ,count(1) as Count_of_transaction 225 | ,ratio = sum(Amount)/count(1) 226 | from credit_card_transactions 227 | where DATEPART(weekday,Date) in (7,1) 228 | group by City 229 | order by 4 desc; 230 | 231 | 232 | 233 | --9. which city took least number of days to reach its 500th transaction after first transaction in that city 234 | 235 | WITH CTE1 AS 236 | ( 237 | select City,count(1) as count_of_trans, 238 | min(Date) AS MIN_DATE, 239 | MAX(DATE) AS MAX_DATE 240 | from credit_card_transactions 241 | group by City 242 | HAVING count(1) >=500 243 | ) 244 | , CTE2 AS ( 245 | SELECT 246 | CITY,DATE, 247 | ROW_NUMBER() OVER(PARTITION BY CITY ORDER BY DATE) AS ROW_NM 248 | FROM credit_card_transactions 249 | WHERE City IN ( SELECT City FROM CTE1) 250 | ) 251 | , CTE3 AS ( 252 | SELECT C1.CITY,C1.MIN_DATE , C1.MAX_DATE ,C1.count_of_trans 253 | ,C2.DATE 254 | FROM CTE1 AS C1 255 | INNER JOIN CTE2 AS C2 256 | ON C1.CITY = C2.CITY 257 | WHERE C2.ROW_NM = 500 258 | ) 259 | SELECT CITY , MIN_DATE AS TRANS_START_DATE 260 | ,DATE AS TRANS_DATE_FOR500TH_TRANS 261 | ,DATEDIFF(DAY,MIN_DATE,DATE) AS DAYS_TO_REACH_500TH_TRANS 262 | FROM CTE3 263 | ORDER BY DAYS_TO_REACH_500TH_TRANS; 264 | 265 | Some of the key insights are : 266 | 1) Allocate additional marketing resources and promotional campaigns to the top 5cities to capitalize on their high spending patterns. 267 | 2) Plan targeted promotional offers or campaigns during the highest spending monthsfor each card type to encourage increased spending. 268 | 3) Investigate the reasons behind the low spending in the identified city and considertargeted marketing strategies or partnerships to increase spending in that location. 269 | 4) Allocate additional staffing or resources in the city with the highest spend-to-transaction ratio during weekends to capitalize on increased spending opportunities. 270 | 5) Identify market potential and consider targeted marketing efforts in the city withthe fastest transaction growth to capture new customers and increase businessgrowth. 271 | 6) Develop specific product or service offerings targeted towards females based ontheir significant contribution to spending in specific expense categories. 272 | -------------------------------------------------------------------------------- /Cryptocurrency/README.MD: -------------------------------------------------------------------------------- 1 | ![crypto (Custom)](https://user-images.githubusercontent.com/120770473/235211571-e15b98e9-8454-4fe7-9b74-82cdf6ff8f21.jpg) 2 | # Cryptocurrency Case Study 3 | 4 | 5 | This repository contains the solutions for the Cryptocurrency case study. This case study is started by Danny Ma through Data With Danny virtual data apprenticeship program,in which I had learnt different SQL functions. I would like to thank Danny Ma to provide the Virtual Data apprenticeship program. 6 | ## Context 7 | 8 | This entire SQL Simplified course focuses on a Cryptocurrency SQL Case Study. 9 | In our fictitious case study - Danny's data mentors from the Data With Danny team have been busy trading cryptocurrency markets since 2017. 10 | 11 | Our main purpose for this case study is to analyze the performance of the DWD mentors over time. 12 | We will accomplish this by writing SQL queries to utilize all available datasets to answer a series of realistic business questions. 13 | 14 | Each case-study folder contains the following files 15 | - A readme file explaining the problem statement and datasets used 16 | - SQL file to create the schema and loading data 17 | - .csv files containing the raw data for each table to import 18 | - MD files which contains SQL queries answering the respective questions 19 | 20 | You can find the solution [here](https://github.com/AmitPatel-analyst/SQL-Case-Study/blob/main/Cryptocurrency/Cryptocurrency%20Solutions.md) 21 | **Note**: 22 | - Solutions are coded in **PostgreSQL** 23 | 24 | ## Support 25 | Give a ⭐️ if you like this project! 26 | # About the Author: Danny Ma 27 | 28 | Danny is the Chief Data Mentor at Data With Danny and the Founder & CEO of Sydney Data Science, a boutique data consultancy based out of Sydney, Australia. 29 | Danny is a regular speaker at global data conferences, meetups and podcasts where he shares the importance of mentorship for all data professionals. He is also a technical author and instructor for O'Reilly. 30 | -------------------------------------------------------------------------------- /Cryptocurrency/Schema_Postgres.sql: -------------------------------------------------------------------------------- 1 | CREATE SCHEMA trading; 2 | 3 | CREATE TABLE trading.members ( 4 | "member_id" VARCHAR(10), 5 | "first_name" VARCHAR(10), 6 | "region" VARCHAR(20) 7 | ); 8 | 9 | CREATE TABLE trading.prices ( 10 | "ticker" VARCHAR(10), 11 | "market_date" DATE, 12 | "price" numeric, 13 | "open" numeric, 14 | "high" numeric, 15 | "low" numeric, 16 | "volume" VARCHAR(10), 17 | "change" VARCHAR(10) 18 | ); 19 | 20 | CREATE TABLE trading.transactions ( 21 | "txn_id" INTEGER, 22 | "member_id" VARCHAR(10), 23 | "ticker" VARCHAR(10), 24 | "txn_date" DATE, 25 | "txn_type" VARCHAR(5), 26 | "quantity" numeric, 27 | "percentage_fee" numeric, 28 | "txn_time" TIMESTAMP 29 | ); 30 | 31 | /* 32 | -- Creating these indexes after loading data 33 | -- will make things run much faster!!! 34 | 35 | CREATE INDEX ON trading.prices (ticker, market_date); 36 | CREATE INDEX ON trading.transactions (txn_date, ticker); 37 | CREATE INDEX ON trading.transactions (txn_date, member_id); 38 | CREATE INDEX ON trading.transactions (member_id); 39 | CREATE INDEX ON trading.transactions (ticker); 40 | 41 | */ 42 | -------------------------------------------------------------------------------- /Cryptocurrency/crypto-erd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmitPatel-analyst/SQL-Case-Study/4dae8f080f537149fb3f2a7233519d9d17903659/Cryptocurrency/crypto-erd.png -------------------------------------------------------------------------------- /Cryptocurrency/members.csv: -------------------------------------------------------------------------------- 1 | member_id,first_name,region 2 | c4ca42,Danny,Australia 3 | c81e72,Vipul,United States 4 | eccbc8,Charlie,United States 5 | a87ff6,Nandita,United States 6 | e4da3b,Rowan,United States 7 | 167909,Ayush,United States 8 | 8f14e4,Alex,United States 9 | c9f0f8,Abe,United States 10 | 45c48c,Ben,Australia 11 | d3d944,Enoch,Africa 12 | 6512bd,Vikram,India 13 | c20ad4,Leah,Asia 14 | c51ce4,Pavan,Australia 15 | aab323,Sonia,Australia 16 | -------------------------------------------------------------------------------- /Indian Bank Case Study/IndianBank_Queries.sql: -------------------------------------------------------------------------------- 1 | /***************************************************************************************** 2 | Name : Code for INDIAN BANK DB 3 | Author : Amit Patel 4 | 5 | 6 | Purpose: This script will create Db and few table in it to store info about INDIAN BANK 7 | *****************************************************************************************/ 8 | create database INDIAN_BANK 9 | go 10 | 11 | USE INDIAN_BANK 12 | GO 13 | 14 | ---create table ProductMaster 15 | create table ProductMaster 16 | ( 17 | PID char(2) Primary Key, 18 | ProductName varchar(25) not null 19 | ) 20 | go 21 | 22 | 23 | --insert 24 | 25 | insert into ProductMaster values('SB','Savings Bank') 26 | insert into ProductMaster values('LA','Loan Account') 27 | insert into ProductMaster values('FD','Fixed Deposit') 28 | insert into ProductMaster values('RD','Recurring Deposit') 29 | 30 | 31 | -- read the data 32 | select * from ProductMaster 33 | go 34 | 35 | ---create table RegionMaster 36 | create table RegionMaster 37 | ( 38 | RID Int Primary key, 39 | RegionName Char(6) not null 40 | ) 41 | go 42 | 43 | --insert 44 | 45 | insert into RegionMaster values(1,'South') 46 | insert into RegionMaster values(2,'North') 47 | insert into RegionMaster values(3,'East') 48 | insert into RegionMaster values(4,'West') 49 | go 50 | 51 | --read the data 52 | 53 | select * from RegionMaster 54 | go 55 | 56 | 57 | ---create table BranchMaster 58 | create table BranchMaster 59 | ( 60 | BRID CHAR(3) Primary Key, 61 | BranchName VARCHAR(30) NOT NULL, 62 | BranchAddress VARCHAR(50) NOT NULL, 63 | RID INT Foreign Key references RegionMaster(RID) 64 | ) 65 | go 66 | 67 | --insert 68 | 69 | insert into BranchMaster values('BR1','Goa','Opp: KLM Mall, Panaji, Goa-677123',4) 70 | 71 | insert into BranchMaster values('BR2','Hyd','Hitech city, Hitex, Hyd-500012',1) 72 | insert into BranchMaster values('BR3','Delhi','Opp: Ambuja Mall, Sadar Bazar, Delhi-110006',2) 73 | 74 | insert into BranchMaster values('BR4','Mumbai','Suman city, Hitex, Mumbai-490001',4) 75 | insert into BranchMaster values('BR5','Nagpur','Opp: Aman Mall, Nagpur-677178',4) 76 | 77 | insert into BranchMaster values('BR6','Raipur','Chetak city, Raipur-492001',3) 78 | insert into BranchMaster values('BR7','Kolkata','Opp: Shyam Mall, Howrah, Kolkata-485177',3) 79 | 80 | insert into BranchMaster values('BR8','Chennai','Sona city, Chennai-504212',1) 81 | insert into BranchMaster values('BR9','Trichy','Eltronic city, Hitex, Trichy-400012',1) 82 | go 83 | 84 | -- read the data 85 | select * from BranchMaster 86 | go 87 | 88 | ---create table UserMaster 89 | create table UserMaster 90 | ( 91 | UserID int Primary Key, 92 | UserName varchar(30) not null, 93 | Designation CHAR(1) NOT NULL CHECK(Designation in ('M', 'T', 'C', 'O')) 94 | 95 | ) 96 | go 97 | --insert 98 | 99 | insert into UserMaster values(1,'Bhaskar Jogi','M') 100 | insert into UserMaster values(2,'Amit','O') 101 | insert into UserMaster values(3,'Hemanth','M') 102 | insert into UserMaster values(4,'John K','C') 103 | insert into UserMaster values(5,'Aman Pandey','T') 104 | insert into UserMaster values(6,'Priyanko','C') 105 | go 106 | --read the data 107 | select * from UserMaster 108 | go 109 | 110 | ---create table AccountMaster 111 | create table AccountMaster 112 | ( ACID INT PRIMARY KEY, 113 | NAME VARCHAR(40) NOT NULL, 114 | ADDRESS VARCHAR(50) NOT NULL, 115 | BRID CHAR(3) NOT NULL Foreign key references BranchMaster(BRID), 116 | PID CHAR(2) NOT NULL Foreign key references ProductMaster(PID), 117 | DOO DATETIME NOT NULL, 118 | CBALANCE MONEY NULL, 119 | UBALANCE MONEY NULL, 120 | STATUS CHAR(1) NOT NULL CHECK(STATUS in ('O','I','C')) 121 | ) 122 | go 123 | 124 | --insert 125 | insert into AccountMaster values(101,'Amit Patel','USA','BR1','SB','2018/12/23',1000,1000,'O') 126 | insert into AccountMaster values(102,'Ahmed Patel','Mumbai','BR3','SB','2018/12/27',2000,2000,'O') 127 | insert into AccountMaster values(103,'Ramesh Jogi','Hyd','BR2','LA','2019/01/01',4000,2000,'O') 128 | insert into AccountMaster values(104,'Nita Sahu','Pune','BR4','FD','2019/01/11',9000,9000,'C') 129 | insert into AccountMaster values(105,'Venu G','Chennai','BR5','SB','2019/01/15',10000,10000,'I') 130 | insert into AccountMaster values(106,'Shyam Verma','Patna','BR6','RD','2019/02/07',15000,15000,'O') 131 | insert into AccountMaster values(107,'Shundar Lal','Nagpur','BR7','SB','2019/02/25',20000,20000,'O') 132 | insert into AccountMaster values(108,'Ramesh K','Vizag','BR8','FD','2019/03/01',7000,7000,'C') 133 | insert into AccountMaster values(109,'Jeni','UK','BR9','SB','2019/03/10',3000,3000,'O') 134 | 135 | insert into AccountMaster values(110,'Shamlal sharma','Pune','BR1','FD','2019/03/11',5000,15000,'O') 136 | insert into AccountMaster values(111,'Shundar KUMAR','Nagpur','BR2','SB','2019/03/15',2000,20000,'C') 137 | insert into AccountMaster values(112,'Namesh K','Virar','BR2','SB','2019/03/16',2000,25000,'O') 138 | insert into AccountMaster values(113,'JYOTI','SURAT','BR5','SB','2019/03/20',3000,13000,'O') 139 | 140 | insert into AccountMaster values(114,'aman Verma','Puri','BR7','RD','2019/03/27',1000,12000,'I') 141 | insert into AccountMaster values(115,'Skumar baghel','Nasik','BR9','SB','2019/03/28',2000,22000,'O') 142 | insert into AccountMaster values(116,'Ranjit sahu','raipur','BR8','RD','2019/04/01',7250,12200,'O') 143 | insert into AccountMaster values(117,'sanjana patel','baroda','BR4','SB','2019/04/05',35000,37000,'O') 144 | 145 | insert into AccountMaster values(118,'amanlal sharma','raipur','BR3','LA','2019/04/11',15000,25000,'O') 146 | insert into AccountMaster values(119,'PREM KUMAR','Nagpur','BR6','RD','2019/04/15',12000,17500,'I') 147 | insert into AccountMaster values(120,'ESHWAR JAIN','VARANASI','BR5','LA','2019/04/16',20000,25000,'O') 148 | insert into AccountMaster values(121,'JYOTIKA','SURAT','BR7','LA','2019/04/20',3000,13000,'I') 149 | 150 | insert into AccountMaster values(122,'SUNDER Verma','PANIPAT','BR5','FD','2019/04/27',1000,12000,'I') 151 | insert into AccountMaster values(123,'GITA baghel','Nasik','BR7','SB','2019/04/28',2000,22000,'O') 152 | insert into AccountMaster values(124,'Ranjit SINGH','raipur','BR1','LA','2019/05/01',750,1200,'O') 153 | insert into AccountMaster values(125,'NAYAN patel','baroda','BR1','SB','2019/04/05',3000,7000,'C') 154 | insert into AccountMaster values(126,'Naina patel','baroda','BR4','SB','2019/04/15',3500,7000,'C') 155 | go 156 | 157 | --read the data 158 | select * from AccountMaster 159 | go 160 | 161 | ---create table TransactionMaster 162 | create table TxnMaster 163 | ( TNO int Primary key Identity(1,1), 164 | DOT datetime not null, 165 | ACID int not null Foreign key references AccountMaster(ACID), 166 | BRID CHAR(3) not null Foreign key references BranchMaster(BRID), 167 | TXN_TYPE CHAR(3) NOT NULL CHECK(TXN_TYPE in ('CW','CD','COD')), 168 | CHQ_NO INT NULL, 169 | CHQ_DATE SMALLDATETIME NULL, 170 | TXN_AMOUNT MONEY NOT NULL, 171 | UserID int not null Foreign key references UserMaster(UserID) 172 | ) 173 | go 174 | 175 | 176 | --insert 177 | insert into TxnMaster values('2019/1/12',101,'BR1','CD',NULL,NULL,1000,1) 178 | insert into TxnMaster values('2019/1/12',102,'BR3','CD',NULL,NULL,4000,3) 179 | insert into TxnMaster values('2019/1/15',102,'BR3','CW',NULL,NULL,2000,4) 180 | insert into TxnMaster values('2019/2/01',101,'BR1','CD',NULL,NULL,5000,6) 181 | insert into TxnMaster values('2019/2/10',103,'BR2','COD',2354,'2019/2/7',500,4) 182 | insert into TxnMaster values('2019/2/15',104,'BR4','COD',1254,'2019/2/12',1500,2) 183 | insert into TxnMaster values('2019/2/01',105,'BR5','CD',NULL,NULL,7000,1) 184 | insert into TxnMaster values('2019/3/10',106,'BR6','COD',7374,'2019/3/7',1500,3) 185 | insert into TxnMaster values('2019/3/18',106,'BR6','CW',NULL,NULL,6000,6) 186 | insert into TxnMaster values('2019/3/25',107,'BR7','CW',NULL,NULL,7000,1) 187 | insert into TxnMaster values('2019/3/30',107,'BR7','CD',NULL,NULL,6000,6) 188 | insert into TxnMaster values('2019/4/01',108,'BR8','CD',NULL,NULL,16000,2) 189 | insert into TxnMaster values('2019/4/11',109,'BR9','COD',7874,'2019/4/17',9000,4) 190 | go 191 | 192 | --read the data 193 | select * from TxnMaster 194 | go 195 | 196 | -------------------------------------------------------------------------------- /Indian Bank Case Study/README.md: -------------------------------------------------------------------------------- 1 | ### IndianBank Case Study 2 | 3 | 1. Create a DATABASE: Ibank 4 | ### TABLES 5 | CREATE THE FOLLOWING TABLES WITH SAME NAMES AND DATA TYPES AS PROVIDED BELOW: 6 | Tips : First create only Those tables and insert data who has a Primary column , after that create other Foriegn key Col Tables. 7 | [Here are the scripts of these tables](https://github.com/AmitPatel-analyst/SQL-Case-Study/blob/main/Indian%20Bank%20Case%20Study/IndianBank_Queries.sql) 8 | #### ProductMaster 9 | |Column Name |Data Type |Remarks| 10 | |-------------|-----------|-------| 11 | |PID |CHAR(2) |Primary Key 12 | |PRODUCT NAME |VARCHAR(25) |NULL not allowed 13 | 14 | #### RegionMaster 15 | |Column Name |Data Type |Remarks| 16 | |-------------|-----------|-------| 17 | |RID |INTEGER |Primary Key| 18 | |REGION NAME |CHAR(6) |NOT NULL| 19 | 20 | #### BranchMaster 21 | |Column Name |Data Type |Remarks| 22 | |-------------|-----------|-------| 23 | |BRID |CHAR(3) |Primary Key 24 | |BRANCH NAME |VARCHAR(30) |NOT NULL 25 | |BRANCH ADDRESS |VARCHAR(50) |NOT NULL 26 | |RID |INT |Foreign Key; NOT NULL 27 | 28 | #### UserMaster 29 | |Column Name |Data Type |Remarks| 30 | |-------------|-----------|-------| 31 | |USERID |INTEGER |Primary Key 32 | |USER NAME |VARCHAR(30) |NOT NULL 33 | |DESIGNATION |CHAR(1) |‘M’ for ‘MANAGER’, ‘T’ for ‘TELLER’, ‘C’ for ‘CLERK’, ‘O’ for ‘OFFICER’; NOT NULL. 34 | 35 | #### AccountMaster 36 | |Column Name |Data Type |Remarks| 37 | |-------------|-----------|-------| 38 | |ACID |INTEGER |Primary Key 39 | |NAME |VARCHAR(40) |NOT NULL 40 | |ADDRESS |VARCHAR(50) |NOT NULL 41 | |BRID |CHAR(3) |NOT NULL, Foreign Key 42 | |PID |CHAR(2) |Foreign Key; NOT NULL 43 | |DATE OF OPENING |DATETIME |NOT NULL 44 | |CLEAR BALANCE |MONEY |NULL ALLOWED 45 | |UNCLEAR BALANCE |MONEY |NULL ALLOWED 46 | |STATUS |CHAR(1) |‘O’ for ‘OPERATIVE’, ‘I’ for ‘INOPERATIVE’, ‘C’ for ‘CLOSED’; NOT NULL; DEFAULT value is ‘O’ (OPERATIVE) 47 | 48 | #### TransactionMaster 49 | |Column Name |Data Type |Remarks| 50 | |-------------|-----------|--------| 51 | |TRANSACTION NUMBER |INTEGER(4) |Primary Key; Identity, seed=1, Increment=1; 52 | |DATE OF TRANSACTION |DATETIME |NOT NULL 53 | |ACID |INTEGER |Foreign Key; NOT NULL 54 | |BRID |CHAR(3) |Foreign Key; NOT NULL 55 | |TXN_TYPE |CHAR(3) |‘CW’ for ‘CASH WITHDRAWAL’, ‘CD’ for ‘CASH DEPOSIT’, ‘CQD’ for ‘CHEQUE DEPOSIT’; NOT NULL 56 | |CHQ_NO |INTEGER |NULL ALLOWED 57 | |CHQ_DATE |SMALLDATETIME |NULL ALLOWED 58 | |TXN_AMOUNT |MONEY |NOT NULL 59 | |USERID |INTEGER |Foreign Key; NOT NULL 60 | 61 | Q.1. List the names of the account holders with corresponding branch names, in respect of the second-highest maximum and minimum Cleared balance 62 | ```sql 63 | SELECT NAME , BranchName ,CBALANCE 64 | FROM AccountMaster AS A 65 | JOIN BranchMaster AS B 66 | ON A.BRID=B.BRID 67 | WHERE CBALANCE IN (SELECT CBALANCE 68 | FROM (SELECT CBALANCE , ROW_NUMBER() OVER(ORDER BY CBALANCE DESC) AS RNK 69 | FROM AccountMaster) AS T 70 | WHERE RNK = 2 71 | UNION 72 | SELECT MIN(CBALANCE) FROM AccountMaster ) ; 73 | ``` 74 | ![image](https://user-images.githubusercontent.com/120770473/229336554-ca5dde9a-9334-458c-9818-affac2deb437.png) 75 | 76 | Q.2. Give the TransactionType-wise, branch-wise, total amount for the day. 77 | ```sql 78 | SELECT TXN_TYPE,BRID, SUM([TXN_AMOUNT]) AS TOTAL_AMT 79 | FROM TxnMaster 80 | GROUP BY TXN_TYPE,BRID 81 | ``` 82 | ![image](https://user-images.githubusercontent.com/120770473/229336604-81894552-616a-46c1-b73c-f3b15b860e95.png) 83 | 84 | Q.3. List the product having the maximum number of accounts 85 | ```sql 86 | SELECT ProductName,MAX_NO_OF_ACCOUNTS 87 | FROM ProductMaster AS P 88 | JOIN (SELECT PID,COUNT(ACID) AS MAX_NO_OF_ACCOUNTS 89 | FROM AccountMaster 90 | GROUP BY PID ) AS L 91 | ON P.PID=L.PID; 92 | ``` 93 | ![image](https://user-images.githubusercontent.com/120770473/229336685-1f8b4c89-1280-43e4-bce6-0fab59518d59.png) 94 | 95 | Q.4 List the names of the account holders and the number of transactions put thru by them, in a given day ( 2022-05-12 ) 96 | ```sql 97 | SELECT A.NAME, NOOFTXN 98 | FROM ( 99 | SELECT ACID,COUNT(DOT) AS NOOFTXN 100 | FROM TXNMASTER 101 | WHERE DOT = '2022-05-12' 102 | GROUP BY ACID 103 | ) AS T 104 | JOIN ACCOUNTMASTER AS A ON T.ACID=A.ACID; 105 | ``` 106 | ![image](https://user-images.githubusercontent.com/120770473/229336773-7bb2eba5-bf25-40af-a494-2c1215398053.png) 107 | 108 | Q.5. List the number of transactions that have been authorized by the Manager so far today 109 | ```sql 110 | SELECT COUNT(*) AS NO_OF_TXNS 111 | FROM TxnMaster 112 | WHERE UserID IN (1,3); 113 | ``` 114 | ![image](https://user-images.githubusercontent.com/120770473/229337103-b86d825f-8007-45e3-8142-01544bf4fb43.png) 115 | 116 | Q.6 Provide the following information from the ‘Account Master’ table: 117 | a) Product-wise, month-wise, number of accounts 118 | b) Total number of accounts for each product 119 | c) Total number of accounts for each month 120 | d) Total number of accounts in our bank 121 | 122 | 123 | ```sql 124 | SELECT PID,DATENAME(MM,DOO) AS MONTH_NM ,count(distinct ACID) as Nos_acc 125 | FROM ACCOUNTMASTER 126 | GROUP BY PID,DATEPART(MM,DOO),DATENAME(MM,DOO) 127 | ORDER BY DATEPART(MM,DOO) 128 | 129 | SELECT PID ,COUNT(ACID) AS NOS_ACC 130 | FROM ACCOUNTMASTER 131 | GROUP BY PID 132 | 133 | SELECT DATENAME(MM,DOO) AS MONTH_NM ,count(ACID) as Nos_acc 134 | FROM ACCOUNTMASTER 135 | GROUP BY DATEPART(MM,DOO),DATENAME(MM,DOO) 136 | ORDER BY DATEPART(MM,DOO) 137 | 138 | SELECT COUNT(*) AS NOS_ACC_in_bank 139 | FROM ACCOUNTMASTER 140 | ``` 141 | ![image](https://user-images.githubusercontent.com/120770473/229337585-8fa10798-0ba0-44d6-8fff-cf22b8cfeec7.png) 142 | ![image](https://user-images.githubusercontent.com/120770473/229337596-62cd8b85-2588-4425-b525-8a39430d6e1f.png) 143 | ![image](https://user-images.githubusercontent.com/120770473/229337615-13afb294-1498-4241-8151-b3d133a5cb6d.png) 144 | ![image](https://user-images.githubusercontent.com/120770473/229337629-122a3592-aab6-4a4c-9029-d64f4fbbecd9.png) 145 | 146 | -------------------------------------------------------------------------------- /Murder Mystery Case Study/GetFitNow members.csv: -------------------------------------------------------------------------------- 1 | Id,Person_Id,Name,Membership_Start_Date,Membership_Status 2 | "NL318",65076,"Everette Koepke","2017-09-26 00:00:00,000","Gold" 3 | "0YJ24",80651,"Waneta Wellard","2017-12-06 00:00:00,000","Gold" 4 | "5Y28Y",15218,"Millicent Yessios","2018-02-05 00:00:00,000","Gold" 5 | "A5N3S",24541,"Mary Cameron","2017-05-01 00:00:00,000","Gold" 6 | "R4J4S",13703,"Lien Yasin","2017-04-14 00:00:00,000","Gold" 7 | "338FV",66842,"Susanne Dubicki","2017-07-19 00:00:00,000","Gold" 8 | "0862D",62171,"Johana Dayer","2017-06-12 00:00:00,000","Gold" 9 | "6071T",85645,"Bea Fadei","2018-02-23 00:00:00,000","Gold" 10 | "82GA2",20716,"Roslyn Gonzaga","2017-08-09 00:00:00,000","Gold" 11 | "12C5R",98098,"Samara Decarolis","2017-03-04 00:00:00,000","Gold" 12 | "8AEAE",62617,"Phung Motts","2018-02-05 00:00:00,000","Gold" 13 | "0CR33",62561,"Noe Elgart","2017-06-27 00:00:00,000","Gold" 14 | "4JX11",32828,"Earnest Dambrose","2017-08-31 00:00:00,000","Gold" 15 | "GHDP5",78264,"Heath Hosack","2017-12-18 00:00:00,000","Gold" 16 | "D8FT1",14297,"Roslyn Ayer","2017-10-22 00:00:00,000","Gold" 17 | "V5260",66908,"Luanne Bhan","2017-09-19 00:00:00,000","Gold" 18 | "6K7I8",83198,"Audria Vosburgh","2018-03-09 00:00:00,000","Gold" 19 | "2B01Y",47735,"Tania Kubesh","2017-04-17 00:00:00,000","Gold" 20 | "7I67C",66010,"Simone Mcglinn","2017-08-02 00:00:00,000","Gold" 21 | "ITWMV",25743,"Elna Reichelderfer","2017-01-23 00:00:00,000","Gold" 22 | "44VO2",86834,"Aaron Larcher","2017-11-22 00:00:00,000","Gold" 23 | "D2KY6",99602,"Joline Hollering","2017-02-20 00:00:00,000","Gold" 24 | "GKZV7",30221,"Jude Fairbairn","2017-01-08 00:00:00,000","Gold" 25 | "1RSYJ",62042,"Clement Gidley","2017-10-16 00:00:00,000","Gold" 26 | "7BQKU",24145,"Valda Eacret","2017-12-07 00:00:00,000","Gold" 27 | "O86WU",68360,"Alex Strohmeier","2017-07-30 00:00:00,000","Gold" 28 | "8G0Z2",71282,"Marquis Stace","2018-04-04 00:00:00,000","Gold" 29 | "I0UIP",49229,"Madge Bernice","2017-05-03 00:00:00,000","Gold" 30 | "N36I5",83633,"Stuart Moitoza","2017-12-08 00:00:00,000","Gold" 31 | "C84S4",80745,"Mervin Eacret","2018-01-08 00:00:00,000","Gold" 32 | "71BOE",61721,"Maximo Oliver","2017-07-05 00:00:00,000","Gold" 33 | "72H2C",49774,"Grant Stutheit","2018-01-21 00:00:00,000","Gold" 34 | "FA4N5",70985,"September Cappelluti","2017-11-09 00:00:00,000","Gold" 35 | "3718M",50049,"Carmine Rodell","2017-08-27 00:00:00,000","Gold" 36 | "3F8N0",10319,"Tama Mazy","2017-10-21 00:00:00,000","Gold" 37 | "0Y6DA",67667,"Araceli Roussin","2017-03-04 00:00:00,000","Gold" 38 | "XTE42",55662,"Sarita Bartosh","2017-05-24 00:00:00,000","Gold" 39 | "P876D",97206,"Elizebeth Torgersen","2018-03-23 00:00:00,000","Gold" 40 | "F83PZ",16232,"Florencio Hodgman","2017-11-28 00:00:00,000","Gold" 41 | "06V71",50993,"Travis Windly","2017-08-12 00:00:00,000","Gold" 42 | "7YL50",82468,"Patricia Matskin","2018-03-04 00:00:00,000","Gold" 43 | "66Z13",44386,"Reinaldo Rhead","2017-05-17 00:00:00,000","Gold" 44 | "5VE7U",66910,"Jacqui Bonda","2017-10-10 00:00:00,000","Gold" 45 | "3DC24",10833,"Queen Radigan","2018-03-20 00:00:00,000","Gold" 46 | "6LSTG",83186,"Burton Grippe","2017-02-14 00:00:00,000","Gold" 47 | "J5IO8",44080,"Amberly Somdah","2017-09-02 00:00:00,000","Gold" 48 | "72B16",36182,"Tanner Faley","2017-08-25 00:00:00,000","Gold" 49 | "5VK83",84412,"Olimpia Romanick","2017-09-21 00:00:00,000","Gold" 50 | "X1IYH",35809,"Tu Lipkovitch","2017-06-08 00:00:00,000","Gold" 51 | "V14R0",83737,"Hosea Kuzyk","2018-04-23 00:00:00,000","Gold" 52 | "VHN8M",26473,"Travis Bressler","2017-05-16 00:00:00,000","Gold" 53 | "0WG67",77090,"Delora Vais","2017-07-06 00:00:00,000","Gold" 54 | "72244",10615,"Harold Fullmer","2017-07-21 00:00:00,000","Gold" 55 | "TY21F",94528,"Terrance Helwig","2017-06-03 00:00:00,000","Gold" 56 | "1YHL3",32045,"Greg Alwardt","2017-05-03 00:00:00,000","Gold" 57 | "O5U57",82818,"Luana Kirstein","2017-08-11 00:00:00,000","Gold" 58 | "R55GR",85724,"Marybeth Ulvan","2017-04-06 00:00:00,000","Gold" 59 | "F3U8A",46617,"Vanna Castillanos","2017-11-05 00:00:00,000","Gold" 60 | "NF4S2",15581,"Simon Weisenberg","2018-04-26 00:00:00,000","Gold" 61 | "3BRSC",19948,"Taylor Skyes","2018-03-14 00:00:00,000","Gold" 62 | "58636",62536,"Harvey Jennkie","2018-03-25 00:00:00,000","Gold" 63 | "NB387",21200,"Molly Dolin","2018-02-06 00:00:00,000","Gold" 64 | "GE5Q8",92736,"Carmen Dimick","2017-06-18 00:00:00,000","Gold" 65 | "0T622",98782,"Sarina Overpeck","2017-11-09 00:00:00,000","Gold" 66 | "BL3KC",24033,"Collene Gulde","2017-09-13 00:00:00,000","Gold" 67 | "48Z7A",28819,"Joe Germuska","2016-03-05 00:00:00,000","Gold" 68 | "48Z55",67318,"Jeremy Bowers","2016-01-01 00:00:00,000","Gold" 69 | "90081",16371,"Annabel Miller","2016-02-08 00:00:00,000","Gold" 70 | "AOE21",39426,"Noe Locascio","2017-10-05 00:00:00,000","Regular" 71 | "2PN28",63823,"Jeromy Heitschmidt","2018-02-15 00:00:00,000","Silver" 72 | "3A08L",32858,"Mei Bianchin","2017-04-01 00:00:00,000","Silver" 73 | "6W6I2",69510,"Paris Accurso","2017-06-13 00:00:00,000","Regular" 74 | "156A7",79701,"Marcelino Leard","2018-04-28 00:00:00,000","Silver" 75 | "X0643",15247,"Shondra Ledlow","2017-05-21 00:00:00,000","Silver" 76 | "8T75D",86040,"Shani Mcclard","2017-04-30 00:00:00,000","Silver" 77 | "R440Q",37694,"Jan Louthan","2017-06-06 00:00:00,000","Silver" 78 | "K055L",60785,"Latisha Larzazs","2017-05-03 00:00:00,000","Regular" 79 | "401Y4",80191,"Gary Hanohano","2017-06-27 00:00:00,000","Regular" 80 | "DEQP8",19108,"Dwight Roussel","2018-01-08 00:00:00,000","Regular" 81 | "N7F6U",52425,"Nick Delage","2017-07-09 00:00:00,000","Silver" 82 | "Z7Y82",31477,"Rigoberto Hartgrave","2017-08-17 00:00:00,000","Silver" 83 | "THJ81",12344,"Thaddeus Modin","2017-06-16 00:00:00,000","Regular" 84 | "51SWX",92075,"Keshia Llanet","2017-08-12 00:00:00,000","Regular" 85 | "4TZ45",61663,"Bebe Heilmann","2018-04-17 00:00:00,000","Silver" 86 | "5Y85X",45746,"Frederic Borsellino","2017-08-03 00:00:00,000","Regular" 87 | "2Q45D",47693,"Owen Caston","2017-01-02 00:00:00,000","Silver" 88 | "E43E8",96742,"Christian Hogue","2017-10-14 00:00:00,000","Silver" 89 | "TXNSC",18044,"Roberto Slot","2017-05-10 00:00:00,000","Silver" 90 | "1G2F7",90543,"Roger Bowling","2017-05-11 00:00:00,000","Silver" 91 | "1G205",31411,"Toshia Sininger","2017-01-25 00:00:00,000","Silver" 92 | "4D5R1",87998,"Richie Zerphey","2017-08-08 00:00:00,000","Regular" 93 | "W83OD",17742,"Danial Bzhyan","2017-11-27 00:00:00,000","Silver" 94 | "T80FG",68244,"Cherilyn Raigosa","2017-06-06 00:00:00,000","Regular" 95 | "0U51D",17301,"Archie Bardwell","2017-05-05 00:00:00,000","Regular" 96 | "355UU",33920,"Brandi Gommer","2017-01-17 00:00:00,000","Silver" 97 | "KE0FH",60260,"Peggy Cortes","2018-04-20 00:00:00,000","Silver" 98 | "133ED",70618,"Marcus Lathan","2017-11-22 00:00:00,000","Silver" 99 | "8SJ77",12526,"Fawn Lindemuth","2017-08-20 00:00:00,000","Regular" 100 | "0F76P",55951,"Ezekiel Nurnberger","2017-12-22 00:00:00,000","Regular" 101 | "ZAB1A",82357,"Shenna Valiente","2018-03-07 00:00:00,000","Regular" 102 | "W5YVL",33243,"Domonique Ballengee","2017-10-29 00:00:00,000","Regular" 103 | "07QP3",15980,"Hugh Piascik","2017-12-22 00:00:00,000","Regular" 104 | "5O6SK",36413,"Gordon Sixt","2017-05-13 00:00:00,000","Silver" 105 | "NJ876",13989,"Norma Gudroe","2017-02-10 00:00:00,000","Regular" 106 | "7788A",73797,"Antone Westgate","2018-01-19 00:00:00,000","Regular" 107 | "PEL8G",83434,"Jerald Aiola","2017-12-16 00:00:00,000","Silver" 108 | "T2JF2",40065,"Ignacio Freistuhler","2017-03-06 00:00:00,000","Regular" 109 | "43N05",15804,"Laurel Diab","2017-01-29 00:00:00,000","Regular" 110 | "HI1Q5",29185,"Yukiko Vogelzang","2018-01-12 00:00:00,000","Silver" 111 | "JQ162",11162,"Dorthy Picardi","2017-07-12 00:00:00,000","Silver" 112 | "402DQ",92379,"Lynwood Punter","2017-01-04 00:00:00,000","Silver" 113 | "33378",32162,"Olen Bolar","2018-03-24 00:00:00,000","Regular" 114 | "31A6U",28007,"Magen Brach","2017-04-25 00:00:00,000","Regular" 115 | "UK1F2",28073,"Zackary Cabotage","2017-08-18 00:00:00,000","Silver" 116 | "NB10J",65316,"Mitzi Bakerville","2017-05-18 00:00:00,000","Silver" 117 | "I827Y",96987,"Larraine Gamel","2017-09-11 00:00:00,000","Silver" 118 | "30Y1F",71173,"Detra Hemeyer","2018-01-10 00:00:00,000","Silver" 119 | "OBU3O",80489,"Wilma Sinstack","2017-02-07 00:00:00,000","Regular" 120 | "JY514",98128,"Keith Savary","2017-02-07 00:00:00,000","Regular" 121 | "E18IN",64073,"Lyda Mandigo","2018-03-16 00:00:00,000","Silver" 122 | "3OOJ1",62367,"Concha Fiveash","2018-01-09 00:00:00,000","Regular" 123 | "B5F34",73608,"George Cali","2017-12-18 00:00:00,000","Regular" 124 | "6NAWM",53382,"Myles Marushia","2017-07-10 00:00:00,000","Regular" 125 | "CGW3X",26187,"Hettie Macreno","2018-03-17 00:00:00,000","Silver" 126 | "03487",73490,"Lyman Harshbarger","2017-05-29 00:00:00,000","Silver" 127 | "7MRZ6",75743,"Brain Puthoff","2018-02-12 00:00:00,000","Silver" 128 | "48100",68693,"Ammie Mossbarger","2017-07-26 00:00:00,000","Silver" 129 | "K3XEZ",30558,"Roxane Bua","2017-07-10 00:00:00,000","Regular" 130 | "530Q6",53788,"Dagny Mikez","2018-02-18 00:00:00,000","Regular" 131 | "2303S",43058,"Willie Mazzella","2017-01-20 00:00:00,000","Regular" 132 | "I733Y",46526,"Carolin Orellano","2018-03-25 00:00:00,000","Silver" 133 | "AD2A5",17882,"Shaun Smeal","2017-10-20 00:00:00,000","Regular" 134 | "1AE2H",10815,"Adriane Pelligra","2017-08-16 00:00:00,000","Silver" 135 | "TQU5U",46224,"Jordan Gough","2018-01-31 00:00:00,000","Silver" 136 | "YD4W4",27287,"Cecil Piraino","2017-07-12 00:00:00,000","Silver" 137 | "7A4NF",98747,"Aleen Bergmann","2017-07-03 00:00:00,000","Silver" 138 | "J2033",51710,"Hal Funchess","2018-03-28 00:00:00,000","Silver" 139 | "344VM",13466,"Armando Huie","2017-10-02 00:00:00,000","Regular" 140 | "X377L",94492,"Alicia Bernson","2017-07-22 00:00:00,000","Silver" 141 | "RXP8M",48557,"Cruz Ewings","2018-01-11 00:00:00,000","Silver" 142 | "7OG72",98043,"Cher Rivenbark","2018-01-23 00:00:00,000","Silver" 143 | "CZ1H8",68418,"Roberto Spearmon","2017-06-04 00:00:00,000","Silver" 144 | "7U8WH",64192,"Bette Mayon","2017-07-22 00:00:00,000","Silver" 145 | "551F6",24618,"Lazaro Walterson","2018-02-03 00:00:00,000","Regular" 146 | "MYQ6A",99462,"Darren Yahraus","2017-07-12 00:00:00,000","Silver" 147 | "TLF67",66160,"Addie Tschirhart","2017-05-21 00:00:00,000","Regular" 148 | "76W33",43768,"Layne Burakowski","2018-02-01 00:00:00,000","Regular" 149 | "QN47X",53753,"Francisco Harcrow","2017-12-14 00:00:00,000","Regular" 150 | "V5M14",79592,"Dirk Malhi","2017-06-26 00:00:00,000","Regular" 151 | "77Q7L",10325,"Bruna Sumlin","2017-06-04 00:00:00,000","Silver" 152 | "MZQ3U",72585,"Mohammad Kotch","2017-11-09 00:00:00,000","Silver" 153 | "4K76L",19138,"Kimbery Pedaci","2017-09-25 00:00:00,000","Regular" 154 | "S702J",92540,"Ladawn Boards","2017-02-01 00:00:00,000","Regular" 155 | "2P828",38212,"Hershel Alstrom","2017-01-13 00:00:00,000","Silver" 156 | "QSD41",73130,"Aurelio Schrage","2017-02-03 00:00:00,000","Regular" 157 | "20U07",21727,"Alvin Fornes","2017-08-15 00:00:00,000","Silver" 158 | "0EY7M",78357,"Lisette Husain","2017-02-27 00:00:00,000","Silver" 159 | "TD7DF",60334,"Faustino Sanmartin","2018-04-04 00:00:00,000","Silver" 160 | "E0ZJ2",59737,"Hank Staufenberger","2017-04-08 00:00:00,000","Silver" 161 | "7MWHJ",31523,"Blossom Crescenzo","2018-03-09 00:00:00,000","Regular" 162 | "8I2HW",26467,"Wayne Bonyai","2017-02-22 00:00:00,000","Silver" 163 | "2STMK",97486,"Milan Knippers","2017-05-23 00:00:00,000","Regular" 164 | "C581L",53262,"Monty Torrico","2017-03-07 00:00:00,000","Silver" 165 | "WF7P1",93937,"Isaiah Bhagat","2017-06-25 00:00:00,000","Regular" 166 | "MORMA",45428,"Nelly Schumachor","2017-02-03 00:00:00,000","Regular" 167 | "48Z38",49550,"Tomas Baisley","2017-02-03 00:00:00,000","Silver" 168 | "3JP0X",91432,"Herschel Deltora","2017-01-31 00:00:00,000","Silver" 169 | "RMX31",99277,"Pia Ruthenberg","2017-11-29 00:00:00,000","Silver" 170 | "J4ML1",48466,"Jarred Newenle","2017-04-24 00:00:00,000","Regular" 171 | "MN1KI",43801,"Art Kunsman","2018-01-18 00:00:00,000","Silver" 172 | "ALP65",46423,"Angel Diachenko","2017-07-17 00:00:00,000","Regular" 173 | "8VM1U",70172,"Kurt Laplante","2017-05-24 00:00:00,000","Silver" 174 | "5SPT1",63130,"Abe Morefield","2017-10-07 00:00:00,000","Regular" 175 | "3S8Z0",12049,"Honey Erbentraut","2017-11-23 00:00:00,000","Regular" 176 | "36646",89559,"Titus Ruan","2018-02-03 00:00:00,000","Regular" 177 | "CGQ1A",99037,"Adrienne Fleishman","2017-07-18 00:00:00,000","Regular" 178 | "4SC0G",92824,"Kennith Splett","2017-07-31 00:00:00,000","Silver" 179 | "4MK73",87882,"Bernard Hardgrove","2017-04-27 00:00:00,000","Regular" 180 | "82OO1",83144,"Grant Saurel","2017-02-02 00:00:00,000","Silver" 181 | "2111X",36646,"Arlie Braig","2017-03-15 00:00:00,000","Silver" 182 | "11ZAK",12107,"Agatha Seddon","2017-06-28 00:00:00,000","Regular" 183 | "HM6U8",50106,"Edgar Bamba","2017-01-28 00:00:00,000","Silver" 184 | "2V137",41693,"Wendell Dulany","2017-12-19 00:00:00,000","Silver" 185 | "4KB72",79110,"Emile Hege","2017-05-22 00:00:00,000","Regular" 186 | -------------------------------------------------------------------------------- /Murder Mystery Case Study/Readme.md: -------------------------------------------------------------------------------- 1 | ## SQL Murder Mystery 2 | 3 | In this Project , I have solved the case of Murder which was happened in Fictional City called - Sql City on Date 15 jan 2018. 4 | I used MS-SQL Server and imported the CSV files to a SQL Database in my local machine to carry forward with the analysis. 5 | I have put all important CSV files in this directory. 6 | 7 | You can find Sql Code [here](https://github.com/AmitPatel-analyst/SQL-Case-Study/blob/main/Murder%20Mystery%20Case%20Study/SQL%20Murder%20Mystery.sql) 8 | There's been a Murder in SQL City! The SQL Murder Mystery is designed to be both a self-directed lesson to learn SQL concepts and commands and a fun game for experienced SQL users to solve an intriguing crime. 9 | 10 | If you just want to solve the mystery, go to [mystery.knightlab.com](https://mystery.knightlab.com). If you're new to SQL, you may want to start at [our walkthrough](https://mystery.knightlab.com/walkthrough.html). It won't teach you everything about SQL, but it should teach you all that you need to solve the mystery. 11 | -------------------------------------------------------------------------------- /Murder Mystery Case Study/SQL Murder Mystery.sql: -------------------------------------------------------------------------------- 1 | use amit 2 | go 3 | -- Case Study - SQL Murder Mystery 4 | -- Credits - The SQL Murder Mystery was created by Joon Park and Cathy He 5 | /* A crime has taken place and the detective needs your help. The detective gave you the crime scene report, but you somehow lost it. 6 | You vaguely remember that the crime was a ​murder​ that occurred sometime on ​Jan 15, 2018​ and that it took place in ​SQL City​. Start by retrieving the corresponding crime scene report from the police department’s database. 7 | so, my first step to solving the mystery is to retrieve the corresponding crime scene report from the police department’s database. 8 | */ 9 | 10 | select * from [Crime Scene] 11 | where Type = 'murder' and cast(Date as date) ='2018-01-15' and City='sql city'; 12 | 13 | /* I got an information that said Security footage shows that there were 2 witnesses. 14 | The first witness lives at the last house on "Northwestern Dr". and 15 | The second witness, named Annabel, lives somewhere on "Franklin Ave". 16 | */ 17 | /* so i have to look for another table to locate these two addresses where these two witness lives 18 | so now i have retrieved the corresponding person report from the police department’s database.*/ 19 | 20 | select top 1 * 21 | from Person 22 | where Address_Street_Name = 'Northwestern Dr' 23 | order by Address_Number desc; 24 | 25 | /* first witness name is Morty Schapiro . 26 | his license_id is 118009. 27 | his Personel id is 14887. 28 | */ 29 | 30 | select * 31 | from Person 32 | where Address_Street_Name = 'Franklin Ave' 33 | and name like 'Annabel%' 34 | 35 | /* Second witness name is Annabel Miller . 36 | his license_id is 490173. 37 | his Personel id is 16371. 38 | Now we have to find what both had said about the Crime which was happend there */ 39 | 40 | select * 41 | from Interviews 42 | where Person_Id in (16371,14887); 43 | /* Morty Schapiro said I heard a gunshot and then saw a man run out. 44 | He had a "Get Fit Now Gym" bag. The membership number on the bag started with "48Z". 45 | Only gold members have those bags. The man got into a car with a plate that included "H42W". 46 | and Annabel Miller said I saw the murder happen, and I recognized the killer from my gym 47 | when I was working out last week on January the 9th. 48 | */ 49 | select * 50 | from [GetFitNow check in] 51 | where Membership_Id like '48Z%' and Check_In_Date ='2018-01-09' 52 | /* As per Annabel Miller said ,on date jan 9th 2018 she saw the killer face on his gym 53 | and his membership_id starts with '48Z', so i have to look his membership_id from the "GetFitNow check in" table as per clue. 54 | I got two membership_id 48Z7A and 48Z55. So i have to find those two person name from the "GetFitNow members" table. 55 | */ 56 | 57 | 58 | select * 59 | from [GetFitNow members] 60 | where Membership_Status = 'Gold' and Id in('48Z7A' ,'48Z55'); 61 | /* these two people (Joe Germuska and Jeremy Bowers) check-in on jan 9th 2018 as per annabel miller's clue. 62 | Now i have to search Who is the Main Person who's did murder ,so now retrieve the data from the driver license table 63 | try to filter the data by plate number that included "H42W" 64 | */ 65 | 66 | select p.Name,p.License_Id,k.Plate_Number 67 | from Person as p join 68 | (select * from 69 | [Drivers license] 70 | where [Plate_Number] like '%H42W%') as k 71 | on p.License_Id=k.Id; 72 | 73 | -- So we knew that the MasterMind person who did murder is "Jeremy Bowers". -------------------------------------------------------------------------------- /Murder Mystery Case Study/Schema.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmitPatel-analyst/SQL-Case-Study/4dae8f080f537149fb3f2a7233519d9d17903659/Murder Mystery Case Study/Schema.png -------------------------------------------------------------------------------- /OYO business case study/Oyo Business Room Sales Analysis_Sql_18June.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmitPatel-analyst/SQL-Case-Study/4dae8f080f537149fb3f2a7233519d9d17903659/OYO business case study/Oyo Business Room Sales Analysis_Sql_18June.pdf -------------------------------------------------------------------------------- /OYO business case study/Oyo_City.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmitPatel-analyst/SQL-Case-Study/4dae8f080f537149fb3f2a7233519d9d17903659/OYO business case study/Oyo_City.xlsx -------------------------------------------------------------------------------- /OYO business case study/Oyo_Sales.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmitPatel-analyst/SQL-Case-Study/4dae8f080f537149fb3f2a7233519d9d17903659/OYO business case study/Oyo_Sales.xlsx -------------------------------------------------------------------------------- /OYO business case study/Readme.md: -------------------------------------------------------------------------------- 1 | #

**`OYO business`** 2 | #

![Oyo1](https://github.com/AmitPatel-analyst/SQL-Case-Study/assets/120770473/1796bf70-d14d-4295-ae5b-018f0f14fa80) 3 | This repository hosts the solutions for Oyo business room sales analysis. 4 | I used various SQL functions such as : 5 | ✅ Case When Statements 6 | ✅ CTE 7 | ✅ Joins 8 | ✅ Logical Operator 9 | ✅ Group by, Order by, Where 10 | ✅ Aggregate functions Sum, Count, Average 11 | ✅ Date functions Datename, Datediff, Month 12 | 13 | Database Used - Microsoft SQL Server 14 | Insights:- 15 | 16 | 1. Banglore , gurgaon & delhi were popular in the bookings, whereas Kolkata is less popular in bookings 17 | 2. Nature of Bookings: 18 | 19 | • Nearly 50 % of the bookings were made on the day of check in only. 20 | • Nearly 85 % of the bookings were made with less than 4 days prior to the date of check in. 21 | • Very few no.of bookings were made in advance(i.e over a 1 month or 2 months). 22 | • Most of the bookings involved only a single room. 23 | • Nearly 80% of the bookings involved a stay of 1 night only. 24 | 25 | 3. Oyo should acquire more hotels in the cities of Pune, Kolkata & Mumbai. Because their average room rates are comparetively higher so more revenue will come. 26 | 27 | 4. The % cancellation Rate is high on all 9 cities except pune , so Oyo should focus on finding reasons about cancellation. 28 | 29 | *** 30 | ## Case Study Solutions 31 | Click [here](https://github.com/AmitPatel-analyst/SQL-Case-Study/blob/main/OYO%20business%20case%20study/SqlCode_OYO_business.sql) 32 | 33 | ## Order of execution of SQL queries 34 | 35 | ``` 36 | FROM – the database gets the data from tables in FROM clause and if necessary performs the JOINs, 37 | WHERE – the data are filtered with conditions specified in WHERE clause, 38 | GROUP BY – the data are grouped by with conditions specified in WHERE clause, 39 | aggregate functions – the aggregate functions are applied to the groups created in the GROUP BY phase, 40 | HAVING – the groups are filtered with the given condition, 41 | window functions, 42 | SELECT – the database selects the given columns, 43 | DISTINCT – repeated values are removed, 44 | UNION/INTERSECT/EXCEPT – the database applies set operations, 45 | ORDER BY – the results are sorted, 46 | OFFSET – the first rows are skipped, 47 | LIMIT/FETCH/TOP – only the first rows are selected 48 | ``` 49 | -------------------------------------------------------------------------------- /OYO business case study/SqlCode_OYO_business.sql: -------------------------------------------------------------------------------- 1 | -- load the excel data into sql database named CS. 2 | 3 | use CS 4 | go 5 | 6 | 7 | select * from [OYO].[Hotel_Sales]; 8 | 9 | select * from [OYO].[City]; 10 | 11 | -- add new columns 12 | /* 13 | alter table [OYO].[Hotel_Sales] 14 | add Price float null; 15 | 16 | update [OYO].[Hotel_Sales] 17 | set Price = amount + discount; 18 | 19 | alter table [OYO].[Hotel_Sales] 20 | add no_of_nights int null; 21 | 22 | update [OYO].[Hotel_Sales] 23 | set no_of_nights = DATEDIFF(day,check_in,check_out); 24 | 25 | alter table [OYO].[Hotel_Sales] 26 | add rate float null; 27 | 28 | update [OYO].[Hotel_Sales] 29 | set rate = ROUND( case when no_of_rooms = 1 then 30 | Price/no_of_nights 31 | else Price/no_of_nights/no_of_rooms end,2) 32 | */ 33 | 34 | select count(1) [total records] 35 | from OYO.Hotel_Sales; 36 | 37 | select count(1) [no of hotels] 38 | from OYO.City; 39 | 40 | select count(distinct city) [total cities] 41 | from OYO.City; 42 | 43 | -- No of hotels in different cities 44 | 45 | select city, COUNT (hotel_id) [no of hotels] 46 | from OYO.City 47 | group by city 48 | order by 2 desc; 49 | 50 | -- average room rates of different cities 51 | 52 | 53 | select b.city ,ROUND( AVG(a.rate),2) [average room rates] 54 | from OYO.Hotel_Sales as a 55 | inner join OYO.City as b 56 | on a.hotel_id = b.hotel_id 57 | group by b.city 58 | order by 2 desc; 59 | 60 | 61 | -- Cancellation rates of different cities 62 | 63 | select b.city as City, 64 | format(100.0* sum(case when status = 'Cancelled' then 1 else 0 end) 65 | /count(date_of_booking),'f1') [% Cancellation Rate] 66 | from [OYO].[Hotel_Sales] as a 67 | inner join OYO.City as b 68 | on a.hotel_id=b.hotel_id 69 | group by b.city 70 | order by 2 desc; 71 | 72 | -- No of bookings of different cities in Jan Feb Mar Months. 73 | 74 | select b.city [City], datename(month,date_of_booking) [Months], count(date_of_booking) [No of bookings] 75 | from [OYO].[Hotel_Sales] as a 76 | inner join OYO.City as b 77 | on a.hotel_id=b.hotel_id 78 | group by b.city, datepart(month,date_of_booking),datename(month,date_of_booking) 79 | order by 1,datepart(month,date_of_booking) ; 80 | 81 | 82 | select b.city [City], datename(month,date_of_booking) [Months], count(date_of_booking) [No of bookings] 83 | from [OYO].[Hotel_Sales] as a 84 | inner join OYO.City as b 85 | on a.hotel_id=b.hotel_id 86 | group by b.city,datename(month,date_of_booking) 87 | order by 1,2 ; 88 | 89 | 90 | 91 | 92 | -- Frequency of early bookings prior to check-in the hotel 93 | 94 | select DATEDIFF(day,date_of_booking,check_in)[Days before check-in] 95 | , count(1)[Frequency_Early_Bookings_Days] 96 | from OYO.Hotel_Sales 97 | group by DATEDIFF( day,date_of_booking,check_in); 98 | 99 | -- Frequency of bookings of no of rooms in Hotel 100 | 101 | select no_of_rooms, count(1) [frequency_of_bookings] 102 | from oyo.Hotel_Sales 103 | group by no_of_rooms 104 | order by no_of_rooms; 105 | 106 | -- net revenue to company (due to some bookings cancelled) & Gross revenue to company 107 | 108 | select city, sum(amount) [gross revenue] , 109 | sum(case when status in ('No Show' ,'Stayed') then amount end) as [net revenue] 110 | from OYO.Hotel_Sales as a 111 | inner join OYO.City as b 112 | on a.hotel_id = b.hotel_id 113 | group by city 114 | order by 1; 115 | 116 | -- Discount offered by different cities 117 | 118 | select city, format(AVG(100.0*discount/Price),'f1') [% Discount offered] 119 | from OYO.Hotel_Sales as a 120 | inner join OYO.City as b 121 | on a.hotel_id = b.hotel_id 122 | group by city 123 | order by 2; 124 | 125 | --done 126 | --------------------------------------------------------------------------- 127 | 128 | -- NEW CUSTOMERS ON JAN MONTH - 719 129 | -- REPEAT CUSTOMER ON FEB MONTH - 133 130 | -- NEW CUSTOMERS ON feb MONTH - 566 131 | -- total customer on feb month - 699 (566 + 133) 132 | 133 | 134 | with Cust_jan as( 135 | select distinct customer_id 136 | from OYO.Hotel_Sales 137 | where MONTH(date_of_booking) = 1 138 | ) 139 | , repeat_cust_feb as( 140 | select distinct s.customer_id 141 | from OYO.Hotel_Sales as s 142 | inner join Cust_jan as b 143 | on b.customer_id = s.customer_id 144 | where MONTH(date_of_booking) = 2 145 | ) 146 | ,total_Cust_feb as ( 147 | select distinct customer_id 148 | from OYO.Hotel_Sales 149 | where MONTH(date_of_booking) = 2 150 | ) 151 | , new_cust_feb as 152 | ( 153 | select customer_id [new customer in feb] 154 | from total_Cust_feb as a 155 | except select customer_id 156 | from repeat_cust_feb as b 157 | ) 158 | SELECT count(c.[new customer in feb]) [repeat customer in feb] 159 | FROM new_cust_feb as c 160 | ORDER BY 1; 161 | 162 | Insights:- 163 | 164 | 1. Banglore , gurgaon & delhi were popular in the bookings, whereas Kolkata is less popular in bookings 165 | 2. Nature of Bookings: 166 | 167 | • Nearly 50 % of the bookings were made on the day of check in only. 168 | • Nearly 85 % of the bookings were made with less than 4 days prior to the date of check in. 169 | • Very few no.of bookings were made in advance(i.e over a 1 month or 2 months). 170 | • Most of the bookings involved only a single room. 171 | • Nearly 80% of the bookings involved a stay of 1 night only. 172 | 173 | 3. Oyo should acquire more hotels in the cities of Pune, kolkata & Mumbai. Because their average room rates are comparetively higher so more revenue will come. 174 | 175 | 4. The % cancellation Rate is high on all 9 cities except pune ,so Oyo should focus on finding reasons about cancellation. 176 | 177 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SQL-Case-Study 2 | 3 | This Github repo showcases my SQL case studies, demonstrating proficiency in solving diverse business problems. Projects cover finance, healthcare and other sectors, and highlight my ability to analyze complex datasets through SQL queries, data manipulation, and visualization techniques. 4 | -------------------------------------------------------------------------------- /School Case Study/README.md: -------------------------------------------------------------------------------- 1 | ### School Case Study 2 | 3 | 1. Create a DATABASE: SCHOOL 4 | ### TABLES 5 | CREATE THE FOLLOWING THREE TABLES WITH SAME NAMES AND DATA TYPES AS PROVIDED BELOW: 6 | 7 | [Here are the scripts of these tables](https://github.com/AmitPatel-analyst/SQL-Case-Study/blob/main/School%20Case%20Study/School_Queries.sql) 8 | #### CourseMaster 9 | 10 | |Column Name | Data Type | Remarks | 11 | |------------|-------------|---------| 12 | |CID | Integer | Primary Key 13 | |CourseName | Varchar(40)| NOT NULL 14 | |Category | Char(1) | NULL, Basic/Medium/Advanced 15 | |Fee | Smallmoney | NOT NULL; Fee can’t be negative 16 | 17 | #### StudentMaster 18 | 19 | |Column Name |Data Type |Remarks| 20 | |-------------|-----------|-------| 21 | |SID |TinyInt |Primary Key 22 | |StudentName |Varchar(40) |NOT NULL 23 | |Origin |Char(1) |NOT NULL, Local/Foreign 24 | |Type |Char(1) |NOT NULL, UnderGraduate/Graduate 25 | 26 | #### EnrollmentMaster 27 | 28 | |Column Name |Data Type |Remarks| 29 | |-------------|-----------|-------| 30 | |CID |Integer |NOT NULL Foreign Key 31 | |SID |Tinyint |NOT NULL Foreign Key 32 | |DOE |DateTime |NOT NULL 33 | |FWF (Fee Waiver Flag) |Bit |NOT NULL 34 | |Grade |Char(1) |O/A/B/C 35 | 36 | Q.1. List the names of the Students who have not enrolled for Java course. 37 | ```sql 38 | SELECT NAME 39 | FROM StudentMaster 40 | WHERE SID in ( 41 | SELECT DISTINCT SID 42 | FROM EnrollmentMaster 43 | WHERE CID not in ( 44 | SELECT DISTINCT CID 45 | FROM CourseMaster 46 | WHERE CourseName = 'JAVA' 47 | ) 48 | ) 49 | ``` 50 | ![image](https://user-images.githubusercontent.com/120770473/228857023-3d241278-be9a-43ff-9f2b-c5c31432fd66.png) 51 | 52 | Q.2. List the name of the advanced course where the enrollment by foreign students is the highest. 53 | ```sql 54 | with cte 55 | as ( 56 | SELECT CourseName,COUNT(*) AS CNT 57 | FROM CourseMaster AS CM 58 | JOIN EnrollmentMaster AS EM 59 | ON CM.CID=EM.CID 60 | JOIN StudentMaster AS SM 61 | ON SM.SID=EM.SID 62 | WHERE Category='A' AND ORIGIN = 'F' 63 | GROUP BY CourseName 64 | ) 65 | select CourseName 66 | from cte 67 | where CNT = (select max(cnt) from cte) 68 | go 69 | ``` 70 | ![image](https://user-images.githubusercontent.com/120770473/229302096-f2323ad1-f9bd-4858-8420-04307118c1ca.png) 71 | 72 | Q.3. List the names of the Undergraduate, local students who have got a “B” grade in any basic course. 73 | 74 | ```sql 75 | SELECT Name 76 | FROM EnrollmentMaster as em 77 | join StudentMaster as sm 78 | on sm.SID=em.sid 79 | join CourseMaster as cm 80 | on cm.CID=em.cid 81 | WHERE Type = 'U' and Origin = 'L' and Grade = 'B' 82 | ``` 83 | ![image](https://user-images.githubusercontent.com/120770473/229302279-7883c325-e554-4fc5-8f59-b6271f4475de.png) 84 | 85 | Q.4. List the names of the courses for which no student has enrolled in the month of SEPT 2022. 86 | 87 | ```sql 88 | SELECT CM.CID , CM.COURSENAME 89 | FROM COURSEMASTER AS CM 90 | LEFT JOIN ( 91 | SELECT DISTINCT CM.CID , COURSENAME 92 | FROM ENROLLMENTMASTER AS EM 93 | JOIN STUDENTMASTER AS SM 94 | ON SM.SID=EM.SID 95 | JOIN COURSEMASTER AS CM 96 | ON CM.CID=EM.CID 97 | WHERE DATENAME(MM,DOE) = 'SEPTEMBER' AND DATEPART(YY,DOE) = 2022 98 | ) AS NEWT 99 | ON NEWT.CID = CM.CID 100 | WHERE NEWT.CID IS NULL 101 | go 102 | ``` 103 | ![image](https://user-images.githubusercontent.com/120770473/229302409-b939ddc3-c842-454c-9dcc-31fc25a7a2c4.png) 104 | 105 | Q.5. List name, Number of Enrollments and Popularity for all Courses. 106 | Popularity has to be displayed as “High” if number of enrollments is higher than 5, 107 | “Medium” if greater than or equal to 3 and less than or equal to 5, and “Low” if the no. is less than 3. 108 | 109 | ```sql 110 | SELECT [COURSENAME] , COUNT(*) AS #ofenrolls, 111 | CASE 112 | WHEN COUNT(*) >5 THEN 'HIGH' 113 | WHEN COUNT(*) BETWEEN 3 AND 5 THEN 'MEDIUM' 114 | ELSE 'LOW' 115 | END AS POPULARITY 116 | FROM COURSEMASTER AS CM JOIN ENROLLMENTMASTER AS EM ON CM.CID=EM.CID 117 | GROUP BY COURSENAME 118 | go 119 | ``` 120 | ![image](https://user-images.githubusercontent.com/120770473/229302549-67b36348-8688-4a06-8660-7894beeed337.png) 121 | 122 | Q.6. List the names of the Local students who have enrolled for exactly 2 basic courses. 123 | 124 | ```sql 125 | SELECT SM.NAME , COUNT(*) AS CourseEnrolled 126 | FROM STUDENTMASTER AS SM 127 | JOIN ENROLLMENTMASTER AS EM 128 | ON SM.SID=EM.SID 129 | JOIN COURSEMASTER AS CM 130 | ON CM.CID=EM.CID 131 | WHERE ORIGIN = 'L' AND CATEGORY = 'B' 132 | GROUP BY SM.NAME 133 | HAVING COUNT(*) = 2 134 | go 135 | ``` 136 | ![image](https://user-images.githubusercontent.com/120770473/229302706-e95a964e-0c54-4d79-b5e2-ac9f0d51af21.png) 137 | 138 | Q.7. List the names of the Courses enrolled by all (every) students. 139 | 140 | ```sql 141 | SELECT DISTINCT CourseName 142 | FROM CourseMaster AS CM 143 | LEFT JOIN ENROLLMENTMASTER AS EM 144 | ON CM.CID = EM.CID 145 | WHERE EM.CID IS NOT NULL 146 | go 147 | ``` 148 | ![image](https://user-images.githubusercontent.com/120770473/229302846-6c393b3a-39c7-41fc-a839-8a36fdb11da4.png) 149 | 150 | Q.8. For those enrollments for which fee have been waived, when fwf = 1 means yes ,provide the names of students who have got ‘O’ grade. 151 | 152 | ```sql 153 | SELECT NAME as studentname, SID 154 | FROM STUDENTMASTER 155 | WHERE SID IN (SELECT DISTINCT SID 156 | FROM ENROLLMENTMASTER 157 | WHERE FWF = 1 AND GRADE = 'O' 158 | ) 159 | go 160 | ``` 161 | ![image](https://user-images.githubusercontent.com/120770473/229303079-dac9ca63-9f82-44f4-8f11-d9537a9be522.png) 162 | 163 | Q.9. List the names of the foreign, undergraduate students who have got grade ‘O’ in any Advanced course. 164 | 165 | ```sql 166 | SELECT sm.SID , sm.Name as [Student Name] 167 | from StudentMaster as sm 168 | join EnrollmentMaster as em 169 | on sm.SID=em.sid 170 | join CourseMaster as cm 171 | on cm.CID=em.cid 172 | where Origin='F' and Type = 'U' and grade = 'o' and Category = 'A' 173 | go 174 | ``` 175 | ![image](https://user-images.githubusercontent.com/120770473/229303299-e68c13c7-49a0-4778-84c7-f78074a4a663.png) 176 | 177 | Q.10. List the course name, total no. of enrollments in the month of April 2021. 178 | 179 | ```sql 180 | SELECT COURSENAME , #ofenrolls 181 | FROM COURSEMASTER AS CM 182 | JOIN 183 | (SELECT CID, COUNT(*) AS #ofenrolls 184 | FROM ENROLLMENTMASTER 185 | WHERE MONTH(DOE) =4 AND YEAR(DOE) = 2021 186 | GROUP BY CID) AS K 187 | ON K.CID=CM.CID 188 | order by CourseName 189 | go 190 | ``` 191 | ![image](https://user-images.githubusercontent.com/120770473/229303691-cf27b9a1-9fa9-4efe-bb0e-b3c0872b715f.png) 192 | 193 | 194 | Click [Here](https://github.com/AmitPatel-analyst/SQL-Case-Study/tree/main) to move back to my another case study repositories! 195 | -------------------------------------------------------------------------------- /School Case Study/School_Queries.sql: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | Name : Code for School DB 3 | Author : Amit Patel 4 | 5 | Purpose: This script will create Db and few table in it to store info 6 | about school 7 | 8 | **********************************************************************/ 9 | 10 | --DB 11 | create database School 12 | go 13 | use School 14 | go 15 | --CourseMaster 16 | create table CourseMaster 17 | ( 18 | CID INT primary key, 19 | CourseName Varchar(40) not null, 20 | Category char(1) null check (Category = 'B' or Category = 'M' or Category = 'A'), 21 | Fee smallmoney not null check (Fee > 0) 22 | 23 | ) 24 | go 25 | --schema of table 26 | sp_help CourseMaster 27 | go 28 | 29 | 30 | 31 | -- insert the data 32 | insert into CourseMaster values(10,'Java','B', 5000) 33 | insert into CourseMaster values(20,'Adv Java','A', 25000) 34 | insert into CourseMaster values(30,'Big Data','A', 40000) 35 | insert into CourseMaster values(40,'Sql Server','M', 20000) 36 | 37 | insert into CourseMaster values(50,'Oracle','M', 15000) 38 | insert into CourseMaster values(60,'Phthon','M', 15000) 39 | insert into CourseMaster values(70,'MSBI','A', 35000) 40 | insert into CourseMaster values(80,'Data Science','A', 90000) 41 | 42 | insert into CourseMaster values(90,'Data Analyst','A', 120000) 43 | insert into CourseMaster values(100,'Machine Learning','A', 125000) 44 | insert into CourseMaster values(110,'Basic C++','B', 10000) 45 | insert into CourseMaster values(120,'Intermediate C++','M', 15000) 46 | insert into CourseMaster values(130,'Dual C & c++','M', 20000) 47 | insert into CourseMaster values(140,'Azure','B', 35000) 48 | insert into CourseMaster values(150,'Microsoft Office Intermediate','B', 22000) 49 | 50 | select * from CourseMaster 51 | go -- TOTAL 15 RECORDS 52 | 53 | -- StudentMaster Table create 54 | create table StudentMaster 55 | ( 56 | SID TINYINT PRIMARY KEY, 57 | Name Varchar(40) not null, 58 | Origin Char(1) not null check (Origin = 'L' or Origin = 'F'), 59 | Type Char(1) not null check (Type = 'U' or Type = 'G') 60 | ) 61 | go 62 | 63 | 64 | --insert data 65 | insert into StudentMaster values(1, 'Bilen Haile','F','G') 66 | insert into StudentMaster values(2, 'Durga Prasad','L','U') 67 | insert into StudentMaster values(3, 'Geni','F','U') 68 | insert into StudentMaster values(4, 'Gopi Krishna','L','G') 69 | insert into StudentMaster values(5, 'Hemanth','L','G') 70 | insert into StudentMaster values(6, 'K Nitish','L','G') 71 | insert into StudentMaster values(7, 'Amit','L','G') 72 | insert into StudentMaster values(8, 'Aman','L','U') 73 | insert into StudentMaster values(9, 'Halen','F','G') 74 | insert into StudentMaster values(10, 'John','F','U') 75 | 76 | insert into StudentMaster values(11, 'Anil','L','U') 77 | insert into StudentMaster values(12, 'Mike','F','G') 78 | insert into StudentMaster values(13, 'Suman','L','U') 79 | insert into StudentMaster values(14, 'Angelina','F','G') 80 | insert into StudentMaster values(15, 'Bhavik','L','U') 81 | insert into StudentMaster values(16, 'Bob Tyson','F','G') 82 | insert into StudentMaster values(17, 'Salman','L','U') 83 | insert into StudentMaster values(18, 'Selina','F','G') 84 | insert into StudentMaster values(19, 'Rajkummar','L','U') 85 | insert into StudentMaster values(20, 'Pooja','L','U') 86 | 87 | 88 | --Read the table data 89 | select * from StudentMaster 90 | go -- TOTAL 20 RECORDS 91 | 92 | -- EnrollmentMaster Table 93 | create table EnrollmentMaster 94 | ( 95 | CID INT not null foreign key References CourseMaster(CID), 96 | SID TINYINT not null foreign key References StudentMaster(SID), 97 | DOE Datetime not null, 98 | FWF Bit not null, 99 | Grade Char(1) null check(Grade ='O' or Grade ='A' or Grade ='B' or Grade ='C') 100 | 101 | ) 102 | go 103 | 104 | 105 | --insert data 106 | insert into EnrollmentMaster values(40,1,'2020/11/19',0 ,'O') 107 | insert into EnrollmentMaster values(70,1,'2020/11/21',0 ,'O') 108 | insert into EnrollmentMaster values(30,2,'2020/11/22',1 ,'A') 109 | insert into EnrollmentMaster values(60,4,'2020/11/25',1 ,'O') 110 | insert into EnrollmentMaster values(40,5,'2020/12/2',1 ,'C') 111 | insert into EnrollmentMaster values(50,7,'2020/12/5',0 ,'B') 112 | insert into EnrollmentMaster values(50,4,'2020/12/10',0 ,'A') 113 | insert into EnrollmentMaster values(80,3,'2020/11/11',1 ,'O') 114 | insert into EnrollmentMaster values(80,4,'2020/12/22',0 ,'B') 115 | insert into EnrollmentMaster values(70,6,'2020/12/25',0 ,'A') 116 | insert into EnrollmentMaster values(60,7,'2021/1/1',0 ,'A') 117 | insert into EnrollmentMaster values(40,8,'2021/1/2',1 ,'O') 118 | insert into EnrollmentMaster values(80,9,'2021/1/3',0 ,'B') 119 | insert into EnrollmentMaster values(20,4,'2021/1/4',0 ,'A') 120 | insert into EnrollmentMaster values(40,9,'2021/4/1',1 ,'O') 121 | insert into EnrollmentMaster values(90,4,'2021/4/5',0 ,'A') 122 | insert into EnrollmentMaster values(30,11,'2021/4/8',0 ,'A') 123 | insert into EnrollmentMaster values(110,11,'2021/4/11',1 ,'B') 124 | insert into EnrollmentMaster values(30,18,'2021/4/12',1 ,'A') 125 | insert into EnrollmentMaster values(130,12,'2021/4/13',0 ,'B') 126 | insert into EnrollmentMaster values(40,10,'2021/4/18',1 ,'O') 127 | insert into EnrollmentMaster values(150,12,'2021/4/22',1 ,'A') 128 | insert into EnrollmentMaster values(70,17,'2021/4/25',0 ,'B') 129 | insert into EnrollmentMaster values(120,1,'2021/4/30',0 ,'O') 130 | insert into EnrollmentMaster values(90,8,'2021/5/02',0 ,'A') 131 | insert into EnrollmentMaster values(100,18,'2021/5/05',0 ,'B') 132 | insert into EnrollmentMaster values(90,10,'2021/5/12',1 ,'O') 133 | insert into EnrollmentMaster values(110,15,'2021/5/15',0 ,'B') 134 | insert into EnrollmentMaster values(120,5,'2021/5/20',1 ,'A') 135 | insert into EnrollmentMaster values(130,6,'2021/5/25',1 ,'O') 136 | insert into EnrollmentMaster values(140,15,'2021/5/28',0 ,'A') 137 | insert into EnrollmentMaster values(120,6,'2021/5/31',0 ,'B') 138 | insert into EnrollmentMaster values(150,5,'2021/6/12',1 ,'A') 139 | insert into EnrollmentMaster values(80,8,'2021/6/15',1 ,'B') 140 | insert into EnrollmentMaster values(140,14,'2021/6/20',0 ,'O') 141 | insert into EnrollmentMaster values(90,3,'2021/6/23',1 ,'O') 142 | insert into EnrollmentMaster values(100,3,'2021/7/02',0 ,'A') 143 | insert into EnrollmentMaster values(40,13,'2021/7/22',0 ,'B') 144 | go 145 | 146 | --Read the table data 147 | select * from EnrollmentMaster 148 | go -- TOTAL 38 RECORDS 149 | 150 | UPDATE EnrollmentMaster 151 | SET DOE='2022-09-12' 152 | WHERE DOE >'2021-05-01' 153 | 154 | -------------------------------------------------------------------------------- /Sql_case_studies(Data In Motion, LLC)/01Sql_Challenge(The Tiny Shop)/PostgreSqlcode.sql: -------------------------------------------------------------------------------- 1 | -- Date : 27-May-2023 2 | -- Public schema 3 | create schema public; 4 | -- Customers table 5 | CREATE TABLE customers ( 6 | customer_id integer PRIMARY KEY, 7 | first_name varchar(100), 8 | last_name varchar(100), 9 | email varchar(100) 10 | ); 11 | -- Products Table 12 | CREATE TABLE products ( 13 | product_id integer PRIMARY KEY, 14 | product_name varchar(100), 15 | price decimal 16 | ); 17 | 18 | -- Orders Table 19 | CREATE TABLE orders ( 20 | order_id integer PRIMARY KEY, 21 | customer_id integer, 22 | order_date date 23 | ); 24 | 25 | -- Order Items Table 26 | CREATE TABLE order_items ( 27 | order_id integer, 28 | product_id integer, 29 | quantity integer 30 | ); 31 | 32 | -- Customers Table 33 | INSERT INTO customers (customer_id, first_name, last_name, email) VALUES 34 | (1, 'John', 'Doe', 'johndoe@email.com'), 35 | (2, 'Jane', 'Smith', 'janesmith@email.com'), 36 | (3, 'Bob', 'Johnson', 'bobjohnson@email.com'), 37 | (4, 'Alice', 'Brown', 'alicebrown@email.com'), 38 | (5, 'Charlie', 'Davis', 'charliedavis@email.com'), 39 | (6, 'Eva', 'Fisher', 'evafisher@email.com'), 40 | (7, 'George', 'Harris', 'georgeharris@email.com'), 41 | (8, 'Ivy', 'Jones', 'ivyjones@email.com'), 42 | (9, 'Kevin', 'Miller', 'kevinmiller@email.com'), 43 | (10, 'Lily', 'Nelson', 'lilynelson@email.com'), 44 | (11, 'Oliver', 'Patterson', 'oliverpatterson@email.com'), 45 | (12, 'Quinn', 'Roberts', 'quinnroberts@email.com'), 46 | (13, 'Sophia', 'Thomas', 'sophiathomas@email.com'); 47 | 48 | -- Products Table 49 | INSERT INTO products (product_id, product_name, price) VALUES 50 | (1, 'Product A', 10.00), 51 | (2, 'Product B', 15.00), 52 | (3, 'Product C', 20.00), 53 | (4, 'Product D', 25.00), 54 | (5, 'Product E', 30.00), 55 | (6, 'Product F', 35.00), 56 | (7, 'Product G', 40.00), 57 | (8, 'Product H', 45.00), 58 | (9, 'Product I', 50.00), 59 | (10, 'Product J', 55.00), 60 | (11, 'Product K', 60.00), 61 | (12, 'Product L', 65.00), 62 | (13, 'Product M', 70.00); 63 | 64 | -- Orders Table 65 | INSERT INTO orders (order_id, customer_id, order_date) VALUES 66 | (1, 1, '2023-05-01'), 67 | (2, 2, '2023-05-02'), 68 | (3, 3, '2023-05-03'), 69 | (4, 1, '2023-05-04'), 70 | (5, 2, '2023-05-05'), 71 | (6, 3, '2023-05-06'), 72 | (7, 4, '2023-05-07'), 73 | (8, 5, '2023-05-08'), 74 | (9, 6, '2023-05-09'), 75 | (10, 7, '2023-05-10'), 76 | (11, 8, '2023-05-11'), 77 | (12, 9, '2023-05-12'), 78 | (13, 10, '2023-05-13'), 79 | (14, 11, '2023-05-14'), 80 | (15, 12, '2023-05-15'), 81 | (16, 13, '2023-05-16'); 82 | 83 | -- Order Items 84 | INSERT INTO order_items (order_id, product_id, quantity) VALUES 85 | (1, 1, 2), 86 | (1, 2, 1), 87 | (2, 2, 1), 88 | (2, 3, 3), 89 | (3, 1, 1), 90 | (3, 3, 2), 91 | (4, 2, 4), 92 | (4, 3, 1), 93 | (5, 1, 1), 94 | (5, 3, 2), 95 | (6, 2, 3), 96 | (6, 1, 1), 97 | (7, 4, 1), 98 | (7, 5, 2), 99 | (8, 6, 3), 100 | (8, 7, 1), 101 | (9, 8, 2), 102 | (9, 9, 1), 103 | (10, 10, 3), 104 | (10, 11, 2), 105 | (11, 12, 1), 106 | (11, 13, 3), 107 | (12, 4, 2), 108 | (12, 5, 1), 109 | (13, 6, 3), 110 | (13, 7, 2), 111 | (14, 8, 1), 112 | (14, 9, 2), 113 | (15, 10, 3), 114 | (15, 11, 1), 115 | (16, 12, 2), 116 | (16, 13, 3); 117 | 118 | 119 | select * from orders; 120 | select * from customers; 121 | select * from order_items; 122 | select * from products; 123 | 124 | --Questions: 125 | 126 | 1) Which product has the highest price? Only return a single row 127 | 128 | select product_name,price 129 | from products 130 | order by price desc 131 | limit 1; 132 | 133 | 2) Which customer has made the most orders? 134 | 135 | select c.first_name ||' '|| c.last_name as fullname, count(o.order_id) as most_orders 136 | from customers c 137 | join orders o 138 | on c.customer_id = o.customer_id 139 | group by c.first_name, c.last_name 140 | having count(o.order_id) > 1; 141 | 142 | 3) What’s the total revenue per product? 143 | 144 | select p.product_id,sum(p.price*o.quantity) as total_revenue_per_product 145 | from products p 146 | inner join order_items o 147 | on p.product_id = o.product_id 148 | group by p.product_id 149 | order by 1; 150 | 151 | 4) Find the day with the highest revenue. 152 | 153 | with cte as 154 | ( 155 | select o.order_id,p.product_id ,sum(p.price*o.quantity) as total_revenue_per_product 156 | from products p 157 | inner join order_items o 158 | on p.product_id = o.product_id 159 | group by o.order_id,p.product_id 160 | order by o.order_id 161 | ),cte_a as 162 | (select order_id, sum(total_revenue_per_product) as total_revenue from cte 163 | group by order_id 164 | order by 2 desc 165 | ) 166 | select B.order_date,A.total_revenue from cte_a as A 167 | inner join orders B on A.order_id = B.order_id 168 | order by 2 desc 169 | limit 1; 170 | 171 | 172 | 5) Find the first order (by date) for each customer. 173 | 174 | with cte_a as 175 | ( 176 | select distinct customer_id as CId, 177 | first_value(Order_date) over(partition by customer_id order by order_date)as first_order 178 | from orders 179 | order by customer_id 180 | ) 181 | select first_order , first_name||' '||last_name as customer_name 182 | from cte_a as A 183 | inner join customers as B 184 | on A.CId = B.customer_id; 185 | 186 | 187 | 6) Find the top 3 customers who have ordered the most distinct products 188 | 189 | select c.customer_id, 190 | c.first_name || ' '|| c.last_name as customer_name, 191 | count( distinct oi.product_id) as distinct_product 192 | from customers c 193 | inner join orders o 194 | on c.customer_id = o.customer_id 195 | inner join order_items oi 196 | on oi.order_id = o.order_id 197 | group by 1,2 198 | order by 3 desc 199 | limit 3; 200 | 201 | 7) Which product has been bought the least in terms of quantity? 202 | 203 | select product_id,sum(quantity) as least_ordered 204 | from order_items 205 | group by product_id 206 | order by 2 207 | limit 1; 208 | 209 | 8 ) What is the median order total? 210 | 211 | with order_total as( 212 | select o.order_id, 213 | sum(oi.quantity * p.price) as total 214 | from orders o 215 | inner join order_items oi 216 | on o.order_id=oi.order_id 217 | inner join products p 218 | on p.product_id= oi.product_id 219 | group by o.order_id 220 | ) 221 | select percentile_cont(0.5) within group (order by total) 222 | as median_order_total 223 | from order_total; 224 | 225 | 9) For each order, determine if it was ‘Expensive’ (total over 300), ‘Affordable’ (total over 100), or ‘Cheap’. 226 | 227 | select o.order_id, 228 | sum(o.quantity * p.price) as total , 229 | case when sum(o.quantity * p.price)>300 then 'Expensive' 230 | when sum(o.quantity * p.price)>100 then 'Affordable' 231 | else 'Cheap' end as purchase_type 232 | from order_items o 233 | inner join products p 234 | on p.product_id=o.product_id 235 | group by o.order_id; 236 | 237 | 238 | 10) Find customers who have ordered the product with the highest price. 239 | 240 | select c.customer_id , c.first_name || ' ' || c.last_name as customer_name , p.price as highest_price 241 | from products as p 242 | inner join order_items as r on p.product_id = r.product_id 243 | inner join orders as o on o.order_id = r.order_id 244 | inner join customers as c on c.customer_id = o.customer_id 245 | where p.price = (select max(price) from products) 246 | ; 247 | -------------------------------------------------------------------------------- /Sql_case_studies(Data In Motion, LLC)/01Sql_Challenge(The Tiny Shop)/Readme.md: -------------------------------------------------------------------------------- 1 | #

**`The_Tiny_Shop`** 2 | #

![Tiny-Shop-Sales-600x600](https://github-production-user-asset-6210df.s3.amazonaws.com/22597020/241724547-8ddaea04-1e46-4d07-a712-bb6acc962560.png) 3 | --- 4 | This repository hosts the solutions to the Weekly SQL case study by Data In Motion. Click [here](https://d-i-motion.com/lessons/customer-orders-analysis/) to view the full challenge 5 | 6 | This case study originally uses PostgreSQL. To successfully answer all the questions you should have been exposed to the following areas of SQL: 7 | 8 | . Basic aggregations 9 | . CASE WHEN statements 10 | . Window Functions 11 | . Joins 12 | . Date time functions 13 | . Median - PERCENTILE_CONT 14 | . CTEs 15 | 16 | *** 17 | ## Case Study Solutions 18 | Click [here](https://github.com/AmitPatel-analyst/SQL-Case-Study/blob/main/Sql_case_studies(Data%20In%20Motion%2C%20LLC)/01Sql_Challenge(The%20Tiny%20Shop)/PostgreSqlcode.sql) 19 | -------------------------------------------------------------------------------- /Sql_case_studies(Data In Motion, LLC)/02Sql_Challenge(Human Resources)/PostgreSqlCode.sql: -------------------------------------------------------------------------------- 1 | Date : 12-June-2023 2 | 3 | -- SQL Challenge Questions: 4 | --1. Find the longest ongoung project for each department. 5 | --2. Find all employees who are not managers. 6 | --3. Find all employees who have been hired after the start of a project in their department. 7 | --4. Rank employees within each department based on their hire date ( earliest hire gets the highest rank). 8 | --5. Find the duration between the hire date of each employee and the hire date of the next employee. 9 | 10 | Solution 1 :- 11 | select department_id, 12 | end_date - start_date as longestperiod_indays 13 | from projects; 14 | 15 | Solution 2 :- 16 | select * 17 | from employees 18 | where job_title not like '%Manager%'; 19 | 20 | Solution 3 :- 21 | select e.id,e.name,e.hire_date,p.start_date as Project_start_date,e.job_title, 22 | e.department_id 23 | from employees as e 24 | inner join departments as d 25 | on d.id= e.department_id 26 | inner join projects as p 27 | on p.department_id = d.id 28 | where hire_date > start_date; 29 | 30 | Solution 4 :- 31 | select department_id , id as emp_id, name as emp_name, hire_date, 32 | rank() over(partition by department_id order by hire_date) as rank_employees 33 | from employees; 34 | 35 | Solution 5 :- 36 | With cte as( 37 | select row_number() over(partition by department_id order by hire_date) as rn , 38 | department_id, hire_date , name, 39 | coalesce(hire_date - lag(hire_date) over(partition by department_id order by hire_date ), 0) as duration_indays 40 | from employees 41 | ) 42 | select department_id , duration_indays 43 | from cte 44 | where duration_indays > 0; 45 | -------------------------------------------------------------------------------- /Sql_case_studies(Data In Motion, LLC)/02Sql_Challenge(Human Resources)/Readme.md: -------------------------------------------------------------------------------- 1 | #

**`Human Resources`** 2 | 3 | #

![custom](https://d-i-motion.com/wp-content/uploads/2023/05/Kedeishas-Banking-Services-1-1-600x600.png) 4 | --- 5 | This repository hosts the solutions to the Weekly SQL case study by Data In Motion. Click [here](https://d-i-motion.com/lessons/kedeishas-banking-services/) to view the full challenge 6 | 7 | This case study originally uses PostgreSQL. To successfully answer all the questions you should have been exposed to the following areas of SQL: 8 | 9 | . Basic aggregations 10 | . CASE WHEN statements 11 | . Window Functions 12 | . Joins 13 | . Date time functions 14 | . Coalesce function 15 | . CTEs 16 | *** 17 | ## Case Study Solutions 18 | Click [here](https://github.com/AmitPatel-analyst/SQL-Case-Study/blob/main/Sql_case_studies(Data%20In%20Motion%2C%20LLC)/02Sql_Challenge(Human%20Resources)/PostgreSqlCode.sql) 19 | -------------------------------------------------------------------------------- /Sql_case_studies(Steel data challenge)/01Sql_challenge (Steve's Car Showroom)/01Sql_challenge (Steve's Car Showroom).jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmitPatel-analyst/SQL-Case-Study/4dae8f080f537149fb3f2a7233519d9d17903659/Sql_case_studies(Steel data challenge)/01Sql_challenge (Steve's Car Showroom)/01Sql_challenge (Steve's Car Showroom).jpeg -------------------------------------------------------------------------------- /Sql_case_studies(Steel data challenge)/01Sql_challenge (Steve's Car Showroom)/Schema.sql: -------------------------------------------------------------------------------- 1 | /***************************************************************************************** 2 | Name : Code for Steel_Data DB 3 | Author : Amit Patel 4 | Date : 12-May-2023 5 | 6 | Purpose: This script will create Db and few table in it to store info about Steve's Car Showroom 7 | *****************************************************************************************/ 8 | create database Steel_Data 9 | go 10 | 11 | USE Steel_Data 12 | GO 13 | ---create table cars 14 | CREATE TABLE cars ( 15 | car_id INT PRIMARY KEY, 16 | make VARCHAR(50), 17 | type VARCHAR(50), 18 | style VARCHAR(50), 19 | cost_$ INT 20 | ); 21 | 22 | --create table salespersons 23 | CREATE TABLE salespersons ( 24 | salesman_id INT PRIMARY KEY, 25 | name VARCHAR(50), 26 | age INT, 27 | city VARCHAR(50) 28 | ); 29 | 30 | --create table sales 31 | CREATE TABLE sales ( 32 | sale_id INT PRIMARY KEY, 33 | car_id INT, 34 | salesman_id INT, 35 | purchase_date DATE, 36 | FOREIGN KEY (car_id) REFERENCES cars(car_id), 37 | FOREIGN KEY (salesman_id) REFERENCES salespersons(salesman_id) 38 | ); 39 | 40 | --insert data 41 | INSERT INTO cars (car_id, make, type, style, cost_$) 42 | VALUES (1, 'Honda', 'Civic', 'Sedan', 30000), 43 | (2, 'Toyota', 'Corolla', 'Hatchback', 25000), 44 | (3, 'Ford', 'Explorer', 'SUV', 40000), 45 | (4, 'Chevrolet', 'Camaro', 'Coupe', 36000), 46 | (5, 'BMW', 'X5', 'SUV', 55000), 47 | (6, 'Audi', 'A4', 'Sedan', 48000), 48 | (7, 'Mercedes', 'C-Class', 'Coupe', 60000), 49 | (8, 'Nissan', 'Altima', 'Sedan', 26000); 50 | -------------------- 51 | INSERT INTO salespersons (salesman_id, name, age, city) 52 | VALUES (1, 'John Smith', 28, 'New York'), 53 | (2, 'Emily Wong', 35, 'San Fran'), 54 | (3, 'Tom Lee', 42, 'Seattle'), 55 | (4, 'Lucy Chen', 31, 'LA'); 56 | -------------------- 57 | INSERT INTO sales (sale_id, car_id, salesman_id, purchase_date) 58 | VALUES (1, 1, 1, '2021-01-01'), 59 | (2, 3, 3, '2021-02-03'), 60 | (3, 2, 2, '2021-02-10'), 61 | (4, 5, 4, '2021-03-01'), 62 | (5, 8, 1, '2021-04-02'), 63 | (6, 2, 1, '2021-05-05'), 64 | (7, 4, 2, '2021-06-07'), 65 | (8, 5, 3, '2021-07-09'), 66 | (9, 2, 4, '2022-01-01'), 67 | (10, 1, 3, '2022-02-03'), 68 | (11, 8, 2, '2022-02-10'), 69 | (12, 7, 2, '2022-03-01'), 70 | (13, 5, 3, '2022-04-02'), 71 | (14, 3, 1, '2022-05-05'), 72 | (15, 5, 4, '2022-06-07'), 73 | (16, 1, 2, '2022-07-09'), 74 | (17, 2, 3, '2023-01-01'), 75 | (18, 6, 3, '2023-02-03'), 76 | (19, 7, 1, '2023-02-10'), 77 | (20, 4, 4, '2023-03-01'); 78 | 79 | -------------------------------------------------------------------------------- /Sql_case_studies(Steel data challenge)/02Sql_challenge (Esports Tournament)/02Sql_challenge (Esports Tournament).jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmitPatel-analyst/SQL-Case-Study/4dae8f080f537149fb3f2a7233519d9d17903659/Sql_case_studies(Steel data challenge)/02Sql_challenge (Esports Tournament)/02Sql_challenge (Esports Tournament).jpeg -------------------------------------------------------------------------------- /Sql_case_studies(Steel data challenge)/02Sql_challenge (Esports Tournament)/Schema.sql: -------------------------------------------------------------------------------- 1 | /***************************************************************************************** 2 | Name : Code for Steel_Data DB 3 | Author : Amit Patel 4 | Date : 12-May-2023 5 | 6 | Purpose: This script will create Db and few table in it to store info about Esports Tournament 7 | *****************************************************************************************/ 8 | create database Steel_Data 9 | go 10 | 11 | USE Steel_Data 12 | GO 13 | ---create table Teams 14 | CREATE TABLE Teams ( 15 | team_id INT PRIMARY KEY, 16 | team_name VARCHAR(50) NOT NULL, 17 | country VARCHAR(50), 18 | captain_id INT 19 | ); 20 | ---create table Players 21 | CREATE TABLE Players ( 22 | player_id INT PRIMARY KEY, 23 | player_name VARCHAR(50) NOT NULL, 24 | team_id INT, 25 | role VARCHAR(50), 26 | salary INT, 27 | FOREIGN KEY (team_id) REFERENCES Teams(team_id) 28 | ); 29 | ---create table Matches 30 | CREATE TABLE Matches ( 31 | match_id INT PRIMARY KEY, 32 | team1_id INT, 33 | team2_id INT, 34 | match_date DATE, 35 | winner_id INT, 36 | score_team1 INT, 37 | score_team2 INT, 38 | FOREIGN KEY (team1_id) REFERENCES Teams(team_id), 39 | FOREIGN KEY (team2_id) REFERENCES Teams(team_id), 40 | FOREIGN KEY (winner_id) REFERENCES Teams(team_id) 41 | ); 42 | 43 | --insert data 44 | INSERT INTO Teams (team_id, team_name, country, captain_id) 45 | VALUES (1, 'Cloud9', 'USA', 1), 46 | (2, 'Fnatic', 'Sweden', 2), 47 | (3, 'SK Telecom T1', 'South Korea', 3), 48 | (4, 'Team Liquid', 'USA', 4), 49 | (5, 'G2 Esports', 'Spain', 5); 50 | -------------------- 51 | INSERT INTO Players (player_id, player_name, team_id, role, salary) 52 | VALUES (1, 'Shroud', 1, 'Rifler', 100000), 53 | (2, 'JW', 2, 'AWP', 90000), 54 | (3, 'Faker', 3, 'Mid laner', 120000), 55 | (4, 'Stewie2k', 4, 'Rifler', 95000), 56 | (5, 'Perkz', 5, 'Mid laner', 110000), 57 | (6, 'Castle09', 1, 'AWP', 120000), 58 | (7, 'Pike', 2, 'Mid Laner', 70000), 59 | (8, 'Daron', 3, 'Rifler', 125000), 60 | (9, 'Felix', 4, 'Mid Laner', 95000), 61 | (10, 'Stadz', 5, 'Rifler', 98000), 62 | (11, 'KL34', 1, 'Mid Laner', 83000), 63 | (12, 'ForceZ', 2, 'Rifler', 130000), 64 | (13, 'Joker', 3, 'AWP', 128000), 65 | (14, 'Hari', 4, 'AWP', 90000), 66 | (15, 'Wringer', 5, 'Mid laner', 105000); 67 | -------------------- 68 | INSERT INTO Matches (match_id, team1_id, team2_id, match_date, winner_id, score_team1, score_team2) 69 | VALUES (1, 1, 2, '2022-01-01', 1, 16, 14), 70 | (2, 3, 5, '2022-02-01', 3, 14, 9), 71 | (3, 4, 1, '2022-03-01', 1, 17, 13), 72 | (4, 2, 5, '2022-04-01', 5, 13, 12), 73 | (5, 3, 4, '2022-05-01', 3, 16, 10), 74 | (6, 1, 3, '2022-02-01', 3, 13, 17), 75 | (7, 2, 4, '2022-03-01', 2, 12, 9), 76 | (8, 5, 1, '2022-04-01', 1, 11, 15), 77 | (9, 2, 3, '2022-05-01', 3, 9, 10), 78 | (10, 4, 5, '2022-01-01', 4, 13, 10); 79 | -------------------------------------------------------------------------------- /Sql_case_studies(Steel data challenge)/03Sql_challenge (Customer Insights)/03Sql_challenge (Customer Insights).jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmitPatel-analyst/SQL-Case-Study/4dae8f080f537149fb3f2a7233519d9d17903659/Sql_case_studies(Steel data challenge)/03Sql_challenge (Customer Insights)/03Sql_challenge (Customer Insights).jpeg -------------------------------------------------------------------------------- /Sql_case_studies(Steel data challenge)/03Sql_challenge (Customer Insights)/Schema.sql: -------------------------------------------------------------------------------- 1 | /***************************************************************************************** 2 | Name : Code for Steel_Data DB 3 | Author : Amit Patel 4 | Date : 12-May-2023 5 | 6 | Purpose: This script will create Db and few table in it to store info about Customer Insights 7 | *****************************************************************************************/ 8 | create database Steel_Data 9 | go 10 | 11 | USE Steel_Data 12 | GO 13 | ---create table country 14 | CREATE TABLE country ( 15 | country_id INT PRIMARY KEY, 16 | country_name VARCHAR(50), 17 | head_office VARCHAR(50) 18 | ); 19 | ---create table customers 20 | CREATE TABLE customers ( 21 | customer_id INT PRIMARY KEY, 22 | first_shop DATE, 23 | age INT, 24 | rewards VARCHAR(50), 25 | can_email VARCHAR(50) 26 | ); 27 | ---create table orders 28 | CREATE TABLE orders ( 29 | order_id INT PRIMARY KEY, 30 | customer_id INT, 31 | date_shop DATE, 32 | sales_channel VARCHAR(50), 33 | country_id INT, 34 | FOREIGN KEY (customer_id) REFERENCES customers(customer_id), 35 | FOREIGN KEY (country_id) REFERENCES country(country_id) 36 | ); 37 | ---create table products 38 | CREATE TABLE products ( 39 | product_id INT PRIMARY KEY, 40 | category VARCHAR(50), 41 | price NUMERIC(5,2) 42 | ); 43 | ---create table baskets 44 | CREATE TABLE baskets ( 45 | order_id INT, 46 | product_id INT, 47 | FOREIGN KEY (order_id) REFERENCES orders(order_id), 48 | FOREIGN KEY (product_id) REFERENCES products(product_id) 49 | ); 50 | 51 | --insert data 52 | INSERT INTO country (country_id, country_name, head_office) 53 | VALUES (1, 'UK', 'London'), 54 | (2, 'USA', 'New York'), 55 | (3, 'China', 'Beijing'); 56 | -------------------- 57 | INSERT INTO customers (customer_id, first_shop, age, rewards, can_email) 58 | VALUES (1, '2022-03-20', 23, 'yes', 'no'), 59 | (2, '2022-03-25', 26, 'no', 'no'), 60 | (3, '2022-04-06', 32, 'no', 'no'), 61 | (4, '2022-04-13', 25, 'yes', 'yes'), 62 | (5, '2022-04-22', 49, 'yes', 'yes'), 63 | (6, '2022-06-18', 28, 'yes', 'no'), 64 | (7, '2022-06-30', 36, 'no', 'no'), 65 | (8, '2022-07-04', 37, 'yes', 'yes'); 66 | -------------------- 67 | INSERT INTO orders (order_id, customer_id, date_shop, sales_channel, country_id) 68 | VALUES (1, 1, '2023-01-16', 'retail', 1), 69 | (2, 4, '2023-01-20', 'retail', 1), 70 | (3, 2, '2023-01-25', 'retail', 2), 71 | (4, 3, '2023-01-25', 'online', 1), 72 | (5, 1, '2023-01-28', 'retail', 3), 73 | (6, 5, '2023-02-02', 'online', 1), 74 | (7, 6, '2023-02-05', 'retail', 1), 75 | (8, 3, '2023-02-11', 'online', 3); 76 | -------------------- 77 | INSERT INTO products (product_id, category, price) 78 | VALUES (1, 'food', 5.99), 79 | (2, 'sports', 12.49), 80 | (3, 'vitamins', 6.99), 81 | (4, 'food', 0.89), 82 | (5, 'vitamins', 15.99); 83 | -------------------- 84 | INSERT INTO baskets (order_id, product_id) 85 | VALUES (1, 1), 86 | (1, 2), 87 | (1, 5), 88 | (2, 4), 89 | (3, 3), 90 | (4, 2), 91 | (4, 1), 92 | (5, 3), 93 | (5, 5), 94 | (6, 4), 95 | (6, 3), 96 | (6, 1), 97 | (7, 2), 98 | (7, 1), 99 | (8, 3), 100 | (8, 3); 101 | -------------------------------------------------------------------------------- /Sql_case_studies(Steel data challenge)/04Sql_challenge (Finance Analysis)/04Sql_challenge (Finance Analysis)Part 1.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmitPatel-analyst/SQL-Case-Study/4dae8f080f537149fb3f2a7233519d9d17903659/Sql_case_studies(Steel data challenge)/04Sql_challenge (Finance Analysis)/04Sql_challenge (Finance Analysis)Part 1.jpeg -------------------------------------------------------------------------------- /Sql_case_studies(Steel data challenge)/04Sql_challenge (Finance Analysis)/04Sql_challenge (Finance Analysis)Part 2.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmitPatel-analyst/SQL-Case-Study/4dae8f080f537149fb3f2a7233519d9d17903659/Sql_case_studies(Steel data challenge)/04Sql_challenge (Finance Analysis)/04Sql_challenge (Finance Analysis)Part 2.jpeg -------------------------------------------------------------------------------- /Sql_case_studies(Steel data challenge)/04Sql_challenge (Finance Analysis)/Schema.sql: -------------------------------------------------------------------------------- 1 | /***************************************************************************************** 2 | Name : Code for Steel_Data DB 3 | Author : Amit Patel 4 | Date : 12-May-2023 5 | 6 | Purpose: This script will create Db and few table in it to store info about Finance Analysis 7 | *****************************************************************************************/ 8 | create database Steel_Data 9 | go 10 | 11 | USE Steel_Data 12 | GO 13 | 14 | -- Create the Customers table 15 | CREATE TABLE Customers ( 16 | CustomerID INT PRIMARY KEY, 17 | FirstName VARCHAR(50) NOT NULL, 18 | LastName VARCHAR(50) NOT NULL, 19 | City VARCHAR(50) NOT NULL, 20 | State VARCHAR(2) NOT NULL 21 | ); 22 | -- Create the Branches table 23 | CREATE TABLE Branches ( 24 | BranchID INT PRIMARY KEY, 25 | BranchName VARCHAR(50) NOT NULL, 26 | City VARCHAR(50) NOT NULL, 27 | State VARCHAR(2) NOT NULL 28 | ); 29 | -- Create the Accounts table 30 | CREATE TABLE Accounts ( 31 | AccountID INT PRIMARY KEY, 32 | CustomerID INT NOT NULL, 33 | BranchID INT NOT NULL, 34 | AccountType VARCHAR(50) NOT NULL, 35 | Balance DECIMAL(10, 2) NOT NULL, 36 | FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID), 37 | FOREIGN KEY (BranchID) REFERENCES Branches(BranchID) 38 | ); 39 | -- Create the Transactions table 40 | CREATE TABLE Transactions ( 41 | TransactionID INT PRIMARY KEY, 42 | AccountID INT NOT NULL, 43 | TransactionDate DATE NOT NULL, 44 | Amount DECIMAL(10, 2) NOT NULL, 45 | FOREIGN KEY (AccountID) REFERENCES Accounts(AccountID) 46 | ); 47 | 48 | --insert data 49 | INSERT INTO Customers (CustomerID, FirstName, LastName, City, State) 50 | VALUES (1, 'John', 'Doe', 'New York', 'NY'), 51 | (2, 'Jane', 'Doe', 'New York', 'NY'), 52 | (3, 'Bob', 'Smith', 'San Francisco', 'CA'), 53 | (4, 'Alice', 'Johnson', 'San Francisco', 'CA'), 54 | (5, 'Michael', 'Lee', 'Los Angeles', 'CA'), 55 | (6, 'Jennifer', 'Wang', 'Los Angeles', 'CA'); 56 | -------------------- 57 | INSERT INTO Branches (BranchID, BranchName, City, State) 58 | VALUES (1, 'Main', 'New York', 'NY'), 59 | (2, 'Downtown', 'San Francisco', 'CA'), 60 | (3, 'West LA', 'Los Angeles', 'CA'), 61 | (4, 'East LA', 'Los Angeles', 'CA'), 62 | (5, 'Uptown', 'New York', 'NY'), 63 | (6, 'Financial District', 'San Francisco', 'CA'), 64 | (7, 'Midtown', 'New York', 'NY'), 65 | (8, 'South Bay', 'San Francisco', 'CA'), 66 | (9, 'Downtown', 'Los Angeles', 'CA'), 67 | (10, 'Chinatown', 'New York', 'NY'), 68 | (11, 'Marina', 'San Francisco', 'CA'), 69 | (12, 'Beverly Hills', 'Los Angeles', 'CA'), 70 | (13, 'Brooklyn', 'New York', 'NY'), 71 | (14, 'North Beach', 'San Francisco', 'CA'), 72 | (15, 'Pasadena', 'Los Angeles', 'CA'); 73 | -------------------- 74 | INSERT INTO Accounts (AccountID, CustomerID, BranchID, AccountType, Balance) 75 | VALUES (1, 1, 5, 'Checking', 1000.00), 76 | (2, 1, 5, 'Savings', 5000.00), 77 | (3, 2, 1, 'Checking', 2500.00), 78 | (4, 2, 1, 'Savings', 10000.00), 79 | (5, 3, 2, 'Checking', 7500.00), 80 | (6, 3, 2, 'Savings', 15000.00), 81 | (7, 4, 8, 'Checking', 5000.00), 82 | (8, 4, 8, 'Savings', 20000.00), 83 | (9, 5, 14, 'Checking', 10000.00), 84 | (10, 5, 14, 'Savings', 50000.00), 85 | (11, 6, 2, 'Checking', 5000.00), 86 | (12, 6, 2, 'Savings', 10000.00), 87 | (13, 1, 5, 'Credit Card', -500.00), 88 | (14, 2, 1, 'Credit Card', -1000.00), 89 | (15, 3, 2, 'Credit Card', -2000.00); 90 | -------------------- 91 | INSERT INTO Transactions (TransactionID, AccountID, TransactionDate, Amount) 92 | VALUES (1, 1, '2022-01-01', -500.00), 93 | (2, 1, '2022-01-02', -250.00), 94 | (3, 2, '2022-01-03', 1000.00), 95 | (4, 3, '2022-01-04', -1000.00), 96 | (5, 3, '2022-01-05', 500.00), 97 | (6, 4, '2022-01-06', 1000.00), 98 | (7, 4, '2022-01-07', -500.00), 99 | (8, 5, '2022-01-08', -2500.00), 100 | (9, 6, '2022-01-09', 500.00), 101 | (10, 6, '2022-01-10', -1000.00), 102 | (11, 7, '2022-01-11', -500.00), 103 | (12, 7, '2022-01-12', -250.00), 104 | (13, 8, '2022-01-13', 1000.00), 105 | (14, 8, '2022-01-14', -1000.00), 106 | (15, 9, '2022-01-15', 500.00); 107 | -------------------------------------------------------------------------------- /Sql_case_studies(Steel data challenge)/04Sql_challenge (Finance Analysis)/Sqlcode(Finance analysis).sql: -------------------------------------------------------------------------------- 1 | Date : 12-May-2023 2 | 3 | --Questions 4 | 5 | 1. What are the names of all the customers who live in New York? 6 | 2. What is the total number of accounts in the Accounts table? 7 | 3. What is the total balance of all checking accounts? 8 | 4. What is the total balance of all accounts associated with customers who live in Los Angeles? 9 | 5. Which branch has the highest average account balance? 10 | 6. Which customer has the highest current balance in their accounts? 11 | 7. Which customer has made the most transactions in the Transactions table? 12 | 8.Which branch has the highest total balance across all of its accounts? 13 | 9. Which customer has the highest total balance across all of their accounts, including savings and checking accounts? 14 | 10. Which branch has the highest number of transactions in the Transactions table? 15 | 16 | 17 | 1. What are the names of all the customers who live in New York? 18 | 19 | SELECT 20 | concat(concat(firstName, ' '), Lastname) AS Customer , City 21 | FROM customers 22 | WHERE city = 'New York'; 23 | 24 | 2. What is the total number of accounts in the Accounts table? 25 | 26 | SELECT 27 | COUNT(accountid) AS No_of_accounts 28 | FROM accounts; 29 | 30 | 3. What is the total balance of all checking accounts? 31 | 32 | SELECT 33 | SUM(Balance) AS Total_balance 34 | FROM accounts 35 | WHERE accounttype = 'Checking'; 36 | 37 | 4. What is the total balance of all accounts associated with customers who live in Los Angeles? 38 | 39 | SELECT 40 | A.CustomerId, 41 | SUM(A.Balance) AS Total_Balance 42 | FROM accounts AS A 43 | WHERE CustomerID IN (SELECT 44 | C.CustomerId 45 | FROM customers AS C 46 | INNER JOIN accounts AS A 47 | ON A.CustomerID = C.CustomerID 48 | WHERE C.City = 'Los Angeles') 49 | GROUP BY A.CustomerId; 50 | 51 | 5. Which branch has the highest average account balance? 52 | 53 | WITH AVG_BALANCE 54 | AS (SELECT 55 | BRANCHID, 56 | AVG(BALANCE) AS AVG_BAL 57 | FROM ACCOUNTS 58 | GROUP BY BRANCHID) 59 | SELECT 60 | B.*, 61 | A.AVG_BAL 62 | FROM AVG_BALANCE AS A 63 | LEFT JOIN BRANCHES AS B 64 | ON A.BRANCHID = B.BRANCHID 65 | WHERE A.AVG_BAL = (SELECT 66 | MAX(AVG_BAL) 67 | FROM AVG_BALANCE); 68 | 69 | 7. Which customer has made the most transactions in the Transactions table? 70 | 71 | Approch - 1 (Cte and joins) 72 | WITH NO_OF_TRANSACTIONS 73 | AS (SELECT 74 | T.ACCOUNTID, 75 | COUNT(TRANSACTIONDATE) AS CNT 76 | FROM transactions T 77 | INNER JOIN accounts A 78 | ON A.ACCOUNTID = T.ACCOUNTID 79 | GROUP BY T.ACCOUNTID), 80 | CUSTOMER_INFO 81 | AS (SELECT DISTINCT 82 | A.CUSTOMERID, 83 | CONCAT(CONCAT(B.FIRSTNAME, ' '), B.LASTNAME) AS CUSTOMERNAME 84 | FROM NO_OF_TRANSACTIONS AS C 85 | INNER JOIN accounts AS A 86 | ON A.ACCOUNTID = C.ACCOUNTID 87 | INNER JOIN customers AS B 88 | ON B.CUSTOMERID = A.CUSTOMERID 89 | WHERE CNT = (SELECT 90 | MAX(CNT) 91 | FROM NO_OF_TRANSACTIONS)) 92 | SELECT * 93 | FROM CUSTOMER_INFO; 94 | 95 | -- OR Approach-2 (Subquery and joins) 96 | 97 | SELECT DISTINCT A.CUSTOMERID 98 | FROM transactions T 99 | INNER JOIN accounts A ON A.ACCOUNTID = T.ACCOUNTID 100 | WHERE T.ACCOUNTID IN ( 101 | SELECT ACCOUNTID 102 | FROM ( 103 | SELECT T.ACCOUNTID, COUNT(TRANSACTIONDATE) AS CNT 104 | FROM transactions T 105 | INNER JOIN accounts A ON A.ACCOUNTID = T.ACCOUNTID 106 | GROUP BY T.ACCOUNTID 107 | ) subquery 108 | WHERE CNT = ( 109 | SELECT MAX(CNT) 110 | FROM ( 111 | SELECT T.ACCOUNTID, COUNT(TRANSACTIONDATE) AS CNT 112 | FROM transactions T 113 | INNER JOIN accounts A ON A.ACCOUNTID = T.ACCOUNTID 114 | GROUP BY T.ACCOUNTID 115 | ) subquery2 116 | ) 117 | ) 118 | 119 | 120 | 8.Which branch has the highest total balance across all of its accounts? 121 | 122 | WITH TOTAL_BALANCE 123 | AS (SELECT 124 | BRANCHID, 125 | SUM(BALANCE) AS SUM_BAL 126 | FROM ACCOUNTS 127 | GROUP BY BRANCHID) 128 | SELECT 129 | B.*, 130 | A.SUM_BAL 131 | FROM TOTAL_BALANCE AS A 132 | LEFT JOIN BRANCHES AS B 133 | ON A.BRANCHID = B.BRANCHID 134 | WHERE A.SUM_BAL = (SELECT 135 | MAX(SUM_BAL) 136 | FROM TOTAL_BALANCE); 137 | 138 | 9. Which customer has the highest total balance across all of their accounts, including savings and checking accounts? 139 | 140 | WITH TOTAL_BALANCE 141 | AS (SELECT 142 | CUSTOMERID, 143 | SUM(BALANCE) AS SUM_BAL 144 | FROM ACCOUNTS 145 | GROUP BY CUSTOMERID) 146 | 147 | SELECT 148 | C.* 149 | FROM TOTAL_BALANCE AS A 150 | INNER JOIN CUSTOMERS AS C 151 | ON A.CUSTOMERID = C.CUSTOMERID 152 | WHERE A.SUM_BAL = (SELECT 153 | MAX(SUM_BAL) 154 | FROM TOTAL_BALANCE); 155 | 156 | 157 | 10. Which branch has the highest number of transactions in the Transactions table? 158 | 159 | WITH NO_OF_TRANSACTIONS 160 | AS (SELECT 161 | T.ACCOUNTID, 162 | COUNT(TRANSACTIONDATE) AS CNT 163 | FROM transactions T 164 | INNER JOIN accounts A 165 | ON A.ACCOUNTID = T.ACCOUNTID 166 | GROUP BY T.ACCOUNTID), 167 | BRANCH_INFO 168 | AS (SELECT DISTINCT 169 | A.BRANCHID, 170 | B.BRANCHNAME, 171 | B.CITY 172 | FROM NO_OF_TRANSACTIONS AS C 173 | INNER JOIN accounts AS A 174 | ON A.ACCOUNTID = C.ACCOUNTID 175 | INNER JOIN BRANCHES AS B 176 | ON B.BRANCHID = A.BRANCHID 177 | WHERE CNT = (SELECT 178 | MAX(CNT) 179 | FROM NO_OF_TRANSACTIONS)) 180 | SELECT * 181 | FROM BRANCH_INFO; 182 | -------------------------------------------------------------------------------- /Sql_case_studies(Steel data challenge)/05Sql_Challenge (Marketing Analysis)/06 Sql_Challenge (Marketing Analysis).sql: -------------------------------------------------------------------------------- 1 | Date : 10-May-2023 2 | 3 | 1. How many transactions were completed during each marketing campaign? 4 | 5 | 6 | 7 | SELECT M.campaign_name, 8 | COUNT(T.transaction_id) AS transaction_completed 9 | FROM transactions AS T 10 | INNER JOIN marketing_campaigns M 11 | ON T.Product_id = M.Product_id 12 | AND T.purchase_date BETWEEN M.start_date AND M.end_date 13 | GROUP BY M.campaign_name; 14 | 15 | 16 | 2. Which product had the highest sales quantity? 17 | 18 | 19 | WITH cte_top AS ( 20 | SELECT TOP 1 21 | product_id, 22 | SUM(quantity) AS total 23 | FROM transactions 24 | GROUP BY product_id 25 | ORDER BY 2 DESC) 26 | SELECT b.product_id, 27 | b.product_name, 28 | a.total AS highest_sales_quantity 29 | FROM cte_top a 30 | LEFT JOIN sustainable_clothing b 31 | ON a.product_id = b.product_id; 32 | 33 | 3. What is the total revenue generated from each marketing campaign? 34 | 35 | 36 | SELECT A.campaign_name, 37 | SUM(B.price * C.quantity) AS total_revenue_generated 38 | FROM marketing_campaigns A 39 | INNER JOIN sustainable_clothing B 40 | ON A.product_id = B.product_id 41 | INNER JOIN transactions C 42 | ON C.product_id = B.product_id 43 | GROUP BY A.campaign_name; 44 | 45 | 46 | 47 | 4. What is the top-selling product category based on the total revenue generated? 48 | 49 | SELECT TOP 1 B.category AS Top_selling_Product_category, 50 | SUM(B.price * C.quantity) AS total_revenue_generated 51 | FROM sustainable_clothing B 52 | INNER JOIN transactions C 53 | ON C.product_id = B.product_id 54 | GROUP BY B.category 55 | ORDER BY 2 DESC; 56 | 57 | 58 | 5. Which products had a higher quantity sold compared to the average quantity sold? 59 | 60 | SELECT DISTINCT product_id, 61 | quantity 62 | FROM transactions 63 | WHERE quantity > (SELECT AVG(quantity) FROM transactions); 64 | 65 | 66 | 6. What is the average revenue generated per day during the marketing campaigns? 67 | 68 | WITH cte_a AS ( 69 | SELECT 70 | T.Product_id, 71 | M.campaign_name, 72 | COUNT(T.transaction_id) AS frequency 73 | FROM transactions AS T 74 | INNER JOIN marketing_campaigns M 75 | ON T.Product_id = M.Product_id 76 | AND T.purchase_date BETWEEN M.start_date AND M.end_date 77 | GROUP BY T.Product_id, 78 | M.campaign_name), 79 | cte_b AS ( 80 | SELECT 81 | C.product_id, 82 | SUM(A.quantity * B.price) AS total_revenue_generated 83 | FROM transactions A 84 | INNER JOIN sustainable_clothing B 85 | ON A.product_id = B.product_id 86 | INNER JOIN marketing_campaigns C 87 | ON C.product_id = A.product_id 88 | AND A.purchase_date BETWEEN C.start_date AND C.end_date 89 | GROUP BY c.product_id) 90 | SELECT A.campaign_name, 91 | (total_revenue_generated / frequency) AS Average_revenue_generated_per_campaign 92 | FROM cte_a A 93 | INNER JOIN cte_b B 94 | ON A.product_id = B.product_id; 95 | 96 | 97 | 7. What is the percentage contribution of each product to the total revenue? 98 | 99 | SELECT sc.product_id, 100 | sc.product_name, 101 | SUM(t.quantity * sc.price) AS revenue, 102 | (SUM(t.quantity * sc.price) / total_revenue.total) * 100 AS percentage_contribution 103 | FROM sustainable_clothing sc 104 | JOIN transactions t 105 | ON sc.product_id = t.product_id 106 | CROSS JOIN (SELECT SUM(quantity * price) AS total 107 | FROM sustainable_clothing sc 108 | JOIN transactions t 109 | ON sc.product_id = t.product_id 110 | ) AS total_revenue 111 | GROUP BY sc.product_id, 112 | sc.product_name, 113 | total_revenue.total 114 | ORDER BY revenue DESC; 115 | 116 | 8. Compare the average quantity sold during marketing campaigns to outside the marketing campaigns 117 | 118 | 119 | WITH campaign_sales AS ( 120 | SELECT 121 | t.product_id, 122 | CASE 123 | WHEN t.product_id IN (SELECT product_id FROM marketing_campaigns) THEN 'During Campaigns' 124 | ELSE 'Outside Campaigns' 125 | END AS campaign, 126 | t.quantity 127 | FROM transactions t) 128 | SELECT campaign, 129 | AVG(quantity) AS average_quantity_sold 130 | FROM campaign_sales 131 | GROUP BY campaign 132 | ORDER BY campaign; 133 | 134 | 135 | 136 | 9. Compare the revenue generated by products inside the marketing campaigns to outside the campaigns 137 | 138 | WITH campaign_revenue AS ( 139 | SELECT 140 | t.product_id, 141 | CASE 142 | WHEN t.product_id IN (SELECT product_id FROM marketing_campaigns) THEN 'During Campaigns' 143 | ELSE 'Outside Campaigns' 144 | END AS campaign, 145 | t.quantity * sc.price AS revenue 146 | FROM transactions t 147 | JOIN sustainable_clothing sc 148 | ON t.product_id = sc.product_id) 149 | SELECT campaign, 150 | SUM(revenue) AS total_revenue 151 | FROM campaign_revenue 152 | GROUP BY campaign 153 | ORDER BY campaign; 154 | 155 | 10. Rank the products by their average daily quantity sold 156 | 157 | WITH daily_quantity AS ( 158 | SELECT 159 | product_id, 160 | SUM(quantity) AS total_quantity, 161 | COUNT(DISTINCT purchase_date) AS total_days, 162 | SUM(quantity) / COUNT(DISTINCT purchase_date) AS average_daily_quantity 163 | FROM transactions 164 | GROUP BY product_id) 165 | SELECT product_id, 166 | average_daily_quantity, 167 | RANK() OVER (ORDER BY average_daily_quantity DESC) AS product_rank 168 | FROM daily_quantity 169 | ORDER BY product_rank; 170 | 171 | 172 | --some valuable insights 173 | 1) "Organic Cotton Sweater" product had sold maximum times. 174 | 2) "Summer Sale" campaign was most sucessful among others which is amount of 639.92 dollars .This suggests that customers are responsive to seasonal discounts and promotions, indicating the potential for increased sales during specific periods. "Super save" campaign sales was only 209.97 dollars, Marketing team should be work on this campaign like giving more discount to who buy products in large quantity. 175 | 3) Top selling Product category in sustainable clothing is "Bottoms" which has highest revenue generted of 1289.79 dollars. 176 | 4) Recycled Polyester Jacket , Bamboo Yoga Leggings , Organic Cotton Sweater , Linen Jumpsuit , Recycled Denim Jeans these are the some top revenue sales generated products. 177 | 5) The Highest average revenue generated per day was "Summer Sale" campaign . 178 | 6) "Recycled Denim Jeans" product had highest percentage contribution of 13.70% to the total revenue & "Hemp Crop Top" product had lowest percentage contribution of 0.53% only. 179 | 7) As per database , More revenue generated on outside campaings as compared to during campaigns. the reason could be only 3 products (Product_id 2,7 & 10) was kept in that campaigns. 180 | 181 | -------------------------------------------------------------------------------- /Sql_case_studies(Steel data challenge)/05Sql_Challenge (Marketing Analysis)/Schema.sql: -------------------------------------------------------------------------------- 1 | /******************************************************************************************************* 2 | Name : Code for Steel_Data DB 3 | Author : Amit Patel 4 | Date : 28-May-2023 5 | 6 | Purpose: This script will create Db and few table in it to store info about Sustainable Clothing Co. 7 | *******************************************************************************************************/ 8 | create database Steel_Data 9 | go 10 | 11 | USE Steel_Data 12 | GO 13 | 14 | -- Create the table 15 | CREATE TABLE sustainable_clothing ( 16 | product_id INT PRIMARY KEY, 17 | product_name VARCHAR(100), 18 | category VARCHAR(50), 19 | size VARCHAR(10), 20 | price FLOAT 21 | ); 22 | -- Insert data into the table 23 | INSERT INTO sustainable_clothing (product_id, product_name, category, size, price) 24 | VALUES 25 | (1, 'Organic Cotton T-Shirt', 'Tops', 'S', 29.99), 26 | (2, 'Recycled Denim Jeans', 'Bottoms', 'M', 79.99), 27 | (3, 'Hemp Crop Top', 'Tops', 'L', 24.99), 28 | (4, 'Bamboo Lounge Pants', 'Bottoms', 'XS', 49.99), 29 | (5, 'Eco-Friendly Hoodie', 'Outerwear', 'XL', 59.99), 30 | (6, 'Linen Button-Down Shirt', 'Tops', 'M', 39.99), 31 | (7, 'Organic Cotton Dress', 'Dresses', 'S', 69.99), 32 | (8, 'Sustainable Swim Shorts', 'Swimwear', 'L', 34.99), 33 | (9, 'Recycled Polyester Jacket', 'Outerwear', 'XL', 89.99), 34 | (10, 'Bamboo Yoga Leggings', 'Activewear', 'XS', 54.99), 35 | (11, 'Hemp Overalls', 'Bottoms', 'M', 74.99), 36 | (12, 'Organic Cotton Sweater', 'Tops', 'L', 49.99), 37 | (13, 'Cork Sandals', 'Footwear', 'S', 39.99), 38 | (14, 'Recycled Nylon Backpack', 'Accessories', 'One Size', 59.99), 39 | (15, 'Organic Cotton Skirt', 'Bottoms', 'XS', 34.99), 40 | (16, 'Hemp Baseball Cap', 'Accessories', 'One Size', 24.99), 41 | (17, 'Upcycled Denim Jacket', 'Outerwear', 'M', 79.99), 42 | (18, 'Linen Jumpsuit', 'Dresses', 'L', 69.99), 43 | (19, 'Organic Cotton Socks', 'Accessories', 'M', 9.99), 44 | (20, 'Bamboo Bathrobe', 'Loungewear', 'XL', 69.99); 45 | -- Create the table 46 | CREATE TABLE marketing_campaigns ( 47 | campaign_id INT PRIMARY KEY, 48 | campaign_name VARCHAR(100), 49 | product_id INT, 50 | start_date DATE, 51 | end_date DATE, 52 | FOREIGN KEY (product_id) REFERENCES sustainable_clothing (product_id) 53 | ); 54 | -- Insert data into the table 55 | INSERT INTO marketing_campaigns (campaign_id, campaign_name, product_id, start_date, end_date) 56 | VALUES 57 | (1, 'Summer Sale', 2, '2023-06-01', '2023-06-30'), 58 | (2, 'New Collection Launch', 10, '2023-07-15', '2023-08-15'), 59 | (3, 'Super Save', 7, '2023-08-20', '2023-09-15'); 60 | -- Create the table 61 | CREATE TABLE transactions ( 62 | transaction_id INT PRIMARY KEY, 63 | product_id INT, 64 | quantity INT, 65 | purchase_date DATE, 66 | FOREIGN KEY (product_id) REFERENCES sustainable_clothing (product_id) 67 | ); 68 | -- Insert data into the table 69 | INSERT INTO transactions (transaction_id, product_id, quantity, purchase_date) 70 | VALUES 71 | (1, 2, 2, '2023-06-02'), 72 | (2, 14, 1, '2023-06-02'), 73 | (3, 5, 2, '2023-06-05'), 74 | (4, 2, 1, '2023-06-07'), 75 | (5, 19, 2, '2023-06-10'), 76 | (6, 2, 1, '2023-06-13'), 77 | (7, 16, 1, '2023-06-13'), 78 | (8, 10, 2, '2023-06-15'), 79 | (9, 2, 1, '2023-06-18'), 80 | (10, 4, 1, '2023-06-22'), 81 | (11, 18, 2, '2023-06-26'), 82 | (12, 2, 1, '2023-06-30'), 83 | (13, 13, 1, '2023-06-30'), 84 | (14, 4, 1, '2023-07-04'), 85 | (15, 6, 2, '2023-07-08'), 86 | (16, 15, 1, '2023-07-08'), 87 | (17, 9, 2, '2023-07-12'), 88 | (18, 20, 1, '2023-07-12'), 89 | (19, 11, 1, '2023-07-16'), 90 | (20, 10, 1, '2023-07-20'), 91 | (21, 12, 2, '2023-07-24'), 92 | (22, 5, 1, '2023-07-29'), 93 | (23, 10, 1, '2023-07-29'), 94 | (24, 10, 1, '2023-08-03'), 95 | (25, 19, 2, '2023-08-08'), 96 | (26, 3, 1, '2023-08-14'), 97 | (27, 10, 1, '2023-08-14'), 98 | (28, 16, 2, '2023-08-20'), 99 | (29, 18, 1, '2023-08-27'), 100 | (30, 12, 2, '2023-09-01'), 101 | (31, 13, 1, '2023-09-05'), 102 | (32, 7, 1, '2023-09-05'), 103 | (33, 6, 1, '2023-09-10'), 104 | (34, 15, 2, '2023-09-14'), 105 | (35, 9, 1, '2023-09-14'), 106 | (36, 11, 2, '2023-09-19'), 107 | (37, 17, 1, '2023-09-23'), 108 | (38, 2, 1, '2023-09-28'), 109 | (39, 14, 1, '2023-09-28'), 110 | (40, 5, 2, '2023-09-30'), 111 | (41, 16, 1, '2023-10-01'), 112 | (42, 12, 2, '2023-10-01'), 113 | (43, 1, 1, '2023-10-01'), 114 | (44, 7, 1, '2023-10-02'), 115 | (45, 18, 2, '2023-10-03'), 116 | (46, 12, 1, '2023-10-03'), 117 | (47, 13, 1, '2023-10-04'), 118 | (48, 4, 1, '2023-10-05'), 119 | (49, 12, 2, '2023-10-05'), 120 | (50, 7, 1, '2023-10-06'), 121 | (51, 4, 2, '2023-10-08'), 122 | (52, 8, 2, '2023-10-08'), 123 | (53, 16, 1, '2023-10-09'), 124 | (54, 19, 1, '2023-10-09'), 125 | (55, 1, 1, '2023-10-10'), 126 | (56, 18, 2, '2023-10-10'), 127 | (57, 2, 1, '2023-10-10'), 128 | (58, 15, 2, '2023-10-11'), 129 | (59, 17, 2, '2023-10-13'), 130 | (60, 13, 1, '2023-10-13'), 131 | (61, 10, 2, '2023-10-13'), 132 | (62, 9, 1, '2023-10-13'), 133 | (63, 19, 2, '2023-10-13'), 134 | (64, 20, 1, '2023-10-14') 135 | -------------------------------------------------------------------------------- /Sql_case_studies(Steel data challenge)/05Sql_Challenge (Marketing Analysis)/Sql Challenge_6_Steeldata.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmitPatel-analyst/SQL-Case-Study/4dae8f080f537149fb3f2a7233519d9d17903659/Sql_case_studies(Steel data challenge)/05Sql_Challenge (Marketing Analysis)/Sql Challenge_6_Steeldata.pdf -------------------------------------------------------------------------------- /Sql_case_studies(Steel data challenge)/05Sql_Challenge (Pub chain) /Schema.sql: -------------------------------------------------------------------------------- 1 | /***************************************************************************************** 2 | Name : Code for Steel_Data DB 3 | Author : Amit Patel 4 | Date : 10-May-2023 5 | 6 | Purpose: This script will create Db and few table in it to store info about Pub Chain 7 | *****************************************************************************************/ 8 | create database Steel_Data 9 | go 10 | 11 | USE Steel_Data 12 | GO 13 | ---create tables 14 | CREATE TABLE pubs ( 15 | pub_id INT PRIMARY KEY, 16 | pub_name VARCHAR(50), 17 | city VARCHAR(50), 18 | state VARCHAR(50), 19 | country VARCHAR(50) 20 | ); 21 | -------------------- 22 | 23 | CREATE TABLE beverages ( 24 | beverage_id INT PRIMARY KEY, 25 | beverage_name VARCHAR(50), 26 | category VARCHAR(50), 27 | alcohol_content FLOAT, 28 | price_per_unit DECIMAL(8, 2) 29 | ); 30 | -------------------- 31 | 32 | CREATE TABLE sales ( 33 | sale_id INT PRIMARY KEY, 34 | pub_id INT, 35 | beverage_id INT, 36 | quantity INT, 37 | transaction_date DATE, 38 | FOREIGN KEY (pub_id) REFERENCES pubs(pub_id), 39 | FOREIGN KEY (beverage_id) REFERENCES beverages(beverage_id) 40 | ); 41 | -------------------- 42 | CREATE TABLE ratings ( 43 | rating_id INT PRIMARY KEY, 44 | pub_id INT, 45 | customer_name VARCHAR(50), 46 | rating FLOAT, 47 | review TEXT, 48 | FOREIGN KEY (pub_id) REFERENCES pubs(pub_id) 49 | ); 50 | -------------------- 51 | INSERT INTO pubs (pub_id, pub_name, city, state, country) 52 | VALUES 53 | (1, 'The Red Lion', 'London', 'England', 'United Kingdom'), 54 | (2, 'The Dubliner', 'Dublin', 'Dublin', 'Ireland'), 55 | (3, 'The Cheers Bar', 'Boston', 'Massachusetts', 'United States'), 56 | (4, 'La Cerveceria', 'Barcelona', 'Catalonia', 'Spain'); 57 | -------------------- 58 | 59 | INSERT INTO beverages (beverage_id, beverage_name, category, alcohol_content, price_per_unit) 60 | VALUES 61 | (1, 'Guinness', 'Beer', 4.2, 5.99), 62 | (2, 'Jameson', 'Whiskey', 40.0, 29.99), 63 | (3, 'Mojito', 'Cocktail', 12.0, 8.99), 64 | (4, 'Chardonnay', 'Wine', 13.5, 12.99), 65 | (5, 'IPA', 'Beer', 6.8, 4.99), 66 | (6, 'Tequila', 'Spirit', 38.0, 24.99); 67 | -------------------- 68 | INSERT INTO sales (sale_id, pub_id, beverage_id, quantity, transaction_date) 69 | VALUES 70 | (1, 1, 1, 10, '2023-05-01'), 71 | (2, 1, 2, 5, '2023-05-01'), 72 | (3, 2, 1, 8, '2023-05-01'), 73 | (4, 3, 3, 12, '2023-05-02'), 74 | (5, 4, 4, 3, '2023-05-02'), 75 | (6, 4, 6, 6, '2023-05-03'), 76 | (7, 2, 3, 6, '2023-05-03'), 77 | (8, 3, 1, 15, '2023-05-03'), 78 | (9, 3, 4, 7, '2023-05-03'), 79 | (10, 4, 1, 10, '2023-05-04'), 80 | (11, 1, 3, 5, '2023-05-06'), 81 | (12, 2, 2, 3, '2023-05-09'), 82 | (13, 2, 5, 9, '2023-05-09'), 83 | (14, 3, 6, 4, '2023-05-09'), 84 | (15, 4, 3, 7, '2023-05-09'), 85 | (16, 4, 4, 2, '2023-05-09'), 86 | (17, 1, 4, 6, '2023-05-11'), 87 | (18, 1, 6, 8, '2023-05-11'), 88 | (19, 2, 1, 12, '2023-05-12'), 89 | (20, 3, 5, 5, '2023-05-13'); 90 | -------------------- 91 | 92 | INSERT INTO ratings (rating_id, pub_id, customer_name, rating, review) 93 | VALUES 94 | (1, 1, 'John Smith', 4.5, 'Great pub with a wide selection of beers.'), 95 | (2, 1, 'Emma Johnson', 4.8, 'Excellent service and cozy atmosphere.'), 96 | (3, 2, 'Michael Brown', 4.2, 'Authentic atmosphere and great beers.'), 97 | (4, 3, 'Sophia Davis', 4.6, 'The cocktails were amazing! Will definitely come back.'), 98 | (5, 4, 'Oliver Wilson', 4.9, 'The wine selection here is outstanding.'), 99 | (6, 4, 'Isabella Moore', 4.3, 'Had a great time trying different spirits.'), 100 | (7, 1, 'Sophia Davis', 4.7, 'Loved the pub food! Great ambiance.'), 101 | (8, 2, 'Ethan Johnson', 4.5, 'A good place to hang out with friends.'), 102 | (9, 2, 'Olivia Taylor', 4.1, 'The whiskey tasting experience was fantastic.'), 103 | (10, 3, 'William Miller', 4.4, 'Friendly staff and live music on weekends.'); 104 | -------------------- 105 | -------------------------------------------------------------------------------- /Sql_case_studies(Steel data challenge)/05Sql_Challenge (Pub chain) /Sqlcode(PubPricinganalysis).sql: -------------------------------------------------------------------------------- 1 | Date : 10 - May - 2023 2 | Questions:- 3 | 4 | 1. How many pubs are located in each country?? 5 | 2. What is the total sales amount for each pub, including the beverage price and quantity sold? 6 | 3. Which pub has the highest average rating? 7 | 4. What are the top 5 beverages by sales quantity across all pubs? 8 | 5. How many sales transactions occurred on each date? 9 | 6. Find the name of someone that had cocktails and which pub they had it in. 10 | 7. What is the average price per unit for each category of beverages, excluding the category 'Spirit'? 11 | 8. Which pubs have a rating higher than the average rating of all pubs? 12 | 9. What is the running total of sales amount for each pub, ordered by the transaction date? 13 | 10. For each country, what is the average price per unit of beverages in each category, and what is the overall average price per unit of beverages across all categories? 14 | 11. For each pub, what is the percentage contribution of each category of beverages to the total sales amount, and what is the pub's overall sales amount? 15 | 16 | 17 | 1. How many pubs are located in each country?? 18 | 19 | select country ,count(*) as Total_pubs 20 | from pubs 21 | group by country; 22 | 23 | 24 | 2. What is the total sales amount for each pub, including the beverage price and quantity sold? 25 | 26 | select p.pub_name, sum(s.quantity * b.price_per_unit) as Total_SalesAmt 27 | from pubs p 28 | inner join sales s on p.pub_id=s.pub_id 29 | inner join beverages b on b.beverage_id=s.beverage_id 30 | group by p.pub_name; 31 | 32 | 33 | 34 | 35 | 3. Which pub has the highest average rating? 36 | 37 | select top 1 p.pub_id , round(avg(r.rating),2) as Highest_avg_rating 38 | from ratings r 39 | inner join pubs p on r.pub_id=p.pub_id 40 | group by p.pub_id 41 | order by 2 desc; -- The Red Lion pub has highest rating of 4.67 42 | 43 | 44 | 4. What are the top 5 beverages by sales quantity across all pubs? 45 | 46 | -- I used Row_number Window function, we can use below window functions as well ; 47 | -- Rank Functions skips the rank if there are duplicate row values. 48 | -- Dense_rank Functions will never skip the rank. 49 | with cte_rank as ( 50 | select beverage_id, 51 | sum(quantity) as total_quantity, 52 | row_number()over(order by sum(quantity) desc) as rnum 53 | from sales 54 | group by beverage_id 55 | ) 56 | select b.beverage_name as [Top 5 beverages] 57 | from cte_rank as c 58 | inner join beverages as b on c.beverage_id = b.beverage_id 59 | where rnum <=5; 60 | 61 | 62 | 5. How many sales transactions occurred on each date? 63 | 64 | select transaction_date,count(*) as Sales_Frequency -- count the total sales transactions 65 | from sales 66 | group by transaction_date -- to create a unique record of each value 67 | order by 1; 68 | 69 | 70 | 71 | 6. Find the name of someone that had cocktails and which pub they had it in. 72 | 73 | -- Approch -1 74 | 75 | select p.pub_name ,k.customer_name 76 | from pubs as p 77 | join ( select customer_name ,pub_id 78 | from ratings 79 | where pub_id in ( 80 | select distinct pub_id 81 | from sales 82 | where beverage_id = ( 83 | select beverage_id 84 | from beverages 85 | where category = 'Cocktail') 86 | ) 87 | ) as k 88 | on k.pub_id = p.pub_id 89 | order by 1 ; 90 | 91 | 92 | -- Approach - 2 (Using Inner join) 93 | select p.pub_name ,r.customer_name 94 | from pubs as p 95 | inner join ratings as r 96 | on p.pub_id = r.pub_id 97 | inner join sales as s 98 | on s.pub_id = r.pub_id 99 | inner join beverages as b 100 | on b.beverage_id = s.beverage_id 101 | where b.category = 'Cocktail' -- to include in the filter criteriaS 102 | order by 1; 103 | 104 | 105 | 106 | 7. What is the average price per unit for each category of beverages, excluding the category 'Spirit'? 107 | 108 | 109 | select category as Beverage_category, avg(price_per_unit) as average_price_per_unit 110 | from beverages 111 | where category <> 'Spirit' -- to not include in the filter criteria 112 | group by category; 113 | 114 | 8. Which pubs have a rating higher than the average rating of all pubs? 115 | 116 | with cte_ratings as( 117 | select Pub_id 118 | ,rating 119 | ,dense_rank() over(partition by Pub_id Order by rating desc) as dRank 120 | -- used partition by to find any duplicate records 121 | from ratings 122 | where rating > (select avg(rating) from ratings) 123 | -- avg rating of all pubs has value of 4.5 124 | ) 125 | select p.*,c.rating 126 | from cte_ratings as c 127 | inner join pubs as p 128 | on c.pub_id = p.pub_id 129 | where dRank = 1; -- filter by only rank 1 (to filter duplicate records) 130 | 131 | 9. What is the running total of sales amount for each pub, ordered by the transaction date? 132 | 133 | -- 134 | with Cte_SalesAmt as ( 135 | select s.pub_id,(s.quantity * b.price_per_unit) as SalesAmt,s.transaction_date 136 | from sales s 137 | inner join beverages as b 138 | on s.beverage_id = b.beverage_id 139 | ) 140 | select *,Sum(SalesAmt) 141 | over(partition by pub_id order by transaction_date) as Running_Total_SalesAmt 142 | from Cte_SalesAmt; 143 | 144 | 145 | 10. For each country, what is the average price per unit of beverages in each category, 146 | and what is the overall average price per unit of beverages across all categories? 147 | 148 | with cte_avg_price as ( 149 | select p.country,b.category,AVG(b.price_per_unit) as [Average_price] 150 | from pubs p 151 | inner join sales s on s.pub_id = p.pub_id 152 | inner join beverages b on b.beverage_id = s.beverage_id 153 | group by p.country,b.category 154 | ), 155 | cte_total_avg_price as ( 156 | select p.country,AVG(b.price_per_unit) as [Total_Average_price] 157 | from pubs p 158 | inner join sales s on s.pub_id = p.pub_id 159 | inner join beverages b on b.beverage_id = s.beverage_id 160 | group by p.country 161 | ) 162 | select a.country as Country , a.category as category 163 | ,a.Average_price as [Avg_price_per_unit] , b.Total_Average_price as [Total_Average_price_unit] 164 | from cte_avg_price a 165 | inner join cte_total_avg_price b 166 | on a.country = b.country; 167 | 168 | 169 | Some of the key insights are- 170 | ✔ The Red Lion Pub has the highest customers average rating of 4.67 171 | ✔ The Red Lion Pub has sold the highest quantity of 55 Drinks and also has generated the highest sales amount of $ 532.66. 172 | ✔ The overall average price per unit of beverages across all categories is highest for country United Kingdom and lowest for country Ireland. 173 | ✔ The average price per unit of beverages is the highest for Whiskey in Ireland and the United Kingdom. And lowest for Beer in the United States. 174 | ✔ The highest transactions were done on Tuesday and Wednesday and the lowest transaction was done on Thursday and Friday. 175 | 176 | ------------------------------------------------------------- 177 | -------------------------------------------------------------------------------- /Sql_case_studies(Steel data challenge)/05Sql_Challenge (Pub chain)/Sql Challenge_5_Steeldata.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmitPatel-analyst/SQL-Case-Study/4dae8f080f537149fb3f2a7233519d9d17903659/Sql_case_studies(Steel data challenge)/05Sql_Challenge (Pub chain)/Sql Challenge_5_Steeldata.pdf -------------------------------------------------------------------------------- /Supplier Case Study/README.md: -------------------------------------------------------------------------------- 1 | ### Supplier Case Study 2 | 3 | 1. Create a DATABASE: SUPPLIER 4 | ### TABLES 5 | CREATE THE FOLLOWING THREE TABLES WITH THE SAME NAMES AND DATA TYPES AS PROVIDED BELOW: 6 | 7 | [Here are the scripts of these tables](https://github.com/AmitPatel-analyst/SQL-Case-Study/blob/main/Supplier%20Case%20Study/Supplier_Queries.sql) 8 | 9 | #### SupplierMaster 10 | |Column Name |Data Type |Remarks| 11 | |-------------|-----------|-------| 12 | |SID |Integer |Primary Key 13 | |NAME |Varchar (40) |NOT NULL 14 | |CITY |Char(6) |NOT NULL 15 | |GRADE |Tinyint |NOT NULL ( 1 to 4) 16 | 17 | #### PartMaster 18 | |Column Name |Data Type |Remarks| 19 | |-------------|-----------|-------| 20 | |PID |TinyInt |Primary Key 21 | |NAME |Varchar (40) |NOT NULL 22 | |PRICE |Money |NOT NULL 23 | |CATEGORY |Tinyint |NOT NULL ( 1 to 3) 24 | |QTYONHAND |Integer |NULL 25 | 26 | #### SupplyDetails 27 | |Column Name |Data Type |Remarks| 28 | |-------------|-----------|-------| 29 | |PID |TinyInt |Foreign Key 30 | |SID |Integer |Foreign Key 31 | |DATEOFSUPPLY |DateTime |NOT NULL 32 | |CITY |Varchar(40) |NOT NULL 33 | |QTYSUPPLIED |Integer |NOT NULL 34 | 35 | Q.1. List the month-wise average supply of parts supplied for all parts. Provide the information only if the average is higher than 250. 36 | ```sql 37 | SELECT DATEPART(MM,DOS) AS MONTH_NO, 38 | DATENAME(MM,DOS) AS MONTHS_NAME, 39 | AVG(QTYSUPPLIED) AS AVG_QTYSUPPLIED 40 | FROM SUPLDETL 41 | GROUP BY DATENAME(MM,DOS), DATEPART(MM,DOS) 42 | HAVING AVG(QTYSUPPLIED) > 250 43 | GO 44 | ``` 45 | ![image](https://user-images.githubusercontent.com/120770473/229304684-dcfe381e-ef9d-4a49-9495-66756aea0ed9.png) 46 | 47 | Q.2. List the names of the Suppliers who do not supply part with PID ‘1’. 48 | ```sql 49 | SELECT DISTINCT NAME 50 | FROM SUPPLIERMASTER 51 | WHERE SID NOT IN ( 52 | SELECT DISTINCT SID 53 | FROM SUPLDETL 54 | WHERE PID = 1 55 | ) 56 | GO 57 | ``` 58 | ![image](https://user-images.githubusercontent.com/120770473/229304795-a3f4299f-6f29-4db3-8454-4d85f83c7041.png) 59 | 60 | Q.3. List the part id, name, price and difference between price and average price of all parts. 61 | ```sql 62 | SELECT PID, 63 | NAME, 64 | PRICE, 65 | PRICE - ( SELECT ROUND(AVG(PRICE),0) FROM PartMaster) AS DIFF 66 | FROM PartMaster 67 | GO 68 | ``` 69 | ![image](https://user-images.githubusercontent.com/120770473/229305494-77660c4f-f67f-49af-ae3c-5973d2798907.png) 70 | 71 | Q.4. List the names of the suppliers who have supplied at least Two parts where the quantity supplied is lower than 500. 72 | ```sql 73 | SELECT S.NAME , 74 | COUNT(S.SID) AS #ofpartssupplied 75 | FROM SUPPLIERMASTER S JOIN SUPLDETL SD 76 | ON S.SID = SD.SID 77 | WHERE QTYSUPPLIED<500 78 | GROUP BY S.NAME 79 | HAVING COUNT(S.SID) >=2 80 | go 81 | ``` 82 | ![image](https://user-images.githubusercontent.com/120770473/229305836-b96fb219-364e-4444-b298-56ac9890e546.png) 83 | 84 | Q.5. List the names of the suppliers who live in a city where no supply has been made. 85 | -- it means supplier iD should matched in both table but city should not be matched. 86 | ```sql 87 | SELECT DISTINCT NAME 88 | FROM SUPPLIERMASTER AS SM 89 | WHERE EXISTS ( SELECT * 90 | FROM SUPLDETL AS SD 91 | WHERE SM.SID=SD.SID AND SM.CITY<>SD.CITY 92 | ) 93 | go 94 | ``` 95 | ![image](https://user-images.githubusercontent.com/120770473/229305902-6d4123eb-d829-482e-9ef5-ba976a100816.png) 96 | 97 | Q.6. List the names of the parts which have not been supplied in the month of May 2019. 98 | ```sql 99 | SELECT Name 100 | FROM PARTMASTER 101 | WHERE PID NOT IN (SELECT PID 102 | FROM SUPLDETL 103 | WHERE DATENAME(MM,DOS)='MAY' AND DATEPART(YY,DOS)=2019 104 | ) 105 | go 106 | ``` 107 | ![image](https://user-images.githubusercontent.com/120770473/229305947-a5ec8f7f-c6a6-4313-ac19-0681dd9d8577.png) 108 | 109 | Q.7. List name and Price category for all parts. Price category has to be displayed as “Cheap” 110 | -- if price is less than 200, “Medium” if the price is greater than or equal to 200 and less than 500, 111 | -- and “Costly” if the price is greater than or equal to 500. 112 | ```sql 113 | SELECT NAME , 114 | PRICE, 115 | CASE 116 | WHEN PRICE>=500 THEN 'COSTLY' 117 | WHEN PRICE <500 AND PRICE >=200 THEN 'MEDIUM' 118 | ELSE 'CHEAP' 119 | END AS PRICE_CATEGORY 120 | FROM PartMaster 121 | go 122 | ``` 123 | ![image](https://user-images.githubusercontent.com/120770473/229305996-c1975618-5d96-42fe-afc6-cc9edf3c3747.png) 124 | 125 | Q.8. List the names of the suppliers who have supplied exactly 100 units of part P16. 126 | ```sql 127 | SELECT NAME , SID 128 | FROM SUPPLIERMASTER 129 | WHERE SID IN (SELECT SID 130 | FROM SUPLDETL 131 | WHERE [QTYSUPPLIED] =100 AND PID = 16 132 | ) 133 | go 134 | ``` 135 | ![image](https://user-images.githubusercontent.com/120770473/229306114-024d78d6-6e8e-4a18-80c0-00bcb7b3d579.png) 136 | 137 | Q.9. List the names of the parts whose price is more than the average price of parts. 138 | ```sql 139 | with AvgPrice 140 | as 141 | (select round(avg(Price),0) as AvgPrice 142 | from PartMaster) 143 | 144 | select p.Name,p.Price,a.AvgPrice from PartMaster p 145 | left join AvgPrice a on p.Price>a.AvgPrice where a.AvgPrice is not null; 146 | ``` 147 | ![image](https://user-images.githubusercontent.com/120770473/229306692-592dc733-3fe6-4dfd-8dd9-5a1cc96abc00.png) 148 | 149 | Q.10. For all products supplied by Supplier S70, List the part name and total quantity SUPPLIED. 150 | ```sql 151 | SELECT NAME AS PNAME, 152 | QTYSUPPLIED 153 | FROM 154 | ( SELECT DISTINCT PID, SID , QTYSUPPLIED 155 | FROM SupLDETL 156 | WHERE SID = 70 157 | ) AS K 158 | JOIN PARTMASTER AS PM 159 | ON K.PID=PM.PID 160 | ; 161 | ``` 162 | ![image](https://user-images.githubusercontent.com/120770473/229306743-b81328fa-5bac-413d-832d-138d98a32231.png) 163 | 164 | Click [Here](https://github.com/AmitPatel-analyst/SQL-Case-Study/tree/main) to move back to my another case study repositories! 165 | -------------------------------------------------------------------------------- /Supplier Case Study/Supplier_Queries.sql: -------------------------------------------------------------------------------- 1 | /***************************************************************************************** 2 | Name : Code for Supplier DB 3 | Author : Amit Patel 4 | Date : Aug 21, 2022 5 | 6 | Purpose: This script will create Db and few table in it to store info about supplier 7 | *****************************************************************************************/ 8 | create database Supplier 9 | go 10 | 11 | use Supplier 12 | go 13 | 14 | --SupplierMaster create table 15 | create table SupplierMaster 16 | ( 17 | SID Int Primary Key, 18 | Name Varchar(40) not null, 19 | City Char(20) not null, 20 | Grade Tinyint not null check(Grade > 0 and Grade < 5) 21 | ) 22 | go 23 | 24 | -- Get the schema of the table 25 | sp_help SupplierMaster 26 | go 27 | 28 | -- insert the data 29 | insert into SupplierMaster values(10,'Usman Khan','Delhi',1) 30 | insert into SupplierMaster values(20,'Nitish K','Kanpur',2) 31 | insert into SupplierMaster values(30,'Shiva','Mumbai',2) 32 | insert into SupplierMaster values(40,'Simran','Raipur',3) 33 | insert into SupplierMaster values(50,'Srikanth','HYD',4) 34 | insert into SupplierMaster values(60,'Neeraj','Delhi',1) 35 | 36 | insert into SupplierMaster values(70,'Hament','Delhi',4) 37 | insert into SupplierMaster values(80,'SKumar','Surat',2) 38 | insert into SupplierMaster values(90,'Sanjay','Nagpur',1) 39 | insert into SupplierMaster values(100,'Amit','Kanpur',3) 40 | insert into SupplierMaster values(110,'Animesh','Bhopal',3) 41 | insert into SupplierMaster values(120,'Gaurav','Chennai',1) 42 | 43 | insert into SupplierMaster values(130,'Hilay','Rajkot',4) 44 | insert into SupplierMaster values(140,'Bhavik','Durgapur',4) 45 | insert into SupplierMaster values(150,'Shyamlal','Dehradun',2) 46 | insert into SupplierMaster values(160,'Anamika','Roorkee',3) 47 | insert into SupplierMaster values(170,'harpreet','Madras',2) 48 | insert into SupplierMaster values(180,'Venugopal','Mumbai',1) 49 | 50 | insert into SupplierMaster values(190,'Hament','Mumbai',1) 51 | insert into SupplierMaster values(200,'SKumar','Madras',4) 52 | insert into SupplierMaster values(210,'Sanjay','Dehradun',2) 53 | insert into SupplierMaster values(220,'Amit','Durgapur',3) 54 | insert into SupplierMaster values(230,'Animesh','Delhi',1) 55 | insert into SupplierMaster values(240,'Gaurav','Delhi',4) 56 | go 57 | 58 | --read the data 59 | select * from SupplierMaster 60 | go -- TOTAL 24 RECORDS 61 | 62 | ---PartMaster 63 | 64 | create table PartMaster 65 | ( PID Tinyint Primary Key, 66 | Name Varchar(40) not null, 67 | Price Money not null, 68 | Category Tinyint not null, --- Category 1,2,3 69 | QtyOnHand Int null, 70 | 71 | ) 72 | go 73 | 74 | -- Insert 75 | 76 | insert into PartMaster values(1,'Lights',1000,1,1200) 77 | insert into PartMaster values(2,'Batteries',5600,1,500) 78 | insert into PartMaster values(3,'Engines',67000,2,4000) 79 | insert into PartMaster values(4,'Tyres',2400,3,5000) 80 | insert into PartMaster values(5,'Tubes',700,3,7800) 81 | insert into PartMaster values(6,'Screws',15,2,2000) 82 | insert into PartMaster values(7,'Mirrors',1000,1,400) 83 | insert into PartMaster values(8,'Clutches',1500,3,1000) 84 | 85 | insert into PartMaster values(9,'Bolts',400,1,12000) 86 | insert into PartMaster values(10,'Nuts',200,1,25000) 87 | insert into PartMaster values(11,'Washers',300,2,4000) 88 | insert into PartMaster values(12,'Gaskets',2400,3,5000) 89 | insert into PartMaster values(13,'Hammers',2000,2,1800) 90 | insert into PartMaster values(14,'Bedsheets',150,1,2200) 91 | insert into PartMaster values(15,'Blankets',350,1,850) 92 | insert into PartMaster values(16,'Windscreens',1800,3,350) 93 | go 94 | 95 | --read the data 96 | select * from PartMaster 97 | go -- -- TOTAL 16 RECORDS 98 | 99 | ---SupplyDetails 100 | create table SuplDetl 101 | ( 102 | PID Tinyint not null Foreign Key references PartMaster(PID), 103 | SID Int not null Foreign Key references SupplierMaster(SID), 104 | DOS DateTime not null, 105 | CITY Varchar(40) not null, 106 | QTYSUPPLIED Int not null 107 | ) 108 | go 109 | 110 | --insert data 111 | insert into SuplDetl values(2,30,'2019/5/21','Delhi',45) 112 | insert into SuplDetl values(3,60,'2019/6/25','Mumbai',80) 113 | insert into SuplDetl values(1,40,'2019/6/30','Mumbai',120) 114 | insert into SuplDetl values(5,10,'2019/7/02','Delhi',45) 115 | insert into SuplDetl values(2,30,'2019/7/10','Kanpur',50) 116 | insert into SuplDetl values(4,50,'2019/7/11','HYD',150) 117 | 118 | insert into SuplDetl values(11,20,'2020/5/21','Bhopal',85) 119 | insert into SuplDetl values(13,70,'2020/6/15','Chennai',100) 120 | insert into SuplDetl values(11,20,'2020/6/10','Dehradun',110) 121 | insert into SuplDetl values(15,50,'2022/7/02','Dehradun',50) 122 | insert into SuplDetl values(12,40,'2022/7/10','HYD',250) 123 | insert into SuplDetl values(14,30,'2022/7/11','Bhopal',450) 124 | 125 | insert into SuplDetl values(16,30,'2022/9/1','Bhopal',155) 126 | insert into SuplDetl values(3,60,'2022/9/5','Madras',180) 127 | insert into SuplDetl values(1,40,'2021/6/30','HYD',200) 128 | insert into SuplDetl values(5,10,'2022/7/02','Delhi',255) 129 | insert into SuplDetl values(12,30,'2022/7/10','Kanpur',350) 130 | insert into SuplDetl values(8,50,'2019/11/11','HYD',185) 131 | 132 | insert into SuplDetl values(6,70,'2021/5/21','Rajkot',150) 133 | insert into SuplDetl values(10,100,'2022/6/25','Roorkee',600) 134 | insert into SuplDetl values(8,80,'2022/7/30','Surat',720) 135 | insert into SuplDetl values(7,90,'2020/7/02','Mumbai',450) 136 | insert into SuplDetl values(9,110,'2020/7/10','Nagpur',350) 137 | insert into SuplDetl values(10,150,'2020/7/11','Madras',225) 138 | 139 | insert into SuplDetl values(6,70,'2022/5/21','Chennai',150) 140 | insert into SuplDetl values(10,100,'2022/5/15','HYD',600) 141 | insert into SuplDetl values(8,80,'2022/6/13','Nagpur',720) 142 | insert into SuplDetl values(7,90,'2022/7/12','Dehradun',450) 143 | insert into SuplDetl values(9,110,'2022/7/11','Bhopal',350) 144 | insert into SuplDetl values(10,150,'2022/8/15','HYD',225) 145 | 146 | insert into SuplDetl values(16,70,'2019/4/11','Chennai',100) 147 | insert into SuplDetl values(1,100,'2021/8/20','HYD',700) 148 | insert into SuplDetl values(12,80,'2020/4/15','Nagpur',740) 149 | insert into SuplDetl values(17,90,'2020/6/01','Dehradun',400) 150 | insert into SuplDetl values(11,110,'2020/6/05','Bhopal',300) 151 | insert into SuplDetl values(10,150,'2021/8/05','HYD',160) 152 | go 153 | 154 | --read the data 155 | select * from SuplDetl 156 | go -- TOTAL 35 RECORDS 157 | --------------------------------------------------------------------------------