0% found this document useful (0 votes)
5 views29 pages

Chapter 4

The document provides an overview of intermediate SQL concepts, focusing on sorting results, grouping data, and filtering grouped data using SQL commands such as ORDER BY, GROUP BY, and HAVING. It includes examples demonstrating the use of these commands to manipulate and analyze data from films and other datasets. Additionally, it highlights the importance of error handling and writing readable code in SQL queries.

Uploaded by

Lim Quan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views29 pages

Chapter 4

The document provides an overview of intermediate SQL concepts, focusing on sorting results, grouping data, and filtering grouped data using SQL commands such as ORDER BY, GROUP BY, and HAVING. It includes examples demonstrating the use of these commands to manipulate and analyze data from films and other datasets. Additionally, it highlights the importance of error handling and writing readable code in SQL queries.

Uploaded by

Lim Quan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Sorting results

I N T E R M E D I AT E S Q L

Jasmin Ludolf
Data Science Content Developer,
DataCamp
Sorting results

INTERMEDIATE SQL
ORDER BY
SELECT title, budget SELECT title, budget
FROM films FROM films
ORDER BY budget; ORDER BY title;

|title |budget| |title |budget |


|--------------------|------| |--------------------------|--------|
|Tarnation |218 | |#Horror |1500000 |
|My Date with Drew |1100 | |10 Cloverfield Lane |15000000|
|A Plague So Pleasant|1400 | |10 Days in a Madhouse |12000000|
|The Mongol King |3250 | |10 Things I Hate About You|16000000|
... ...

INTERMEDIATE SQL
ASCending
SELECT title, budget
FROM films
ORDER BY budget ASC;

|title |budget|
|--------------------|------|
|Tarnation |218 |
|My Date with Drew |1100 |
|A Plague So Pleasant|1400 |
|The Mongol King |3250 |
...

INTERMEDIATE SQL
DESCending
SELECT title, budget SELECT title, budget
FROM films FROM films
ORDER BY budget DESC; WHERE budget IS NOT NULL
ORDER BY budget DESC;
|title |budget|
|------------------------------|------| |title |budget |
|Love and Death on Long Island |null | |-----------------------|-----------|
|The Chambermaid on the Titanic|null | |The Host |12215500000|
|51 Birch Street |null | |Lady Vengeance |4200000000 |
... ...

INTERMEDIATE SQL
Sorting fields
SELECT title SELECT title, release_year
FROM films FROM films
ORDER BY release_year; ORDER BY release_year;

|title | |title |release_year|


|-------------------------------------| |------------------------|------------|
|Intolerance: Love's Struggle Throu...| |Intolerance: Love's S...|1916 |
|Over the Hill to the Poorhouse | |Over the Hill to the ...|1920 |
|The Big Parade | |The Big Parade |1925 |
|Metropolis | |Metropolis |1927 |
... ...

INTERMEDIATE SQL
ORDER BY multiple fields
ORDER BY field_one, field_two Think of field_two as a tie-breaker

SELECT title, wins SELECT title, wins, imdb_score


FROM best_movies FROM best_movies
ORDER BY wins DESC; ORDER BY wins DESC, imdb_score DESC;

|title |wins| |title |wins|imdb_score|


|--------------------------------|----| |---------------------|----|----------|
|Lord of the Rings:Return of t...|11 | |Lord of the Rings:...|11 |9 |
|Titanic |11 | |Ben-Hur |11 |8.1 |
|Ben-Hur |11 | |Titanic |11 |7.9 |

INTERMEDIATE SQL
Different orders
SELECT birthdate, name
FROM people
ORDER BY birthdate, name DESC;

|birthdate |name |
|----------|----------------|
|1990-01-01|Robert Brown |
|1990-02-02|Anne Smith |
|1991-05-14|Amy Miller |
|1991-11-22|Adam Waters |
...

INTERMEDIATE SQL
Order of execution
-- Written code: -- Order of execution:
SELECT item SELECT item
FROM coats FROM coats
WHERE color = `yellow` WHERE color = `yellow`
ORDER BY length ORDER BY length
LIMIT 3; LIMIT 3;

INTERMEDIATE SQL
-- Select country and language name (aliased)
SELECT [Link] AS country, [Link] AS language
-- From countries (aliased)
FROM countries AS c
-- Join to languages (aliased)
INNER JOIN languages AS l
-- Use code as the joining field with the USING keyword
USING(code)
-- Filter for the Bhojpuri language
WHERE [Link] = 'Bhojpuri';

Let's practice!
I N T E R M E D I AT E S Q L
Grouping data
I N T E R M E D I AT E S Q L

Jasmin Ludolf
Data Science Content Developer,
DataCamp
Grouping data

INTERMEDIATE SQL
GROUP BY single fields
SELECT certification, COUNT(title) AS title_count
FROM films
GROUP BY certification;

|certification|title_count|
|-------------|-----------|
|Unrated |62 |
|M |5 |
|G |112 |
|NC-17 |7 |
...

