This SQL cheatsheet for backend developers covers all the essential SQL commands and syntax you need to know, including SELECT, INSERT, UPDATE, DELETE, JOIN, and more. It's a quick and easy reference guide for anyone who wants to learn or brush up on their SQL skills.
SQL: Creating, Updating, and Deleting Data
Column constraints are the rules applied to the values of individual columns:
PRIMARY KEY
constraint can be used to uniquely identify the row.UNIQUE
columns have a different value for every row.NOT NULL
columns must have a value.DEFAULT
assigns a default value for the column when no value is specified.There can be only one PRIMARY KEY
column per table and multiple UNIQUE
columns.
CREATE TABLE student (
id INTEGER PRIMARY KEY,
name TEXT UNIQUE,
grade INTEGER NOT NULL,
age INTEGER DEFAULT 10
);
CREATE TABLE
StatementThe CREATE TABLE
statement creates a new table in a database. It allows one to specify the name of the table and the name of each column in the table.
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype
);
INSERT
StatementThe INSERT INTO
statement is used to add a new record (row) to a table.
It has two forms as shown:
-- Insert into columns in order:
INSERT INTO table_name
VALUES (value1, value2);
-- Insert into columns by name:
INSERT INTO table_name (column1, column2)
VALUES (value1, value2);
ALTER TABLE
StatementThe ALTER TABLE
statement is used to modify the columns of an existing table. When combined with the ADD COLUMN
clause, it is used to add a new column.
ALTER TABLE table_name
ADD column_name datatype;
DELETE
StatementThe DELETE
statement is used to delete records (rows) in a table. The WHERE
clause specifies which record or records that should be deleted. If the WHERE
clause is omitted, all records will be deleted.
DELETE FROM table_name
WHERE some_column = some_value;
UPDATE
StatementThe UPDATE
statement is used to edit records (rows) in a table. It includes a SET
clause that indicates the column to edit and a WHERE
clause for specifying the record(s).
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE some_column = some_value;
Columns of a PostgreSQL database table must have a data type, which constrains the type of information that can be entered into that column. This is important in order to ensure data integrity and consistency over time. Some common PostgreSQL types are integer
, decimal
, varchar
, and boolean
. Data types are defined in a CREATE TABLE
statement by indicating the data type after each column name.
CREATE TABLE tablename (
myNum integer,
myString varchar(50)
);
When using PostgreSQL, it can be important to enforce check constraints on columns of a database table in order to ensure data integrity and consistency over time. Check constraints can be enforced on a single column, multiple columns, or on all columns. They are implemented within a CREATE TABLE
statement using the CHECK
keyword.
CREATE TABLE table_name (
column_1 integer,
column_2 text,
column_3 numeric CHECK (column_3 > 0),
column_4 numeric CHECK (column_4 > 0),
CHECK (column_3 > column_4)
);
Columns in a database table can have multiple constraints. Multiple constraints can be implemented by listing them in a row following the relevant column name and data type in a CREATE TABLE
statement. The order of the constraints does not matter.
CREATE TABLE table_name (
column_1 integer NOT NULL CHECK (column_3 > 0),
column_2 text UNIQUE NOT NULL,
column_3 numeric
);
In PostgreSQL, NOT NULL
constraints can be used to ensure that particular columns of a database table do not contain missing data. This is important for ensuring database integrity and consistency over time. NOT NULL
constraints can be enforced within a CREATE TABLE
statement using NOT NULL
.
CREATE TABLE table_name (
column_1 integer NOT NULL,
column_2 text NOT NULL,
column_3 numeric
);
In PostgreSQL, UNIQUE
constraints can be used to ensure that elements of a particular column (or group of columns) are unique (i.e., no two rows have the same value or combination of values). This is important for ensuring database integrity and consistency over time. UNIQUE
constraints can be enforced within a CREATE TABLE
statement using the UNIQUE
keyword.
CREATE TABLE table_name (
column_1 integer UNIQUE,
column_2 text UNIQUE,
column_3 numeric,
column_4 text,
UNIQUE(column_3, column_4)
);
In PostgreSQL, a primary key constraint indicates that a particular column (or group of columns) in a database table can be used to identify a unique row in that table. In terms of restrictions, this is equivalent to a UNIQUE NOT NULL
constraint; however, a table may only have one primary key, whereas multiple columns can be constrained as UNIQUE NOT NULL
. A primary key constraint can be enforced within a CREATE TABLE
statement using PRIMARY KEY
-- A primary key on one column
CREATE TABLE table_name (
column_1 integer PRIMARY KEY,
column_2 text,
);
-- A composite primary key
CREATE TABLE table_name (
column_1 integer,
column_2 text,
column_2 integer,
PRIMARY KEY (column_1, column_2),
);
In PostgreSQL, a foreign key constraint ensures that the values in a particular column of a database table exactly match values in another database table. This is important to ensure the “referential integrity” of the database. A foreign key constraint can be enforced within a CREATE TABLE
statement either by adding REFERENCES other_table_name (other_table_primary_key)
after the relevant column name and type or using FOREIGN KEY (column_1, column_2) REFERENCES other_table_name (other_key1, other_key2)
to indicate a link between groups of columns.
CREATE TABLE table_1 (
column_1 integer PRIMARY KEY,
column_2 text,
column_3 numeric
);
-- Option 1
CREATE TABLE table_2 (
column_a integer PRIMARY KEY,
column_b integer REFERENCES table_1 (column_2),
column_c integer
);
-- Option 2 - Creating columns b and c are a composite foreign key referencing columns c1 and c2 from other_table
CREATE TABLE t1 (
a integer PRIMARY KEY,
b integer,
c integer,
FOREIGN KEY (b, c) REFERENCES other_table (c1, c2)
);
In PostgreSQL, when implementing foreign key constraints in a database table, it is possible to preemptively specify database behavior when values in a referenced column are updated or deleted. Specifying ON DELETE RESTRICT
or ON UPDATE RESTRICT
after a foreign key constraint ensures that referenced values/rows cannot be deleted/updated.
Specifying ON DELETE CASCADE
or ON UPDATE CASCADE
ensures that updated/deleted values/rows in the referenced table are automatically updated/deleted in the referencing table.
CREATE TABLE products (
product_no integer PRIMARY KEY,
name text,
price numeric
);
CREATE TABLE orders (
order_id integer PRIMARY KEY,
shipping_address text,
);
CREATE TABLE order_items (
product_no integer REFERENCES products ON DELETE RESTRICT,
order_id integer REFERENCES orders ON DELETE CASCADE,
quantity integer,
PRIMARY KEY (product_no, order_id)
);
In PostgreSQL, when implementing a constraint on an existing table, the table must already be consistent with the constraint or PostgreSQL will reject the new constraint. A DB user may backfill the table using UPDATE
or ALTER TABLE
statements to make the table consistent with the constraint.
CREATE TABLE products (
product_no integer PRIMARY KEY,
name text,
price numeric,
sale_price numeric
);
--Assume some values in `sale_price` are missing, and we'd like to apply a `NOT NULL` constraint on `sale_price`. We must `UPDATE` the table before applying our constraint.
UPDATE TABLE products
SET sale_price = 0 WHERE sale_price IS NULL
SQL Queries
AND
OperatorThe AND
operator allows multiple conditions to be combined. Records must match both conditions that are joined by AND
to be included in the result set. The given query will match any car that is blue and made after 2014.
SELECT model
FROM cars
WHERE color = 'blue'
AND year > 2014;
AS
ClauseColumns or tables can be aliased using the AS
clause. This allows columns or tables to be specifically renamed in the returned result set. The given query will return a result set with the column for name
renamed to movie_title
.
SELECT name AS 'movie_title'
FROM movies;
OR
OperatorThe OR
operator allows multiple conditions to be combined. Records matching either condition joined by the OR
are included in the result set. The given query will match customers whose state is either 'CA'
or 'NY'
.
SELECT name
FROM customers
WHERE state = 'CA'
OR state = 'NY';
%
WildcardThe %
wildcard can be used in a LIKE
operator pattern to match zero or more unspecified character(s). The given query will match any movie that begins with The
, followed by zero or more of any characters.
SELECT name
FROM movies
WHERE name LIKE 'The%';
SELECT
StatementThe SELECT *
statement returns all columns from the provided table in the result set. The given query will fetch all columns and records (rows) from the movies
table.
SELECT *
FROM movies;
_
WildcardThe _
wildcard can be used in a LIKE
operator pattern to match any single unspecified character. The given query will match any movie which begins with a single character, followed by ove
.
SELECT name
FROM movies
WHERE name LIKE '_ove';
ORDER BY
ClauseThe ORDER BY
clause can be used to sort the result set by a particular column either alphabetically or numerically. It can be ordered in two ways:
DESC
is a keyword used to sort the results in descending order.ASC
is a keyword used to sort the results in ascending order (default).SELECT *
FROM contacts
ORDER BY birth_date DESC;
LIKE
OperatorThe LIKE
operator can be used inside of a WHERE
clause to match a specified pattern. The given query will match any movie that begins with Star
in its title.
SELECT name
FROM movies
WHERE name LIKE 'Star%';
DISTINCT
ClauseUnique values of a column can be selected using a DISTINCT
query. For a table contact_details
having five rows in which the city
column contains Chicago, Madison, Boston, Madison, and Denver, the given query would return:
Chicago
Madison
Boston
Denver
SELECT DISTINCT city
FROM contact_details;
BETWEEN
OperatorThe BETWEEN
operator can be used to filter by a range of values. The range of values can be text, numbers, or date data. The given query will match any movie made between the years 1980 and 1990, inclusive.
SELECT *
FROM movies
WHERE year BETWEEN 1980 AND 1990;
LIMIT
ClauseThe LIMIT
clause is used to narrow, or limit, a result set to the specified number of rows. The given query will limit the result set to 5 rows.
SELECT *
FROM movies
LIMIT 5;
NULL
ValuesColumn values can be NULL
, or have no value. These records can be matched (or not matched) using the IS NULL
and IS NOT NULL
operators in combination with the WHERE
clause. The given query will match all addresses where the address has a value or is not NULL
.
SELECT address
FROM records
WHERE address IS NOT NULL;
WHERE
ClauseThe WHERE
clause is used to filter records (rows) that match a certain condition. The given query will select all records where the pub_year
equals 2017
.
SELECT title
FROM library
WHERE pub_year = 2017;
Performing SQL Calculations
The GROUP BY
and ORDER BY
clauses can reference the selected columns by number in which they appear in the SELECT
statement. The example query will count the number of movies per rating, and will:
GROUP BY
column 2
(rating
)ORDER BY
column 1
(total_movies
)SELECT COUNT(*) AS 'total_movies',
rating
FROM movies
GROUP BY 2
ORDER BY 1;
SUM()
Aggregate FunctionThe SUM()
aggregate function takes the name of a column as an argument and returns the sum of all the value in that column.
SELECT SUM(salary)
FROM salary_disbursement;
MAX()
Aggregate FunctionThe MAX()
aggregate function takes the name of a column as an argument and returns the largest value in a column. The given query will return the largest value from the amount
column.
SELECT MAX(amount)
FROM transactions;
COUNT()
Aggregate FunctionThe COUNT()
aggregate function returns the total number of rows that match the specified criteria. For instance, to find the total number of employees who have less than 5 years of experience, the given query can be used.
Note: A column name of the table can also be used instead of *
. Unlike COUNT(*)
, this variation COUNT(column)
will not count NULL
values in that column.
SELECT COUNT(*)
FROM employees
WHERE experience < 5;
GROUP BY
ClauseThe GROUP BY
clause will group records in a result set by identical values in one or more columns. It is often used in combination with aggregate functions to query information of similar records. The GROUP BY
clause can come after FROM
or WHERE
but must come before any ORDER BY
or LIMIT
clause.
The given query will count the number of movies per rating.
SELECT rating,
COUNT(*)
FROM movies
GROUP BY rating;
MIN()
Aggregate FunctionThe MIN()
aggregate function returns the smallest value in a column. For instance, to find the smallest value of the amount
column from the table named transactions
, the given query can be used.
SELECT MIN(amount)
FROM transactions;
AVG()
Aggregate FunctionThe AVG()
aggregate function returns the average value in a column. For instance, to find the average salary
for the employees who have less than 5 years of experience, the given query can be used.
SELECT AVG(salary)
FROM employees
WHERE experience < 5;
HAVING
ClauseThe HAVING
clause is used to further filter the result set groups provided by the GROUP BY
clause. HAVING
is often used with aggregate functions to filter the result set groups based on an aggregate property. The given query will select only the records (rows) from only years where more than 5 movies were released per year.
The HAVING
clause must always come after a GROUP BY
clause but must come before any ORDER BY
or LIMIT
clause.
SELECT year,
COUNT(*)
FROM movies
GROUP BY year
HAVING COUNT(*) > 5;
Aggregate functions perform a calculation on a set of values and return a single value:
COUNT()
SUM()
MAX()
MIN()
AVG()
ROUND()
FunctionThe ROUND()
function will round a number value to a specified number of places. It takes two arguments: a number, and a number of decimal places. It can be combined with other aggregate functions, as shown in the given query. This query will calculate the average rating of movies from 2015, rounding to 2 decimal places.
Aggregate Functions
Aggregate functions perform a calculation on a set of values and return a single value:
COUNT()
SUM()
MAX()
MIN()
AVG()
ROUND() Function
The ROUND() function will round a number value to a specified number of places. It takes two arguments: a number, and a number of decimal places. It can be combined with other aggregate functions, as shown in the given query. This query will calculate the average rating of movies from 2015, rounding to 2 decimal places.
Working with Multiple SQL Tables
An outer join will combine rows from different tables even if the join condition is not met. In a LEFT JOIN
, every row in the left table is returned in the result set, and if the join condition is not met, then NULL
values are used to fill in the columns from the right table.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
WITH
ClauseThe WITH
clause stores the result of a query in a temporary table (temporary_movies
) using an alias.
Multiple temporary tables can be defined with one instance of the WITH
keyword.
WITH temporary_movies AS (
SELECT *
FROM movies
)
SELECT *
FROM temporary_movies
WHERE year BETWEEN 2000 AND 2020;
UNION
ClauseThe UNION
clause is used to combine results that appear from multiple SELECT
statements and filter duplicates.
For example, given a first_names
table with a column name
containing rows of data “James” and “Hermione”, and a last_names
table with a column name
containing rows of data “James”, “Hermione” and “Cassidy”, the result of this query would contain three name
s: “Cassidy”, “James”, and “Hermione”.
SELECT name
FROM first_names
UNION
SELECT name
FROM last_names
CROSS JOIN
ClauseThe CROSS JOIN
clause is used to combine each row from one table with each row from another in the result set. This JOIN
is helpful for creating all possible combinations for the records (rows) in two tables.
The given query will select the shirt_color
and pants_color
columns from the result set, which will contain all combinations of combining the rows in the shirts
and pants
tables. If there are 3 different shirt colors in the shirts
table and 5 different pants colors in the pants
table then the result set will contain 3 x 5 = 15 rows.
SELECT shirts.shirt_color,
pants.pants_color
FROM shirts
CROSS JOIN pants;
A foreign key is a reference in one table’s records to the primary key of another table. To maintain multiple records for a specific row, the use of foreign key plays a vital role. For instance, to track all the orders of a specific customer, the table order
(illustrated at the bottom of the image) can contain a foreign key.
A primary key column in a SQL table is used to uniquely identify each record in that table. A primary key cannot be NULL
. In the example, customer_id
is the primary key. The same value cannot re-occur in a primary key column. Primary keys are often used in JOIN
operations.
The JOIN
clause allows for the return of results from more than one table by joining them together with other results based on common column values specified using an ON
clause. INNER JOIN
is the default JOIN
and it will only return results matching the condition specified by ON
.
SELECT *
FROM books
JOIN authors
ON books.author_id = authors.id;
#sql #database