Filtering grouped data

Intermediate SQL

Jasmin Ludolf

Data Science Content Developer, DataCamp

HAVING

SELECT 
       release_year,
       COUNT(title) AS title_count
FROM films
GROUP BY release_year
WHERE COUNT(title) > 10;
syntax error at or near "WHERE"
LINE 4: WHERE COUNT(title) > 10;
        ^
SELECT 
       release_year,
       COUNT(title) AS title_count
FROM films
GROUP BY release_year
HAVING COUNT(title) > 10;
|release_year|title_count|
|------------|-----------|
|1988        |31         |
|null        |42         |
|2008        |225        |
...
Intermediate SQL

Order of execution

-- Written code:

SELECT certification, COUNT(title) AS title_count FROM films WHERE certification IN ('G', 'PG', 'PG-13') GROUP BY certification HAVING COUNT(title) > 500 ORDER BY title_count DESC LIMIT 3;
-- Order of execution:

SELECT certification, COUNT(title) AS title_count
FROM films
WHERE certification IN ('G', 'PG', 'PG-13')
GROUP BY certification
HAVING COUNT(title) > 500
ORDER BY title_count DESC
LIMIT 3;
Intermediate SQL

HAVING vs WHERE

  • WHERE filters individual records, HAVING filters grouped records
  • What films were released in the year 2000?
SELECT title
FROM films
WHERE release_year = 2000;
|title         |
|--------------|
|102 Dalmatians|
|28 Days       |
...
  • In what years was the average film duration over two hours?
Intermediate SQL

HAVING vs WHERE

  • In what years was the average film duration over two hours?
SELECT release_year
FROM films

GROUP BY release_year
HAVING AVG(duration) > 120;
|release_year|
|------------|
|1954        |
|1959        |
...
Intermediate SQL

Let's practice!

Intermediate SQL

Preparing Video For Download...