0% found this document useful (0 votes)
8 views23 pages

Chapter 1

Uploaded by

Rahul Bhole
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)
8 views23 pages

Chapter 1

Uploaded by

Rahul Bhole
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
You are on page 1/ 23

Essential SQL

A P P LY I N G S Q L T O R E A L-W O R L D P R O B L E M S

Dmitriy (Dima) Gorenshteyn


Lead Data Scientist, Memorial Sloan
Kettering Cancer Center
What you will learn!
Chapter 1: Use Real World SQL
Chapter 2: Find Your Data

Chapter 3: Manage Your Data

Chapter 4: Best Practices for Writing SQL

APPLYING SQL TO REAL-WORLD PROBLEMS


The database
DVD Rental Store Tables
Based off the pagila database 1 actor film_actor
address inventory
category language
customer payment
film rental
Copyright (c) Devrim Gündüz <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of
the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

1 https://github.com/devrimgunduz/pagila

APPLYING SQL TO REAL-WORLD PROBLEMS


Essential SQL commands
SELECT title, length
FROM film AS f
INNER JOIN category AS c
ON f.film_id = c.film_id
WHERE c.category = 'Documentary'
AND f.rating IN ('G', 'PG')
ORDER BY length DESC;

APPLYING SQL TO REAL-WORLD PROBLEMS


SELECT, FROM
SELECT title, length SELECT
FROM film
SELECT <column1>, <column2>
-- OR
SELECT *

; FROM

FROM <table_name>

APPLYING SQL TO REAL-WORLD PROBLEMS


INNER JOIN
SELECT title, length AS
FROM film AS f
INNER JOIN category AS c <column_name> AS <alias1>
ON f.film_id = c.film_id
INNER JOIN

; INNER JOIN <table_name> AS <alias2>

ON

ON <alias1>.<column> = <alias2>.<column>

APPLYING SQL TO REAL-WORLD PROBLEMS


WHERE, AND, IN
SELECT title, length WHERE
FROM film AS f
INNER JOIN category AS c WHERE <condition>
ON f.film_id = c.film_id
WHERE c.category = 'Documentary' AND
AND f.rating IN ('G', 'PG')
; AND <condition>

IN

IN ('<item1>', '<item2>', ...)

APPLYING SQL TO REAL-WORLD PROBLEMS


ORDER BY, DESC
SELECT title, length ORDER BY
FROM film AS f
INNER JOIN category AS c ORDER BY <column_name>
ON f.film_id = c.film_id
WHERE c.category = 'Documentary' DESC
AND f.rating IN ('G', 'PG')
ORDER BY length DESC; ORDER BY <column_name> DESC

APPLYING SQL TO REAL-WORLD PROBLEMS


Practice the
essentials!
A P P LY I N G S Q L T O R E A L-W O R L D P R O B L E M S
Transforming your
results
A P P LY I N G S Q L T O R E A L-W O R L D P R O B L E M S

Dmitriy (Dima) Gorenshteyn


Lead Data Scientist, Memorial Sloan
Kettering Cancer Center
Transforming strings
UPPER(<column>) & LOWER(<column>)

SELECT city,
UPPER(city) AS upper_city,
LOWER(city) AS lower_city
FROM address;

city lower_city upper_city


Lethbridge lethbridge LETHBRIDGE
Woodridge woodridge WOODRIDGE
Lethbridge lethbridge LETHBRIDGE
Woodridge woodridge WOODRIDGE

APPLYING SQL TO REAL-WORLD PROBLEMS


Transforming numbers
Operators: add ( + ), subtract ( - ), divide ( / ), multiply( * )

SELECT replacement_cost,
replacement_cost + 2 AS updated_cost,
replacement_cost / length AS cost_per_minute
FROM film;

replacement_cost updated_cost cost_per_minute


20.99 22.99 0.24406977
12.99 14.99 0.27062500
18.99 20.99 0.37980000

APPLYING SQL TO REAL-WORLD PROBLEMS


Transforming dates
EXTRACT(<part> FROM <date_column>)

SELECT rental_date,
EXTRACT(YEAR FROM rental_date) AS rental_year,
EXTRACT(HOUR FROM rental_date) AS rental_hour
FROM rental;

rental_date rental_year rental_hour


2005-05-30 23:54:19 2005 23
2005-05-30 23:55:36 2005 23
2005-05-31 00:06:02 2005 0

APPLYING SQL TO REAL-WORLD PROBLEMS


Time to transform!
A P P LY I N G S Q L T O R E A L-W O R L D P R O B L E M S
Working with
aggregate functions
A P P LY I N G S Q L T O R E A L-W O R L D P R O B L E M S

Dmitriy (Dima) Gorenshteyn


Lead Data Scientist, Memorial Sloan
Kettering Cancer Center
GROUP BY
SELECT rating,
AVG(replacement_cost)
FROM film
GROUP BY rating

APPLYING SQL TO REAL-WORLD PROBLEMS


GROUP BY
SELECT rating,
AVG(replacement_cost)
FROM film
GROUP BY rating

APPLYING SQL TO REAL-WORLD PROBLEMS


GROUP BY
SELECT rating,
AVG(replacement_cost)
FROM film
GROUP BY rating

APPLYING SQL TO REAL-WORLD PROBLEMS


GROUP BY
SELECT rating,
AVG(replacement_cost)
FROM film
GROUP BY rating

APPLYING SQL TO REAL-WORLD PROBLEMS


GROUP BY
SELECT rating,
AVG(replacement_cost)
FROM film
GROUP BY rating

APPLYING SQL TO REAL-WORLD PROBLEMS


Numeric aggregate functions
SELECT rating,
AVG(replacement_cost) AS avg_cost,
COUNT(rating) AS number_elements,
SUM(replacement_cost) AS total_cost
FROM film
GROUP BY rating;

rating avg_cost number_elements total_cost


PG-13 20.40256 223 4549.77
R 20.23103 195 3945.05
G 20.12483 178 3582.22
PG 18.95907 194 3678.06

APPLYING SQL TO REAL-WORLD PROBLEMS


String aggregate functions
STRING_AGG(<column>, '<separator>')

SELECT rating,
STRING_AGG(title, ',') as films
FROM film
GROUP BY rating;

rating films
PG-13 AIRPLANE SIERRA,ALABAMA DEVIL,...
R AIRPORT POLLOCK,DATE SPEED,...
G ACE GOLDFINGER,AFFAIR PREJUDICE,...

APPLYING SQL TO REAL-WORLD PROBLEMS


Time to aggregate!
A P P LY I N G S Q L T O R E A L-W O R L D P R O B L E M S

You might also like