INTERMEDIATE SQL
Error handling
SELECT certification, title SELECT
FROM films certification,
GROUP BY certification; COUNT(title) AS count_title
FROM films
column "[Link]" must appear in the GROUP BY certification;

GROUP BY clause or be used in an


aggregate function |certification|count_title|
LINE 1: SELECT certification, title |-------------|-----------|
^ |Unrated |62 |
|M |5 |
|G |112 |
...

INTERMEDIATE SQL
GROUP BY multiple fields
SELECT certification, language, COUNT(title) AS title_count
FROM films
GROUP BY certification, language;

|certification|language |title_count|
|-------------|---------|-----------|
|null |null |5 |
|Unrated |Japanese |2 |
|R |Norwegian|2 |
...

INTERMEDIATE SQL
GROUP BY with ORDER BY
SELECT SELECT
certification, certification,
COUNT(title) AS title_count COUNT(title) AS title_count
FROM films FROM films
GROUP BY certification; GROUP BY certification
ORDER BY title_count DESC;
|certification|title_count|
|-------------|-----------| |certification|title_count|
|Unrated |62 | |-------------|-----------|
|M |5 | |R |2118 |
|G |112 | |PG-13 |1462 |
... ...

INTERMEDIATE SQL
Order of execution
-- Written code: -- Order of execution:
SELECT SELECT
certification, certification,
COUNT(title) AS title_count COUNT(title) AS title_count
FROM films FROM films
GROUP BY certification GROUP BY certification
ORDER BY title_count DESC ORDER BY title_count DESC
LIMIT 3; LIMIT 3;

INTERMEDIATE SQL
-- Find the release_year and film_count of each year SELECT release_year,
SELECT release_year, count(*) as film_count COUNT(DISTINCT(language))
FROM films FROM FILMS
GROUP BY release_year; GROUP BY release_year
ORDER BY COUNT;

Let's practice!
I N T E R M E D I AT E S Q L
Filtering grouped
data
I N T E R M E D I AT E S Q L

Jasmin Ludolf
Data Science Content Developer,
DataCamp
HAVING
SELECT SELECT
release_year, release_year,
COUNT(title) AS title_count COUNT(title) AS title_count
FROM films FROM films
GROUP BY release_year GROUP BY release_year
WHERE COUNT(title) > 10; HAVING COUNT(title) > 10;

syntax error at or near "WHERE" |release_year|title_count|


LINE 4: WHERE COUNT(title) > 10; |------------|-----------|
^ |1988 |31 |
|null |42 |
|2008 |225 |
...

INTERMEDIATE SQL
Order of execution
-- Written code: -- Order of execution:
SELECT SELECT
certification, certification,
COUNT(title) AS title_count COUNT(title) AS title_count
FROM films FROM films
WHERE certification WHERE certification
IN ('G', 'PG', 'PG-13') IN ('G', 'PG', 'PG-13')
GROUP BY certification GROUP BY certification
HAVING COUNT(title) > 500 HAVING COUNT(title) > 500
ORDER BY title_count DESC ORDER BY title_count DESC
LIMIT 3; 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
-- Select the country and distinct count of certification as certification_count
SELECT country, COUNT(DISTINCT certification) AS certification_count
FROM films
-- Group by country
GROUP BY country
-- Filter results to countries with more than 10 different certifications
HAVING COUNT(DISTINCT(certification)) > 10

Let's practice!
I N T E R M E D I AT E S Q L

SELECT release_year, AVG(budget) AS avg_budget,


AVG(gross) AS avg_gross
-- Select the country and average_budget from films FROM films
SELECT country, AVG(budget) AS average_budget WHERE release_year > 1990
FROM films GROUP BY release_year
-- Group by country HAVING AVG(budget) > 60000000
GROUP BY country -- Order the results from highest to lowest average
-- Filter to countries with an average_budget of more than one billion gross and limit to one
HAVING AVG(budget) > 1000000000 ORDER BY AVG(gross) DESC
-- Order by descending order of the aggregated budget LIMIT 1
ORDER BY average_budget DESC
Congratulations!
I N T E R M E D I AT E S Q L

Jasmin Ludolf
Data Science Content Developer,
DataCamp
What you've learned
Chapter 1: Selecting with COUNT() , LIMIT
Chapter 2: Filtering with WHERE , BETWEEN , AND , OR , LIKE , NOT LIKE , IN , % , _ ,
IS NULL , IS NOT NULL

Chapter 3: ROUND() and aggregate functions

Chapter 4: Sorting and grouping with ORDER BY , DESC , GROUP BY , HAVING

Comparison operators

Arithmetic

INTERMEDIATE SQL
Skills
Error handling
Debugging

Writing readable code

Selecting data

Querying data

Filtering and summarizing data

Sorting and grouping data

INTERMEDIATE SQL
What's next?
DataCamp SQL joining course
DataCamp Project, Practice, Competitions

Workspace

INTERMEDIATE SQL
Congratulations!
I N T E R M E D I AT E S Q L

You might also like