SQL Cheat Sheet
SQL Cheat Sheet
Cheat Sheet
Mosh Hamedani
This cheat sheet includes the materials I’ve covered in my SQL tutorial for
Beginners on YouTube.
https://youtu.be/7S_tz1z_5bA
Both the YouTube tutorial and this cheat cover the core language constructs and
they are not complete by any means.
If you want to learn everything SQL has to offer and become a SQL expert, check
out my Complete SQL Mastery Course.
Use the coupon code CHEATSHEET upon checkout to get this course with a
90% discount:
https://codewithmosh.com/p/complete-sql-mastery/
About the Author
https://codewithmosh.com
https://youtube.com/user/programmingwithmosh
https://twitter.com/moshhamedani
https://facebook.com/programmingwithmosh/
Retrieving Data from a Single Table ...................5
Basics ........................................................................................................ 5
Comments ................................................................................................. 5
SELECT Clause ........................................................................................5
WHERE Clause ........................................................................................ 6
Logical Operators .................................................................................... 6
IN Operator ..............................................................................................7
BETWEEN Operator ................................................................................7
LIKE Operator .......................................................................................... 7
REGEXP Operator.................................................................................... 7
IS NULL Operator ...................................................................................8
ORDER BY Clause ...................................................................................8
LIMIT Clause ............................................................................................8
Retrieving Data from Multiple Tables...............10
Inner Joins .............................................................................................10
Outer Joins .............................................................................................10
USING Clause .........................................................................................10
Cross Joins ..............................................................................................10
Unions .....................................................................................................10
Inserting, Updating, and Deleting Data ............12
Inserting Data ........................................................................................12
Creating a Copy of a Table ....................................................................12
Updating Data .......................................................................................12
Deleting Data .........................................................................................13
Summarizing Data .............................................14
Want to Become a SQL Expert? .............................................................15
Retrieving Data from a Single Table
Basics
USE sql_store;
SELECT *
FROM customers
WHERE state = ‘CA’
ORDER BY first_name
LIMIT 3;
Comments
We use comments to add notes to our code.
SELECT Clause
—- Using expressions
Order of operations:
• Parenthesis
• Multiplication / division
• Addition / subtraction
—- Removing duplicates
SELECT DISTINCT state
FROM customers
WHERE Clause
We use the WHERE clause to filter data.
Comparison operators:
• Equal: =
• Not equal: !=
Logical Operators
—- AND (both conditions must be True)
SELECT *
FROM customers
WHERE birthdate > ‘1990-01-01’ AND points > 1000
BETWEEN Operator
SELECT *
FROM customers
WHERE points BETWEEN 100 AND 200
LIKE Operator
—- Returns customers whose first name starts with b
SELECT *
FROM customers
WHERE first_name LIKE ‘b%’
REGEXP Operator
—- Returns customers whose first name starts with a
SELECT *
FROM customers
WHERE first_name REGEXP ‘^a’
• ^: beginning of a string
• $: end of a string
• |: logical OR
IS NULL Operator
—- Returns customers who don’t have a phone number
SELECT *
FROM customers
WHERE phone IS NULL
ORDER BY Clause
—- Sort customers by state (in ascending order), and then
—- by their first name (in descending order)
SELECT *
FROM customers
ORDER BY state, first_name DESC
LIMIT Clause
—- Return only 3 customers
SELECT *
FROM customers
LIMIT 3
—- Skip 6 customers and return 3
SELECT *
FROM customers
LIMIT 6, 3
Retrieving Data from Multiple Tables
Inner Joins
SELECT *
FROM customers c
JOIN orders o
ON c.customer_id = o.customer_id
Outer Joins
—- Return all customers whether they have any orders or not
SELECT *
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
USING Clause
If column names are exactly the same, you can simplify the join with the USING
clause.
SELECT *
FROM customers c
JOIN orders o
USING (customer_id)
Cross Joins
—- Combine every color with every size
SELECT *
FROM colors
CROSS JOIN sizes
Unions
—- Combine records from multiple result sets
SELECT name, address
FROM customers
UNION
SELECT name, address
FROM clients
Inserting, Updating, and Deleting Data
Inserting Data
—- Insert a single record
INSERT INTO customers(first_name, phone, points)
VALUES (‘Mosh’, NULL, DEFAULT)
Updating Data
UPDATE invoices
SET payment_total = 10, payment_date = ‘2019-01-01’
WHERE invoice_id = 1
Deleting Data
DELETE FROM invoices
WHERE invoice_id = 1
Summarizing Data
Aggregate functions
• MAX(col)
• MIN(col)
• AVG(col)
• SUM(col)
SELECT
MAX(payment_total),
MIN(payment_total)
FROM invoices
• 10 hours of HD video
The price for this course is $149 but the first 200 people who have downloaded this
cheat sheet can get it for $12.99 using the coupon code CHEATSHEET:
https://codewithmosh.com/p/complete-sql-mastery/