Data Manipulation in SQL
Here you can access every table used in the course. To access each table, you will need to specify the soccer schema in your queries (e.g., soccer.match for the match table, and soccer.league for the league table).
Take Notes
Add notes about the concepts you've learned and SQL cells with queries you want to keep.
SUBQUERIES SQL
Use as subquery em WHERE para realizar filtros.
Use as subquery em FROM - pode fazer mais de uma em FROM EX:
SELECT team, home_avg FROM (SELECT t.team_long_name AS team, AVG(m.home_goal) AS home_avg FROM match AS m LEFT JOIN teams AS t ON m.hometeam_id = t.team_api_id WHERE season = '2011/2012' GROUP BY team) AS subquery ORDER BY home_avg DESC LIMIT 3;
Também pode usar em SELECT para realizar outros filtros e comparar valores com a subquery. Aqui a subquery deve gerar apenas um resultado. EX:
SELECT date, (home_goal + away goal) AS goals, (home_goal + away goal) - (SELECT AVG(home_goal + away_goal) FROM match WHERE season = '2011/2012') AS diff FROM match WHERE SEASON = '2011/2012'
FROM (SELECT
t.team_long_name AS team,
AVG(m.home_goal) AS home_avg
FROM (match AS m
LEFT JOIN teams AS t
ON m.hometeam_id = t.team_api_id
WHERE season = '2011/2012'
GROUP BY team) AS subquery
ORDER BY home_avg DESC
LIMIT 3;Também pode usar em SELECT para realizar outros filtros e comparar valores com a subquery.
Explore Datasets
Use the match, league, and country tables to explore the data and practice your skills!
- Use the
match,league, andcountrytables to return the number of matches played in Great Britain versus elsewhere in the world.- "England", "Scotland", and "Wales" should be categorized as "Great Britain"
- All other leagues will need to be categorized as "World".
- Use the
matchandcountrytables to return the countries in which the average number of goals (home and away goals) scored are greater than the average number of goals of all matches. - In a soccer league, points are assigned to teams based on the result of a game. Here, let's assume that 3 points are awarded for a win, 1 for a tie, and 0 for a defeat. Use the
matchtable to calculate the running total of points earned by the team "Chelsea" (team id 8455) in the season "2014/2015".- The final output should have the match date, the points earned by Chelsea, and the running total.