SQL Cheat Sheet for Data Analysts
Basic Commands
Function/Command Purpose Example
SELECT Retrieve data from one or more tables.
SELECT name, age FROM employees;
FROM Specify the table to query data from.
SELECT * FROM customers FROM orders;
WHERE Filter rows based on a condition. SELECT * FROM sales WHERE amount > 1000;
ORDER BY Sort query results ascending/descending.
SELECT * FROM sales ORDER BY amount DESC;
DISTINCT Return unique values in a column.SELECT DISTINCT country FROM customers;
LIMIT Limit the number of rows in the result.
SELECT * FROM sales LIMIT 5;
Aggregate Functions
Function/Command Purpose Example
COUNT() Count the number of rows. SELECT COUNT(*) FROM orders;
SUM() Calculate the sum of a column. SELECT SUM(amount) FROM sales;
AVG() Find the average value of a column.
SELECT AVG(salary) FROM employees;
MAX() Return the maximum value in a column.
SELECT MAX(price) FROM products;
MIN() Return the minimum value in a column.
SELECT MIN(price) FROM products;
GROUP BY Group rows sharing the same values.
SELECT department, SUM(salary) FROM employees GROUP BY departm
HAVING Filter groups based on a condition.SELECT department, SUM(salary) FROM employees GROUP BY departm
String Functions
Function/Command Purpose Example
UPPER() Convert text to uppercase. SELECT UPPER(name) FROM customers;
LOWER() Convert text to lowercase. SELECT LOWER(name) FROM customers;
CONCAT() Combine two or more strings. SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM emplo
SUBSTRING() Extract part of a string. SELECT SUBSTRING(name, 1, 3) FROM employees;
LENGTH() Return the length of a string. SELECT LENGTH(name) FROM customers;
TRIM() Remove spaces from a string. SELECT TRIM(' hello ') AS clean_string;
Date & Time Functions
Function/Command Purpose Example
NOW() Return the current date and time. SELECT NOW();
CURDATE() Return the current date. SELECT CURDATE();
YEAR() Extract the year from a date. SELECT YEAR(order_date) FROM orders;
MONTH() Extract the month from a date. SELECT MONTH(order_date) FROM orders;
DATEDIFF() Calculate days between two dates.SELECT DATEDIFF('2024-01-01', '2023-12-01');
DATE_FORMAT() Format a date. SELECT DATE_FORMAT(order_date, '%Y-%m-%d') FROM orders;
Joins
Function/Command Purpose Example
INNER JOIN Return rows with matching values SELECT
in both tables.
a.name, b.order_id FROM customers a INNER JOIN orders b O
LEFT JOIN Return all rows from the left table and
SELECT
matches.
a.name, b.order_id FROM customers a LEFT JOIN orders b ON
RIGHT JOIN Return all rows from the right tableSELECT
and matches.
a.name, b.order_id FROM customers a RIGHT JOIN orders b O
FULL JOIN Return rows with matches in eitherSELECT
table. a.name, b.order_id FROM customers a FULL JOIN orders b ON
Conditions
Function/Command Purpose Example
AND Combine multiple conditions (all true).
SELECT * FROM sales WHERE region = 'North' AND amount > 1000;
OR Combine multiple conditions (any true).
SELECT * FROM sales WHERE region = 'North' OR region = 'South';
IN Match a value in a list of values. SELECT * FROM customers WHERE country IN ('USA', 'Canada');
BETWEEN Match a range of values. SELECT * FROM sales WHERE amount BETWEEN 100 AND 500;
LIKE Match a pattern using wildcards. SELECT * FROM customers WHERE name LIKE 'A%';
IS NULL Check for NULL values. SELECT * FROM orders WHERE delivery_date IS NULL;