Skip to content
PostgreSQL Summary Stats and Window Functions
PostgreSQL Summary Stats and Window Functions
Here you can access the summer_medals
table used in the course. To access the table, you will need to specify the medals
schema in your queries (e.g., medals.summer_medals
).
Note: When using sample integrations such as those that contain course data, you have read-only access. You can run queries, but cannot make any changes such as adding, deleting, or modifying the data (e.g., creating tables, views, etc.).
Take Notes
Add notes about the concepts you've learned and SQL cells with queries you want to keep.
Add your notes here
DataFrameas
movie_info
variable
-- Add your own queries here
SELECT *
FROM medals.summer_medals
LIMIT 5
Explore Datasets
Use the summer_medals
table to explore the data and practice your skills!
- Select the
athlete
,event
, andyear
from thesummer_medals
table.- Add another column,
previous_winner
, which contains the previous winner of the same event. - Filter your results for gold medalists.
- Add another column,
- Return the
year
, total number of medalists per year, and running total number of medalists in the history of the Summer Olympics.- Order your results by year in ascending order.
- Return the
country
,year
, and the number of gold medals earned.- Limit your results to the years 2004, 2008, and 2012.
- Each country should have a subtotal of all gold medals earned across the three years.
DataFrameas
df
variable
WITH Tennis_Gold AS (
SELECT DISTINCT
Gender, Year, Country
FROM Summer_Medals
WHERE
Year >= 2000 AND
Event = 'Javelin Throw' AND
Medal = 'Gold')
SELECT
Gender, Year,
Country AS Champion,
-- Fetch the previous year's champion by gender
LAG(country) OVER (PARTITION BY gender
ORDER BY gender ASC) AS Last_Champion
FROM Tennis_Gold
ORDER BY Gender ASC, Year ASC;
DataFrameas
df1
variable
WITH Athlete_Medals AS (
SELECT Athlete, COUNT(*) AS Medals
FROM Summer_Medals
GROUP BY Athlete
HAVING COUNT(*) > 1),
Thirds AS (
SELECT
Athlete,
Medals,
NTILE(3) OVER (ORDER BY Medals DESC) AS Third
FROM Athlete_Medals)
SELECT
-- Get the average medals earned in each third
Third,
AVG(Medals) AS Avg_Medals
FROM Thirds
GROUP BY Third
ORDER BY Third ASC;
DataFrameas
df2
variable
WITH Country_Medals AS (
SELECT
Year, Country, COUNT(*) AS Medals
FROM Summer_Medals
GROUP BY Year, Country)
SELECT
Year, Country, Medals,
-- Calculate each country's 3-game moving total
SUM(Medals) OVER
(PARTITION BY country
ORDER BY Year ASC
ROWS BETWEEN
2 PRECEDING AND CURRENT ROW) AS Medals_MA
FROM Country_Medals
ORDER BY Country ASC, Year ASC;