Welcome back! In this lesson, we're going to dive into SQL aggregate functions on multiple tables. This lesson will combine everything you have learned so far about SQL queries.
Let's get started putting everything you've learned together.
For convenience, the tables and columns in our Marvel movies dataset are:
Movies Table
Movie Details Table
Characters Table
Let's begin with our first example, where we will aggregate total box office earnings by phase. We will use an INNER JOIN on the movies and movie_details tables. We will then use the SUM function and the GROUP BY clause to find the total box office earnings per phase. This advanced query is:
There's a lot going on in this query. Let's break it down step by step.
- We first use
SELECTto obtain themovies.phasecolumn and the sum of box office sales. We use the aliasTotal Box Officefor the result of theSUM. FROM moviesselects the primary tableINNER JOIN movie_detailsspecifies the table to join withmoviesON movies.movie_id = movie_details.movie_idmatches the rows of themoviesandmovie_detailscolumn based on themovie_idGROUP BY movies.phasespecifies the rows of the output table each correspond to a phase
The output is:
The output shows the sum of box office sales for each phase.
In this example, we want to find the average IMDb score for the movies in each phase that feature the character Thor. The steps are:
- Use
INNER JOINto join all 3 tables based on themovie_idcolumn. - Find the average IMDb rating
- Filter the movies by only using movies that feature Thor.
- Group the rows by
movies.phase - Sort the average IMDb ratings in descending order.
Let's take a look at the most advanced query yet:
Let's look at each line individually:
SELECT movies.phase, AVG(movie_details.imdb_rating) AS "Average Rating"
- This line specifies that our result should have a
phasecolumn from themoviestable and a column calledAverage Ratingwith the average IMDb rating
FROM movies
- Specifies the
moviestable as the primary table from which data will be fetched.
INNER JOIN movie_details ON movies.movie_id = movie_details.movie_id
- Combines the
moviestable with themovie_detailstable, matching rows based on themovie_idcolumn, which must be present in both tables.
INNER JOIN characters ON movies.movie_id = characters.movie_id
- Further combines the result set with the
characterstable, again matching rows based on themovie_idcolumn.
WHERE characters.character_name LIKE '%Thor%'
- Filters the records to include only those where the
character_namecolumn in thecharacterstable contains the text "Thor". The%wildcard allows for any characters to appear before or after "Thor".
GROUP BY movies.phase
- Groups the resulting records by the
phasecolumn from themoviestable, ensuring that the average IMDb rating is calculated separately for each phase.
ORDER BY "Average Rating" DESC;
- Sorts the final result set in descending order based on the calculated
Average Rating.
The output is:
Let's analze this output:
phase: This column represents the phase number from themoviestable.Average Rating: This column shows the calculated average IMDb rating for movies featuring Thor.
Phase 3
- The average IMDb rating for movies featuring Thor in Phase 3 is approximately 8.23.
Phase 1
- The average IMDb rating for Thor movies in Phase 1 is 7.50.
Phase 4
- The average IMDb rating for Thor movies in Phase 4 is also 7.50.
Phase 2
- The average IMDb rating for Thor movies in Phase 2 is approximately 7.10.
This allows us to see at a glance which phase had the highest and lowest average IMDb ratings for movies featuring Thor.
Congratulations on finishing the last lesson of this course. In this lesson, we covered how to use SQL aggregate functions such as SUM() and AVG() alongside the GROUP BY clause in PostgreSQL. We also demonstrated how to join multiple tables and filter data using the WHERE clause with conditions like LIKE.
Now it's your turn to practice these concepts. The upcoming exercises will help you solidify your understanding and build confidence in writing SQL queries using aggregate functions.
Great work, and happy querying!
