IniziaInizia gratis

Conditional selection and summation with CASE WHEN

You can use CASE statements to apply a constraint and run a calculation by including the statement inside an aggregate function like SUM()!

In this exercise, your goal is to filter for a specific team (Real Sociedad) and calculate their total home and away goals per season.

Questo esercizio fa parte del corso

Data Manipulation in SQL

Visualizza il corso

Istruzioni dell'esercizio

  • Create a CASE statement to calculate the total number of home goals where the hometeam_id is 8560.
  • Create a second CASE statement to calculate the total number of away goals where the awayteam_id is 8560, aliasing the column as away_goals.
  • Group the query by season.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

SELECT season,
	-- SUM the home goals
    ___(___ ___ hometeam_id = 8560 THEN ___ END) AS home_goals,
    -- SUM the away goals
    ___
FROM match
-- Group the results by season
___
Modifica ed esegui il codice