SQL_Summary MS Access
SQL_Summary MS Access
CHEATSHEET
1
SELECT <field(s)>
FROM <table>
ORDER BY <field1>, … , <fieldn>
//Order the records alphabetically by the first field then the next field
SELECT <field(s)>
FROM <table>
ORDER BY <field1> DESC, … , <fieldn> DESC
//Order the records alphabetically in descending order by the first field then the next field
Use a comma to separate multiple sorting fields NOT AND.
SELECT *
FROM <table>
//Show all fields of the table
SELECT <field(s)>
FROM <table>
WHERE <condition> LIKE <*pattern>
//The records where the pattern exists at the end of the value
LIKE <pattern*>
//The records where the pattern exists at the beginning of the value
LIKE <*pattern*>
//The records where the pattern exists anywhere in the value
SELECT <field(s)>
FROM <table>
WHERE <condition> AND <condition>
//The records where both the conditions are true
SELECT <field(s)>
FROM <table>
WHERE <condition> OR <condition>
//The records where either of the conditions are true
SELECT <field(s)>
FROM <table>
WHERE <field> BETWEEN <value1> AND <value2>
//The records where the field has values between the two values specified
SELECT <field(s)>
FROM <table>
WHERE <field> IN (<value1>, <value2>, …. ,<value2>)
//The records where the field has values in the square brackets. This is the same as
2
WHERE <field> = <value1> OR <field> = <value2> OR … OR <field> = <value2>
SELECT <field(s)>
FROM <table>
WHERE <field> NOT IN (<value1>, <value2>, …. ,<value2>)
//The records where the field has NONE of the values in the square brackets
SELECT <field(s)>
FROM <table>
WHERE <field> IS NULL
//The records where the field has a NULL value. Do not use =.
SELECT <fields>
FROM <table>
WHERE <condition>
GROUP BY <field>
HAVING (<condition1>, <condition2>…)
//Having is for the conditions for the new result set – i.e. use WHERE when the field is in the existing table,
use HAVING when you have GROUP BY and have created a new field.
NB* use only when using GROUP BY
Embedded Queries
SELECT Name, Salary
FROM tblTeachers
WHERE Salary =
(SELECT MAX(Salary)
FROM tblTeachers)
//Criteria for the WHERE statement is another SQL statement
3
Finding the person with the highest salary
//If you want the person who got the highest salary - assuming only ONE person got it
SELECT TOP 1 name, salary
FROM tblTeachers
ORDER BY salary DESC
//If you want the person who got the highest salary. You may have more than one person who earns the
highest salary
SELECT Name, Salary
FROM tblTeachers
WHERE Salary = (SELECT MAX(Salary) FROM tblTeachers)
4
SELECT MONTH(<field>) AS <field>
FROM <table>
//gives the month of the date
To calculate age accurately use: (NOW() – <DOB>)/365.25 where <DOB> is a field that stores the
birth date in your table.
SELECT COUNT(*)
FROM <table>
WHERE <condition>
//Counts the amount of records in the table with the condition
5
SELECT INT(<value>,<value>) AS <field>
FROM <table>
//Gives the integer value of a number after truncating the decimals.
Also FLOOR and CEILING
SELECT <field>
FROM <table>
WHERE <field> IN (<value>, <value> …)
//Same as saying <field> = <value> OR <field> = <value> …
SELECT <table>.<field>
FROM <table1>
INNER JOIN <table2> ON <table1>.<field> = <table2>.<field>
//Inner join with full format and the order of the tables do not matter
SELECT <table>.<field>
FROM <table1>
LEFT OUTER JOIN <table2> ON <table1>.<field> = <table2>.<field>
//Outer join, table 1 contains info and the second has null value
To find the fields that are only in table1 but NOT table 2:
SELECT <table>.<field>
FROM <table1>
LEFT OUTER JOIN <table2> ON <table1>.<field> = <table2>.<field>
WHERE <table1.field> IS NULL
OR
SELECT <table1>.<field>
FROM <table1>
WHERE <table1.field> NOT IN
(SELECT <table2>.<field> FROM <table2>)
SELECT <table>.<field>
FROM <table1>
RIGHT OUTER JOIN <table2> ON <table1>.<field> = <table2>.<field>
//Outer join, table 2 contains info and the first has null value
SELECT <table>.<field>
FROM <table1>
FULL JOIN <table2> ON <table1>.<field> = <table2>.<field>
//Outer join, either table has info and either can have null value