AVG, MIN, MAX Aggregate Function Use

Here is the link of the project I am working on:

https://www.codecademy.com/paths/analyze-data-with-sql/tracks/analyze-data-sql-calculate-and-summarize-data/modules/analyze-data-sql-practice-aggregate-functions/projects/sql-the-met

What would be returned from these queries:

The MIN() aggregate function returns the smallest value in a column.

SELECT title, medium, MIN(date)
FROM met;

Agree!

The MAX() aggregate function takes the name of a column as an argument and returns the largest value in a column.

SELECT title, medium, MAX(date)
FROM met;

Agree!

The AVG() aggregate function returns the average value in a column.

SELECT title, medium, AVG(date)
FROM met;

Question: How does that compute a result for title and medium when the AVG returned doesn’t have value that matches it?

Hi, Welcome to the forums.

Dates are tricky sometimes!
You cannot use AVG on that date column in the same way you’d use MAX & MIN in this exercise (same way you couldn’t use SUM here).
If you check out the table’s schema at the bottom of the lesson, you’ll see that it’s data type is TEXT.

You’d have to convert it using CAST or possibly DATEDIFF & DATEADD (I’m not sure which one, specifically. You’d have to test them out in that lesson)
Ex: CAST(expression AS datatype(length))

See here:
https://stackoverflow.com/questions/13031850/how-to-find-the-average-value-in-a-column-of-dates-in-sql-server

and here for reference:
https://sqlsunday.com/2019/10/10/aggregating-date-and-time-values/

1 Like

I understand it better now, Thank you for the answer and for this tip. I think it will be useful in the future and I hope it will be helpful for others.

You’re welcome.
Happy coding!

1 Like