├── README.md ├── SQL Exercises - JOINS on Sales Database.sql ├── SQL Exercises - SUBQUERIES on Sales Database.sql ├── SQL Exercises - JOINS on HR Database.sql └── SQL Exercises - SUBQUERIES on HR Database.sql /README.md: -------------------------------------------------------------------------------- 1 | # w3resource-SQL-Exercises 2 | 3 | [SQL Exercises](https://www.w3resource.com/sql-exercises/) 4 | -------------------------------------------------------------------------------- /SQL Exercises - JOINS on Sales Database.sql: -------------------------------------------------------------------------------- 1 | /*** SQL Exercises - JOINS on Sales Database ***/ 2 | -- https://www.w3resource.com/sql-exercises/sql-joins-exercises.php 3 | 4 | Table: salesman 5 | 6 | salesman_id name city commission 7 | ----------- ---------- ---------- ---------- 8 | 5001 James Hoog New York 0.15 9 | 5002 Nail Knite Paris 0.13 10 | 5005 Pit Alex London 0.11 11 | 5006 Mc Lyon Paris 0.14 12 | 5003 Lauson Hen 0.12 13 | 5007 Paul Adam Rome 0.13 14 | 15 | 16 | Table: customer 17 | 18 | customer_id cust_name city grade salesman_id 19 | ----------- ------------ ---------- ---------- ----------- 20 | 3002 Nick Rimando New York 100 5001 21 | 3005 Graham Zusi California 200 5002 22 | 3001 Brad Guzan London 5005 23 | 3004 Fabian Johns Paris 300 5006 24 | 3007 Brad Davis New York 200 5001 25 | 3009 Geoff Camero Berlin 100 5003 26 | 3008 Julian Green London 300 5002 27 | 3003 Jozy Altidor Moscow 200 5007 28 | 29 | 30 | Table: orders 31 | 32 | ord_no purch_amt ord_date customer_id salesman_id 33 | ---------- ---------- ---------- ----------- ----------- 34 | 70001 150.5 2012-10-05 3005 5002 35 | 70009 270.65 2012-09-10 3001 5005 36 | 70002 65.26 2012-10-05 3002 5001 37 | 70004 110.5 2012-08-17 3009 5003 38 | 70007 948.5 2012-09-10 3005 5002 39 | 70005 2400.6 2012-07-27 3007 5001 40 | 70008 5760 2012-09-10 3002 5001 41 | 70010 1983.43 2012-10-10 3004 5006 42 | 70003 2480.4 2012-10-10 3009 5003 43 | 70012 250.45 2012-06-27 3008 5002 44 | 70011 75.29 2012-08-17 3003 5007 45 | 70013 3045.6 2012-04-25 3002 5001 46 | 47 | 48 | Table: company_mast 49 | 50 | COM_ID COM_NAME 51 | ------ ------------- 52 | 11 Samsung 53 | 12 iBall 54 | 13 Epsion 55 | 14 Zebronics 56 | 15 Asus 57 | 16 Frontech 58 | 59 | 60 | Table: item_mast 61 | 62 | PRO_ID PRO_NAME PRO_PRICE PRO_COM 63 | ------- ------------------------- ---------- ---------- 64 | 101 Mother Board 3200 15 65 | 102 Key Board 450 16 66 | 103 ZIP drive 250 14 67 | 104 Speaker 550 16 68 | 105 Monitor 5000 11 69 | 106 DVD drive 900 12 70 | 107 CD drive 800 12 71 | 108 Printer 2600 13 72 | 109 Refill cartridge 350 13 73 | 110 Mouse 250 12 74 | 75 | 76 | Table: emp_department 77 | 78 | DPT_CODE DPT_NAME DPT_ALLOTMENT 79 | -------- --------------- ------------- 80 | 57 IT 65000 81 | 63 Finance 15000 82 | 47 HR 240000 83 | 27 RD 55000 84 | 89 QC 75000 85 | 86 | 87 | Table: emp_details 88 | 89 | EMP_IDNO EMP_FNAME EMP_LNAME EMP_DEPT 90 | --------- --------------- --------------- ---------- 91 | 127323 Michale Robbin 57 92 | 526689 Carlos Snares 63 93 | 843795 Enric Dosio 57 94 | 328717 Jhon Snares 63 95 | 444527 Joseph Dosni 47 96 | 659831 Zanifer Emily 47 97 | 847674 Kuleswar Sitaraman 57 98 | 748681 Henrey Gabriel 47 99 | 555935 Alex Manuel 57 100 | 539569 George Mardy 27 101 | 733843 Mario Saule 63 102 | 631548 Alan Snappy 27 103 | 839139 Maria Foster 57 104 | 105 | 106 | /* 1. Write a SQL statement to prepare a list with salesman name, customer name and their cities for the salesmen and customer who belongs to the same city. */ 107 | 108 | SELECT s.name, 109 | c.cust_name, 110 | c.city 111 | FROM salesman s 112 | INNER JOIN customer c 113 | ON s.city = c.city; 114 | 115 | 116 | /* 2. Write a SQL statement to make a list with order no, purchase amount, customer name and their cities for those orders which order amount between 500 and 2000 */ 117 | 118 | SELECT o.ord_no, 119 | o.purch_amt, 120 | c.cust_name, 121 | c.city 122 | FROM orders o 123 | INNER JOIN customer c 124 | ON o.customer_id = c.customer_id 125 | WHERE o.purch_amt BETWEEN 500 AND 2000; 126 | 127 | 128 | /* 3. Write a SQL statement to know which salesman are working for which customer. */ 129 | 130 | SELECT c.cust_name AS "Customer Name", 131 | s.name AS "Salesman" 132 | FROM customer c 133 | INNER JOIN salesman s 134 | ON c.salesman_id = s.salesman_id; 135 | 136 | 137 | /* 4. Write a SQL statement to find the list of customers who appointed a salesman for their jobs who gets a commission from the company is more than 12%. */ 138 | 139 | SELECT c.cust_name AS "Customer Name", 140 | c.city, 141 | s.name AS "Salesman", 142 | s.commission 143 | FROM customer c 144 | INNER JOIN salesman s 145 | ON c.salesman_id = s.salesman_id 146 | WHERE s.commission > 0.12; 147 | 148 | 149 | /* 5. Write a SQL statement to find the list of customers who appointed a salesman for their jobs who does not live in the same city where their customer lives, and gets a commission is above 12% . */ 150 | 151 | SELECT c.cust_name AS "Customer Name", 152 | c.city AS "Customer City", 153 | s.name AS "Salesman", 154 | s.city AS "Salesman City", 155 | s.commission 156 | FROM customer c 157 | INNER JOIN salesman s 158 | ON c.salesman_id = s.salesman_id 159 | WHERE s.commission > 0.12 160 | AND c.city != s.city; 161 | 162 | 163 | /* 6. Write a SQL statement to find the details of an order i.e. order number, order date, amount of order, which customer gives the order and which salesman works for that customer and how much commission he gets for an order. */ 164 | 165 | SELECT o.ord_no, 166 | o.ord_date, 167 | o.purch_amt, 168 | c.cust_name AS "Customer Name", 169 | s.name AS "Salesman", 170 | s.commission 171 | FROM orders o 172 | INNER JOIN customer c 173 | ON o.customer_id=c.customer_id 174 | INNER JOIN salesman s 175 | ON o.salesman_id=s.salesman_id; 176 | 177 | 178 | /* 7. Write a SQL statement to make a join on the tables salesman, customer and orders in such a form that the same column of each table will appear once and only the relational rows will come. 179 | 180 | SELECT * 181 | FROM orders 182 | NATURAL JOIN customer 183 | NATURAL JOIN salesman; 184 | 185 | 186 | /* 8. Write a SQL statement to make a list in ascending order for the customer who works either through a salesman or by own. */ 187 | 188 | SELECT c.cust_name AS "Customer Name" 189 | FROM customer c 190 | LEFT JOIN salesman s 191 | ON c.salesman_id=s.salesman_id 192 | ORDER BY c.customer_id ASC; 193 | 194 | 195 | /* 9. Write a SQL statement to make a list in ascending order for the customer who holds a grade less than 300 and works either through a salesman or by own. */ 196 | 197 | SELECT c.cust_name AS "Customer Name", 198 | c.grade 199 | FROM customer c 200 | LEFT JOIN salesman s 201 | ON c.salesman_id=s.salesman_id 202 | WHERE c.grade < 300 203 | ORDER BY c.customer_id ASC; 204 | 205 | 206 | /* 10. Write a SQL statement to make a report with customer name, city, order number, order date, and order amount in ascending order according to the order date to find that either any of the existing customers have placed no order or placed one or more orders. */ 207 | 208 | SELECT c.cust_name AS "Customer Name", 209 | c.city, 210 | o.ord_no, 211 | o.ord_date, 212 | o.purch_amt 213 | FROM customer c 214 | LEFT OUTER JOIN orders o 215 | ON c.customer_id=o.customer_id 216 | ORDER BY o.ord_date; 217 | 218 | 219 | /* 11. Write a SQL statement to make a report with customer name, city, order number, order date, order amount, salesman name and commission to find that either any of the existing customers have placed no order or placed one or more orders by their salesman or by own. */ 220 | 221 | SELECT c.cust_name AS "Customer Name", 222 | c.city, 223 | o.ord_no, 224 | o.ord_date, 225 | o.purch_amt, 226 | s.name AS "Salesman", 227 | s.commission 228 | FROM customer c 229 | LEFT OUTER JOIN orders o 230 | ON c.customer_id=o.customer_id 231 | LEFT OUTER JOIN salesman s 232 | ON c.salesman_id=s.salesman_id; 233 | 234 | 235 | /* 12. Write a SQL statement to make a list in ascending order for the salesmen who works either for one or more customer or not yet join under any of the customers. */ 236 | 237 | SELECT s.name AS "Salesman" 238 | FROM salesman s 239 | LEFT OUTER JOIN customer c 240 | ON s.salesman_id=c.salesman_id 241 | ORDER BY c.salesman_id ASC; 242 | 243 | 244 | /* 13. Write a SQL statement to make a list for the salesmen who works either for one or more customer or not yet join under any of the customers who placed either one or more orders or no order to their supplier. */ 245 | 246 | SELECT s.name AS "Salesman" 247 | FROM salesman s 248 | LEFT OUTER JOIN customer c 249 | ON s.salesman_id=c.salesman_id 250 | LEFT OUTER JOIN orders o 251 | ON c.customer_id=o.customer_id; 252 | 253 | 254 | /* 14. Write a SQL statement to make a list for the salesmen who either work for one or more customers or yet to join any of the customer. The customer may have placed, either one or more orders on or above order amount 2000 and must have a grade, or he may not have placed any order to the associated supplier. /* 255 | 256 | SELECT s.name AS "Salesman" 257 | FROM salesman s 258 | LEFT OUTER JOIN customer c 259 | ON s.salesman_id=c.salesman_id 260 | LEFT OUTER JOIN orders o 261 | ON c.customer_id=o.customer_id 262 | WHERE o.purch_amt >= 2000 263 | AND grade IS NOT NULL; 264 | 265 | 266 | /* 15. Write a SQL statement to make a report with customer name, city, order no, order date, purchase amount for those customers from the existing list who placed one or more orders or which order(s) have been placed by the customer who is not on the list. */ 267 | 268 | SELECT c.cust_name AS "Customer Name", 269 | c.city, 270 | o.ord_no, 271 | o.ord_date, 272 | o.purch_amt 273 | FROM customer c 274 | RIGHT JOIN orders o 275 | ON c.customer_id= o.customer_id; 276 | 277 | 278 | /* 16. Write a SQL statement to make a report with customer name, city, order no. order date, purchase amount for only those customers on the list who must have a grade and placed one or more orders or which order(s) have been placed by the customer who is neither in the list nor have a grade. */ 279 | 280 | SELECT c.cust_name AS "Customer Name", 281 | c.city, 282 | o.ord_no, 283 | o.ord_date, 284 | o.purch_amt 285 | FROM customer c 286 | FULL OUTER JOIN orders o 287 | ON c.customer_id= o.customer_id 288 | AND c.grade IS NOT NULL; 289 | 290 | 291 | /* 17. Write a SQL statement to make a cartesian product between salesman and customer i.e. each salesman will appear for all customer and vice versa. */ 292 | 293 | SELECT s.name AS "Salesman", 294 | c.cust_name AS "Customer Name" 295 | FROM salesman s 296 | CROSS JOIN customer c; 297 | 298 | 299 | /* 18. Write a SQL statement to make a cartesian product between salesman and customer i.e. each salesman will appear for all customer and vice versa for that customer who belongs to a city. */ 300 | 301 | SELECT s.name AS "Salesman", 302 | c.cust_name AS "Customer Name" 303 | FROM salesman s 304 | CROSS JOIN customer c 305 | WHERE s.city IS NOT NULL; 306 | 307 | 308 | /* 19. Write a SQL statement to make a cartesian product between salesman and customer i.e. each salesman will appear for all customer and vice versa for those salesmen who belongs to a city and the customers who must have a grade. */ 309 | 310 | SELECT s.name AS "Salesman", 311 | c.cust_name AS "Customer Name" 312 | FROM salesman s 313 | CROSS JOIN customer c 314 | WHERE s.city IS NOT NULL 315 | AND c.grade IS NOT NULL; 316 | 317 | 318 | /* 20. Write a SQL statement to make a cartesian product between salesman and customer i.e. each salesman will appear for all customer and vice versa for those salesmen who must belong a city which is not the same as his customer and the customers should have an own grade. */ 319 | 320 | SELECT s.name AS "Salesman", 321 | c.cust_name AS "Customer Name" 322 | FROM salesman s 323 | CROSS JOIN customer c 324 | WHERE s.city IS NOT NULL 325 | AND s.city != c.city 326 | AND c.grade IS NOT NULL; 327 | 328 | 329 | /* 21. Write a SQL query to display all the data from the item_mast, including all the data for each item's producer company. */ 330 | 331 | SELECT * 332 | FROM item_mast i 333 | INNER JOIN company_mast c 334 | ON i.pro_com=c.com_id; 335 | 336 | 337 | /* 22. Write a SQL query to display the item name, price, and company name of all the products. */ 338 | 339 | SELECT i.pro_name, 340 | i.pro_price, 341 | c.com_name 342 | FROM item_mast i 343 | INNER JOIN company_mast c 344 | ON i.pro_com=c.com_id; 345 | 346 | 347 | /* 23. Write a SQL query to display the average price of items of each company, showing the name of the company. */ 348 | 349 | SELECT c.com_name, 350 | AVG(i.pro_price) 351 | FROM item_mast i 352 | INNER JOIN company_mast c 353 | ON i.pro_com=c.com_id 354 | GROUP BY c.com_name; 355 | 356 | 357 | /* 24. Write a SQL query to display the names of the company whose products have an average price larger than or equal to Rs. 350. */ 358 | 359 | SELECT c.com_name, 360 | AVG(i.pro_price) 361 | FROM item_mast i 362 | INNER JOIN company_mast c 363 | ON i.pro_com=c.com_id 364 | GROUP BY c.com_name 365 | HAVING AVG(i.pro_price) >= 350; 366 | 367 | 368 | /* 25. Write a SQL query to display the name of each company along with the ID and price for their most expensive product. */ 369 | 370 | SELECT c.com_name, 371 | i.pro_name, 372 | i.pro_price 373 | FROM item_mast i 374 | INNER JOIN company_mast c 375 | ON i.pro_com=c.com_id 376 | AND i.pro_price = (SELECT MAX(i.pro_price) 377 | FROM item_mast i 378 | WHERE i.pro_com = c.com_id); 379 | 380 | 381 | /* 26. Write a query in SQL to display all the data of employees including their department. */ 382 | 383 | SELECT * 384 | FROM emp_details edet 385 | INNER JOIN emp_department edep 386 | ON edet.emp_dept = edep.dpt_code; 387 | 388 | 389 | /* 27. Write a query in SQL to display the first name and last name of each employee, along with the name and sanction amount for their department. */ 390 | 391 | SELECT edet.emp_fname AS "First Name", 392 | edet.emp_lname AS "Last Name", 393 | edep.dpt_name AS "Department Name", 394 | edep.dpt_allotment AS "Amount Allotted" 395 | FROM emp_details edet 396 | INNER JOIN emp_department edep 397 | ON edet.emp_dept = edep.dpt_code; 398 | 399 | 400 | /* 28. Write a query in SQL to find the first name and last name of employees working for departments with a budget more than Rs. 50000. */ 401 | 402 | SELECT edet.emp_fname AS "First Name", 403 | edet.emp_lname AS "Last Name" 404 | FROM emp_details edet 405 | INNER JOIN emp_department edep 406 | ON edet.emp_dept = edep.dpt_code 407 | AND edep.dpt_allotment > 50000; 408 | 409 | 410 | /* 29. Write a query in SQL to find the names of departments where more than two employees are working. */ 411 | 412 | SELECT edep.dpt_name, 413 | COUNT(edet.emp_idno) 414 | FROM emp_details edet 415 | INNER JOIN emp_department edep 416 | ON edet.emp_dept = edep.dpt_code 417 | GROUP BY edep.dpt_name 418 | HAVING COUNT(edet.emp_idno) > 2; 419 | 420 | -------------------------------------------------------------------------------- /SQL Exercises - SUBQUERIES on Sales Database.sql: -------------------------------------------------------------------------------- 1 | /*** SQL Exercises - SUBQUERIES on Sales Database ***/ 2 | -- https://www.w3resource.com/sql-exercises/subqueries/index.php 3 | 4 | Table: salesman 5 | 6 | salesman_id name city commission 7 | ----------- ---------- ---------- ---------- 8 | 5001 James Hoog New York 0.15 9 | 5002 Nail Knite Paris 0.13 10 | 5005 Pit Alex London 0.11 11 | 5006 Mc Lyon Paris 0.14 12 | 5003 Lauson Hen 0.12 13 | 5007 Paul Adam Rome 0.13 14 | 15 | 16 | Table: customer 17 | 18 | customer_id cust_name city grade salesman_id 19 | ----------- ------------ ---------- ---------- ----------- 20 | 3002 Nick Rimando New York 100 5001 21 | 3005 Graham Zusi California 200 5002 22 | 3001 Brad Guzan London 5005 23 | 3004 Fabian Johns Paris 300 5006 24 | 3007 Brad Davis New York 200 5001 25 | 3009 Geoff Camero Berlin 100 5003 26 | 3008 Julian Green London 300 5002 27 | 3003 Jozy Altidor Moscow 200 5007 28 | 29 | 30 | Table: orders 31 | 32 | ord_no purch_amt ord_date customer_id salesman_id 33 | ---------- ---------- ---------- ----------- ----------- 34 | 70001 150.5 2012-10-05 3005 5002 35 | 70009 270.65 2012-09-10 3001 5005 36 | 70002 65.26 2012-10-05 3002 5001 37 | 70004 110.5 2012-08-17 3009 5003 38 | 70007 948.5 2012-09-10 3005 5002 39 | 70005 2400.6 2012-07-27 3007 5001 40 | 70008 5760 2012-09-10 3002 5001 41 | 70010 1983.43 2012-10-10 3004 5006 42 | 70003 2480.4 2012-10-10 3009 5003 43 | 70012 250.45 2012-06-27 3008 5002 44 | 70011 75.29 2012-08-17 3003 5007 45 | 70013 3045.6 2012-04-25 3002 5001 46 | 47 | 48 | Table: company_mast 49 | 50 | COM_ID COM_NAME 51 | ------ ------------- 52 | 11 Samsung 53 | 12 iBall 54 | 13 Epsion 55 | 14 Zebronics 56 | 15 Asus 57 | 16 Frontech 58 | 59 | 60 | Table: item_mast 61 | 62 | PRO_ID PRO_NAME PRO_PRICE PRO_COM 63 | ------- ------------------------- ---------- ---------- 64 | 101 Mother Board 3200 15 65 | 102 Key Board 450 16 66 | 103 ZIP drive 250 14 67 | 104 Speaker 550 16 68 | 105 Monitor 5000 11 69 | 106 DVD drive 900 12 70 | 107 CD drive 800 12 71 | 108 Printer 2600 13 72 | 109 Refill cartridge 350 13 73 | 110 Mouse 250 12 74 | 75 | 76 | Table: emp_department 77 | 78 | DPT_CODE DPT_NAME DPT_ALLOTMENT 79 | -------- --------------- ------------- 80 | 57 IT 65000 81 | 63 Finance 15000 82 | 47 HR 240000 83 | 27 RD 55000 84 | 89 QC 75000 85 | 86 | 87 | Table: emp_details 88 | 89 | EMP_IDNO EMP_FNAME EMP_LNAME EMP_DEPT 90 | --------- --------------- --------------- ---------- 91 | 127323 Michale Robbin 57 92 | 526689 Carlos Snares 63 93 | 843795 Enric Dosio 57 94 | 328717 Jhon Snares 63 95 | 444527 Joseph Dosni 47 96 | 659831 Zanifer Emily 47 97 | 847674 Kuleswar Sitaraman 57 98 | 748681 Henrey Gabriel 47 99 | 555935 Alex Manuel 57 100 | 539569 George Mardy 27 101 | 733843 Mario Saule 63 102 | 631548 Alan Snappy 27 103 | 839139 Maria Foster 57 104 | 105 | 106 | /* 1. Write a query to display all the orders from the orders table issued by the salesman 'Paul Adam'. */ 107 | 108 | SELECT * 109 | FROM orders 110 | WHERE salesman_id = (SELECT salesman_id 111 | FROM salesman 112 | WHERE name = 'Paul Adam'); 113 | 114 | 115 | /* 2. Write a query to display all the orders for the salesman who belongs to the city London. *.' 116 | 117 | SELECT * 118 | FROM orders 119 | WHERE salesman_id = (SELECT salesman_id 120 | FROM salesman 121 | WHERE city = 'London'); 122 | 123 | 124 | /* 3. Write a query to find all the orders issued against the salesman who may works for customer whose id is 3007. *. 125 | 126 | SELECT * 127 | FROM orders 128 | WHERE salesman_id = (SELECT salesman_id 129 | FROM orders 130 | WHERE customer_id = 3007); 131 | 132 | 133 | /* 4. Write a query to display all the orders which values are greater than the average order value for 10th October 2012. /* 134 | 135 | SELECT * 136 | FROM orders 137 | WHERE purch_amt > (SELECT AVG(purch_amt) 138 | FROM orders 139 | WHERE ord_date = '2012-10-10'); 140 | 141 | 142 | /* 5. Write a query to find all orders attributed to a salesman in New York. */ 143 | 144 | SELECT * 145 | FROM orders 146 | WHERE salesman_id IN (SELECT salesman_id 147 | FROM salesman 148 | WHERE city = 'New York'); 149 | 150 | 151 | /* 6. Write a query to display the commission of all the salesmen servicing customers in Paris. */ 152 | 153 | SELECT commission 154 | FROM salesman 155 | WHERE salesman_id IN (SELECT salesman_id 156 | FROM customer 157 | WHERE city = 'Paris'); 158 | 159 | 160 | /* 7. Write a query to display all the customers whose id is 2001 bellow the salesman ID of Mc Lyon. */ 161 | 162 | SELECT * 163 | FROM customer 164 | WHERE customer_id = (SELECT salesman_id - 2001 165 | FROM salesman 166 | WHERE name = 'Mc Lyon'); 167 | 168 | 169 | /* 8. Write a query to count the customers with grades above New York's average. */ 170 | 171 | SELECT COUNT(*) 172 | FROM customer 173 | WHERE grade > (SELECT AVG(grade) 174 | FROM customer 175 | WHERE city = 'New York'); 176 | 177 | 178 | /* 9. Write a query to display all customers with orders on October 5, 2012. */ 179 | /* Note: These set of exercises focus on subqueries, so I wrote the query below. The 2nd query also works and displays order information as well. */ 180 | 181 | SELECT * 182 | FROM customer 183 | WHERE customer_id IN (SELECT customer_id 184 | FROM orders 185 | WHERE ord_date = '2012-10-05'); 186 | 187 | SELECT * 188 | FROM customer a, orders b 189 | WHERE a.customer_id = b.customer_id 190 | AND b.ord_date = '2012-10-05'; 191 | 192 | 193 | /* 10. Write a query to display all the customers with orders issued on date 17th August, 2012. */ 194 | /* Note: These set of exercises focus on subqueries, so I wrote the query below. The 2nd query also works and displays order information as well. */ 195 | 196 | SELECT * 197 | FROM customer 198 | WHERE customer_id IN (SELECT customer_id 199 | FROM orders 200 | WHERE ord_date = '2012-08-17'); 201 | 202 | SELECT b.*, 203 | a.cust_name 204 | FROM customer a, orders b 205 | WHERE a.customer_id = b.customer_id 206 | AND b.ord_date = '2012-08-17'; 207 | 208 | 209 | /* 11. Write a query to find the name and numbers of all salesmen who had more than one customer. */ 210 | 211 | SELECT salesman_id, 212 | name 213 | FROM salesman 214 | WHERE salesman_id IN (SELECT salesman_id 215 | FROM customer 216 | GROUP BY salesman_id 217 | HAVING COUNT(*) > 1); 218 | 219 | SELECT salesman_id, 220 | name 221 | FROM salesman a 222 | WHERE 1 < (SELECT COUNT(*) 223 | FROM customer 224 | WHERE salesman_id = a.salesman_id); 225 | 226 | 227 | /* 12. Write a query to find all orders with order amounts which are above-average amounts for their customers. */ 228 | 229 | SELECT * 230 | FROM orders a 231 | WHERE purch_amt > (SELECT AVG(purch_amt) 232 | FROM orders b 233 | WHERE b.customer_id = a.customer_id); 234 | 235 | 236 | /* 13. Write a queries to find all orders with order amounts which are on or above-average amounts for their customers. */ 237 | 238 | SELECT * 239 | FROM orders a 240 | WHERE purch_amt >= (SELECT AVG(purch_amt) 241 | FROM orders b 242 | WHERE b.customer_id = a.customer_id); 243 | 244 | 245 | /* 14. Write a query to find the sums of the amounts from the orders table, grouped by date, eliminating all those dates where the sum was not at least 1000.00 above the maximum order amount for that date. */ 246 | 247 | SELECT ord_date, 248 | SUM(purch_amt) 249 | FROM orders a 250 | GROUP BY ord_date 251 | HAVING SUM(purch_amt) > (SELECT MAX(purch_amt) + 1000 252 | FROM orders b 253 | WHERE a.ord_date = b.ord_date); 254 | 255 | 256 | /* 15. Write a query to extract the data from the customer table if and only if one or more of the customers in the customer table are located in London. */ 257 | 258 | SELECT * 259 | FROM customer 260 | WHERE EXISTS (SELECT * 261 | FROM customer 262 | WHERE city = 'London'); 263 | 264 | 265 | /* 16. Write a query to find the salesmen who have multiple customers. */ 266 | 267 | SELECT * 268 | FROM salesman 269 | WHERE salesman_id IN (SELECT salesman_id 270 | FROM customer 271 | GROUP BY salesman_id 272 | HAVING COUNT(*) > 1); 273 | 274 | SELECT * 275 | FROM salesman 276 | WHERE salesman_id IN (SELECT DISTINCT salesman_id 277 | FROM customer a 278 | WHERE EXISTS (SELECT * 279 | FROM customer b 280 | WHERE b.salesman_id=a.salesman_id 281 | AND b.cust_name <> a.cust_name)); 282 | 283 | 284 | /* 17. Write a query to find all the salesmen who worked for only one customer. */ 285 | 286 | SELECT * 287 | FROM salesman 288 | WHERE salesman_id IN (SELECT salesman_id 289 | FROM customer 290 | GROUP BY salesman_id 291 | HAVING COUNT(customer_id) = 1); 292 | 293 | 294 | /* 18. Write a query that extract the rows of all salesmen who have customers with more than one orders. */ 295 | 296 | SELECT * 297 | FROM salesman 298 | WHERE salesman_id IN (SELECT salesman_id 299 | FROM customer 300 | WHERE customer_id IN (SELECT customer_id 301 | FROM orders 302 | GROUP BY customer_id 303 | HAVING COUNT(*) > 1)); 304 | 305 | SELECT * 306 | FROM salesman a 307 | WHERE EXISTS (SELECT * 308 | FROM customer b 309 | WHERE a.salesman_id=b.salesman_id 310 | AND 1 < (SELECT COUNT(*) 311 | FROM orders 312 | WHERE orders.customer_id=b.customer_id)); 313 | 314 | 315 | /* 19. Write a query to find salesmen with all information who lives in the city where any of the customers lives. */ 316 | 317 | SELECT * 318 | FROM salesman 319 | WHERE city IN (SELECT city 320 | FROM customer); 321 | 322 | SELECT * 323 | FROM salesman 324 | WHERE city = ANY (SELECT city 325 | FROM customer); 326 | 327 | 328 | /* 20. Write a query to find all the salesmen for whom there are customers that follow them. */ 329 | 330 | SELECT * 331 | FROM salesman 332 | WHERE city IN (SELECT city 333 | FROM customer); 334 | 335 | 336 | /* 21. Write a query to display the salesmen which name are alphabetically lower than the name of the customers. */ 337 | 338 | SELECT * 339 | FROM salesman a 340 | WHERE EXISTS (SELECT * 341 | FROM customer b 342 | WHERE a.name < b.cust_name); 343 | 344 | 345 | /* 22. Write a query to display the customers who have a greater gradation than any customer who belongs to the alphabetically lower than the city New York. */ 346 | 347 | SELECT * 348 | FROM customer 349 | WHERE grade > ANY (SELECT grade 350 | FROM customer 351 | WHERE city < 'New York'); 352 | 353 | 354 | /* 23. Write a query to display all the orders that had amounts that were greater than at least one of the orders on September 10th 2012. */ 355 | 356 | SELECT * 357 | FROM orders 358 | WHERE purch_amt > ANY (SELECT purch_amt 359 | FROM orders 360 | WHERE ord_date = '2012-09-10'); 361 | 362 | 363 | /* 24. Write a query to find all orders with an amount smaller than any amount for a customer in London. */ 364 | 365 | SELECT * 366 | FROM orders 367 | WHERE purch_amt < ANY (SELECT purch_amt 368 | FROM orders 369 | WHERE customer_id IN (SELECT customer_id 370 | FROM customer 371 | WHERE city = 'London')); 372 | 373 | SELECT * 374 | FROM orders 375 | WHERE purch_amt < ANY (SELECT purch_amt 376 | FROM orders a, customer b 377 | WHERE a.customer_id = b.customer_id 378 | AND b.city = 'London'); 379 | 380 | 381 | /* 25. Write a query to display all orders with an amount smaller than the maximum amount for a customers in London. */ 382 | 383 | SELECT * 384 | FROM orders 385 | WHERE purch_amt < (SELECT MAX(purch_amt) 386 | FROM orders 387 | WHERE customer_id IN (SELECT customer_id 388 | FROM customer 389 | WHERE city = 'London')); 390 | 391 | SELECT * 392 | FROM orders 393 | WHERE purch_amt < (SELECT MAX(purch_amt) 394 | FROM orders a, customer b 395 | WHERE a.customer_id = b.customer_id 396 | AND b.city = 'London'); 397 | 398 | 399 | /* 26. Write a query to display only those customers whose grade are, in fact, higher than every customer in New York. */ 400 | 401 | SELECT * 402 | FROM customer 403 | WHERE grade > ALL (SELECT grade 404 | FROM customer 405 | WHERE city = 'New York'); 406 | 407 | 408 | /* 27. Write a query to find only those customers whose grade are, higher than every customer to the city New York. */ 409 | 410 | SELECT * 411 | FROM customer 412 | WHERE grade > ALL (SELECT grade 413 | FROM customer 414 | WHERE city = 'New York'); 415 | 416 | 417 | /* 28. Write a query to get all the information for those customers whose grade is not as the grade of customer who belongs to the city London */ 418 | 419 | SELECT * 420 | FROM customer 421 | WHERE grade != ANY (SELECT grade 422 | FROM customer 423 | WHERE city = 'London'); 424 | 425 | 426 | /* 29. Write a query to find all those customers whose grade are not as the grade, belongs to the city Paris. */ 427 | 428 | SELECT * 429 | FROM customer 430 | WHERE grade != ANY (SELECT grade 431 | FROM customer 432 | WHERE city = 'Paris'); 433 | 434 | SELECT * 435 | FROM customer 436 | WHERE grade NOT IN (SELECT grade 437 | FROM customer 438 | WHERE city = 'Paris'); 439 | 440 | 441 | /* 30. Write a query to find all those customers who hold a different grade than any customer of the city Dallas. */ 442 | 443 | SELECT * 444 | FROM customer 445 | WHERE grade NOT IN (SELECT grade 446 | FROM customer 447 | WHERE city = 'Dallas'); 448 | 449 | SELECT * 450 | FROM customer 451 | WHERE NOT grade = ANY (SELECT grade 452 | FROM customer 453 | WHERE city = 'Dallas'); 454 | 455 | 456 | /* 31. Write a SQL query to find the average price of each manufacturer's products along with their name. */ 457 | 458 | SELECT AVG(pro_price), 459 | com_name 460 | FROM item_mast, company_mast 461 | WHERE item_mast.pro_com = company_mast.com_id 462 | GROUP BY com_name; 463 | 464 | 465 | /* 32. Write a SQL query to display the average price of the products which is more than or equal to 350 along with their names. */ 466 | 467 | SELECT c.com_name, 468 | AVG(i.pro_price) 469 | FROM item_mast i 470 | INNER JOIN company_mast c 471 | ON i.pro_com=c.com_id 472 | GROUP BY c.com_name 473 | HAVING AVG(i.pro_price) >= 350; 474 | 475 | SELECT AVG(pro_price) AS "Average Price", 476 | company_mast.com_name AS "Company" 477 | FROM item_mast, company_mast 478 | WHERE item_mast.pro_com = company_mast.com_id 479 | GROUP BY company_mast.com_name 480 | HAVING AVG(pro_price) >= 350; 481 | 482 | 483 | /* 33. Write a SQL query to display the name of each company, price for their most expensive product along with their Name. */ 484 | 485 | SELECT c.com_name, 486 | i.pro_name, 487 | i.pro_price 488 | FROM item_mast i 489 | INNER JOIN company_mast c 490 | ON i.pro_com = c.com_id 491 | AND i.pro_price = (SELECT MAX(i.pro_price) 492 | FROM item_mast i 493 | WHERE i.pro_com = c.com_id); 494 | 495 | SELECT c.com_name, 496 | i.pro_name, 497 | i.pro_price 498 | FROM item_mast i, company_mast c 499 | WHERE i.pro_com = c.com_id 500 | AND i.pro_price = (SELECT MAX(i.pro_price) 501 | FROM item_mast i 502 | WHERE i.pro_com = c.com_id); 503 | 504 | 505 | /* 34. Write a query in SQL to find all the details of employees whose last name is Gabriel or Dosio. */ 506 | 507 | SELECT * 508 | FROM emp_details 509 | WHERE emp_lname IN ('Gabriel', 'Dosio'); 510 | 511 | 512 | /* 35. Write a query in SQL to display all the details of employees who works in department 89 or 63. */ 513 | 514 | SELECT * 515 | FROM emp_details 516 | WHERE emp_dept IN (89, 63); 517 | 518 | 519 | /* 36. Write a query in SQL to display the first name and last name of employees working for the department which allotment amount is more than Rs.50000. */ 520 | 521 | SELECT emp_fname, 522 | emp_lname 523 | FROM emp_details 524 | WHERE emp_dept IN (SELECT dpt_code 525 | FROM emp_department 526 | WHERE dpt_allotment > 50000); 527 | 528 | 529 | /* 37. Write a query in SQL to find the departments which sanction amount is larger than the average sanction amount of all the departments. */ 530 | 531 | SELECT * 532 | FROM emp_department 533 | WHERE dpt_allotment > (SELECT AVG(dpt_allotment) 534 | FROM emp_department); 535 | 536 | 537 | /* 38. Write a query in SQL to find the names of departments with more than two employees are working. */ 538 | 539 | SELECT dpt_name 540 | FROM emp_department 541 | WHERE dpt_code IN (SELECT emp_dept 542 | FROM emp_details 543 | GROUP BY emp_dept 544 | HAVING COUNT(*) > 2); 545 | 546 | 547 | /* 39. Write a query in SQL to find the first name and last name of employees working for departments which sanction amount is second lowest. */ 548 | 549 | SELECT emp_fname, 550 | emp_lname 551 | FROM emp_details 552 | WHERE emp_dept IN (SELECT dpt_code 553 | FROM emp_department 554 | WHERE dpt_allotment = (SELECT MIN(dpt_allotment) 555 | FROM emp_department 556 | WHERE dpt_allotment > (SELECT MIN(dpt_allotment) 557 | FROM emp_department))); 558 | 559 | -------------------------------------------------------------------------------- /SQL Exercises - JOINS on HR Database.sql: -------------------------------------------------------------------------------- 1 | /*** SQL Exercises - JOINS on HR Database***/ 2 | -- https://www.w3resource.com/sql-exercises/joins-hr/index.php 3 | 4 | Table: employees 5 | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ 6 | | EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER | HIRE_DATE | JOB_ID | SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID | 7 | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ 8 | | 100 | Steven | King | SKING | 515.123.4567 | 2003-06-17 | AD_PRES | 24000.00 | 0.00 | 0 | 90 | 9 | | 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | 2005-09-21 | AD_VP | 17000.00 | 0.00 | 100 | 90 | 10 | | 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | 2001-01-13 | AD_VP | 17000.00 | 0.00 | 100 | 90 | 11 | | 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | 2006-01-03 | IT_PROG | 9000.00 | 0.00 | 102 | 60 | 12 | | 104 | Bruce | Ernst | BERNST | 590.423.4568 | 2007-05-21 | IT_PROG | 6000.00 | 0.00 | 103 | 60 | 13 | | 105 | David | Austin | DAUSTIN | 590.423.4569 | 2005-06-25 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | 14 | | 106 | Valli | Pataballa | VPATABAL | 590.423.4560 | 2006-02-05 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | 15 | | 107 | Diana | Lorentz | DLORENTZ | 590.423.5567 | 2007-02-07 | IT_PROG | 4200.00 | 0.00 | 103 | 60 | 16 | | 108 | Nancy | Greenberg | NGREENBE | 515.124.4569 | 2002-08-17 | FI_MGR | 12008.00 | 0.00 | 101 | 100 | 17 | | 109 | Daniel | Faviet | DFAVIET | 515.124.4169 | 2002-08-16 | FI_ACCOUNT | 9000.00 | 0.00 | 108 | 100 | 18 | | 110 | John | Chen | JCHEN | 515.124.4269 | 2005-09-28 | FI_ACCOUNT | 8200.00 | 0.00 | 108 | 100 | 19 | | 111 | Ismael | Sciarra | ISCIARRA | 515.124.4369 | 2005-09-30 | FI_ACCOUNT | 7700.00 | 0.00 | 108 | 100 | 20 | | 112 | Jose Manuel | Urman | JMURMAN | 515.124.4469 | 2006-03-07 | FI_ACCOUNT | 7800.00 | 0.00 | 108 | 100 | 21 | | 113 | Luis | Popp | LPOPP | 515.124.4567 | 2007-12-07 | FI_ACCOUNT | 6900.00 | 0.00 | 108 | 100 | 22 | | 114 | Den | Raphaely | DRAPHEAL | 515.127.4561 | 2002-12-07 | PU_MAN | 11000.00 | 0.00 | 100 | 30 | 23 | | 115 | Alexander | Khoo | AKHOO | 515.127.4562 | 2003-05-18 | PU_CLERK | 3100.00 | 0.00 | 114 | 30 | 24 | | 116 | Shelli | Baida | SBAIDA | 515.127.4563 | 2005-12-24 | PU_CLERK | 2900.00 | 0.00 | 114 | 30 | 25 | | 117 | Sigal | Tobias | STOBIAS | 515.127.4564 | 2005-07-24 | PU_CLERK | 2800.00 | 0.00 | 114 | 30 | 26 | | 118 | Guy | Himuro | GHIMURO | 515.127.4565 | 2006-11-15 | PU_CLERK | 2600.00 | 0.00 | 114 | 30 | 27 | | 119 | Karen | Colmenares | KCOLMENA | 515.127.4566 | 2007-08-10 | PU_CLERK | 2500.00 | 0.00 | 114 | 30 | 28 | | 120 | Matthew | Weiss | MWEISS | 650.123.1234 | 2004-07-18 | ST_MAN | 8000.00 | 0.00 | 100 | 50 | 29 | | 121 | Adam | Fripp | AFRIPP | 650.123.2234 | 2005-04-10 | ST_MAN | 8200.00 | 0.00 | 100 | 50 | 30 | | 122 | Payam | Kaufling | PKAUFLIN | 650.123.3234 | 2003-05-01 | ST_MAN | 7900.00 | 0.00 | 100 | 50 | 31 | | 123 | Shanta | Vollman | SVOLLMAN | 650.123.4234 | 2005-10-10 | ST_MAN | 6500.00 | 0.00 | 100 | 50 | 32 | | 124 | Kevin | Mourgos | KMOURGOS | 650.123.5234 | 2007-11-16 | ST_MAN | 5800.00 | 0.00 | 100 | 50 | 33 | | 125 | Julia | Nayer | JNAYER | 650.124.1214 | 2005-07-16 | ST_CLERK | 3200.00 | 0.00 | 120 | 50 | 34 | | 126 | Irene | Mikkilineni | IMIKKILI | 650.124.1224 | 2006-09-28 | ST_CLERK | 2700.00 | 0.00 | 120 | 50 | 35 | | 127 | James | Landry | JLANDRY | 650.124.1334 | 2007-01-14 | ST_CLERK | 2400.00 | 0.00 | 120 | 50 | 36 | | 128 | Steven | Markle | SMARKLE | 650.124.1434 | 2008-03-08 | ST_CLERK | 2200.00 | 0.00 | 120 | 50 | 37 | | 129 | Laura | Bissot | LBISSOT | 650.124.5234 | 2005-08-20 | ST_CLERK | 3300.00 | 0.00 | 121 | 50 | 38 | | 130 | Mozhe | Atkinson | MATKINSO | 650.124.6234 | 2005-10-30 | ST_CLERK | 2800.00 | 0.00 | 121 | 50 | 39 | | 131 | James | Marlow | JAMRLOW | 650.124.7234 | 2005-02-16 | ST_CLERK | 2500.00 | 0.00 | 121 | 50 | 40 | | 132 | TJ | Olson | TJOLSON | 650.124.8234 | 2007-04-10 | ST_CLERK | 2100.00 | 0.00 | 121 | 50 | 41 | | 133 | Jason | Mallin | JMALLIN | 650.127.1934 | 2004-06-14 | ST_CLERK | 3300.00 | 0.00 | 122 | 50 | 42 | | 134 | Michael | Rogers | MROGERS | 650.127.1834 | 2006-08-26 | ST_CLERK | 2900.00 | 0.00 | 122 | 50 | 43 | | 135 | Ki | Gee | KGEE | 650.127.1734 | 2007-12-12 | ST_CLERK | 2400.00 | 0.00 | 122 | 50 | 44 | | 136 | Hazel | Philtanker | HPHILTAN | 650.127.1634 | 2008-02-06 | ST_CLERK | 2200.00 | 0.00 | 122 | 50 | 45 | | 137 | Renske | Ladwig | RLADWIG | 650.121.1234 | 2003-07-14 | ST_CLERK | 3600.00 | 0.00 | 123 | 50 | 46 | | 138 | Stephen | Stiles | SSTILES | 650.121.2034 | 2005-10-26 | ST_CLERK | 3200.00 | 0.00 | 123 | 50 | 47 | | 139 | John | Seo | JSEO | 650.121.2019 | 2006-02-12 | ST_CLERK | 2700.00 | 0.00 | 123 | 50 | 48 | | 140 | Joshua | Patel | JPATEL | 650.121.1834 | 2006-04-06 | ST_CLERK | 2500.00 | 0.00 | 123 | 50 | 49 | | 141 | Trenna | Rajs | TRAJS | 650.121.8009 | 2003-10-17 | ST_CLERK | 3500.00 | 0.00 | 124 | 50 | 50 | | 142 | Curtis | Davies | CDAVIES | 650.121.2994 | 2005-01-29 | ST_CLERK | 3100.00 | 0.00 | 124 | 50 | 51 | | 143 | Randall | Matos | RMATOS | 650.121.2874 | 2006-03-15 | ST_CLERK | 2600.00 | 0.00 | 124 | 50 | 52 | | 144 | Peter | Vargas | PVARGAS | 650.121.2004 | 2006-07-09 | ST_CLERK | 2500.00 | 0.00 | 124 | 50 | 53 | | 145 | John | Russell | JRUSSEL | 011.44.1344.429268 | 2004-10-01 | SA_MAN | 14000.00 | 0.40 | 100 | 80 | 54 | | 146 | Karen | Partners | KPARTNER | 011.44.1344.467268 | 2005-01-05 | SA_MAN | 13500.00 | 0.30 | 100 | 80 | 55 | | 147 | Alberto | Errazuriz | AERRAZUR | 011.44.1344.429278 | 2005-03-10 | SA_MAN | 12000.00 | 0.30 | 100 | 80 | 56 | | 148 | Gerald | Cambrault | GCAMBRAU | 011.44.1344.619268 | 2007-10-15 | SA_MAN | 11000.00 | 0.30 | 100 | 80 | 57 | | 149 | Eleni | Zlotkey | EZLOTKEY | 011.44.1344.429018 | 2008-01-29 | SA_MAN | 10500.00 | 0.20 | 100 | 80 | 58 | | 150 | Peter | Tucker | PTUCKER | 011.44.1344.129268 | 2005-01-30 | SA_REP | 10000.00 | 0.30 | 145 | 80 | 59 | | 151 | David | Bernstein | DBERNSTE | 011.44.1344.345268 | 2005-03-24 | SA_REP | 9500.00 | 0.25 | 145 | 80 | 60 | | 152 | Peter | Hall | PHALL | 011.44.1344.478968 | 2005-08-20 | SA_REP | 9000.00 | 0.25 | 145 | 80 | 61 | | 153 | Christopher | Olsen | COLSEN | 011.44.1344.498718 | 2006-03-30 | SA_REP | 8000.00 | 0.20 | 145 | 80 | 62 | | 154 | Nanette | Cambrault | NCAMBRAU | 011.44.1344.987668 | 2006-12-09 | SA_REP | 7500.00 | 0.20 | 145 | 80 | 63 | | 155 | Oliver | Tuvault | OTUVAULT | 011.44.1344.486508 | 2007-11-23 | SA_REP | 7000.00 | 0.15 | 145 | 80 | 64 | | 156 | Janette | King | JKING | 011.44.1345.429268 | 2004-01-30 | SA_REP | 10000.00 | 0.35 | 146 | 80 | 65 | | 157 | Patrick | Sully | PSULLY | 011.44.1345.929268 | 2004-03-04 | SA_REP | 9500.00 | 0.35 | 146 | 80 | 66 | | 158 | Allan | McEwen | AMCEWEN | 011.44.1345.829268 | 2004-08-01 | SA_REP | 9000.00 | 0.35 | 146 | 80 | 67 | | 159 | Lindsey | Smith | LSMITH | 011.44.1345.729268 | 2005-03-10 | SA_REP | 8000.00 | 0.30 | 146 | 80 | 68 | | 160 | Louise | Doran | LDORAN | 011.44.1345.629268 | 2005-12-15 | SA_REP | 7500.00 | 0.30 | 146 | 80 | 69 | | 161 | Sarath | Sewall | SSEWALL | 011.44.1345.529268 | 2006-11-03 | SA_REP | 7000.00 | 0.25 | 146 | 80 | 70 | | 162 | Clara | Vishney | CVISHNEY | 011.44.1346.129268 | 2005-11-11 | SA_REP | 10500.00 | 0.25 | 147 | 80 | 71 | | 163 | Danielle | Greene | DGREENE | 011.44.1346.229268 | 2007-03-19 | SA_REP | 9500.00 | 0.15 | 147 | 80 | 72 | | 164 | Mattea | Marvins | MMARVINS | 011.44.1346.329268 | 2008-01-24 | SA_REP | 7200.00 | 0.10 | 147 | 80 | 73 | | 165 | David | Lee | DLEE | 011.44.1346.529268 | 2008-02-23 | SA_REP | 6800.00 | 0.10 | 147 | 80 | 74 | | 166 | Sundar | Ande | SANDE | 011.44.1346.629268 | 2008-03-24 | SA_REP | 6400.00 | 0.10 | 147 | 80 | 75 | | 167 | Amit | Banda | ABANDA | 011.44.1346.729268 | 2008-04-21 | SA_REP | 6200.00 | 0.10 | 147 | 80 | 76 | | 168 | Lisa | Ozer | LOZER | 011.44.1343.929268 | 2005-03-11 | SA_REP | 11500.00 | 0.25 | 148 | 80 | 77 | | 169 | Harrison | Bloom | HBLOOM | 011.44.1343.829268 | 2006-03-23 | SA_REP | 10000.00 | 0.20 | 148 | 80 | 78 | | 170 | Tayler | Fox | TFOX | 011.44.1343.729268 | 2006-01-24 | SA_REP | 9600.00 | 0.20 | 148 | 80 | 79 | | 171 | William | Smith | WSMITH | 011.44.1343.629268 | 2007-02-23 | SA_REP | 7400.00 | 0.15 | 148 | 80 | 80 | | 172 | Elizabeth | Bates | EBATES | 011.44.1343.529268 | 2007-03-24 | SA_REP | 7300.00 | 0.15 | 148 | 80 | 81 | | 173 | Sundita | Kumar | SKUMAR | 011.44.1343.329268 | 2008-04-21 | SA_REP | 6100.00 | 0.10 | 148 | 80 | 82 | | 174 | Ellen | Abel | EABEL | 011.44.1644.429267 | 2004-05-11 | SA_REP | 11000.00 | 0.30 | 149 | 80 | 83 | | 175 | Alyssa | Hutton | AHUTTON | 011.44.1644.429266 | 2005-03-19 | SA_REP | 8800.00 | 0.25 | 149 | 80 | 84 | | 176 | Jonathon | Taylor | JTAYLOR | 011.44.1644.429265 | 2006-03-24 | SA_REP | 8600.00 | 0.20 | 149 | 80 | 85 | | 177 | Jack | Livingston | JLIVINGS | 011.44.1644.429264 | 2006-04-23 | SA_REP | 8400.00 | 0.20 | 149 | 80 | 86 | | 178 | Kimberely | Grant | KGRANT | 011.44.1644.429263 | 2007-05-24 | SA_REP | 7000.00 | 0.15 | 149 | 0 | 87 | | 179 | Charles | Johnson | CJOHNSON | 011.44.1644.429262 | 2008-01-04 | SA_REP | 6200.00 | 0.10 | 149 | 80 | 88 | | 180 | Winston | Taylor | WTAYLOR | 650.507.9876 | 2006-01-24 | SH_CLERK | 3200.00 | 0.00 | 120 | 50 | 89 | | 181 | Jean | Fleaur | JFLEAUR | 650.507.9877 | 2006-02-23 | SH_CLERK | 3100.00 | 0.00 | 120 | 50 | 90 | | 182 | Martha | Sullivan | MSULLIVA | 650.507.9878 | 2007-06-21 | SH_CLERK | 2500.00 | 0.00 | 120 | 50 | 91 | | 183 | Girard | Geoni | GGEONI | 650.507.9879 | 2008-02-03 | SH_CLERK | 2800.00 | 0.00 | 120 | 50 | 92 | | 184 | Nandita | Sarchand | NSARCHAN | 650.509.1876 | 2004-01-27 | SH_CLERK | 4200.00 | 0.00 | 121 | 50 | 93 | | 185 | Alexis | Bull | ABULL | 650.509.2876 | 2005-02-20 | SH_CLERK | 4100.00 | 0.00 | 121 | 50 | 94 | | 186 | Julia | Dellinger | JDELLING | 650.509.3876 | 2006-06-24 | SH_CLERK | 3400.00 | 0.00 | 121 | 50 | 95 | | 187 | Anthony | Cabrio | ACABRIO | 650.509.4876 | 2007-02-07 | SH_CLERK | 3000.00 | 0.00 | 121 | 50 | 96 | | 188 | Kelly | Chung | KCHUNG | 650.505.1876 | 2005-06-14 | SH_CLERK | 3800.00 | 0.00 | 122 | 50 | 97 | | 189 | Jennifer | Dilly | JDILLY | 650.505.2876 | 2005-08-13 | SH_CLERK | 3600.00 | 0.00 | 122 | 50 | 98 | | 190 | Timothy | Gates | TGATES | 650.505.3876 | 2006-07-11 | SH_CLERK | 2900.00 | 0.00 | 122 | 50 | 99 | | 191 | Randall | Perkins | RPERKINS | 650.505.4876 | 2007-12-19 | SH_CLERK | 2500.00 | 0.00 | 122 | 50 | 100 | | 192 | Sarah | Bell | SBELL | 650.501.1876 | 2004-02-04 | SH_CLERK | 4000.00 | 0.00 | 123 | 50 | 101 | | 193 | Britney | Everett | BEVERETT | 650.501.2876 | 2005-03-03 | SH_CLERK | 3900.00 | 0.00 | 123 | 50 | 102 | | 194 | Samuel | McCain | SMCCAIN | 650.501.3876 | 2006-07-01 | SH_CLERK | 3200.00 | 0.00 | 123 | 50 | 103 | | 195 | Vance | Jones | VJONES | 650.501.4876 | 2007-03-17 | SH_CLERK | 2800.00 | 0.00 | 123 | 50 | 104 | | 196 | Alana | Walsh | AWALSH | 650.507.9811 | 2006-04-24 | SH_CLERK | 3100.00 | 0.00 | 124 | 50 | 105 | | 197 | Kevin | Feeney | KFEENEY | 650.507.9822 | 2006-05-23 | SH_CLERK | 3000.00 | 0.00 | 124 | 50 | 106 | | 198 | Donald | OConnell | DOCONNEL | 650.507.9833 | 2007-06-21 | SH_CLERK | 2600.00 | 0.00 | 124 | 50 | 107 | | 199 | Douglas | Grant | DGRANT | 650.507.9844 | 2008-01-13 | SH_CLERK | 2600.00 | 0.00 | 124 | 50 | 108 | | 200 | Jennifer | Whalen | JWHALEN | 515.123.4444 | 2003-09-17 | AD_ASST | 4400.00 | 0.00 | 101 | 10 | 109 | | 201 | Michael | Hartstein | MHARTSTE | 515.123.5555 | 2004-02-17 | MK_MAN | 13000.00 | 0.00 | 100 | 20 | 110 | | 202 | Pat | Fay | PFAY | 603.123.6666 | 2005-08-17 | MK_REP | 6000.00 | 0.00 | 201 | 20 | 111 | | 203 | Susan | Mavris | SMAVRIS | 515.123.7777 | 2002-06-07 | HR_REP | 6500.00 | 0.00 | 101 | 40 | 112 | | 204 | Hermann | Baer | HBAER | 515.123.8888 | 2002-06-07 | PR_REP | 10000.00 | 0.00 | 101 | 70 | 113 | | 205 | Shelley | Higgins | SHIGGINS | 515.123.8080 | 2002-06-07 | AC_MGR | 12008.00 | 0.00 | 101 | 110 | 114 | | 206 | William | Gietz | WGIETZ | 515.123.8181 | 2002-06-07 | AC_ACCOUNT | 8300.00 | 0.00 | 205 | 110 | 115 | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ 116 | 117 | Table: departments 118 | +---------------+----------------------+------------+-------------+ 119 | | DEPARTMENT_ID | DEPARTMENT_NAME | MANAGER_ID | LOCATION_ID | 120 | +---------------+----------------------+------------+-------------+ 121 | | 10 | Administration | 200 | 1700 | 122 | | 20 | Marketing | 201 | 1800 | 123 | | 30 | Purchasing | 114 | 1700 | 124 | | 40 | Human Resources | 203 | 2400 | 125 | | 50 | Shipping | 121 | 1500 | 126 | | 60 | IT | 103 | 1400 | 127 | | 70 | Public Relations | 204 | 2700 | 128 | | 80 | Sales | 145 | 2500 | 129 | | 90 | Executive | 100 | 1700 | 130 | | 100 | Finance | 108 | 1700 | 131 | | 110 | Accounting | 205 | 1700 | 132 | | 120 | Treasury | 0 | 1700 | 133 | | 130 | Corporate Tax | 0 | 1700 | 134 | | 140 | Control And Credit | 0 | 1700 | 135 | | 150 | Shareholder Services | 0 | 1700 | 136 | | 160 | Benefits | 0 | 1700 | 137 | | 170 | Manufacturing | 0 | 1700 | 138 | | 180 | Construction | 0 | 1700 | 139 | | 190 | Contracting | 0 | 1700 | 140 | | 200 | Operations | 0 | 1700 | 141 | | 210 | IT Support | 0 | 1700 | 142 | | 220 | NOC | 0 | 1700 | 143 | | 230 | IT Helpdesk | 0 | 1700 | 144 | | 240 | Government Sales | 0 | 1700 | 145 | | 250 | Retail Sales | 0 | 1700 | 146 | | 260 | Recruiting | 0 | 1700 | 147 | | 270 | Payroll | 0 | 1700 | 148 | +---------------+----------------------+------------+-------------+ 149 | 150 | Table: locations 151 | +-------------+------------------------------------------+-------------+---------------------+-------------------+------------+ 152 | | LOCATION_ID | STREET_ADDRESS | POSTAL_CODE | CITY | STATE_PROVINCE | COUNTRY_ID | 153 | +-------------+------------------------------------------+-------------+---------------------+-------------------+------------+ 154 | | 1000 | 1297 Via Cola di Rie | 989 | Roma | | IT | 155 | | 1100 | 93091 Calle della Testa | 10934 | Venice | | IT | 156 | | 1200 | 2017 Shinjuku-ku | 1689 | Tokyo | Tokyo Prefecture | JP | 157 | | 1300 | 9450 Kamiya-cho | 6823 | Hiroshima | | JP | 158 | | 1400 | 2014 Jabberwocky Rd | 26192 | Southlake | Texas | US | 159 | | 1500 | 2011 Interiors Blvd | 99236 | South San Francisco | California | US | 160 | | 1600 | 2007 Zagora St | 50090 | South Brunswick | New Jersey | US | 161 | | 1700 | 2004 Charade Rd | 98199 | Seattle | Washington | US | 162 | | 1800 | 147 Spadina Ave | M5V 2L7 | Toronto | Ontario | CA | 163 | | 1900 | 6092 Boxwood St | YSW 9T2 | Whitehorse | Yukon | CA | 164 | | 2000 | 40-5-12 Laogianggen | 190518 | Beijing | | CN | 165 | | 2100 | 1298 Vileparle (E) | 490231 | Bombay | Maharashtra | IN | 166 | | 2200 | 12-98 Victoria Street | 2901 | Sydney | New South Wales | AU | 167 | | 2300 | 198 Clementi North | 540198 | Singapore | | SG | 168 | | 2400 | 8204 Arthur St | | London | | UK | 169 | | 2500 | Magdalen Centre, The Oxford Science Park | OX9 9ZB | Oxford | Oxford | UK | 170 | | 2600 | 9702 Chester Road | 9629850293 | Stretford | Manchester | UK | 171 | | 2700 | Schwanthalerstr. 7031 | 80925 | Munich | Bavaria | DE | 172 | | 2800 | Rua Frei Caneca 1360 | 01307-002 | Sao Paulo | Sao Paulo | BR | 173 | | 2900 | 20 Rue des Corps-Saints | 1730 | Geneva | Geneve | CH | 174 | | 3000 | Murtenstrasse 921 | 3095 | Bern | BE | CH | 175 | | 3100 | Pieter Breughelstraat 837 | 3029SK | Utrecht | Utrecht | NL | 176 | | 3200 | Mariano Escobedo 9991 | 11932 | Mexico City | Distrito Federal, | MX | 177 | +-------------+------------------------------------------+-------------+---------------------+-------------------+------------+ 178 | 179 | Table: countries 180 | +------------+--------------------------+-----------+ 181 | | COUNTRY_ID | COUNTRY_NAME | REGION_ID | 182 | +------------+--------------------------+-----------+ 183 | | AR | Argentina | 2 | 184 | | AU | Australia | 3 | 185 | | BE | Belgium | 1 | 186 | | BR | Brazil | 2 | 187 | | CA | Canada | 2 | 188 | | CH | Switzerland | 1 | 189 | | CN | China | 3 | 190 | | DE | Germany | 1 | 191 | | DK | Denmark | 1 | 192 | | EG | Egypt | 4 | 193 | | FR | France | 1 | 194 | | IL | Israel | 4 | 195 | | IN | India | 3 | 196 | | IT | Italy | 1 | 197 | | JP | Japan | 3 | 198 | | KW | Kuwait | 4 | 199 | | ML | Malaysia | 3 | 200 | | MX | Mexico | 2 | 201 | | NG | Nigeria | 4 | 202 | | NL | Netherlands | 1 | 203 | | SG | Singapore | 3 | 204 | | UK | United Kingdom | 1 | 205 | | US | United States of America | 2 | 206 | | ZM | Zambia | 4 | 207 | | ZW | Zimbabwe | 4 | 208 | +------------+--------------------------+-----------+ 209 | 210 | Table: job_history 211 | +-------------+------------+------------+------------+---------------+ 212 | | EMPLOYEE_ID | START_DATE | END_DATE | JOB_ID | DEPARTMENT_ID | 213 | +-------------+------------+------------+------------+---------------+ 214 | | 102 | 2001-01-13 | 2006-07-24 | IT_PROG | 60 | 215 | | 101 | 1997-09-21 | 2001-10-27 | AC_ACCOUNT | 110 | 216 | | 101 | 2001-10-28 | 2005-03-15 | AC_MGR | 110 | 217 | | 201 | 2004-02-17 | 2007-12-19 | MK_REP | 20 | 218 | | 114 | 2006-03-24 | 2007-12-31 | ST_CLERK | 50 | 219 | | 122 | 2007-01-01 | 2007-12-31 | ST_CLERK | 50 | 220 | | 200 | 1995-09-17 | 2001-06-17 | AD_ASST | 90 | 221 | | 176 | 2006-03-24 | 2006-12-31 | SA_REP | 80 | 222 | | 176 | 2007-01-01 | 2007-12-31 | SA_MAN | 80 | 223 | | 200 | 2002-07-01 | 2006-12-31 | AC_ACCOUNT | 90 | 224 | +-------------+------------+------------+------------+---------------+ 225 | 226 | Table: jobs 227 | +------------+---------------------------------+------------+------------+ 228 | | JOB_ID | JOB_TITLE | MIN_SALARY | MAX_SALARY | 229 | +------------+---------------------------------+------------+------------+ 230 | | AD_PRES | President | 20080 | 40000 | 231 | | AD_VP | Administration Vice President | 15000 | 30000 | 232 | | AD_ASST | Administration Assistant | 3000 | 6000 | 233 | | FI_MGR | Finance Manager | 8200 | 16000 | 234 | | FI_ACCOUNT | Accountant | 4200 | 9000 | 235 | | AC_MGR | Accounting Manager | 8200 | 16000 | 236 | | AC_ACCOUNT | Public Accountant | 4200 | 9000 | 237 | | SA_MAN | Sales Manager | 10000 | 20080 | 238 | | SA_REP | Sales Representative | 6000 | 12008 | 239 | | PU_MAN | Purchasing Manager | 8000 | 15000 | 240 | | PU_CLERK | Purchasing Clerk | 2500 | 5500 | 241 | | ST_MAN | Stock Manager | 5500 | 8500 | 242 | | ST_CLERK | Stock Clerk | 2008 | 5000 | 243 | | SH_CLERK | Shipping Clerk | 2500 | 5500 | 244 | | IT_PROG | Programmer | 4000 | 10000 | 245 | | MK_MAN | Marketing Manager | 9000 | 15000 | 246 | | MK_REP | Marketing Representative | 4000 | 9000 | 247 | | HR_REP | Human Resources Representative | 4000 | 9000 | 248 | | PR_REP | Public Relations Representative | 4500 | 10500 | 249 | +------------+---------------------------------+------------+------------+ 250 | 251 | Table: job_grade 252 | +-------------+------------+-------------+ 253 | | GRADE_LEVEL | LOWEST_SAL | HIGHEST_SAL | 254 | +-------------+------------+-------------+ 255 | | A | 1000 | 2999 | 256 | | B | 3000 | 5999 | 257 | | C | 6000 | 9999 | 258 | | D | 10000 | 14999 | 259 | | E | 15000 | 24999 | 260 | | F | 25000 | 40000 | 261 | +-------------+------------+-------------+ 262 | 263 | 264 | /* 1. Write a query in SQL to display the first name, last name, department number, and department name for each employee. */ 265 | 266 | SELECT e.first_name, 267 | e.last_name, 268 | e.department_id, 269 | d.department_name 270 | FROM employees e 271 | INNER JOIN departments d 272 | ON e.department_id = d.department_id; 273 | 274 | 275 | /* 2. Write a query in SQL to display the first and last name, department, city, and state province for each employee. */ 276 | 277 | SELECT e.first_name, 278 | e.last_name, 279 | d.department_name, 280 | l.city, 281 | l.state_province 282 | FROM employees e 283 | INNER JOIN departments d 284 | ON e.department_id = d.department_id 285 | INNER JOIN locations l 286 | ON d.location_id = l.location_id; 287 | 288 | 289 | /* 3. Write a query in SQL to display the first name, last name, salary, and job grade for all employees. */ 290 | 291 | SELECT e.first_name, 292 | e.last_name, 293 | e.salary, 294 | j.grade_level 295 | FROM employees e 296 | INNER JOIN job_grades j 297 | ON e.salary BETWEEN j.lowest_sal AND j.highest_sal; 298 | 299 | 300 | /* 4. Write a query in SQL to display the first name, last name, department number and department name, for all employees for departments 80 or 40. */ 301 | 302 | SELECT e.first_name, 303 | e.last_name, 304 | d.department_id, 305 | d.department_name 306 | FROM employees e 307 | INNER JOIN departments d 308 | ON e.department_id = d.department_id 309 | AND d.department_id IN (80, 40) 310 | ORDER BY e.last_name; 311 | 312 | 313 | /* 5. Write a query in SQL to display those employees who contain a letter z to their first name and also display their last name, department, city, and state province. */ 314 | 315 | SELECT e.first_name, 316 | e.last_name, 317 | d.department_name, 318 | l.city, 319 | l.state_province 320 | FROM employees e 321 | INNER JOIN departments d 322 | ON e.department_id = d.department_id 323 | INNER JOIN locations l 324 | ON d.location_id = l.location_id 325 | WHERE e.first_name LIKE '%z%'; 326 | 327 | 328 | /* 6. Write a query in SQL to display all departments including those where does not have any employee. */ 329 | 330 | SELECT e.first_name, 331 | e.last_name, 332 | d.department_id, 333 | d.department_name 334 | FROM departments d 335 | LEFT JOIN employees e 336 | ON d.department_id = e.department_id; 337 | 338 | 339 | /* 7. Write a query in SQL to display the first and last name and salary for those employees who earn less than the employee earn whose number is 182. */ 340 | 341 | SELECT e1.first_name, 342 | e1.last_name, 343 | e1.salary 344 | FROM employees e1 345 | INNER JOIN employees e2 346 | ON e1.salary < e2.salary 347 | AND e2.employee_id = 182; 348 | 349 | 350 | /* 8. Write a query in SQL to display the first name of all employees including the first name of their manager. */ 351 | 352 | SELECT e1.first_name AS "employee_name", 353 | e2.first_name AS "manager_name" 354 | FROM employees e1 355 | INNER JOIN employees e2 356 | ON e1.manager_id = e2.employee_id; 357 | 358 | 359 | /* 9. Write a query in SQL to display the department name, city, and state province for each department. */ 360 | 361 | SELECT d.department_name, 362 | l.city, 363 | l.state_province 364 | FROM departments d 365 | INNER JOIN locations l 366 | ON d.location_id = l.location_id; 367 | 368 | 369 | /* 10. Write a query in SQL to display the first name, last name, department number and name, for all employees who have or have not any department. */ 370 | 371 | SELECT e.first_name, 372 | e.last_name, 373 | d.department_id, 374 | d.department_name 375 | FROM employees e 376 | LEFT JOIN departments d 377 | ON e.department_id = d.department_id; 378 | 379 | 380 | /* 11. Write a query in SQL to display the first name of all employees and the first name of their manager including those who does not working under any manager. */ 381 | 382 | SELECT e1.first_name AS "employee_name", 383 | e2.first_name AS "manager_name" 384 | FROM employees e1 385 | LEFT JOIN employees e2 386 | ON e1.manager_id = e2.employee_id; 387 | 388 | 389 | /* 12. Write a query in SQL to display the first name, last name, and department number for those employees who works in the same department as the employee who holds the last name as Taylor. */ 390 | 391 | SELECT e1.first_name, 392 | e1.last_name, 393 | e1.department_id 394 | FROM employees e1 395 | INNER JOIN employees e2 396 | ON e1.department_id = e2.department_id 397 | AND e2.last_name = 'Taylor'; 398 | 399 | 400 | /* 13. Write a query in SQL to display the job title, department name, full name (first and last name ) of employee, and starting date for all the jobs which started on or after 1st January, 1993 and ending with on or before 31 August, 1997 */ 401 | 402 | SELECT j.job_title, 403 | d.department_name, 404 | CONCAT(e.first_name, ' ', e.last_name) AS full_name, 405 | jh.start_date 406 | FROM employees e 407 | INNER JOIN job_history jh 408 | ON e.employee_id = jh.employee_id 409 | AND jh.start_date BETWEEN '1993-01-01' AND '1997-08-31' 410 | INNER JOIN jobs j 411 | ON jh.job_id = j.job_id 412 | INNER JOIN departments d 413 | ON jh.department_id = d.department_id; 414 | 415 | 416 | /* 14. Write a query in SQL to display job title, full name (first and last name ) of employee, and the difference between maximum salary for the job and salary of the employee. */ 417 | 418 | SELECT j.job_title, 419 | CONCAT(e.first_name, ' ', e.last_name) AS full_name, 420 | (j.max_salary - e.salary) AS salary_diff 421 | FROM employees e 422 | INNER JOIN jobs j 423 | ON e.job_id = j.job_id; 424 | 425 | -- Note: This also works using a NATURAL JOIN which creates an implicit join based on the common columns in the two tables being joined. 426 | 427 | SELECT j.job_title, 428 | CONCAT(e.first_name, ' ', e.last_name) AS full_name, 429 | (j.max_salary - e.salary) AS salary_diff 430 | FROM employees e 431 | NATURAL INNER JOIN jobs j; 432 | 433 | 434 | /* 15. Write a query in SQL to display the name of the department, average salary and number of employees working in that department who got commission. */ 435 | 436 | SELECT d.department_name, 437 | AVG(e.salary), 438 | COUNT(commission_pct) 439 | FROM employees e 440 | JOIN departments d 441 | ON e.department_id = d.department_id 442 | GROUP BY d.department_name; 443 | 444 | 445 | /* 16. Write a query in SQL to display the full name (first and last name ) of employees, job title and the salary differences to their own job for those employees who is working in the department ID 80. */ 446 | 447 | SELECT CONCAT(e.first_name, ' ', e.last_name) AS full_name, 448 | j.job_title, 449 | (j.max_salary - e.salary) AS salary_diff 450 | FROM employees e 451 | INNER JOIN jobs j 452 | ON e.job_id = j.job_id 453 | WHERE e.department_id = 80; 454 | 455 | 456 | /* 17. Write a query in SQL to display the name of the country, city, and the departments which are running there. */ 457 | 458 | SELECT c.country_name, 459 | l.city, 460 | d.department_name 461 | FROM countries c 462 | INNER JOIN locations l 463 | ON c.country_id = l.country_id 464 | INNER JOIN departments d 465 | ON l.location_id = d.location_id; 466 | 467 | -- Note: This also works using JOIN and USING on the common columns. 468 | 469 | SELECT c.country_name, 470 | l.city, 471 | d.department_name 472 | FROM countries c 473 | INNER JOIN locations l USING (country_id) 474 | INNER JOIN departments d USING (location_id); 475 | 476 | 477 | /* 18. Write a query in SQL to display department name and the full name (first and last name) of the manager. */ 478 | 479 | SELECT d.department_name, 480 | CONCAT(e.first_name, ' ', e.last_name) AS full_name 481 | FROM departments d 482 | INNER JOIN employees e 483 | ON d.manager_id = e.employee_id; 484 | 485 | 486 | /* 19. Write a query in SQL to display job title and average salary of employees. */ 487 | 488 | SELECT j.job_title, 489 | AVG(e.salary) 490 | FROM employees e 491 | INNER JOIN jobs j 492 | ON e.job_id = j.job_id 493 | GROUP BY j.job_title; 494 | 495 | 496 | /* 20. Write a query in SQL to display the details of jobs which was done by any of the employees who is presently earning a salary on and above 12000. */ 497 | 498 | SELECT jh.* 499 | FROM employees e 500 | INNER JOIN job_history jh 501 | ON e.employee_id = jh.employee_id 502 | WHERE salary >= 12000.00; 503 | 504 | 505 | /* 21. Write a query in SQL to display the country name, city, and number of those departments where at least 2 employees are working. */ 506 | 507 | SELECT c.country_name, 508 | l.city, 509 | COUNT(d.department_id) 510 | FROM countries c 511 | INNER JOIN locations l 512 | ON c.country_id = l.country_id 513 | INNER JOIN departments d 514 | ON l.location_id = d.location_id 515 | WHERE d.department_id IN (SELECT e.department_id 516 | FROM employees e 517 | GROUP BY e.department_id 518 | HAVING COUNT(e.department_id) >= 2) 519 | GROUP BY c.country_name, l.city; 520 | 521 | 522 | /* 22. Write a query in SQL to display the department name, full name (first and last name) of manager, and their city. */ 523 | 524 | SELECT d.department_name, 525 | CONCAT(e.first_name, ' ', e.last_name) AS full_name, 526 | l.city 527 | FROM employees e 528 | INNER JOIN departments d 529 | ON e.employee_id = d.manager_id 530 | INNER JOIN locations l 531 | ON d.location_id = l.location_id; 532 | 533 | 534 | /* 23. Write a query in SQL to display the employee ID, job name, number of days worked in for all those jobs in department 80. */ 535 | 536 | SELECT jh.employee_id, 537 | j.job_title, 538 | (jh.end_date - jh.start_date) AS num_days 539 | FROM jobs j 540 | INNER JOIN job_history jh 541 | ON j.job_id = jh.job_id 542 | WHERE jh.department_id = 80; 543 | 544 | -- Note: This also works using a NATURAL JOIN which creates an implicit join based on the common columns in the two tables being joined. 545 | 546 | SELECT jh.employee_id, 547 | j.job_title, 548 | (jh.end_date - jh.start_date) AS num_days 549 | FROM jobs j 550 | NATURAL INNER JOIN job_history jh 551 | WHERE jh.department_id = 80; 552 | 553 | 554 | /* 24. Write a query in SQL to display the full name (first and last name), and salary of those employees who working in any department located in London. */ 555 | 556 | SELECT CONCAT(e.first_name, ' ', e.last_name) AS full_name, 557 | e.salary 558 | FROM employees e 559 | INNER JOIN departments d 560 | ON e.department_id = d.department_id 561 | INNER JOIN locations l 562 | ON d.location_id = l.location_id 563 | WHERE l.city = 'London'; 564 | 565 | 566 | /* 25. Write a query in SQL to display full name(first and last name), job title, starting and ending date of last jobs for those employees with worked without a commission percentage. */ 567 | 568 | SELECT CONCAT(e.first_name, ' ', e.last_name) AS full_name, 569 | j.job_title, 570 | jh.* 571 | FROM employees e 572 | INNER JOIN (SELECT MAX(start_date) AS starting_date, 573 | MAX(end_date) AS ending_date, 574 | employee_id 575 | FROM job_history 576 | GROUP BY employee_id) jh 577 | ON e.employee_id = jh.employee_id 578 | INNER JOIN jobs j 579 | ON e.job_id = j.job_id 580 | WHERE e.commission_pct = 0; 581 | 582 | 583 | /* 26. Write a query in SQL to display the department name and number of employees in each of the department. */ 584 | 585 | SELECT d.department_name, 586 | COUNT(e.employee_id) AS num_employees 587 | FROM departments d 588 | INNER JOIN employees e 589 | ON d.department_id = e.department_id 590 | GROUP BY d.department_name; 591 | 592 | 593 | /* 27. Write a query in SQL to display the full name (first and last name) of employee with ID and name of the country presently where (s)he is working. */ 594 | 595 | SELECT CONCAT(e.first_name, ' ', e.last_name) AS full_name, 596 | e.employee_id, 597 | c.country_name 598 | FROM employees e 599 | INNER JOIN departments d 600 | ON e.department_id = d.department_id 601 | INNER JOIN locations l 602 | ON d.location_id = l.location_id 603 | INNER JOIN countries c 604 | ON l.country_id = c.country_id; 605 | 606 | -------------------------------------------------------------------------------- /SQL Exercises - SUBQUERIES on HR Database.sql: -------------------------------------------------------------------------------- 1 | /*** SQL Exercises - SUBQUERIES on HR Database ***/ 2 | -- https://www.w3resource.com/sql-exercises/sql-subqueries-exercises.php 3 | 4 | Table: employees 5 | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ 6 | | EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER | HIRE_DATE | JOB_ID | SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID | 7 | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ 8 | | 100 | Steven | King | SKING | 515.123.4567 | 2003-06-17 | AD_PRES | 24000.00 | 0.00 | 0 | 90 | 9 | | 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | 2005-09-21 | AD_VP | 17000.00 | 0.00 | 100 | 90 | 10 | | 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | 2001-01-13 | AD_VP | 17000.00 | 0.00 | 100 | 90 | 11 | | 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | 2006-01-03 | IT_PROG | 9000.00 | 0.00 | 102 | 60 | 12 | | 104 | Bruce | Ernst | BERNST | 590.423.4568 | 2007-05-21 | IT_PROG | 6000.00 | 0.00 | 103 | 60 | 13 | | 105 | David | Austin | DAUSTIN | 590.423.4569 | 2005-06-25 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | 14 | | 106 | Valli | Pataballa | VPATABAL | 590.423.4560 | 2006-02-05 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | 15 | | 107 | Diana | Lorentz | DLORENTZ | 590.423.5567 | 2007-02-07 | IT_PROG | 4200.00 | 0.00 | 103 | 60 | 16 | | 108 | Nancy | Greenberg | NGREENBE | 515.124.4569 | 2002-08-17 | FI_MGR | 12008.00 | 0.00 | 101 | 100 | 17 | | 109 | Daniel | Faviet | DFAVIET | 515.124.4169 | 2002-08-16 | FI_ACCOUNT | 9000.00 | 0.00 | 108 | 100 | 18 | | 110 | John | Chen | JCHEN | 515.124.4269 | 2005-09-28 | FI_ACCOUNT | 8200.00 | 0.00 | 108 | 100 | 19 | | 111 | Ismael | Sciarra | ISCIARRA | 515.124.4369 | 2005-09-30 | FI_ACCOUNT | 7700.00 | 0.00 | 108 | 100 | 20 | | 112 | Jose Manuel | Urman | JMURMAN | 515.124.4469 | 2006-03-07 | FI_ACCOUNT | 7800.00 | 0.00 | 108 | 100 | 21 | | 113 | Luis | Popp | LPOPP | 515.124.4567 | 2007-12-07 | FI_ACCOUNT | 6900.00 | 0.00 | 108 | 100 | 22 | | 114 | Den | Raphaely | DRAPHEAL | 515.127.4561 | 2002-12-07 | PU_MAN | 11000.00 | 0.00 | 100 | 30 | 23 | | 115 | Alexander | Khoo | AKHOO | 515.127.4562 | 2003-05-18 | PU_CLERK | 3100.00 | 0.00 | 114 | 30 | 24 | | 116 | Shelli | Baida | SBAIDA | 515.127.4563 | 2005-12-24 | PU_CLERK | 2900.00 | 0.00 | 114 | 30 | 25 | | 117 | Sigal | Tobias | STOBIAS | 515.127.4564 | 2005-07-24 | PU_CLERK | 2800.00 | 0.00 | 114 | 30 | 26 | | 118 | Guy | Himuro | GHIMURO | 515.127.4565 | 2006-11-15 | PU_CLERK | 2600.00 | 0.00 | 114 | 30 | 27 | | 119 | Karen | Colmenares | KCOLMENA | 515.127.4566 | 2007-08-10 | PU_CLERK | 2500.00 | 0.00 | 114 | 30 | 28 | | 120 | Matthew | Weiss | MWEISS | 650.123.1234 | 2004-07-18 | ST_MAN | 8000.00 | 0.00 | 100 | 50 | 29 | | 121 | Adam | Fripp | AFRIPP | 650.123.2234 | 2005-04-10 | ST_MAN | 8200.00 | 0.00 | 100 | 50 | 30 | | 122 | Payam | Kaufling | PKAUFLIN | 650.123.3234 | 2003-05-01 | ST_MAN | 7900.00 | 0.00 | 100 | 50 | 31 | | 123 | Shanta | Vollman | SVOLLMAN | 650.123.4234 | 2005-10-10 | ST_MAN | 6500.00 | 0.00 | 100 | 50 | 32 | | 124 | Kevin | Mourgos | KMOURGOS | 650.123.5234 | 2007-11-16 | ST_MAN | 5800.00 | 0.00 | 100 | 50 | 33 | | 125 | Julia | Nayer | JNAYER | 650.124.1214 | 2005-07-16 | ST_CLERK | 3200.00 | 0.00 | 120 | 50 | 34 | | 126 | Irene | Mikkilineni | IMIKKILI | 650.124.1224 | 2006-09-28 | ST_CLERK | 2700.00 | 0.00 | 120 | 50 | 35 | | 127 | James | Landry | JLANDRY | 650.124.1334 | 2007-01-14 | ST_CLERK | 2400.00 | 0.00 | 120 | 50 | 36 | | 128 | Steven | Markle | SMARKLE | 650.124.1434 | 2008-03-08 | ST_CLERK | 2200.00 | 0.00 | 120 | 50 | 37 | | 129 | Laura | Bissot | LBISSOT | 650.124.5234 | 2005-08-20 | ST_CLERK | 3300.00 | 0.00 | 121 | 50 | 38 | | 130 | Mozhe | Atkinson | MATKINSO | 650.124.6234 | 2005-10-30 | ST_CLERK | 2800.00 | 0.00 | 121 | 50 | 39 | | 131 | James | Marlow | JAMRLOW | 650.124.7234 | 2005-02-16 | ST_CLERK | 2500.00 | 0.00 | 121 | 50 | 40 | | 132 | TJ | Olson | TJOLSON | 650.124.8234 | 2007-04-10 | ST_CLERK | 2100.00 | 0.00 | 121 | 50 | 41 | | 133 | Jason | Mallin | JMALLIN | 650.127.1934 | 2004-06-14 | ST_CLERK | 3300.00 | 0.00 | 122 | 50 | 42 | | 134 | Michael | Rogers | MROGERS | 650.127.1834 | 2006-08-26 | ST_CLERK | 2900.00 | 0.00 | 122 | 50 | 43 | | 135 | Ki | Gee | KGEE | 650.127.1734 | 2007-12-12 | ST_CLERK | 2400.00 | 0.00 | 122 | 50 | 44 | | 136 | Hazel | Philtanker | HPHILTAN | 650.127.1634 | 2008-02-06 | ST_CLERK | 2200.00 | 0.00 | 122 | 50 | 45 | | 137 | Renske | Ladwig | RLADWIG | 650.121.1234 | 2003-07-14 | ST_CLERK | 3600.00 | 0.00 | 123 | 50 | 46 | | 138 | Stephen | Stiles | SSTILES | 650.121.2034 | 2005-10-26 | ST_CLERK | 3200.00 | 0.00 | 123 | 50 | 47 | | 139 | John | Seo | JSEO | 650.121.2019 | 2006-02-12 | ST_CLERK | 2700.00 | 0.00 | 123 | 50 | 48 | | 140 | Joshua | Patel | JPATEL | 650.121.1834 | 2006-04-06 | ST_CLERK | 2500.00 | 0.00 | 123 | 50 | 49 | | 141 | Trenna | Rajs | TRAJS | 650.121.8009 | 2003-10-17 | ST_CLERK | 3500.00 | 0.00 | 124 | 50 | 50 | | 142 | Curtis | Davies | CDAVIES | 650.121.2994 | 2005-01-29 | ST_CLERK | 3100.00 | 0.00 | 124 | 50 | 51 | | 143 | Randall | Matos | RMATOS | 650.121.2874 | 2006-03-15 | ST_CLERK | 2600.00 | 0.00 | 124 | 50 | 52 | | 144 | Peter | Vargas | PVARGAS | 650.121.2004 | 2006-07-09 | ST_CLERK | 2500.00 | 0.00 | 124 | 50 | 53 | | 145 | John | Russell | JRUSSEL | 011.44.1344.429268 | 2004-10-01 | SA_MAN | 14000.00 | 0.40 | 100 | 80 | 54 | | 146 | Karen | Partners | KPARTNER | 011.44.1344.467268 | 2005-01-05 | SA_MAN | 13500.00 | 0.30 | 100 | 80 | 55 | | 147 | Alberto | Errazuriz | AERRAZUR | 011.44.1344.429278 | 2005-03-10 | SA_MAN | 12000.00 | 0.30 | 100 | 80 | 56 | | 148 | Gerald | Cambrault | GCAMBRAU | 011.44.1344.619268 | 2007-10-15 | SA_MAN | 11000.00 | 0.30 | 100 | 80 | 57 | | 149 | Eleni | Zlotkey | EZLOTKEY | 011.44.1344.429018 | 2008-01-29 | SA_MAN | 10500.00 | 0.20 | 100 | 80 | 58 | | 150 | Peter | Tucker | PTUCKER | 011.44.1344.129268 | 2005-01-30 | SA_REP | 10000.00 | 0.30 | 145 | 80 | 59 | | 151 | David | Bernstein | DBERNSTE | 011.44.1344.345268 | 2005-03-24 | SA_REP | 9500.00 | 0.25 | 145 | 80 | 60 | | 152 | Peter | Hall | PHALL | 011.44.1344.478968 | 2005-08-20 | SA_REP | 9000.00 | 0.25 | 145 | 80 | 61 | | 153 | Christopher | Olsen | COLSEN | 011.44.1344.498718 | 2006-03-30 | SA_REP | 8000.00 | 0.20 | 145 | 80 | 62 | | 154 | Nanette | Cambrault | NCAMBRAU | 011.44.1344.987668 | 2006-12-09 | SA_REP | 7500.00 | 0.20 | 145 | 80 | 63 | | 155 | Oliver | Tuvault | OTUVAULT | 011.44.1344.486508 | 2007-11-23 | SA_REP | 7000.00 | 0.15 | 145 | 80 | 64 | | 156 | Janette | King | JKING | 011.44.1345.429268 | 2004-01-30 | SA_REP | 10000.00 | 0.35 | 146 | 80 | 65 | | 157 | Patrick | Sully | PSULLY | 011.44.1345.929268 | 2004-03-04 | SA_REP | 9500.00 | 0.35 | 146 | 80 | 66 | | 158 | Allan | McEwen | AMCEWEN | 011.44.1345.829268 | 2004-08-01 | SA_REP | 9000.00 | 0.35 | 146 | 80 | 67 | | 159 | Lindsey | Smith | LSMITH | 011.44.1345.729268 | 2005-03-10 | SA_REP | 8000.00 | 0.30 | 146 | 80 | 68 | | 160 | Louise | Doran | LDORAN | 011.44.1345.629268 | 2005-12-15 | SA_REP | 7500.00 | 0.30 | 146 | 80 | 69 | | 161 | Sarath | Sewall | SSEWALL | 011.44.1345.529268 | 2006-11-03 | SA_REP | 7000.00 | 0.25 | 146 | 80 | 70 | | 162 | Clara | Vishney | CVISHNEY | 011.44.1346.129268 | 2005-11-11 | SA_REP | 10500.00 | 0.25 | 147 | 80 | 71 | | 163 | Danielle | Greene | DGREENE | 011.44.1346.229268 | 2007-03-19 | SA_REP | 9500.00 | 0.15 | 147 | 80 | 72 | | 164 | Mattea | Marvins | MMARVINS | 011.44.1346.329268 | 2008-01-24 | SA_REP | 7200.00 | 0.10 | 147 | 80 | 73 | | 165 | David | Lee | DLEE | 011.44.1346.529268 | 2008-02-23 | SA_REP | 6800.00 | 0.10 | 147 | 80 | 74 | | 166 | Sundar | Ande | SANDE | 011.44.1346.629268 | 2008-03-24 | SA_REP | 6400.00 | 0.10 | 147 | 80 | 75 | | 167 | Amit | Banda | ABANDA | 011.44.1346.729268 | 2008-04-21 | SA_REP | 6200.00 | 0.10 | 147 | 80 | 76 | | 168 | Lisa | Ozer | LOZER | 011.44.1343.929268 | 2005-03-11 | SA_REP | 11500.00 | 0.25 | 148 | 80 | 77 | | 169 | Harrison | Bloom | HBLOOM | 011.44.1343.829268 | 2006-03-23 | SA_REP | 10000.00 | 0.20 | 148 | 80 | 78 | | 170 | Tayler | Fox | TFOX | 011.44.1343.729268 | 2006-01-24 | SA_REP | 9600.00 | 0.20 | 148 | 80 | 79 | | 171 | William | Smith | WSMITH | 011.44.1343.629268 | 2007-02-23 | SA_REP | 7400.00 | 0.15 | 148 | 80 | 80 | | 172 | Elizabeth | Bates | EBATES | 011.44.1343.529268 | 2007-03-24 | SA_REP | 7300.00 | 0.15 | 148 | 80 | 81 | | 173 | Sundita | Kumar | SKUMAR | 011.44.1343.329268 | 2008-04-21 | SA_REP | 6100.00 | 0.10 | 148 | 80 | 82 | | 174 | Ellen | Abel | EABEL | 011.44.1644.429267 | 2004-05-11 | SA_REP | 11000.00 | 0.30 | 149 | 80 | 83 | | 175 | Alyssa | Hutton | AHUTTON | 011.44.1644.429266 | 2005-03-19 | SA_REP | 8800.00 | 0.25 | 149 | 80 | 84 | | 176 | Jonathon | Taylor | JTAYLOR | 011.44.1644.429265 | 2006-03-24 | SA_REP | 8600.00 | 0.20 | 149 | 80 | 85 | | 177 | Jack | Livingston | JLIVINGS | 011.44.1644.429264 | 2006-04-23 | SA_REP | 8400.00 | 0.20 | 149 | 80 | 86 | | 178 | Kimberely | Grant | KGRANT | 011.44.1644.429263 | 2007-05-24 | SA_REP | 7000.00 | 0.15 | 149 | 0 | 87 | | 179 | Charles | Johnson | CJOHNSON | 011.44.1644.429262 | 2008-01-04 | SA_REP | 6200.00 | 0.10 | 149 | 80 | 88 | | 180 | Winston | Taylor | WTAYLOR | 650.507.9876 | 2006-01-24 | SH_CLERK | 3200.00 | 0.00 | 120 | 50 | 89 | | 181 | Jean | Fleaur | JFLEAUR | 650.507.9877 | 2006-02-23 | SH_CLERK | 3100.00 | 0.00 | 120 | 50 | 90 | | 182 | Martha | Sullivan | MSULLIVA | 650.507.9878 | 2007-06-21 | SH_CLERK | 2500.00 | 0.00 | 120 | 50 | 91 | | 183 | Girard | Geoni | GGEONI | 650.507.9879 | 2008-02-03 | SH_CLERK | 2800.00 | 0.00 | 120 | 50 | 92 | | 184 | Nandita | Sarchand | NSARCHAN | 650.509.1876 | 2004-01-27 | SH_CLERK | 4200.00 | 0.00 | 121 | 50 | 93 | | 185 | Alexis | Bull | ABULL | 650.509.2876 | 2005-02-20 | SH_CLERK | 4100.00 | 0.00 | 121 | 50 | 94 | | 186 | Julia | Dellinger | JDELLING | 650.509.3876 | 2006-06-24 | SH_CLERK | 3400.00 | 0.00 | 121 | 50 | 95 | | 187 | Anthony | Cabrio | ACABRIO | 650.509.4876 | 2007-02-07 | SH_CLERK | 3000.00 | 0.00 | 121 | 50 | 96 | | 188 | Kelly | Chung | KCHUNG | 650.505.1876 | 2005-06-14 | SH_CLERK | 3800.00 | 0.00 | 122 | 50 | 97 | | 189 | Jennifer | Dilly | JDILLY | 650.505.2876 | 2005-08-13 | SH_CLERK | 3600.00 | 0.00 | 122 | 50 | 98 | | 190 | Timothy | Gates | TGATES | 650.505.3876 | 2006-07-11 | SH_CLERK | 2900.00 | 0.00 | 122 | 50 | 99 | | 191 | Randall | Perkins | RPERKINS | 650.505.4876 | 2007-12-19 | SH_CLERK | 2500.00 | 0.00 | 122 | 50 | 100 | | 192 | Sarah | Bell | SBELL | 650.501.1876 | 2004-02-04 | SH_CLERK | 4000.00 | 0.00 | 123 | 50 | 101 | | 193 | Britney | Everett | BEVERETT | 650.501.2876 | 2005-03-03 | SH_CLERK | 3900.00 | 0.00 | 123 | 50 | 102 | | 194 | Samuel | McCain | SMCCAIN | 650.501.3876 | 2006-07-01 | SH_CLERK | 3200.00 | 0.00 | 123 | 50 | 103 | | 195 | Vance | Jones | VJONES | 650.501.4876 | 2007-03-17 | SH_CLERK | 2800.00 | 0.00 | 123 | 50 | 104 | | 196 | Alana | Walsh | AWALSH | 650.507.9811 | 2006-04-24 | SH_CLERK | 3100.00 | 0.00 | 124 | 50 | 105 | | 197 | Kevin | Feeney | KFEENEY | 650.507.9822 | 2006-05-23 | SH_CLERK | 3000.00 | 0.00 | 124 | 50 | 106 | | 198 | Donald | OConnell | DOCONNEL | 650.507.9833 | 2007-06-21 | SH_CLERK | 2600.00 | 0.00 | 124 | 50 | 107 | | 199 | Douglas | Grant | DGRANT | 650.507.9844 | 2008-01-13 | SH_CLERK | 2600.00 | 0.00 | 124 | 50 | 108 | | 200 | Jennifer | Whalen | JWHALEN | 515.123.4444 | 2003-09-17 | AD_ASST | 4400.00 | 0.00 | 101 | 10 | 109 | | 201 | Michael | Hartstein | MHARTSTE | 515.123.5555 | 2004-02-17 | MK_MAN | 13000.00 | 0.00 | 100 | 20 | 110 | | 202 | Pat | Fay | PFAY | 603.123.6666 | 2005-08-17 | MK_REP | 6000.00 | 0.00 | 201 | 20 | 111 | | 203 | Susan | Mavris | SMAVRIS | 515.123.7777 | 2002-06-07 | HR_REP | 6500.00 | 0.00 | 101 | 40 | 112 | | 204 | Hermann | Baer | HBAER | 515.123.8888 | 2002-06-07 | PR_REP | 10000.00 | 0.00 | 101 | 70 | 113 | | 205 | Shelley | Higgins | SHIGGINS | 515.123.8080 | 2002-06-07 | AC_MGR | 12008.00 | 0.00 | 101 | 110 | 114 | | 206 | William | Gietz | WGIETZ | 515.123.8181 | 2002-06-07 | AC_ACCOUNT | 8300.00 | 0.00 | 205 | 110 | 115 | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ 116 | 117 | Table: departments 118 | +---------------+----------------------+------------+-------------+ 119 | | DEPARTMENT_ID | DEPARTMENT_NAME | MANAGER_ID | LOCATION_ID | 120 | +---------------+----------------------+------------+-------------+ 121 | | 10 | Administration | 200 | 1700 | 122 | | 20 | Marketing | 201 | 1800 | 123 | | 30 | Purchasing | 114 | 1700 | 124 | | 40 | Human Resources | 203 | 2400 | 125 | | 50 | Shipping | 121 | 1500 | 126 | | 60 | IT | 103 | 1400 | 127 | | 70 | Public Relations | 204 | 2700 | 128 | | 80 | Sales | 145 | 2500 | 129 | | 90 | Executive | 100 | 1700 | 130 | | 100 | Finance | 108 | 1700 | 131 | | 110 | Accounting | 205 | 1700 | 132 | | 120 | Treasury | 0 | 1700 | 133 | | 130 | Corporate Tax | 0 | 1700 | 134 | | 140 | Control And Credit | 0 | 1700 | 135 | | 150 | Shareholder Services | 0 | 1700 | 136 | | 160 | Benefits | 0 | 1700 | 137 | | 170 | Manufacturing | 0 | 1700 | 138 | | 180 | Construction | 0 | 1700 | 139 | | 190 | Contracting | 0 | 1700 | 140 | | 200 | Operations | 0 | 1700 | 141 | | 210 | IT Support | 0 | 1700 | 142 | | 220 | NOC | 0 | 1700 | 143 | | 230 | IT Helpdesk | 0 | 1700 | 144 | | 240 | Government Sales | 0 | 1700 | 145 | | 250 | Retail Sales | 0 | 1700 | 146 | | 260 | Recruiting | 0 | 1700 | 147 | | 270 | Payroll | 0 | 1700 | 148 | +---------------+----------------------+------------+-------------+ 149 | 150 | Table: locations 151 | +-------------+------------------------------------------+-------------+---------------------+-------------------+------------+ 152 | | LOCATION_ID | STREET_ADDRESS | POSTAL_CODE | CITY | STATE_PROVINCE | COUNTRY_ID | 153 | +-------------+------------------------------------------+-------------+---------------------+-------------------+------------+ 154 | | 1000 | 1297 Via Cola di Rie | 989 | Roma | | IT | 155 | | 1100 | 93091 Calle della Testa | 10934 | Venice | | IT | 156 | | 1200 | 2017 Shinjuku-ku | 1689 | Tokyo | Tokyo Prefecture | JP | 157 | | 1300 | 9450 Kamiya-cho | 6823 | Hiroshima | | JP | 158 | | 1400 | 2014 Jabberwocky Rd | 26192 | Southlake | Texas | US | 159 | | 1500 | 2011 Interiors Blvd | 99236 | South San Francisco | California | US | 160 | | 1600 | 2007 Zagora St | 50090 | South Brunswick | New Jersey | US | 161 | | 1700 | 2004 Charade Rd | 98199 | Seattle | Washington | US | 162 | | 1800 | 147 Spadina Ave | M5V 2L7 | Toronto | Ontario | CA | 163 | | 1900 | 6092 Boxwood St | YSW 9T2 | Whitehorse | Yukon | CA | 164 | | 2000 | 40-5-12 Laogianggen | 190518 | Beijing | | CN | 165 | | 2100 | 1298 Vileparle (E) | 490231 | Bombay | Maharashtra | IN | 166 | | 2200 | 12-98 Victoria Street | 2901 | Sydney | New South Wales | AU | 167 | | 2300 | 198 Clementi North | 540198 | Singapore | | SG | 168 | | 2400 | 8204 Arthur St | | London | | UK | 169 | | 2500 | Magdalen Centre, The Oxford Science Park | OX9 9ZB | Oxford | Oxford | UK | 170 | | 2600 | 9702 Chester Road | 9629850293 | Stretford | Manchester | UK | 171 | | 2700 | Schwanthalerstr. 7031 | 80925 | Munich | Bavaria | DE | 172 | | 2800 | Rua Frei Caneca 1360 | 01307-002 | Sao Paulo | Sao Paulo | BR | 173 | | 2900 | 20 Rue des Corps-Saints | 1730 | Geneva | Geneve | CH | 174 | | 3000 | Murtenstrasse 921 | 3095 | Bern | BE | CH | 175 | | 3100 | Pieter Breughelstraat 837 | 3029SK | Utrecht | Utrecht | NL | 176 | | 3200 | Mariano Escobedo 9991 | 11932 | Mexico City | Distrito Federal, | MX | 177 | +-------------+------------------------------------------+-------------+---------------------+-------------------+------------+ 178 | 179 | Table: countries 180 | +------------+--------------------------+-----------+ 181 | | COUNTRY_ID | COUNTRY_NAME | REGION_ID | 182 | +------------+--------------------------+-----------+ 183 | | AR | Argentina | 2 | 184 | | AU | Australia | 3 | 185 | | BE | Belgium | 1 | 186 | | BR | Brazil | 2 | 187 | | CA | Canada | 2 | 188 | | CH | Switzerland | 1 | 189 | | CN | China | 3 | 190 | | DE | Germany | 1 | 191 | | DK | Denmark | 1 | 192 | | EG | Egypt | 4 | 193 | | FR | France | 1 | 194 | | IL | Israel | 4 | 195 | | IN | India | 3 | 196 | | IT | Italy | 1 | 197 | | JP | Japan | 3 | 198 | | KW | Kuwait | 4 | 199 | | ML | Malaysia | 3 | 200 | | MX | Mexico | 2 | 201 | | NG | Nigeria | 4 | 202 | | NL | Netherlands | 1 | 203 | | SG | Singapore | 3 | 204 | | UK | United Kingdom | 1 | 205 | | US | United States of America | 2 | 206 | | ZM | Zambia | 4 | 207 | | ZW | Zimbabwe | 4 | 208 | +------------+--------------------------+-----------+ 209 | 210 | Table: job_history 211 | +-------------+------------+------------+------------+---------------+ 212 | | EMPLOYEE_ID | START_DATE | END_DATE | JOB_ID | DEPARTMENT_ID | 213 | +-------------+------------+------------+------------+---------------+ 214 | | 102 | 2001-01-13 | 2006-07-24 | IT_PROG | 60 | 215 | | 101 | 1997-09-21 | 2001-10-27 | AC_ACCOUNT | 110 | 216 | | 101 | 2001-10-28 | 2005-03-15 | AC_MGR | 110 | 217 | | 201 | 2004-02-17 | 2007-12-19 | MK_REP | 20 | 218 | | 114 | 2006-03-24 | 2007-12-31 | ST_CLERK | 50 | 219 | | 122 | 2007-01-01 | 2007-12-31 | ST_CLERK | 50 | 220 | | 200 | 1995-09-17 | 2001-06-17 | AD_ASST | 90 | 221 | | 176 | 2006-03-24 | 2006-12-31 | SA_REP | 80 | 222 | | 176 | 2007-01-01 | 2007-12-31 | SA_MAN | 80 | 223 | | 200 | 2002-07-01 | 2006-12-31 | AC_ACCOUNT | 90 | 224 | +-------------+------------+------------+------------+---------------+ 225 | 226 | Table: jobs 227 | +------------+---------------------------------+------------+------------+ 228 | | JOB_ID | JOB_TITLE | MIN_SALARY | MAX_SALARY | 229 | +------------+---------------------------------+------------+------------+ 230 | | AD_PRES | President | 20080 | 40000 | 231 | | AD_VP | Administration Vice President | 15000 | 30000 | 232 | | AD_ASST | Administration Assistant | 3000 | 6000 | 233 | | FI_MGR | Finance Manager | 8200 | 16000 | 234 | | FI_ACCOUNT | Accountant | 4200 | 9000 | 235 | | AC_MGR | Accounting Manager | 8200 | 16000 | 236 | | AC_ACCOUNT | Public Accountant | 4200 | 9000 | 237 | | SA_MAN | Sales Manager | 10000 | 20080 | 238 | | SA_REP | Sales Representative | 6000 | 12008 | 239 | | PU_MAN | Purchasing Manager | 8000 | 15000 | 240 | | PU_CLERK | Purchasing Clerk | 2500 | 5500 | 241 | | ST_MAN | Stock Manager | 5500 | 8500 | 242 | | ST_CLERK | Stock Clerk | 2008 | 5000 | 243 | | SH_CLERK | Shipping Clerk | 2500 | 5500 | 244 | | IT_PROG | Programmer | 4000 | 10000 | 245 | | MK_MAN | Marketing Manager | 9000 | 15000 | 246 | | MK_REP | Marketing Representative | 4000 | 9000 | 247 | | HR_REP | Human Resources Representative | 4000 | 9000 | 248 | | PR_REP | Public Relations Representative | 4500 | 10500 | 249 | +------------+---------------------------------+------------+------------+ 250 | 251 | 252 | /* 1. Write a query to display the name (first name and last name) for those employees who gets more salary than the employee whose ID is 163. */ 253 | 254 | SELECT first_name, 255 | last_name 256 | FROM employees 257 | WHERE salary > (SELECT salary 258 | FROM employees 259 | WHERE employee_id = 163); 260 | 261 | 262 | /* 2. Write a query to display the name (first name and last name), salary, department id, job id for those employees who works in the same designation as the employee works whose id is 169. */ 263 | 264 | SELECT first_name, 265 | last_name, 266 | salary, 267 | department_id, 268 | job_id 269 | FROM employees 270 | WHERE job_id = (SELECT job_id 271 | FROM employees 272 | WHERE employee_id = 169); 273 | 274 | 275 | /* 3. Write a query to display the name (first name and last name), salary, department id for those employees who earn such amount of salary which is the smallest salary of any of the departments. */ 276 | 277 | SELECT first_name, 278 | last_name, 279 | salary, 280 | department_id 281 | FROM employees 282 | WHERE salary IN (SELECT MIN(salary) 283 | FROM employees 284 | GROUP BY department_id); 285 | 286 | 287 | /* 4. Write a query to display the employee id, employee name (first name and last name) for all employees who earn more than the average salary. */ 288 | 289 | SELECT employee_id, 290 | first_name, 291 | last_name 292 | FROM employees 293 | WHERE salary > (SELECT AVG(salary) 294 | FROM employees); 295 | 296 | 297 | /* 5. Write a query to display the employee name (first name and last name), employee id and salary of all employees who report to Payam. */ 298 | 299 | SELECT first_name, 300 | last_name, 301 | employee_id, 302 | salary 303 | FROM employees 304 | WHERE manager_id = (SELECT employee_id 305 | FROM employees 306 | WHERE first_name = 'Payam'); 307 | 308 | 309 | /* 6. Write a query to display the department number, name (first name and last name), job_id and department name for all employees in the Finance department. */ 310 | 311 | SELECT e.department_id, 312 | e.first_name, 313 | e.last_name, 314 | e.job_id, 315 | d.department_name 316 | FROM employees e 317 | INNER JOIN departments d 318 | ON e.department_id = d.department_id 319 | WHERE d.department_name = 'Finance'; 320 | 321 | 322 | /* 7. Write a query to display all the information of an employee whose salary and reporting person id is 3000 and 121, respectively. */ 323 | 324 | SELECT * 325 | FROM employees 326 | WHERE salary = 3000.00 327 | AND manager_id = 121; 328 | 329 | -- Note: This also works using subquery. 330 | 331 | SELECT * 332 | FROM employees 333 | WHERE (salary, manager_id) = (SELECT 3000, 121); 334 | 335 | 336 | /* 8. Display all the information of an employee whose id is any of the number 134, 159 and 183. */ 337 | 338 | SELECT * 339 | FROM employees 340 | WHERE employee_id IN (134, 159, 183); 341 | 342 | 343 | /* 9. Write a query to display all the information of the employees whose salary is within the range 1000 and 3000. */ 344 | 345 | SELECT * 346 | FROM employees 347 | WHERE salary BETWEEN 1000.00 AND 3000.00; 348 | 349 | 350 | /* 10. Write a query to display all the information of the employees whose salary is within the range of smallest salary and 2500. */ 351 | 352 | SELECT * 353 | FROM employees 354 | WHERE salary BETWEEN (SELECT MIN(salary) 355 | FROM employees) AND 2500.00; 356 | 357 | 358 | /* 11. Write a query to display all the information of the employees who does not work in those departments where some employees works whose manager id within the range 100 and 200. */ 359 | 360 | SELECT * 361 | FROM employees 362 | WHERE department_id NOT IN (SELECT department_id 363 | FROM departments 364 | WHERE manager_id BETWEEN 100 AND 200); 365 | 366 | 367 | /* 12. Write a query to display all the information for those employees whose id is any id who earn the second highest salary. */ 368 | 369 | SELECT * 370 | FROM employees 371 | WHERE employee_id IN (SELECT employee_id 372 | FROM employees 373 | WHERE salary IN (SELECT MAX(salary) 374 | FROM employees 375 | WHERE salary < (SELECT MAX(salary) 376 | FROM employees))); 377 | 378 | 379 | /* 13. Write a query to display the employee name (first name and last name) and hire date for all employees in the same department as Clara. Exclude Clara. */ 380 | 381 | SELECT first_name, 382 | last_name, 383 | hire_date 384 | FROM employees 385 | WHERE department_id = (SELECT department_id 386 | FROM employees 387 | WHERE first_name = 'Clara') 388 | AND first_name != 'Clara'; 389 | 390 | 391 | /* 14. Write a query to display the employee number and name (first name and last name) for all employees who work in a department with any employee whose name contains a T. */ 392 | 393 | SELECT employee_id, 394 | first_name, 395 | last_name 396 | FROM employees 397 | WHERE department_id IN (SELECT department_id 398 | FROM employees 399 | WHERE first_name LIKE '%T%'); 400 | 401 | 402 | /* 15. Write a query to display the employee number, name (first name and last name), and salary for all employees who earn more than the average salary and who work in a department with any employee with a J in their name. */ 403 | 404 | SELECT employee_id, 405 | first_name, 406 | last_name, 407 | salary 408 | FROM employees 409 | WHERE salary > (SELECT AVG(salary) 410 | FROM employees) 411 | AND department_id IN (SELECT department_id 412 | FROM employees 413 | WHERE first_name LIKE '%J%'); 414 | 415 | 416 | /* 16. Display the employee name (first name and last name), employee id, and job title for all employees whose department location is Toronto. */ 417 | 418 | SELECT first_name, 419 | last_name, 420 | employee_id, 421 | job_id 422 | FROM employees 423 | WHERE department_id IN (SELECT department_id 424 | FROM departments 425 | WHERE location_id IN (SELECT location_id 426 | FROM locations 427 | WHERE city = 'Toronto')); 428 | 429 | 430 | /* 17. Write a query to display the employee number, name (first name and last name) and job title for all employees whose salary is smaller than any salary of those employees whose job title is MK_MAN. */ 431 | 432 | SELECT employee_id, 433 | first_name, 434 | last_name, 435 | job_id 436 | FROM employees 437 | WHERE salary < ANY (SELECT salary 438 | FROM employees 439 | WHERE job_id = 'MK_MAN'); 440 | 441 | 442 | /* 18. Write a query to display the employee number, name (first name and last name) and job title for all employees whose salary is smaller than any salary of those employees whose job title is MK_MAN. Exclude Job title MK_MAN. */ 443 | 444 | SELECT employee_id, 445 | first_name, 446 | last_name, 447 | job_id 448 | FROM employees 449 | WHERE salary < ANY (SELECT salary 450 | FROM employees 451 | WHERE job_id = 'MK_MAN') 452 | AND job_id != 'MK_MAN'; 453 | 454 | 455 | /* 19. Write a query to display the employee number, name (first name and last name) and job title for all employees whose salary is more than any salary of those employees whose job title is PU_MAN. Exclude job title PU_MAN. */ 456 | 457 | SELECT employee_id, 458 | first_name, 459 | last_name, 460 | job_id 461 | FROM employees 462 | WHERE salary > ANY (SELECT salary 463 | FROM employees 464 | WHERE job_id = 'PU_MAN') 465 | AND job_id != 'PU_MAN'; 466 | 467 | 468 | /* 20. Write a query to display the employee number, name (first name and last name) and job title for all employees whose salary is more than any average salary of any department. */ 469 | 470 | SELECT employee_id, 471 | first_name, 472 | last_name, 473 | job_id 474 | FROM employees 475 | WHERE salary > ANY (SELECT AVG(salary) 476 | FROM employees 477 | GROUP BY department_id); 478 | 479 | 480 | /* 21. Write a query to display the employee name( first name and last name ) and department for all employees for any existence of those employees whose salary is more than 3700. */ 481 | 482 | SELECT first_name, 483 | last_name, 484 | department_id 485 | FROM employees 486 | WHERE EXISTS (SELECT * 487 | FROM employees 488 | WHERE salary > 3700.00); 489 | 490 | 491 | /* 22. Write a query to display the department id and the total salary for those departments which contains at least one employee. */ 492 | 493 | SELECT department_id, 494 | SUM(salary) 495 | FROM employees 496 | WHERE department_id IN (SELECT department_id 497 | FROM departments) 498 | GROUP BY department_id 499 | HAVING COUNT(department_id) >= 1; 500 | 501 | 502 | /* 23. Write a query to display the employee id, name (first name and last name) and the job id column with a modified title SALESMAN for those employees whose job title is ST_MAN and DEVELOPER for whose job title is IT_PROG. */ 503 | 504 | SELECT employee_id, 505 | first_name, 506 | last_name, 507 | CASE WHEN job_id = 'ST_MAN' THEN 'SALESMAN' 508 | WHEN job_id = 'IT_PROG' THEN 'DEVELOPER' 509 | ELSE job_id END AS job_id_mod 510 | FROM employees; 511 | 512 | 513 | /* 24. Write a query to display the employee id, name (first name and last name), salary and the SalaryStatus column with a title HIGH and LOW respectively for those employees whose salary is more than and less than the average salary of all employees. */ 514 | 515 | SELECT employee_id, 516 | first_name, 517 | last_name, 518 | salary, 519 | CASE WHEN salary >= (SELECT AVG(salary) FROM employees) THEN 'HIGH' 520 | ELSE 'LOW' END AS salary_status 521 | FROM employees; 522 | 523 | 524 | /* 25. Write a query to display the employee id, name (first name and last name), Salary, AvgCompare (salary - the average salary of all employees) and the SalaryStatus column with a title HIGH and LOW respectively for those employees whose salary is more than and less than the average salary of all employees. */ 525 | 526 | SELECT employee_id, 527 | first_name, 528 | last_name, 529 | salary AS salary_drawn, 530 | ROUND(salary - (SELECT AVG(salary) FROM employees), 2) AS avg_compare, 531 | CASE WHEN salary >= (SELECT AVG(salary) FROM employees) THEN 'HIGH' 532 | ELSE 'LOW' END AS salary_status 533 | FROM employees; 534 | 535 | 536 | /* 26. Write a subquery that returns a set of rows to find all departments that do actually have one or more employees assigned to them. */ 537 | 538 | SELECT department_name 539 | FROM departments 540 | WHERE department_id IN (SELECT DISTINCT department_id 541 | FROM employees); 542 | 543 | 544 | /* 27. Write a query that will identify all employees who work in departments located in the United Kingdom. */ 545 | 546 | SELECT * 547 | FROM employees 548 | WHERE department_id IN (SELECT department_id 549 | FROM departments 550 | WHERE location_id IN (SELECT location_id 551 | FROM locations 552 | WHERE country_id IN (SELECT country_id 553 | FROM countries 554 | WHERE country_name = 'United Kingdom'))); 555 | 556 | 557 | /* 28. Write a query to identify all the employees who earn more than the average and who work in any of the IT departments. */ 558 | 559 | SELECT * 560 | FROM employees 561 | WHERE salary > (SELECT AVG(salary) 562 | FROM employees) 563 | AND department_id IN (SELECT department_id 564 | FROM departments 565 | WHERE department_name LIKE ('%IT%')); 566 | 567 | 568 | /* 29. Write a query to determine who earns more than Mr. Ozer. */ 569 | 570 | SELECT first_name, 571 | last_name, 572 | salary 573 | FROM employees 574 | WHERE salary > (SELECT salary 575 | FROM employees 576 | WHERE last_name = 'Ozer'); 577 | 578 | 579 | /* 30. Write a query to find out which employees have a manager who works for a department based in the US. */ 580 | 581 | SELECT first_name, 582 | last_name 583 | FROM employees 584 | WHERE manager_id IN (SELECT employee_id 585 | FROM employees 586 | WHERE department_id IN (SELECT department_id 587 | FROM departments 588 | WHERE location_id IN (SELECT location_id 589 | FROM locations 590 | WHERE country_id = 'US'))); 591 | 592 | 593 | /* 31. Write a query which is looking for the names of all employees whose salary is greater than 50% of their department’s total salary bill. */ 594 | 595 | SELECT e1.first_name, 596 | e1.last_name 597 | FROM employees e1 598 | WHERE salary > (SELECT SUM(salary)*0.5 599 | FROM employees e2 600 | WHERE e1.department_id = e2.department_id); 601 | 602 | 603 | /* 32. Write a query to get the details of employees who are managers. */ 604 | 605 | SELECT * 606 | FROM employees 607 | WHERE employee_id IN (SELECT manager_id 608 | FROM departments); 609 | 610 | 611 | /* 33. Write a query to get the details of employees who manage a department. */ 612 | 613 | SELECT * 614 | FROM employees 615 | WHERE employee_id = ANY (SELECT manager_id 616 | FROM departments); 617 | 618 | 619 | /* 34. Write a query to display the employee id, name (first name and last name), salary, department name and city for all the employees who gets the salary as the salary earn by the employee which is maximum within the joining person January 1st, 2002 and December 31st, 2003. */ 620 | 621 | SELECT e.employee_id, 622 | e.first_name, 623 | e.last_name, 624 | e.salary, 625 | d.department_name, 626 | l.city 627 | FROM employees e 628 | INNER JOIN departments d 629 | ON e.department_id = d.department_id 630 | INNER JOIN locations l 631 | ON d.location_id = l.location_id 632 | WHERE e.salary = (SELECT MAX(salary) 633 | FROM employees 634 | WHERE hire_date BETWEEN '2002-01-01' AND '2003-12-31'); 635 | 636 | 637 | /* 35. Write a query in SQL to display the department code and name for all departments which located in the city London. */ 638 | 639 | SELECT department_id, 640 | department_name 641 | FROM departments 642 | WHERE location_id IN (SELECT location_id 643 | FROM locations 644 | WHERE city = 'London'); 645 | 646 | 647 | /* 36. Write a query in SQL to display the first and last name, salary, and department ID for all those employees who earn more than the average salary and arrange the list in descending order on salary. */ 648 | 649 | SELECT first_name, 650 | last_name, 651 | salary, 652 | department_id 653 | FROM employees 654 | WHERE salary > (SELECT AVG(salary) 655 | FROM employees) 656 | ORDER BY salary DESC; 657 | 658 | 659 | /* 37. Write a query in SQL to display the first and last name, salary, and department ID for those employees who earn more than the maximum salary of a department which ID is 40. */ 660 | 661 | SELECT first_name, 662 | last_name, 663 | salary, 664 | department_id 665 | FROM employees 666 | WHERE salary > (SELECT MAX(salary) 667 | FROM employees 668 | WHERE department_id = 40); 669 | 670 | 671 | /* 38. Write a query in SQL to display the department name and Id for all departments where they located, that Id is equal to the Id for the location where department number 30 is located. */ 672 | 673 | SELECT department_name, 674 | department_id 675 | FROM departments 676 | WHERE location_id = (SELECT location_id 677 | FROM departments 678 | WHERE department_id = 30); 679 | 680 | 681 | /* 39. Write a query in SQL to display the first and last name, salary, and department ID for all those employees who work in that department where the employee works who hold the ID 201. */ 682 | 683 | SELECT first_name, 684 | last_name, 685 | salary, 686 | department_id 687 | FROM employees 688 | WHERE department_id = (SELECT department_id 689 | FROM employees 690 | WHERE employee_id = 201); 691 | 692 | 693 | /* 40. Write a query in SQL to display the first and last name, salary, and department ID for those employees whose salary is equal to the salary of the employee who works in that department which ID is 40. */ 694 | 695 | SELECT first_name, 696 | last_name, 697 | salary, 698 | department_id 699 | FROM employees 700 | WHERE salary IN (SELECT salary 701 | FROM employees 702 | WHERE department_id = 40); 703 | 704 | 705 | /* 41. Write a query in SQL to display the first and last name, and department code for all employees who work in the department Marketing. */ 706 | 707 | SELECT first_name, 708 | last_name, 709 | department_id 710 | FROM employees 711 | WHERE department_id IN (SELECT department_id 712 | FROM departments 713 | WHERE department_name = 'Marketing'); 714 | 715 | 716 | /* 42. Write a query in SQL to display the first and last name, salary, and department ID for those employees who earn more than the minimum salary of a department which ID is 40. */ 717 | 718 | SELECT first_name, 719 | last_name, 720 | salary, 721 | department_id 722 | FROM employees 723 | WHERE salary > (SELECT MIN(salary) 724 | FROM employees 725 | WHERE department_id = 40); 726 | 727 | 728 | /* 43. Write a query in SQL to display the full name, email, and hire date for all those employees who was hired after the employee whose ID is 165. */ 729 | 730 | SELECT CONCAT(first_name, ' ', last_name) AS full_name, 731 | email, 732 | hire_date 733 | FROM employees 734 | WHERE hire_date > (SELECT hire_date 735 | FROM employees 736 | WHERE employee_id = 165); 737 | 738 | 739 | /* 44. Write a query in SQL to display the first and last name, salary, and department ID for those employees who earn less than the minimum salary of a department which ID is 70. */ 740 | 741 | SELECT first_name, 742 | last_name, 743 | salary, 744 | department_id 745 | FROM employees 746 | WHERE salary < (SELECT MIN(salary) 747 | FROM employees 748 | WHERE department_id = 70); 749 | 750 | 751 | /* 45. Write a query in SQL to display the first and last name, salary, and department ID for those employees who earn less than the average salary, and also work at the department where the employee Laura is working as a first name holder. */ 752 | 753 | SELECT first_name, 754 | last_name, 755 | salary, 756 | department_id 757 | FROM employees 758 | WHERE salary < (SELECT AVG(salary) 759 | FROM employees) 760 | AND department_id = (SELECT department_id 761 | FROM employees 762 | WHERE first_name = 'Laura'); 763 | 764 | 765 | /* 46. Write a query in SQL to display the first and last name, salary, and department ID for those employees whose department is located in the city London. */ 766 | 767 | SELECT first_name, 768 | last_name, 769 | salary, 770 | department_id 771 | FROM employees 772 | WHERE department_id IN (SELECT department_id 773 | FROM departments 774 | WHERE location_id IN (SELECT location_id 775 | FROM locations 776 | WHERE city = 'London')); 777 | 778 | 779 | /* 47. Write a query in SQL to display the city of the employee whose ID 134 and works there. */ 780 | 781 | SELECT city 782 | FROM locations 783 | WHERE location_id IN (SELECT location_id 784 | FROM departments 785 | WHERE department_id IN (SELECT department_id 786 | FROM employees 787 | WHERE employee_id = 134)); 788 | 789 | 790 | /* 48. Write a query in SQL to display the the details of those departments which max salary is 7000 or above for those employees who already done one or more jobs. */ 791 | 792 | SELECT * 793 | FROM departments 794 | WHERE department_id IN (SELECT department_id 795 | FROM employees 796 | WHERE employee_id IN (SELECT employee_id 797 | FROM job_history 798 | GROUP BY employee_id 799 | HAVING COUNT(*) > 1) 800 | GROUP BY department_id 801 | HAVING MAX(salary) > 7000); 802 | 803 | 804 | /* 49. Write a query in SQL to display the detail information of those departments which starting salary is at least 8000. */ 805 | 806 | SELECT * 807 | FROM departments 808 | WHERE department_id IN (SELECT department_id 809 | FROM employees 810 | GROUP BY department_id 811 | HAVING MIN(salary) >= 8000); 812 | 813 | 814 | /* 50. Write a query in SQL to display the full name (first and last name) of manager who is supervising 4 or more employees. */ 815 | 816 | SELECT CONCAT(first_name, ' ', last_name) AS full_name 817 | FROM employees 818 | WHERE employee_id IN (SELECT manager_id 819 | FROM employees 820 | GROUP BY manager_id 821 | HAVING COUNT(*) >= 4); 822 | 823 | 824 | /* 51. Write a query in SQL to display the details of the current job for those employees who worked as a Sales Representative in the past. */ 825 | 826 | SELECT * 827 | FROM jobs 828 | WHERE job_id IN (SELECT job_id 829 | FROM employees 830 | WHERE employee_id IN (SELECT employee_id 831 | FROM job_history 832 | WHERE job_id = 'SA_REP')); 833 | 834 | 835 | /* 52. Write a query in SQL to display all the information about those employees who earn second lowest salary of all the employees. */ 836 | 837 | SELECT * 838 | FROM employees 839 | WHERE salary IN (SELECT MIN(salary) 840 | FROM employees 841 | WHERE salary > (SELECT MIN(salary) 842 | FROM employees)); 843 | 844 | 845 | /* 53. Write a query in SQL to display the details of departments managed by Susan. */ 846 | 847 | SELECT * 848 | FROM departments 849 | WHERE manager_id IN (SELECT employee_id 850 | FROM employees 851 | WHERE first_name = 'Susan'); 852 | 853 | 854 | /* 54. Write a query in SQL to display the department ID, full name (first and last name), salary for those employees who is highest salary drawer in a department. */ 855 | 856 | SELECT department_id, 857 | CONCAT(first_name, ' ', last_name) AS full_name, 858 | salary 859 | FROM employees e 860 | WHERE salary IN (SELECT MAX(salary) 861 | FROM employees 862 | WHERE department_id = e.department_id); 863 | 864 | 865 | /* 55. Write a query in SQL to display all the information of those employees who did not have any job in the past. */ 866 | 867 | SELECT * 868 | FROM employees 869 | WHERE employee_id NOT IN (SELECT employee_id 870 | FROM job_history); 871 | 872 | --------------------------------------------------------------------------------