Essential SQL Commands Cheat Sheet
Essential SQL Commands Cheat Sheet
Query Commands
SELECT Basic query building block to retrieve data. SELECT 1 FROM table_name;
SELECT * Using * with SELECT returns all columns. SELECT * FROM table_name;
SELECT column Specify exact columns with their name. SELECT column_name FROM table_name;
SELECT table.column Reference a column from a specific table. SELECT table_name.column_name FROM table_name, table_2_name;
ORDER BY column DESC Order the results by a column in descending order. SELECT * FROM table_name ORDER BY column_name DESC;
LIMIT Restrict the number of results returned. SELECT * FROM table_name LIMIT 5;
OFFSET Skip the first OFFSET number of rows. Often used with LIMIT. SELECT * FROM table_name LIMIT 5 OFFSET 10;
SELECT column FROM table_name where column_name IN (SELECT
SUBQUERY Run a query to retrieve data for another query.
column_2_name FROM table_2_name);
Aggregate Functions²
COUNT Count the number of rows that match the query. SELECT COUNT(column_name) FROM table_name;
MAX Return the highest value in a numeric column. SELECT MAX(column_name) FROM table_name;
MIN Return the lowest value in a numeric column. SELECT MIN(column_name) FROM table_name;
SUM Sum the values of a numeric column. SELECT SUM(column_name) FROM table_name;
AVG Calculate the average value for a numeric column. SELECT AVG(column_name) FROM table_name;
HAVING Used with aggregate functions instead of the WHERE clause. SELECT COUNT(column_name) FROM table_name HAVING column_name > 10;
GROUP BY Used to refine an aggregate result. SELECT COUNT(column_name) FROM table_name GROUP BY column_2_name;
Operators
LIKE Case-sensitive search for a pattern with a wildcard operator (%). SELECT column_name FROM table_name WHERE column_name LIKE '%VALUE%';
ILIKE Case-insensitive search for a pattern with a wildcard operator (%). SELECT column_name FROM table_name WHERE column_name ILIKE '%value%';
Search for a value between two values. Works with dates or SELECT column_name FROM table_name WHERE column_name BETWEEN 1 AND
BETWEEN
numbers. 10;
> Search for values greater than a condition. SELECT column_name FROM table_name WHERE column_name > 10;
>= Search for values greater or equal to a condition. SELECT column_name FROM table_name WHERE column_name >= 10;
< Search for values less than a condition. SELECT column_name FROM table_name WHERE column_name < 10;
<= Search for values less than or equal to a condition. SELECT column_name FROM table_name WHERE column_name <= 10;
= Search for values matching a condition exactly. SELECT column_name FROM table_name where column_name = 10;
<> Search for values not equal to a condition. SELECT column_name FROM table_name WHERE column_name <> 10;
Combine two unique queries (with the same columns) into one SELECT column_name FROM table_name UNION SELECT column_2_name FROM
UNION
result. table_2_name;
Combine two queries (with the same columns) into one result. SELECT column_name FROM table_name UNION ALL SELECT column_2_name
UNION ALL
Duplicates allowed. FROM table_2_name;
IN Shorthand for WHERE. Specifies multiple OR conditions. SELECT column_name FROM table_name where column_name IN ('A', 'B', 'C');
Shorthand for WHERE. Specifies multiple OR conditions
NOT IN SELECT column_name FROM table_name where column_name NOT IN ('A', 'B', 'C');
(inverted) or not equal to.
IS NULL Check for empty values. SELECT column_name FROM table_name WHERE column_name IS NULL;
IS NOT NULL Check for no empty values. SELECT column_name FROM table_name WHERE column_name IS NOT NULL;
SELECT column_name FROM table_name INTERSECT SELECT column_2_name
INTERSECT Return results which match two queries.
FROM table_2_name;
SELECT column_name FROM table_name MINUS SELECT column_2_name FROM
MINUS ²Return results in one query which are not in another query.
table_2_name;
Copyright ©2020 MakeUseOf. For more cheat sheets, head over to www.makeuseof.com
Command Name Description Example
Joins
SELECT * FROM table_name LEFT OUTER JOIN table_2_name ON
ON Used to specify the column to compare and match results.
table_name.column_name = table_2_name.column_name;
Shorthand for ON, used when the column name is the same in SELECT * FROM table_name LEFT OUTER JOIN table_2_name ON
USING
both tables. table_name.column_name = table_2_name.column_2_name;
All the results from the left table, with only the matching results SELECT * FROM table_name LEFT OUTER JOIN table_2_name ON
LEFT OUTER JOIN
from the right table. table_name.column_name = table_2_name.column_2_name;
SELECT * FROM table_name LEFT OUTER JOIN table_2_name ON
LEFT OUTER JOIN (WITH (With null) All the results from the left table but not in the right
table_name.column_name = table_2_name.column_2_name WHERE
NULL) table.
table_2_name.column_2_name IS NULL;
SELECT * FROM table_name INNER JOIN table_2_name ON
INNER JOIN All the results that match in both the left and right tables.
table_name.column_name = table_2_name.column_2_name;
NULL Allow empty values for this field. CREATE TABLE table_name (column_name column_name datatype NULL);
NOT NULL Don't allow empty values for this field. CREATE TABLE table_name (column_name column_name datatype NOT NULL);
DEFAULT A value to populate the field with if one is not supplied. CREATE TABLE table_name (column_name datatype DEFAULT 'makeuseof');
Create a new table based on the structure of an existing table.
AS CREATE TABLE table_2_name AS SELECT * FROM table_name;
The new table will contain the data from the old table.
ALTER TABLE (ADD
Add a new column to an existing table. ALTER TABLE table_name ADD COLUMN column_2_name datatype;
COLUMN)
ALTER TABLE (DROP
Remove a column from an existing table. ALTER TABLE table_name DROP COLUMN column_2_name;
COLUMN)
ALTER TABLE (ALTER
Change the datatype of an existing column. ALTER TABLE table_2_name ALTER COLUMN column_name datatype;
COLUMN)
ALTER TABLE (RENAME ALTER TABLE table_name RENAME COLUMN column_name TO
Rename an existing column.
COLUMN) new_column_name datatype;
ALTER TABLE (RENAME
Rename an existing table. RENAME TABLE table_name TO new_table_name;
TABLE)
ALTER TABLE (MODIFY
Allow null values for a column. ALTER TABLE table_name MODIFY column_name datatype NULL;
NULL)
ALTER TABLE (MODIFY NOT
Prevent null values for a column. ALTER TABLE table_name MODIFY column_name datatype NOT NULL;
NULL)
DROP TABLE Delete a table and all its data. DROP TABLE table_name;
TRUNCATE TABLE Delete all the data in a table, but not the table itself. TRUNCATE TABLE table_name;
Constraints
A value that uniquely identifies a record in a table. A combination CREATE TABLE table_name (column_name datatype column_2_name datatype,
PRIMARY KEY
of NOT NULL and UNIQUE. PRIMARY KEY (column_name, column_2_name));
References a unique value in another table. Often a primary key in CREATE TABLE table_name (column_name datatype column_2_name datatype,
FOREIGN KEY
the other table. FOREIGN KEY (column_name) REFERENCES table_2_name (column_2_name));
INDEX (CREATE UNIQUE) Create an index that does not allow duplicate values. CREATE UNIQUE INDEX index_name ON table_name(column_name);
Copyright ©2020 MakeUseOf. For more cheat sheets, head over to www.makeuseof.com
Command Name Description Example
INSERT (MULTIPLE VALUES) Add several new records to a table. INSERT INTO table_name(column_name) VALUES(value_1),(value_2);
INSERT (SELECT) Add records to a table, but get the values from an existing table. INSERT INTO table_name(column_name) SELECT * FROM table_2_name;
UPDATE (ALL) Modify all existing records in a table. UPDATE table_name SET column_name = 10;
UPDATE (WHERE) Modify existing records in a table which match a condition. UPDATE table_name SET column_name = 10 WHERE column_2_name = 5;
DELETE (ALL) Remove all records from a table. DELETE FROM table_name;
DELETE (WHERE) Remove records from a table which match a condition. DELETE FROM table_name WHERE column_name = 5;
CREATE TRIGGER (OR Create a trigger, or update an existing trigger if one is found with CREATE OR MODIFY TRIGGER trigger_name BEFORE INSERT ON table_name FOR
MODIFY) the same name. EACH ROW EXECUTE stored_procedure;
TRIGGER_TYPE (FOR EACH CREATE TRIGGER trigger_name BEFORE INSERT ON table_name FOR EACH ROW
Execute the trigger for every row changed.
ROW) EXECUTE stored_procedure;
TRIGGER_TYPE (FOR EACH Execute the trigger once per SQL statement, regardless of how CREATE TRIGGER trigger_name BEFORE INSERT ON table_name FOR EACH ROW
STATEMENT) many rows are altered. STATEMENT stored_procedure;
AS Define where to retrieve the data for a view. CREATE VIEW view_name(column_name) AS SELECT * FROM table_name;
WITH CASCADED CHECK Ensure any data modified through a view meets the rules defined CREATE VIEW view_name(column_name) AS SELECT * FROM table_name WITH
OPTION by the rule. Apply this to any other views. CASCADED CHECK OPTION;
Ensure any data modified through a view meets the rules defined CREATE VIEW view_name(column_name) AS SELECT * FROM table_name WITH
WITH LOCAL CHECK OPTION
by the rule. Ignore this for any other views. LOCAL CHECK OPTION;
Create a recursive view (one that refers to a recursive common CREATE RECURSIVE VIEW view_name(column_name) AS SELECT * FROM
CREATE RECURSIVE VIEW
table expression). table_name;
Copyright ©2020 MakeUseOf. For more cheat sheets, head over to www.makeuseof.com