Chapter 1
Chapter 1
database
I N T E R M E D I AT E S Q L
Jasmin Ludolf
Data Science Content Developer,
DataCamp
Course roadmap
Querying databases
Filtering
Aggregate functions
INTERMEDIATE SQL
Our films database
INTERMEDIATE SQL
COUNT()
COUNT()
|count_birthdates|
|----------------|
|6152 |
INTERMEDIATE SQL
COUNT() multiple fields
SELECT COUNT(name) AS count_names, COUNT(birthdate) AS count_birthdates
FROM people;
|count_names|count_birthdates|
|-----------|----------------|
|6397 |6152 |
INTERMEDIATE SQL
Using * with COUNT()
COUNT(field_name) counts values in a field
|total_records|
|-------------|
|8397 |
INTERMEDIATE SQL
DISTINCT
DISTINCT removes duplicates to return Which languages are in our films table?
only unique values
SELECT language
SELECT DISTINCT language
FROM films;
FROM films;
|language |
|language |
|---------|
|---------|
|Danish |
|Danish |
|Danish |
|Greek |
|Greek |
|Greek |
|Greek |
INTERMEDIATE SQL
COUNT() with DISTINCT
Combine COUNT() with DISTINCT to count unique values
|count_distinct_birthdates|
|-------------------------|
|5398 |
INTERMEDIATE SQL
Let's practice!
I N T E R M E D I AT E S Q L
Query execution
I N T E R M E D I AT E S Q L
Jasmin Ludolf
Data Science Content Developer,
DataCamp
Order of execution
SQL is not processed in its written order
-- Order of execution
SELECT name
FROM people
LIMIT 10;
INTERMEDIATE SQL
Debugging SQL
SELECT nme
FROM people;
Misspelling
Incorrect capitalization
INTERMEDIATE SQL
Comma errors
Look out for comma errors!
INTERMEDIATE SQL
Keyword errors
SELCT title, country, duration
FROM films;
INTERMEDIATE SQL
Final note on errors
Most common errors:
Misspelling
Incorrect capitalization
Incorrect or missing punctuation, especially
commas
INTERMEDIATE SQL
Let's practice!
I N T E R M E D I AT E S Q L
SQL style
I N T E R M E D I AT E S Q L
Jasmin Ludolf
Data Science Content Developer,
DataCamp
SQL formatting
Formatting is not required
But lack of formatting can cause issues
|title |release_year|country|
|------------------------------------------------|------------|-------|
|Intolerance: Love's Struggle Throughout the Ages|1916 |USA |
|Over the Hill to the Poorhouse |1920 |USA |
|The Big Parade |1925 |USA |
INTERMEDIATE SQL
Best practices
SELECT title, release_year, country
FROM films
LIMIT 3;
|title |release_year|country|
|------------------------------------------------|------------|-------|
|Intolerance: Love's Struggle Throughout the Ages|1916 |USA |
|Over the Hill to the Poorhouse |1920 |USA |
|The Big Parade |1925 |USA |
Capitalize keywords
Add new lines
INTERMEDIATE SQL
Style guides
SELECT
title,
release_year,
country
FROM films
LIMIT 3;
|title |release_year|country|
|------------------------------------------------|------------|-------|
|Intolerance: Love's Struggle Throughout the Ages|1916 |USA |
|Over the Hill to the Poorhouse |1920 |USA |
|The Big Parade |1925 |USA |
INTERMEDIATE SQL
Style guides
INTERMEDIATE SQL
Semicolon
SELECT title, release_year, country
FROM films
LIMIT 3;
Best practice
INTERMEDIATE SQL
Dealing with non-standard field names
release year instead of release_year
INTERMEDIATE SQL
Why do we format?
Easier collaboration
Clean and readable
Looks professional
Easier to understand
Easier to debug
INTERMEDIATE SQL
Let's practice!
I N T E R M E D I AT E S Q L