Skip to content

Instantly share code, notes, and snippets.

@clarade
Created March 24, 2021 21:51
Show Gist options
  • Save clarade/1393d8a2ca0eca784abfe48ffa02496c to your computer and use it in GitHub Desktop.
Save clarade/1393d8a2ca0eca784abfe48ffa02496c to your computer and use it in GitHub Desktop.
Exercise about sql advanced basics
mysql> SELECT team.name, COUNT(*) AS nb_student
-> FROM team JOIN player ON team.id=player.team_id
-> GROUP BY team_id
-> ORDER BY nb_student DESC;
+------------+------------+
| name | nb_student |
+------------+------------+
| Gryffindor | 36 |
| Slytherin | 21 |
| Ravenclaw | 15 |
| Hufflepuff | 12 |
+------------+------------+
4 rows in set (0,00 sec)
mysql> SELECT team.name
-> FROM team JOIN player ON team.id=player.team_id
-> GROUP BY team.name
-> HAVING COUNT(*) >= 14
-> ORDER BY team.name ASC;
+------------+
| name |
+------------+
| Gryffindor |
| Ravenclaw |
| Slytherin |
+------------+
3 rows in set (0,00 sec)
mysql> SELECT team.name, wizard.firstname, wizard.lastname, enrollment_date
-> FROM team JOIN player ON team.id=player.team_id
-> JOIN wizard ON wizard.id=player.wizard_id
-> WHERE team.id=1 AND DAYOFWEEK(enrollment_date) = 2
-> ORDER BY enrollment_date ASC;
+------------+-----------+------------+-----------------+
| name | firstname | lastname | enrollment_date |
+------------+-----------+------------+-----------------+
| Gryffindor | George | Weasley | 1991-08-26 |
| Gryffindor | Alice | Longbottom | 1992-02-17 |
| Gryffindor | Cadogan | | 1993-01-04 |
| Gryffindor | Godric | Gryffindor | 1993-08-30 |
| Gryffindor | Sirius | Black | 1994-01-10 |
| Gryffindor | Aberforth | Dumbledore | 1995-04-24 |
| Gryffindor | Augusta | Longbottom | 1999-10-25 |
+------------+-----------+------------+-----------------+
7 rows in set (0,01 sec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment