SQL Cheat Sheet
SQL Cheat Sheet
productLine
products
productCode
orderdetails
orderNumber
textDescription
productName
productCode
This cheat sheet provides a quick reference for common SQL htmlDescription
productLine
quantityOrdered
operations and functions, adapted to work with the Classic image productScale
priceEach
productDescription
quantityInStock
buyPrice
orders
lastName
MSRP orderNumber
firstName
orderDate
customers
requiredDate
customerNumber
shippedDate
officeCode
customerName
status
reportsTo
contactLastName
comments
jobTitle
contactFirstName
customerNumber
phone
offices
addressLine1
officeCode
addressLine2
payments
city
city
customerNumber
phone
state
checkNumber
addressLine1
postalCode
paymentDate
addressLine2
country
amount
state
salesRepEmployeeNumber
country
creditLimit
postalCode
territory
Select SELECT *
Display all columns from SUM SELECT SUM(quantityOrdered * priceEach)
Calculates the total sales
FROM products; products table. AS total_sales
from the orderdetails
FROM orderdetails; table.
SELECT productName, buyPrice
Display only productName
FROM products; and buyPrice columns from
products table.
AVG SELECT AVG(buyPrice) AS average_price
Averages the buyPrice
FROM products; values in products.
Order By SELECT productName, buyPrice
Sort the selected columns by
FROM products
buyPrice in descending
ORDER BY buyPrice DESC; order.
ROUND SELECT ROUND(AVG(buyPrice), 2)
Rounds the average of
AS average_price
buyPrice to two decimal
SELECT productName, buyPrice
Sort the selected columns by
productName in ascending
FROM products; places.
FROM products
ORDER BY productName ASC;
order.
MIN SELECT MIN(buyPrice) AS lowest_price
Finds the minimum value in
FROM products; the buyPrice column of
SELECT orderNumber, customerNumber,
Sorts the data by
customerNumber and then products.
orderDate, status
FROM orders
by orderDate within each
ORDER BY customerNumber ASC, orderDate DESC;
customerNumber. MAX SELECT MAX(buyPrice) AS highest_price
Finds the maximum value in
FROM products; the buyPrice column of
products.
Distinct SELECT DISTINCT productLine
Retrieve unique values from
FROM products; productLine in products
table. COUNT SELECT COUNT(*) AS total_orders
Counts the total number of
FROM orders; rows in orders.
SELECT DISTINCT city, country
Retrieve unique
FROM customers
combinations of city and
ORDER BY country, city; country where customers Note COUNT(*) includes all rows, while COUNT(column_name) excludes NULL values in the specified column.
are located, sorted by
country and then city.
GROUP BY SELECT productLine, AVG(buyPrice)
Groups rows by HAVING SELECT productLine, AVG(buyPrice)
Filters product lines to only
AS avg_price
productLine and AS avg_price
include those with average
FROM products
calculates the average FROM products
price greater than 50.
GROUP BY productLine; price for each product line. GROUP BY productLine
FROM products
FROM products; rows in the products table,
with price over 100 and
WHERE buyPrice > 100
returning the total number of
calculates the average price
GROUP BY productLine; products. This includes all
for each product line.
rows, regardless of NULL
values in any columns.
SELECT customerNumber, COUNT(orderNumber)
Groups orders by
AS order_count
customerNumber, counts the
FROM orders
number of orders for each
SELECT COUNT(reportsTo)
Counts the number of non-
WHERE orderDate >= '2023-01-01'
customer in 2023 and AS employees_with_manager
null values in the reportsTo
GROUP BY customerNumber
beyond, and sorts the results FROM employees; column of the employees
ORDER BY order_count DESC;
by the order count in table, showing how many
descending order.
ELSE 'Premium'
firstName,
value among extension,
END AS price_category
lastName,
email, or 'No contact
FROM products;
COALESCE(extension, email, 'No
information'.
contact information') AS contact_info
SELECT orderNumber,
Categorizes orders into FROM employees;
orderDate,
different sale seasons based
CASE
on the order date.
WHEN CAST(strftime('%m',
Converts the orderDate to
Cast SELECT orderNumber, CAST(orderDate AS DATE)
orderDate) AS INTEGER)
AS order_day
DATE type.
BETWEEN 3 AND 5 THEN
FROM orders;
'Spring Sale'
WHEN CAST(strftime('%m',
orderDate) AS INTEGER)
'Summer Sale'
WHEN CAST(strftime('%m',
orderDate) AS INTEGER)
'Fall Sale'
END AS sale_season
FROM orders;
SELECT productName
SELECT customerNumber, customerName
FROM products
FROM customers
FROM products
PARTITION
SELECT employeeNumber,
Calculates the average Dense
SELECT productCode,
select emp_id, sal_amount,
sal_date,
Ranks products based on
BY officeCode,
extension length within each Rank dense_ran k() over (
officeCode column.
FROM employees; FROM products;
are ties.
FROM employees; ) AS extension_rank_in_office
FROM employees;
PARTITION
SELECT employeeNumber,
Calculates a running total of
BY
ROW
SELECT
select orderNumber,
emp_id, sal_amount,
sal_date,
Assigns a unique row
officeCode,
extension lengths within
NUMBER dense_ran k()Date,
) AS running_total_length customerNumber
FROM orders;
INNER
SELECT o.orderNumber,
Joins orders and CROSS
SELECT p.productName,
Returns all possible
JOIN o.orderDate,
customers tables, returning JOIN pl.textDescription
combinations of products
c.customerName
only matching rows. This is FROM products AS p
and product line
FROM orders AS o
the default join type when CROSS JOIN productlines AS pl; descriptions.
INNER JOIN customers AS c
JOIN is used without
ON o.customerNumber = c.customerNumber;
specifying LEFT, RIGHT, or
FULL.
join
SELECT o.orderNumber,
Joins four tables: orders,
LEFT
Joins products and
multiple c.customerName,
customers, orderdetails,
SELECT p.productCode,
JOIN p.productName,
orderdetails tables, p.productName
and products.
od.orderNumber
returning all products and FROM orders AS o
ON o.orderNumber = od.orderNumber
JOIN products p
ON od.productCode = p.productCode;
RIGHT
SELECT e.employeeNumber,
Joins offices and
Subquery
SELECT productName,
Includes a subquery that Subquery
SELECT productName,
Finds products that were
in SELECT buyPrice,
calculates the average price with IN
buyPrice
ordered in order 10100.
(SELECT AVG(buyPrice) FROM
for all products. FROM products
products) AS avg_price
WHERE productCode IN (
FROM orderdetails
Subquery
SELECT productLine,
Finds product lines with an );
in FROM avg_price
average price greater than
FROM (SELECT productLine,
100 using a subquery.
AVG(buyPrice) AS avg_price
Subquery
his query retrieves the
SELECT customerName
T
FROM products
with
= c.customerNumber
Subquery
SELECT productName,
This query selects products AND o.orderDate >= '2023-01-01'
in WHERE buyPrice
that are more expensive than );
FROM products p1
the average price in their
WHERE p1.buyPrice > (
respective product line,
SELECT AVG(p2.buyPrice)
p2.productLine)
ORDER BY productLine,
buyPrice DESC;
= SELECT orderNumber,
This query selects all orders .tables Lists all tables in the current database.
orderDate,
for a specific customer
totalAmount
named ‘Mini Gifts .schema table_name Shows the schema for the specified table.
FROM orders
Distributors Ltd.’, ordered by
WHERE customerNumber = (
date from most recent to .mode column
Sets output to column mode with headers
SELECT customerNumber
oldest. .headers on for better readability.
FROM customers
WHERE customerName = 'Mini Gifts
SELECT o.orderNumber,
by total amount in
o.orderDate,
descending order.
ot.total_amount
FROM orders o
JOIN order_totals ot
ON o.orderNumber = ot.orderNumber
Note SQLite doesn’t have a built-in user management system like PostgreSQL, so commands related to user
management are not applicable.