Last active
June 4, 2019 15:18
-
-
Save santiagocodes/5a4a2006b761a57aba37553e6fed80af to your computer and use it in GitHub Desktop.
Advanced SQL Workshop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Create a DB from the following script: | |
https://gist.github.com/sblondeau/5cdc304b160960bb20c1cd0ba314cf3e | |
Once the tables are created, write the queries to display: | |
1. The first name, name and age of the characters | |
mysql> SELECT firstname, lastname, age FROM Person; | |
2. The first name, name of the characters and their kingdom, only for those connected to a kingdom | |
mysql> SELECT ch.firstname, ch.lastname, ki.name | |
> FROM Person AS ch | |
> JOIN Kingdom AS ki ON ki.id=ch.kingdom_id; | |
3. The same thing including all the characters | |
mysql> SELECT ch.firstname, ch.lastname, ki.name | |
> FROM Person AS ch | |
> LEFT JOIN Kingdom AS ki ON ki.id=ch.kingdom_id; | |
4. The average age of the characters | |
mysql> SELECT AVG(age) AS AverageAge FROM Person; | |
5. The number of characters per kingdom (include kingdoms with no character) | |
mysql> SELECT kingdom_id, count(*) AS NumChar FROM Person GROUP BY kingdom_id; | |
6. The average age by role | |
mysql> SELECT Role.id, AVG(age) AS AverageAge | |
> FROM Person JOIN Role ON Role.id=Person.role_id | |
> GROUP BY role_id; | |
7. The average is a little high no? Retrieve then the average of all the characters not being magicians | |
mysql> SELECT AVG(age) AS AverageAge FROM Person WHERE kingdom_id != 3; | |
8. List all the characters with their role and possible realm (kingdoms) | |
mysql> SELECT Person.firstname, Role.role | |
> FROM Person | |
> JOIN Role ON Role.id=Person.role_id; | |
9. Show realms(kingdom) with at least 2 topics | |
mysql> SELECT Kingdom |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment