SQL Cheat Sheet
SQL Cheat Sheet
A. SELECT
A common use is to select data from the tables located in a database. Immediately, we see two
keywords: we need to SELECT information FROM a table.
SELECT column_name
FROM table_name
To illustrate the above example, assume that we have the following table:
Table Store_Information
SELECT Store_name
FROM Store_Information
Result:
Store names
============
Los Angeles
San Diego
Los Angeles
Boston
1
B. DISTINCT
The SELECT keyword allows us to grab all information from a column (or columns) on a table. This,
of course, necessarily means that there will be duplicates. What if we only want to select each
DISTINCT element? All we need to do is to add DISTINCT after SELECT.
Table Store_Information
We key in:
Result:
Store names
============
Los Angeles
San Diego
Boston
2
C. WHERE
This is used to conditionally select the data from a table. For example, we may want to only
retrieve stores with sales above $1,000. To do this, we use the WHERE keyword.
SELECT column_name
FROM table_name
WHERE condition
For example, to select all stores with sales above $1,000 in Table Store_Information,
Table Store_Information
Result:
Store name
===========
Los Angeles
SELECT column_name
FROM table_name
WHERE simple condition
AND/OR simple condition
(AND NOT simple condition)
e.g. WHERE Sales > 1000 AND NOT Store_Name = ‘Los Angeles’
C2. In
SELECT column_name
FROM table_name
WHERE column_name IN ('value1', 'value2', ...)
3
OR WHERE Store_name = ‘Boston’ OR Store_name = ‘NYC’
….etc
C3. Between
SELECT column_name
FROM table_name
WHERE column_name BETWEEN 'value1' AND 'value2'
C4. Like
* for Access
SELECT column_name
FROM table_name
WHERE column_name LIKE ‘part of word%’
SELECT column_name
FROM table_name
ORDER BY column_name ASC/DESC
ASC means that the results will be shown in ascending order, and DESC means that the results will
be shown in descending order. If neither is specified, the default is ASC.
SELECT column_name
FROM table_name
WHERE condition
ORDER BY column_name ASC/DESC
E. INSERT INTO
To add records to a table
The syntax for inserting data into a table one row at a time is as follows:
4
Assuming that we have a table that has the following structure:
Table Store_Information
We now wish to insert one additional row into the table representing the sales data for Los
Angeles on January 10, 1999. On that day, this store had $900 in sales.
This will add a new record (row) at the end of the table.
F. DELETE FROM
To delete data from a table (usually used with a WHERE clause)
Sometimes we may wish to get rid of records from a table. To do so, we can use the DELETE FROM
command. The syntax for this is
5
It is easiest to use an example. Say we currently have a table as below:
Table Store_Information
Table Store_Information
6
G. UPDATE & SET
Once there's data in the table, we might find that there is a need to modify the data.
The syntax for this is:
UPDATE table_name
SET column_1 = new value
WHERE condition
Table Store_Information
We notice that the sales for Los Angeles on 01/08/1999 is actually $500 instead of $300, and that
particular entry needs to be updated.
Table Store_Information
Store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Las Angeles $500 Jan-08-1999
Boston $700 Jan-08-1999
7
For example, say we currently have a table as below:
Table Store_Information
A new field called Total must be created that contains the sales + tax. For example, Los Angeles’
total would be $1650 ($1500+$150).
This generates a field called Total which holds a calculated value of the two columns added
together.
Another example:
Table Learner_Table
A new field called Term_Mark must be calculated. Term_Mark is calculated by adding the three
marks together and dividing them by 3.
8
The resulting printout would look like this:
Table Learner_Table
A new field called Term_Mark must be calculated. Term_Mark is calculated by adding the three
marks together and dividing them by 3 and rounding the answer off to 2 decimals.
SELECT * ,
ROUND ((Mark1 + Mark2 + Mark3) / 3 , 2 )
AS Term_Mark
FROM Learner_Table
If the answer had to be converted to an integer (int), we would have used the INT function which
throws away any decimals.
For example:
SELECT * ,
Int((Mark1 + Mark2 + Mark3) / 3)
AS Term_Mark
FROM Learner_Table
9
J. Summative functions: SUM, AVG and COUNT
The syntax for using summative functions is:
For example, if we want to get the sum of all sales from the following table:
Table Store_Information
Total
$2750
If we want to get the average of all sales from the same table, we’d type in:
Another arithmetic function is COUNT. This allows us to COUNT up the number of row in a certain
table. The syntax is:
SELECT Count(column_name)
FROM table_name
For example, if we want to find the number of store entries in our table, Table Store_Information
we'd key in:
SELECT Count(Store_name)
FROM Store_Information
SELECT Count(*)
AS row_count
FROM Store_Information
10
COUNT and DISTINCT can be used together in a statement to fetch the number of distinct entries
in a table. For example, if we want to find out the number of distinct stores, we'd type:
GROUP BY is always used in conjunction with Summative Functions like SUM or AVERAGE.
SELECT column_name1,
SUM(column_name2) AS new_field_name
FROM table_name
GROUP BY column_name1
Table Store_Information
We want to find total sales (stored in Total_sales) for each store. To do so, we would key in:
11
L. Date functions: YEAR, MONTH and DAY
Dates are stored as follows in a database that uses SQL:
YYYY-MM-DD HH:mm:SSss
(Year, Month, Day, Hour, Minute, Second and Milliseconds)
For example:
2007-02-17 00:00:0070
For the following table, you would like to access all the records with a date in a certain month
(May).
Table Store_Information
SELECT Store_Name
FROM Store_Information
WHERE MONTH(Sale_Date) = 5
Note: Days and months are all given numerically (e.g. March = 3, Tuesday = 2, etc.)
e.g SELECT *
FROM Store_Name
WHERE Sale_date > # 12/12/1998 #
12
M. WHERE (revisited) – creating the relationship
Sometimes we want to access two (or more) tables in the same SQL statement.
We want to output a list of the store names against the supplier names.
To do so, we would key in:
13