01 SQL Fundamentals
01 SQL Fundamentals
Fundamentals
SQL
x 23 a 1 Q 13 c 0 12
y 18 b 2 R 34 b 0 24
z 46 c 3 S 56 b 1 45
SQL
x 23 a 1 Q 13 c 0 12
y 18 b 2 R 34 b 0 24
z 46 c 3 S 56 b 1 45
SQL
x 23 a 1 Q 13 c 0 12
y 18 b 2 R 34 b 0 24
z 46 c 3 S 56 b 1 45
SQL
x 23 a 1 Q 13 c 0 12
y 18 b 2 R 34 b 0 24
z 46 c 3 S 56 b 1 45
SQL
x 23 a 1 Q 13 c 0 12
y 18 b 2 R 34 b 0 24
z 46 c 3 S 56 b 1 45
SQL
x 23 a 1 Q 13 c 0 12
y 18 b 2 R 34 b 0 24
z 46 c 3 S 56 b 1 45
SQL
x 23 a 1 Q 13 c 0 12
y 18 b 2 R 34 b 0 24
z 46 c 3 S 56 b 1 45
SQL
● Challenge Structures
○ Business Situation
○ Challenge Question
○ Expected Answer
○ Hints
○ Solution
SQL
● Challenge Structures
○ Business Situation
○ Challenge Question
○ Expected Answer
○ Hints
○ Solution
SQL
● Situation
○ We want to send out a promotional email to our
existing customers!
SQL
● Situation
○ We want to send out a promotional email to our
existing customers!
SQL
● Challenge
○ Use a SELECT statement to grab the first and
last names of every customer and their email
address.
SQL
● Hints
○ Use the customer table
○ You can use the table drop-down to view what
columns are available
○ You could also use SELECT * FROM
customer to see all the columns.
SQL
● Solution
○ Let’s check it out in pgadmin!
SELECT DISTINCT
SQL
Zach Green
David Green
Claire Yellow
David Red
SQL
Zach Green
David Green
Claire Yellow
David Red
SQL
Name Choice
Zach Green
David Green
Claire Yellow
David Red
SQL
Name
Zach
David
Claire
SQL
Zach Green
David Green
Claire Yellow
David Red
SQL
Green
Yellow
Red
SQL
● Situation
● SQL Challenge
● Expected Results
● Hints
● Solution
SQL
● Situation
○ An Australian visitor isn’t familiar with MPAA
movie ratings (e.g. PG , PG-13, R, etc…)
○ We want to know the types of ratings we have in
our database.
○ What ratings do we have available?
SQL
● SQL Challenge
○ Use what you’ve learned about SELECT
DISTINCT to retrieve the distinct rating types our
films could have in our database.
SQL
● Expected Result
SQL
● Hints
○ Use the film table
○ Use SELECT * FROM film; to see what columns
are available.
○ Or use drop down table menu in pgadmin.
SQL
● Solution
○ SELECT DISTINCT rating FROM film;
COUNT
SQL
Name Choice
Zach Green
David Green
Claire Yellow
David Red
SQL
Name Choice
Zach Green
David Green
Claire Yellow
David Red
SQL
Count
4
SQL
4
SQL
Name Choice
Zach Green
David Green
Claire Yellow
David Red
SQL
4
SQL
Zach Green
David Green
Claire Yellow
David Red
SQL
Zach Green
David Green
Claire Yellow
David Red
SQL
Zach Green
David Green
Claire Yellow
David Red
SQL
Zach Green
David Green
Claire Yellow
SQL
3
SQL
3
SQL
● SELECT COUNT(DISTINCT(name))
FROM table;
Count
3
SQL
● Comparison Operators
○ Compare a column value to something.
■ Is the price greater than $3.00?
■ Is the pet’s name equal to “Sam”?
SQL
● Comparison Operators
Operator Description
= Equal
● Logical Operators
○ Allow us to combine multiple comparison
operators
■ AND
■ OR
■ NOT
SQL
Name Choice
Zach Green
David Green
Claire Yellow
David Red
SQL
Name Choice
Zach Green
David Green
Claire Yellow
David Red
SQL
Zach Green
David Green
Claire Yellow
David Red
SQL
David Green
David Red
SQL
David
David
SQL
David Green
David Red
SQL
David Red
SELECT WHERE
PART TWO
Code Along Examples
SELECT WHERE
Challenge Task
SQL
● For example:
○ How many customers have the first name Jared?
● Instead of:
○ Use SELECT WHERE to find “Jared” in the
first_name column in the customer table.
SQL
● Business Situation/Question
● Expected Result
● Hints
● Solution
SQL
● Challenge No. 1
○ A customer forgot their wallet at our store! We
need to track down their email to inform them.
○ What is the email for the customer with the name
Nancy Thomas?
SQL
● Challenge No. 2
○ A customer wants to know what the movie
“Outlaw Hanky” is about.
○ Could you give them the description for the movie
“Outlaw Hanky”?
SQL
● Challenge No. 3
○ A customer is late on their movie return, and we’ve
mailed them a letter to their address at ‘259 Ipoh
Drive’. We should also call them on the phone to
let them know.
○ Can you get the phone number for the customer
who lives at ‘259 Ipoh Drive’?
SQL
● Challenge Task
○ We want to reward our first 10 paying customers.
○ What are the customer ids of the first 10 customers
who created a payment?
SQL
● Expected Result
SQL
● Hints
○ Use the payment table
○ You will need to use both ORDER BY and LIMIT
○ Remember you may need to specify ASC or DESC
SQL
● Solution
SELECT customer_id FROM payment
ORDER BY payment_date ASC
LIMIT 10;
SQL
● Challenge Task
○ A customer wants to quickly rent a video to watch
over their short lunch break.
○ What are the titles of the 5 shortest (in length of
runtime) movies?
SQL
● Expected Results
SQL
● Hints
○ Use the film table
○ Take a look at the length column
○ You can use ORDER BY and LIMIT
○ Remember to use ASC or DESC to get desired
results
SQL
● Example Solution
SELECT title,length FROM film
ORDER BY length ASC
LIMIT 5;
SQL
● Expected Result
○ 37
SQL
● Example query:
○ SELECT color FROM table
WHERE color IN (‘red’,’blue’)
SQL
● Example query:
○ SELECT color FROM table
WHERE color IN (‘red’,’blue’,’green’)
SQL
● Example query:
○ SELECT color FROM table
WHERE color NOT IN (‘red’,’blue’)
SQL
● The online doc has the task and the expected result.
● This video will have:
○ The Task
○ The Expected Result
○ Hints
● The next video will work through the solutions.
SQL
● Task No. 1