Sora952

SQL advanced wild !

May 28th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.16 KB | None | 0 0
  1. mysql> select t.name, COUNT(p.team_id) AS nb_players FROM player p JOIN team t ON t.id=p.team_id GROUP BY team_id ORDER BY nb_players DESC;
  2. +------------+------------+
  3. | name       | nb_players |
  4. +------------+------------+
  5. | Gryffindor |         36 |
  6. | Slytherin  |         21 |
  7. | Ravenclaw  |         15 |
  8. | Hufflepuff |         12 |
  9. +------------+------------+
  10. 4 rows in set (0.01 sec)
  11.  
  12. mysql>
  13.  
  14. mysql> select t.name, COUNT(p.team_id) AS nb_players FROM player p JOIN team t ON t.id=p.team_id GROUP BY team_id HAVING nb_players > 14 ORDER BY t.name;
  15. +------------+------------+
  16. | name       | nb_players |
  17. +------------+------------+
  18. | Gryffindor |         36 |
  19. | Ravenclaw  |         15 |
  20. | Slytherin  |         21 |
  21. +------------+------------+
  22. 3 rows in set (0.00 sec)
  23.  
  24. mysql>                                                                          
  25.  
  26.  
  27. mysql> select w.lastname, w.firstname FROM player p JOIN wizard w ON w.id=p.wizard_id WHERE DAYOFWEEK(enrollment_date) = 2 GROUP BY wizard_id;
  28. +------------+-------------+
  29. | lastname   | firstname   |
  30. +------------+-------------+
  31. | Abbott     | Hannah      |
  32. | Black      | Sirius      |
  33. | Chang      | Cho         |
  34. | Dumbledore | Aberforth   |
  35. | Flint      | Marcus      |
  36. | Gryffindor | Godric      |
  37. | Longbottom | Alice       |
  38. | Longbottom | Augusta     |
  39. | Lovegood   | Xenophilius |
  40. | Nott       | Theodore    |
  41. | Weasley    | George      |
  42. | Zabini     | Blaise      |
  43. |            | Cadogan     |
  44. +------------+-------------+
  45. 13 rows in set (0.00 sec)
  46.  
  47. mysql> select w.lastname, w.firstname FROM player p JOIN wizard w ON w.id=p.wizard_id JOIN team t ON t.id=p.team_id WHERE DAYOFWEEK(enrollment_date) = 2 AND t.name='Gryffindor' GROUP BY p.id ORDER BY p.enrollment_date;
  48. +------------+-----------+
  49. | lastname   | firstname |
  50. +------------+-----------+
  51. | Weasley    | George    |
  52. | Longbottom | Alice     |
  53. |            | Cadogan   |
  54. | Gryffindor | Godric    |
  55. | Black      | Sirius    |
  56. | Dumbledore | Aberforth |
  57. | Longbottom | Augusta   |
  58. +------------+-----------+
  59. 7 rows in set (0.00 sec)
  60.  
  61. mysql>
Add Comment
Please, Sign In to add comment