Complete SQL Cheat Sheet Tables
Complete SQL Cheat Sheet Tables
Joins
Command Description Syntax Example
INNER JOIN Matching rows in SELECT * FROM t1 INNER SELECT * FROM employees INNER JOIN
both tables JOIN t2 ON t1.col = t2.col; departments ON
employees.department_id =
departments.id;
LEFT JOIN All from left + SELECT * FROM t1 LEFT JOIN SELECT * FROM employees LEFT JOIN
matching right t2 ON t1.col = t2.col; departments ON
employees.department_id =
departments.id;
RIGHT JOIN All from right + SELECT * FROM t1 RIGHT SELECT * FROM employees RIGHT JOIN
matching left JOIN t2 ON t1.col = t2.col; departments ON
employees.department_id =
departments.department_id;
FULL JOIN All rows from both LEFT JOIN + RIGHT JOIN with SELECT * FROM employees LEFT JOIN
tables UNION departments ON employees.id =
departments.id UNION SELECT * FROM
employees RIGHT JOIN departments ON
employees.id = departments.id;
CROSS JOIN Cartesian product SELECT * FROM t1 CROSS SELECT * FROM employees CROSS JOIN
JOIN t2; departments;
SELF JOIN Join the same SELECT * FROM t1, t1 t2 SELECT * FROM employees t1,
table WHERE t1.col = t2.col; employees t2 WHERE t1.manager_id =
t2.employee_id;
NATURAL JOIN Join by common SELECT * FROM t1 NATURAL SELECT * FROM employees NATURAL
columns JOIN t2; JOIN departments;
Subqueries
Command Description Syntax Example
IN Value in subquery result SELECT * FROM table WHERE SELECT * FROM customers WHERE
col IN (subquery); city IN (SELECT city FROM suppliers);
ANY Compare to any value SELECT * FROM table WHERE SELECT * FROM products WHERE
val < ANY (subquery); price < ANY (SELECT unit_price FROM
supplier_products);
ALL Compare to all values SELECT * FROM table WHERE SELECT * FROM orders WHERE
val > ALL (subquery); order_amount > ALL (SELECT
total_amount FROM previous_orders);
Aggregate Functions
Command Description Syntax Example
COUNT() Count rows SELECT COUNT(col) FROM SELECT COUNT(age) FROM
table; employees;
SUM() Sum values SELECT SUM(col) FROM table; SELECT SUM(revenue) FROM sales;
AVG() Average value SELECT AVG(col) FROM table; SELECT AVG(price) FROM products;
MIN() Minimum value SELECT MIN(col) FROM table; SELECT MIN(price) FROM products;
MAX() Maximum value SELECT MAX(col) FROM table; SELECT MAX(price) FROM products;
String Functions
Command Description Syntax Example
CONCAT() Combine strings SELECT CONCAT(str1, str2) SELECT CONCAT(first_name, ' ',
FROM table; last_name) FROM employees;
SUBSTRING() Extract substring SELECT SUBSTRING(str FROM SELECT
start FOR len) FROM table; SUBSTRING(product_name FROM
1 FOR 5) FROM products;
CHAR_LENGTH() String length SELECT CHAR_LENGTH(str) SELECT
FROM table; CHAR_LENGTH(product_name)
FROM products;
UPPER() To uppercase SELECT UPPER(str) FROM table; SELECT UPPER(first_name) FROM
employees;
LOWER() To lowercase SELECT LOWER(str) FROM table; SELECT LOWER(last_name) FROM
employees;
TRIM() Trim spaces SELECT TRIM(str) FROM table; SELECT TRIM(full_name) FROM
customers;
LEFT() Left part of string SELECT LEFT(str, n) FROM table; SELECT LEFT(product_name, 5)
FROM products;
RIGHT() Right part of string SELECT RIGHT(str, n) FROM SELECT RIGHT(order_number, 4)
table; FROM orders;
REPLACE() Replace substring SELECT REPLACE(str, old, new) SELECT REPLACE(description,
FROM table; 'old', 'new') FROM
product_descriptions;
Set Operations
Command Description Syntax Example
UNION Combine unique rows SELECT col FROM table1 SELECT first_name FROM
UNION SELECT col FROM customers UNION SELECT
table2; first_name FROM employees;
INTERSECT Common rows SELECT col FROM table1 SELECT first_name FROM
INTERSECT SELECT col customers INTERSECT SELECT
FROM table2; first_name FROM employees;
EXCEPT Rows in first not second SELECT col FROM table1 SELECT first_name FROM
EXCEPT SELECT col FROM customers EXCEPT SELECT
table2; first_name FROM employees;
Transaction Control
Command Description Syntax Example
BEGIN Start transaction BEGIN; BEGIN;
COMMIT Save changes COMMIT; COMMIT;
ROLLBACK Undo changes ROLLBACK; ROLLBACK;
SAVEPOINT Set rollback point SAVEPOINT sp_name; SAVEPOINT before_update;
ROLLBACK TO Rollback to point ROLLBACK TO sp_name; ROLLBACK TO before_update;
SET TRANSACTION Set transaction mode SET TRANSACTION SET TRANSACTION ISOLATION
ISOLATION LEVEL LEVEL; LEVEL READ COMMITTED;