0% found this document useful (0 votes)
132 views5 pages

SQL Cheat Sheet DATAwithBARAA

This document provides a concise cheat sheet on querying and filtering data with SQL. It covers common SQL statements like SELECT, FROM, WHERE, ORDER BY, DISTINCT, BETWEEN, IN, LIKE and logical operators. It also demonstrates how to join multiple tables using INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN. The cheat sheet serves as a helpful reference for beginners to learn essential SQL concepts and commands in 3 sentences or less.

Uploaded by

dennismagui27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
132 views5 pages

SQL Cheat Sheet DATAwithBARAA

This document provides a concise cheat sheet on querying and filtering data with SQL. It covers common SQL statements like SELECT, FROM, WHERE, ORDER BY, DISTINCT, BETWEEN, IN, LIKE and logical operators. It also demonstrates how to join multiple tables using INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN. The cheat sheet serves as a helpful reference for beginners to learn essential SQL concepts and commands in 3 sentences or less.

Uploaded by

dennismagui27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

D A TA w it h B A R AA

SQL Cheat Sheet datawithbaraa.com


SQL Youtube Tutorials

QUERYING DATA Comparison Operators

SELECT
Retrieve all data and columns from customers
SELECT *
FROM customers

Retrieve ONLY first name and country of all customers


Find all customers whose score is greater than 500
SELECT
first_name, SELECT *
country FROM customers
FROM customers WHERE score > 500

Find all customers whose score is less than 500


DISTINCT
SELECT *
List all countries of all customers without duplicates FROM customers
SELECT DISTINCT country WHERE score < 500
FROM customers
Find all customers whose score is less than or equal to 500
ORDER BY SELECT *
FROM customers
Retrieve all customers where the result is sorted by score
WHERE score <= 500
(smallest first)
SELECT * SELECT * Find all customers whose score is higher than or equal to
FROM customers FROM customers 500
ORDER BY score ASC ORDER BY score SELECT *
FROM customers
Retrieve all customers where the result is sorted by score WHERE score >= 500
(highest first)
SELECT * Find all non-german customers
FROM customers SELECT *
ORDER BY score DESC FROM customers
WHERE country !=‘Germany‘
Retrieve all customers, sorting the result by country
(alphabetically) and then by score (highest first)
SELECT *
Logical Operators
FROM customers
ORDER BY country ASC, score DESC

FILTERING DATA
WHERE
List only german customers AND
SELECT * Find all customers who come from Germany AND whose
FROM customers score is less than 400
WHERE customers = ‘Germany‘ SELECT *
FROM customers
WHERE country = ‘Germany‘
AND score <= 500
D A TA w it h B A R AA
SQL Cheat Sheet datawithbaraa.com
SQL Youtube Tutorials

OR Find all customers whose first name contains r


Find all customers who come from Germany OR whose SELECT *
score is less than 400 FROM customers
SELECT * WHERE first_name LIKE ‘%r%‘
FROM customers
Find all customers whose first name contains r in 3d
WHERE country = ‘Germany‘
position
OR score < 400
SELECT *
NOT FROM customers
WHERE first_name LIKE ‘__r%‘
Find all customers whose score is NOT less than 400
SELECT * ALIASES
FROM customers Columns
WHERE NOT score < 400 SELECT customer_id AS cid
FROM customers
BETWEEN
Tables
Find all customers whose score falls in the range between
SELECT c.customer_id AS cid
100 and 500
SELECT * FROM customers AS c
FROM customers
WHERE score BETWEEN 100 AND 500
JOINING TABLES
Or we can solve the same task with the following SQL
SELECT * INNER JOIN
FROM customers List customer ID, first name, order ID, quantity. Exclude
WHERE score >= 100 AND score <= 500 the customers who have not placed any orders
SELECT
c.customer_id,
IN c.first_name,
Find all customers whose ID is equal to 1, 2 or 5 o.order_id,
o.quantity
SELECT *
FROM customers AS c
FROM customers
INNER JOIN orders AS o
WHERE customer_id IN (1,2,5)
ON c.customer_id = o.customer_id

LIKE LEFT JOIN


Find all customers whose first name starts with M List customer ID, first name, order ID, quantity. Include
SELECT * the customers who have not placed any orders
FROM customers SELECT
WHERE first_name LIKE ‘M%‘ c.customer_id,
c.first_name,
o.order_id,
Find all customers whose first name ends with n
o.quantity
SELECT * FROM customers AS c
FROM customers LEFT JOIN orders AS o
WHERE first_name LIKE ‘%n‘ ON c.customer_id = o.customer_id
D A TA w it h B A R AA
SQL Cheat Sheet datawithbaraa.com
SQL Youtube Tutorials

RIGHT JOIN UNION


