SQL Cheat Sheet
by Rahil (rahilkasimi) via [Link]/102433/cs/32112/
COMMANDS
COMMAND CODE DESCRIPTION
Create CREATE DATABASE <DATABASE NAME> used to create a new database or table
CREATE TABLE <TABLE NAME>
Drop DROP DATABASE <DATABASE NAME> used to delete an existing database or table
DROP TABLE <TABLE NAME>
Truncate TRUNCATE TABLE <TABLE NAME> used to delete information in the table but doesn
t delete the table itself
Alter ALTER TABLE <TABLE NAME> used to delete, add or modify constraints or
ADD <COLUMN NAME> <DATA TYPE> columns in a table
ALTER TABLE <TABLE NAME>
DROP COLUMN <COLUMN NAME>
ALTER TABLE <TABLE NAME>
ALTER COLUMN <COLUMN NAME> <DATA TYPE>
Backup BACKUP DATABASE <DATABASE NAME> used to create a backup on an existing database
TO DISK = ‘<PATH>’
Insert INSERT INTO <TABLE NAME> (<COLUMN1>, ....) used to insert new tuples (rows) in a table
VALUES (<VALUE1>, ....) *you do not need to specify all columns if you
will add values for all the columns
Delete DELETE FROM <TABLE NAME> used to delete tuples (rows) from a table
WHERE <CONDITION> *if you don t add the WHERE clause, all rows
will be deleted
Update UPDATE <TABLE NAME> used to modify existing records in a table
SET <COLUMN NAME> = <NEW VALUE>
WHERE <CONDITION>
Select SELECT <ATTRIBUTE LIST> used to select data from a table
FROM <TABLE NAME> *if you want all attributes of a table use (*)
WHERE <CONDITION>
Union, Intersect, <FIRST SELECT STATEMENT> equivalent to the set operations: union, inters‐
Except UNION / INTERSECT / EXCEPT ection and difference.
<SECOND SELECT STATEMENT>
In SELECT <ATTRIBUTE LIST> compares a value with a set of values, returns
FROM <TABLE NAME> true if the value is one of the elements of the set.
WHERE <VALUE> IN <ANOTHER SELECT QUERY>
Null <ATTRIBUTE NAME> IS (NOT) NULL used to check whether a value is NULL
Join SELECT <ATTRIBUTES LIST> used to join two tables based on a related
FROM <TABLE 1> JOIN <TABLE 2> column between them
ON <JOIN CONDITION>
WHERE <SELECTION CONDITION>
By Rahil (rahilkasimi) Published 17th May, 2022. Sponsored by [Link]
[Link]/rahilkasimi/ Last updated 17th May, 2022. Measure your website readability!
Page 1 of 3. [Link]
SQL Cheat Sheet
by Rahil (rahilkasimi) via [Link]/102433/cs/32112/
COMMANDS (cont)
Assertion CREATE ASSERTION <ASSERTION NAME> used to ensure a certain condition is always met
CHECK (<CONDITION>) in the database
Trigger CREATE TRIGGER <TRIGGER NAME> Triggers are activated when a defined action is
BEFORE / AFTER executed for the table
INSERT / UPDATE / DELETE
ON <TABLE NAME>
FOR EACH ROW
<TRIGGER BODY>
Data Types Numeric - INT, SMALLINT, DECIMAL(i, j)
String - CHAR, CHAR(n), VARCHAR(n)
Bit Sring - BIT, BIT(n)
Date and Time - DATE, TIME, TIME(i)
Timestamp -TIMESTAMP
Referential ON DELETE <OPTION> used to set what happens on updating or
Triggered Action ON UPDATE <OPTION> deleting a tuple (row) in the database that
references another row
OPTIONS:
SET NULL
SET DEFAULT
CASCADE
Renaming <TABLE NAME> AS <NEW TABLE NAME> Relation and attribute names can be renamed
(Aliasing) (<NEW ATTRIBUTE 1 NAME>, .....) for conenience or to remove ambiguity using the
keyword AS
Cross Product (,) SELECT <ATTRIBUTE LIST> used to produce a result table that has the
FROM <TABLE 1>, <TABLE 2> number of rows of the first table multiplied by the
number of rows of the second table
Duplicates SELECT ALL <ATTRIBUTE LIST> DISTINCT is used to eliminate duplicates
FROM <TABLE NAME> <ATTRIBUTE> LIKE <PATTERN> <AT‐ ALL is used to allow duplicates *SELECT
TRIBUTE NAME> IS (NOT) NULL without ALL or DISTINCT is equivalent to ALL
SELECT <ATTRIBUTES LIST>
FROM <TABLE 1> JOIN <TABLE 2> ON <JOIN CONDITION>
WHERE <SELECTION CONDITION> SELECT DISTINCT <AT‐
TRIBUTE LIST> FROM <TABLE NAME>
String Compar‐ <ATTRIBUTE> LIKE <PATTERN> LIKE is used for string comparisoJ
isons (%) replaces an arbitary number of characters
(_) replaces one character
By Rahil (rahilkasimi) Published 17th May, 2022. Sponsored by [Link]
[Link]/rahilkasimi/ Last updated 17th May, 2022. Measure your website readability!
Page 2 of 3. [Link]
SQL Cheat Sheet
by Rahil (rahilkasimi) via [Link]/102433/cs/32112/
COMMANDS (cont)
Arithmetic # (+) add # (*) multiply
Operators # ( ) subtract # (/) divide
Ordering <SELECT STATEMENT> ORDER BY is used to order the resulting tuples
ORDER BY <ATTRIBUTE> <ASC / DESC> The keyword ASC (ascending) and DESC can
be used. *The default is ASC (ascending)
Set Comparisons SELECT <ATTRIBUTE LIST> ANY and ALL can be used with (=, >, >=, <, <=,
FROM <TABLE NAME> <>) to compare a value with a set
WHERE <VALUE> > ALL / ANY <ANOTHER SELECT QUERY> #CONTAINS Compares two sets and returns
true if one set contains the other
#EXISTS It checks whether the result of a
nested query is empty or not
#UNIQUE checks if the table has duplicates
Aggregate #COUNT - Counts how many rows in a
Functions particular column
#SUM - adds together all the values in a
particular column
#MIN returns the minumum value in a column
#MAX returns the maximum value in a column
#AVG - returns the average of a group of
selected values
Types of Join Inner join
Left Join
Right Join
Full Outer Join
Credit: @yosracodes
By Rahil (rahilkasimi) Published 17th May, 2022. Sponsored by [Link]
[Link]/rahilkasimi/ Last updated 17th May, 2022. Measure your website readability!
Page 3 of 3. [Link]