SQL style

Intermediate SQL

Jasmin Ludolf

Data Science Content Developer, DataCamp

SQL formatting

  • Formatting is not required
  • But lack of formatting can cause issues
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

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

 

Holywell's style guide: https://www.sqlstyle.guide/

Sequence of scribbles becoming a straight line

Write clear and readable code

Intermediate SQL

Semicolon

SELECT title, release_year, country
FROM films 
LIMIT 3;

 

  • Best practice
  • Easier to translate between SQL flavors
  • Indicates the end of a query
Intermediate SQL

Dealing with non-standard field names

  • release year instead of release_year
  • Put non-standard field names in double-quotes
SELECT title, "release year", country
FROM films 
LIMIT 3;
Intermediate SQL

Why do we format?

  • Easier collaboration
  • Clean and readable
  • Looks professional
  • Easier to understand
  • Easier to debug
Intermediate SQL

Let's practice!

Intermediate SQL

Preparing Video For Download...