List customer ID, first name, order ID, quantity. Include all
List first name, last name and country of all persons from
orders, regardless of whether there is a matching
customers and employees
customer
SELECT
SELECT
first_name,
c.customer_id,
last_name,
c.first_name,
country
o.order_id,
FROM customers
o.quantity
UNION ALL
FROM customers AS c
SELECT
RIGHT JOIN orders AS o
first_name,
ON c.customer_id = o.customer_id
last_name,
emp_country
FULL JOIN FROM orders
List customer ID, first name, order ID, quantity. Include all List first name, last name and country of all persons from
customers and all orders. customers and employees without duplicates
All databases that support FULL JOIN (MySQL doesnt SELECT
support it) first_name,
SELECT last_name,
c.customer_id, country
c.first_name, FROM customers
o.order_id, UNION
o.quantity SELECT
FROM customers AS c first_name,
FULL JOIN orders AS o last_name,
ON c.customer_id = o.customer_id emp_country
FROM orders
Workaround for databases that doenst support FULL JOIN
Like MySQL
SELECT
c.customer_id,
c.first_name,
o.order_id,
o.quantity
FROM customers AS c
LEFT JOIN orders AS o
ON c.customer_id = o.customer_id
UNION
SELECT
c.customer_id,
c.first_name,
o.order_id,
o.quantity
FROM customers AS c
RIGHT JOIN orders AS o
ON c.customer_id = o.customer_id
D A TA w it h B A R AA
SQL Cheat Sheet datawithbaraa.com
SQL Youtube Tutorials
Aggregate Functions String Functions

COUNT()
Find the total number of customers CONCAT()
SELECT COUNT(*) AS total_customers
List all customer names, where the name is combination
FROM customers of first name and last name
SUM() SELECT
Find the total quantity of all orders CONCAT(first_name,‘-‘,lastname) AS
customer_name
SELECT SUM(quantity) AS sum_quantity
FROM customers
FROM orders
LOWER()
AVG() List the first name of all customers in lowercase
Find the average score of all customers
SELECT
SELECT AVG(score) AS avg_score
LOWER(first_name) AS low_first_name
FROM orders
FROM customers

MAX() MIN() UPPER()


Find the highest score of all customers List the first name of all customers in lowercase

SELECT MAX(score) AS max_score SELECT


FROM customers UPPER(first_name) AS up_first_name
FROM customers
Find the lowest score of all customers
SELECT LOW(score) AS low_score TRIM()
FROM customers List the last name of all customers and remove all the
white spaces in the names
GROUP BY SELECT
Find the total number of customers for each country TRIM(last_name) AS trim_last_name
SELECT FROM customers
COUNT(*) AS total_customers, LENGTH()
country
FROM Customers Find the length of the last name of all customers
GROUP BY country SELECT
LENGTH(last_name) AS len_last_name
HAVING FROM customers
Find the total number of customers for each country and
only include countries that have more than 1 customer
SUBSTRING()
SELECT Subtract 3 characters from the last name of all customers,
COUNT(*) AS total_customers, starting from the 2d position
country SELECT
FROM Customers SUBSTRING(last_name,2,3) AS
GROUP BY Country sub_last_name
HAVING COUNT(*) > 1 FROM customers
D A TA w it h B A R AA
SQL Cheat Sheet datawithbaraa.com
SQL Youtube Tutorials
Modifying Data Defining Data
INSERT CREATE
Insert new customer Anna Nixon from UK Create new SQL table called Persons with 4 columns:
INSERT INTO customers ID, person name, birth date, and phone
(first_name, last_name, country)
CREATE TABLE persons (
VALUES (‘Anna‘,‘Nixon‘,‘UK‘)
id INT PRIMARY_KEY AUTO_INCREMENT,
Person_name VARCHAR(50) NOT NULL,
UPDATE birth_date DATE,
Change the country of customer ID 7 to Germany phone VARCHAR(15) NOT NULL UNIQUE
)
UPDATE customers
SET country = ‘Germany‘
WHERE customer_id = 7 ALTER
Change the score of the customer Anna to 100 and change Add new column called email to table Persons
her country from UK to USA
UPDATE customers ALTER TABLE persons
SET country = ‘USA‘, ADD email VARCHAR(15) NOT NULL
country = 100
WHERE customer_id = 6 DROP
DELETE Delete the new table Persons from our database

Delete both customers Anna and Max from our database


DROP TABLE persons
DELETE FROM customers
WHERE customer_id IN (6,7)
Subqueries
TRUNCATE IN
Delete all customers from our database Find all orders placed from customers whose score higher
TRUNCATE customers than 500 using customer_id
SELECT *
FROM orders
WHERE customer_id IN (
SELECT customer_id
FROM customers
WHERE score > 500)
IN
Find all orders placed from customers whose score higher
than 500 using customer_id
SELECT *
FROM orders AS o
WHERE EXISTS (
SELECT 1
FROM customers AS c
WHERE c.customer_id = o.customer_id
AND score > 500)

You might also like