SQL BOLT
Recently, while browsing the internet to learn new things, I discovered an amazing website that teaches how to use SQL queries. For the first time in my life, I was convinced that SQL queries could be fun. I had always been scared of them because my instructor made them seem like a nightmare.
SQL Lesson 1: SELECT queries 101
-- to select specific columns
SELECT column, another_column, ...
FROM mytable
-- to get every thing we can use *
SELECT *
FROM mytableExercise 1 — Tasks
- Find the title of each film
- Find the director of each film
- Find the title and director of each film
- Find the title and year of each film
- Find all the information about each film
Table: movies
| id | title | director | year | length_minutes |
|---|---|---|---|---|
| 1 | Toy Story | John Lasseter | 1995 | 81 |
| 2 | A Bug's Life | John Lasseter | 1998 | 95 |
| 3 | Toy Story 2 | John Lasseter | 1999 | 93 |
| 4 | Monsters, Inc. | Pete Docter | 2001 | 92 |
| 5 | Finding Nemo | Andrew Stanton | 2003 | 107 |
| 6 | The Incredibles | Brad Bird | 2004 | 116 |
| 7 | Cars | John Lasseter | 2006 | 117 |
| 8 | Ratatouille | Brad Bird | 2007 | 115 |
| 9 | WALL-E | Andrew Stanton | 2008 | 104 |
| 10 | Up | Pete Docter | 2009 | 101 |
| 11 | Toy Story 3 | Lee Unkrich | 2010 | 103 |
| 12 | Cars 2 | John Lasseter | 2011 | 120 |
| 13 | Brave | Brenda Chapman | 2012 | 102 |
| 14 | Monsters University | Dan Scanlon | 2013 | 110 |
-- Find the title of each film
SELECT title FROM movies
-- Find the director of each film
SELECT Director FROM movies
-- Find the title and director of each film
SELECT Director, Title FROM movies
-- Find the title and year of each film
SELECT year, Title FROM movies
-- Find all the information about each film
SELECT * FROM movies
SQL Lesson 2: Queries with constraints (Pt. 1)
SELECT column, another_column, …
FROM mytable
WHERE condition
AND/OR another_condition
AND/OR …;| Operator | Condition | SQL Example |
|---|---|---|
| =, !=, < , < = >, >= | Standard numerical operators | col_name != 4 |
| BETWEEN … AND … | Number is within range of two values (inclusive) | col_name BETWEEN 1.5 AND 10.5 |
| NOT BETWEEN … AND … | Number is not within range of two values (inclusive) | col_name NOT BETWEEN 1 AND 10 |
| IN (…) | Number exists in a list | col_name IN (2, 4, 6) |
| NOT IN (…) | Number does not exist in a list | col_name NOT IN (1, 3, 5) |
Exercise 2 — Tasks
- Find the movie with a row id of 6
- Find the movies released in the years between 2000 and 2010
- Find the movies not released in the years between 2000 and 2010
- Find the first 5 Pixar movies and their release year
-- Find the movie with a row id of 6
SELECT id, title FROM movies
WHERE id == 6
-- Find the movies released in the years between 2000 and 2010
SELECT * FROM movies
WHERE year BETWEEN 2000 AND 2010
-- Find the movies not released in the years between 2000 and 2010
SELECT * FROM movies
WHERE year NOT BETWEEN 2000 AND 2010
-- Find the first 5 Pixar movies and their release year
SELECT * FROM movies
WHERE id BETWEEN 0 AND 5SQL Lesson 3: Queries with constraints (Pt. 2)
| Operator | Condition | Example |
|---|---|---|
| = | Case sensitive exact string comparison (notice the single equals) | col_name = "abc" |
| != or < > | Case sensitive exact string inequality comparison | col_name != "abcd" |
| LIKE | Case insensitive exact string comparison | col_name LIKE "ABC" |
| NOT LIKE | Case insensitive exact string inequality comparison | col_name NOT LIKE "ABCD" |
| % | Used anywhere in a string to match a sequence of zero or more characters | col_name LIKE "%AT%" (matches "AT", "ATTIC", "CAT" or even "BATS") |
| _ | Used anywhere in a string to match a single character | col_name LIKE "AN_" (matches "AND", but not "AN") |
| IN (…) | String exists in a list | col_name IN ("A", "B", "C") |
| NOT IN (…) | String does not exist in a list | col_name NOT IN ("D", "E", "F") |
We should note that while most database implementations are quite efficient when using these operators, full-text search is best left to dedicated libraries like Apache Lucene (opens in a new tab) or Sphinx (opens in a new tab). These libraries are designed specifically to do full text search, and as a result are more efficient and can support a wider variety of search features including internationalization and advanced queries.
Exercise 3 — Tasks
- Find all the Toy Story movies
- Find all the movies directed by John Lasseter
- Find all the movies (and director) not directed by John Lasseter
- Find all the WALL-* movies
-- Find all the Toy Story movies
SELECT title FROM movies
WHERE title
LIKE "%Toy%"
-- Find all the movies directed by John Lasseter
SELECT * FROM movies
WHERE Director = "John Lasseter"
-- Find all the movies (and director) not directed by John Lasseter
SELECT * FROM movies
WHERE Director != "John Lasseter"
-- Find all the WALL-* movies
SELECT * FROM movies
WHERE Title LIKE "WALL%" SQL Lesson 4: Filtering and sorting Query results
- DISTINCT
SELECT DISTINCT column, another_column, …
FROM mytable
WHERE condition(s);- ORDER BY column ASC/DESC;
SELECT column, another_column, …
FROM mytable
WHERE condition(s)
ORDER BY column ASC/DESC;- LIMIT num_limit OFFSET num_offset;
SELECT column, another_column, …
FROM mytable
WHERE condition(s)
ORDER BY column ASC/DESC
LIMIT num_limit OFFSET num_offset;Exercise 4 — Tasks
- List all directors of Pixar movies (alphabetically), without duplicates ✓
- List the last four Pixar movies released (ordered from most recent to least)
- List the first five Pixar movies sorted alphabetically
- List the next five Pixar movies sorted alphabetically
-- List all directors of Pixar movies (alphabetically), without duplicates ✓
SELECT DISTINCT Director FROM movies
ORDER BY Director ASC
-- List the last four Pixar movies released (ordered from most recent to least)
SELECT * FROM movies
ORDER BY year DESC
LIMIT 4
-- List the first five Pixar movies sorted alphabetically
SELECT * FROM movies
ORDER BY title ASC
LIMIT 5
-- List the next five Pixar movies sorted alphabetically
SELECT * FROM movies
ORDER BY title ASC
LIMIT 5 OFFSET 5SQL Review: Simple SELECT Queries
Table: North_american_cities
| City | Country | Population | Latitude | Longitude |
|---|---|---|---|---|
| Guadalajara | Mexico | 1,500,800 | 20.659699 | -103.349609 |
| Toronto | Canada | 2,795,060 | 43.653226 | -79.383184 |
| Houston | United States | 2,195,914 | 29.760427 | -95.369803 |
| New York | United States | 8,405,837 | 40.712784 | -74.005941 |
| Philadelphia | United States | 1,553,165 | 39.952584 | -75.165222 |
| Havana | Cuba | 2,106,146 | 23.054070 | -82.345189 |
| Mexico City | Mexico | 8,555,500 | 19.432608 | -99.133208 |
| Phoenix | United States | 1,513,367 | 33.448377 | -112.074037 |
| Los Angeles | United States | 3,884,307 | 34.052234 | -118.243685 |
| Ecatepec de Morelos | Mexico | 1,742,000 | 19.601841 | -99.050674 |
| Montreal | Canada | 1,717,767 | 45.501689 | -73.567256 |
| Chicago | United States | 2,718,782 | 41.878114 | -87.629798 |
Review 1 — Tasks
- List all the Canadian cities and their populations ✓
- Order all the cities in the United States by their latitude from north to south
- List all the cities west of Chicago, ordered from west to east
- List the two largest cities in Mexico (by population)
- List the third and fourth largest cities (by population) in the United States and their population
-- List all the Canadian cities and their populations ✓
SELECT * FROM north_american_cities
WHERE Country LIKE "Canada"
-- Order all the cities in the United States by their latitude from north to south
SELECT * FROM north_american_cities
WHERE Country LIKE "United States"
ORDER BY Latitude DESC
-- I Directly copy pasted this one becase i dont want to put energy on learning Longitude and Latidude
-- List all the cities west of Chicago, ordered from west to east
SELECT city, longitude FROM north_american_cities
WHERE longitude < -87.629798
ORDER BY longitude ASC;
-- List the two largest cities in Mexico (by population)
SELECT * FROM North_american_cities
WHERE Country LIKE "Mexico"
ORDER BY Population DESC
LIMIT 2
-- List the third and fourth largest cities (by population) in the United States and their population
SELECT * FROM North_american_cities
WHERE Country LIKE "United States"
ORDER BY Population DESC
LIMIT 2 OFFSET 2SQL Lesson 6: Multi-table queries with JOINs
There are Two Table to work with
Table: Movies (Read-Only)
| id | title | director | year | length_minutes |
|---|---|---|---|---|
| 1 | Toy Story | John Lasseter | 1995 | 81 |
| 2 | A Bug's Life | John Lasseter | 1998 | 95 |
| 3 | Toy Story 2 | John Lasseter | 1999 | 93 |
| 4 | Monsters, Inc. | Pete Docter | 2001 | 92 |
| 5 | Finding Nemo | Andrew Stanton | 2003 | 107 |
| 6 | The Incredibles | Brad Bird | 2004 | 116 |
| 7 | Cars | John Lasseter | 2006 | 117 |
| 8 | Ratatouille | Brad Bird | 2007 | 115 |
| 9 | WALL-E | Andrew Stanton | 2008 | 104 |
| 10 | Up | Pete Docter | 2009 | 101 |
| 11 | Toy Story 3 | Lee Unkrich | 2010 | 103 |
| 12 | Cars 2 | John Lasseter | 2011 | 120 |
| 13 | Brave | Brenda Chapman | 2012 | 102 |
| 14 | Monsters University | Dan Scanlon | 2013 | 110 |
Table: Boxoffice (Read-Only)
| movie_id | rating | domestic_sales | international_sales |
|---|---|---|---|
| 5 | 8.2 | 380,843,261 | 555,900,000 |
| 14 | 7.4 | 268,492,764 | 475,066,843 |
| 8 | 8.0 | 206,445,654 | 417,277,164 |
| 12 | 6.4 | 191,452,396 | 368,400,000 |
| 3 | 7.9 | 245,852,179 | 239,163,000 |
| 6 | 8.0 | 261,441,092 | 370,001,000 |
| 9 | 8.5 | 223,808,164 | 297,503,696 |
| 11 | 8.4 | 415,004,880 | 648,167,031 |
| 1 | 8.3 | 191,796,233 | 170,162,503 |
| 7 | 7.2 | 244,082,982 | 217,900,167 |
| 10 | 8.3 | 293,004,164 | 438,338,580 |
| 4 | 8.1 | 289,916,256 | 272,900,000 |
| 2 | 7.2 | 162,798,565 | 200,600,000 |
| 13 | 7.2 | 237,283,207 | 301,700,000 |
SELECT column, another_table_column, …
FROM mytable
INNER JOIN another_table
ON mytable.id = another_table.id
WHERE condition(s)
ORDER BY column, … ASC/DESC
LIMIT num_limit OFFSET num_offset;Exercise 6 — Tasks
- Find the domestic and international sales for each movie
- Show the sales numbers for each movie that did better internationally rather than domestically
- List all the movies by their ratings in descending order
-- Find the domestic and international sales for each movie
SELECT * FROM movies
INNER JOIN boxoffice
ON movies.id = boxoffice.movie_id
-- Show the sales numbers for each movie that did better internationally rather than domestically
SELECT * FROM movies
INNER JOIN boxoffice
ON movies.id = boxoffice.movie_id
WHERE international_sales >= Domestic_sales
-- List all the movies by their ratings in descending order
SELECT * FROM movies
INNER JOIN boxoffice
ON movies.id = boxoffice.movie_id
ORDER BY rating DESC