SQL Cheat Sheet PDF
SQL Cheat Sheet PDF
Retrieve Data From One Or More Tables Filter Rows Based On Specified Conditions Combines Multiple Conditions In A WHERE
Clause
SELECT * FROM employees; SELECT * FROM employees WHERE
department = 'IT'; SELECT * FROM employees WHERE
department = 'IT' AND salary >
60000;
DISTINCT LIKE OR
Select Unique Values From A Column Match A Pattern In A Column Specifies Multiple Conditions Where Any One
Of Them Should Be True
SELECT DISTINCT department FROM SELECT * FROM employees WHERE
employees; first_name LIKE 'J%'; SELECT * FROM employees WHERE
department = 'HR' OR department =
'Finance';
WHERE IN NOT
Filter Rows Based On Specified Conditions Match Any Value In A List Negates A Condition
SELECT * FROM employees WHERE SELECT * FROM employees WHERE SELECT * FROM employees WHERE NOT
salary > 55000.00; department IN ('HR', 'Finance'); department = 'IT';
Limit The Number Of Rows Returned In The Match Values Within A Specified Range Sorts the Result Set in Ascending or
Result Set Descending Order
SELECT * FROM employees WHERE
SELECT * FROM employees LIMIT 3; salary BETWEEN 50000 AND 60000; SELECT * FROM employees ORDER BY
salary DESC;
Retrieve A Specified Number Of Rows From Match NULL Values Groups Rows that have the Same Values into
The Result Set Summary Rows
SELECT * FROM employees WHERE
SELECT * FROM employees FETCH FIRST department IS NULL; SELECT department, COUNT(*) AS
3 ROWS ONLY; employee_count FROM employees GROUP
BY department;
Count The Number Of Rows In A Result Set Retrieves Records That Have Matching Create an Index on a Table
Values in Both Tables
SELECT COUNT(*) FROM employees; CREATE INDEX idx_department ON
SELECT * FROM employees INNER JOIN employees (department);
departments ON
employees.department_id =
departments.department_id;
Calculate The Sum Of Values In A Column Retrieves All Records from the Left Table and Remove an Index
the Matched Records from the Right Table
SELECT SUM(salary) FROM employees; DROP INDEX IF EXISTS
SELECT * FROM employees LEFT JOIN idx_department;
departments ON
employees.department_id =
departments.department_id;
Calculate The Average Value Of A Column Retrieves All Records from the Right Table Start a New Transaction
and the Matched Records from the Left Table
SELECT AVG(salary) FROM employees; BEGIN TRANSACTION;
SELECT * FROM employees RIGHT JOIN
departments ON
employees.department_id =
departments.department_id;
Find the Minimum Value in a Column Retrieves All Records When There Is a Match Save Changes Made During the Current
in Either the Left or Right Table Transaction
SELECT MIN(salary) FROM employees;
SELECT * FROM employees FULL OUTER COMMIT;
JOIN departments ON
employees.department_id =
departments.department_id;
Find the Maximum Value in a Column Retrieves the Cartesian Product of the Two Undo Changes Made During the Current
Tables Transaction
SELECT MAX(salary) FROM employees;
SELECT * FROM employees CROSS JOIN ROLLBACK;
departments;