How to Open SQL View in Access
1
Click Create in the ribbon β click Query Design
2
A "Show Table" dialog appears β click Close (don't add any tables)
3
Click the View dropdown in the top-left of the ribbon β select SQL View
4
Type your SQL statement in the box. Click Run (the red exclamation mark !) to execute.
5
To write a new query: go to View β SQL View again. Select all text (
Ctrl+A) and delete it. Type your new statement.
β οΈ Access SQL quirks: Use double-quotes around text values (e.g.,
"Pop"). The wildcard character is * (not %). LIKE uses * for "any characters".
SELECT β Choose Columns
Choose which fields (columns) to show. Use * for all fields.
SELECT * FROM Songs;
SELECT Title, Artist FROM Songs;
β Result (SELECT Title, Artist)
| Title | Artist |
|---|---|
| Neon Skyline | The Midnight |
| Bad Habits | Ed Sheeran |
| β¦ | β¦ |
WHERE β Filter Rows
Only return rows that match the condition.
SELECT * FROM Songs
WHERE Genre = "Pop";
SELECT * FROM Songs
WHERE Streams > 5000000;
β Only rows where Genre = "Pop"
| Title | Genre | Streams |
|---|---|---|
| Blinding Lights | Pop | 12000000 |
| Bad Habits | Pop | 9800000 |
ORDER BY β Sort Results
ASC = AβZ or lowestβhighest. DESC = ZβA or highestβlowest.
SELECT Title, Streams
FROM Songs
ORDER BY Streams DESC;
SELECT * FROM Songs
ORDER BY Title ASC;
AND / OR β Combine Conditions
AND = both must be true. OR = either can be true.
-- AND: Pop songs with 1M+ streams
SELECT * FROM Songs
WHERE Genre = "Pop"
AND Streams > 1000000;
-- OR: Pop OR Rock songs
SELECT * FROM Songs
WHERE Genre = "Pop"
OR Genre = "Rock";
LIKE β Wildcard Matching
Use * to match any characters. Use ? for exactly one character.
-- Starts with "The"
SELECT * FROM Songs
WHERE Title LIKE "The*";
-- Contains "rain"
SELECT * FROM Songs
WHERE Title LIKE "*rain*";
BETWEEN β Range Filter
Shorthand for >= AND <=. Both end values are included.
SELECT * FROM Songs
WHERE ReleaseYear
BETWEEN 2019 AND 2021;
SELECT * FROM Songs
WHERE DurationMins
BETWEEN 3 AND 4;
Putting It All Together
You can combine SELECT, WHERE, ORDER BY in one statement. The order always goes: SELECT β FROM β WHERE β ORDER BY.
-- Pop songs with 1M+ streams, sorted by streams (most first)
SELECT Title, Artist, Streams
FROM Songs
WHERE Genre = "Pop"
AND Streams > 1000000
ORDER BY Streams DESC;
| Keyword | What It Does | Required? |
|---|---|---|
| SELECT | Choose which fields to show | β Always first |
| FROM | Specify which table to query | β Always second |
| WHERE | Filter which rows to return | Optional |
| AND / OR | Combine multiple conditions | Optional |
| ORDER BY | Sort the results | Optional (always last) |
Common Mistakes to Avoid
β Wrong wildcard
WHERE Title LIKE "The%" β wrong!
Access uses * not %. Use "The*"
β Single quotes
WHERE Genre = 'Pop' β wrong!
Access needs double-quotes: "Pop"
β Wrong keyword order
WHERE β¦ FROM β¦ β wrong!
Always: SELECT β FROM β WHERE β ORDER BY
β Missing semicolon
SELECT * FROM Songs β missing ;
End every statement with a semicolon ;