Understanding SQL Statements

Learn the language underneath every query, and read a SELECT statement well enough to know what a query actually does.

By the end of this lesson you can

  • Explain what SQL is and where it runs
  • Read and write a basic SELECT statement
  • Filter rows with WHERE and order results with ORDER BY
  • Switch between Access's Design view and SQL view for the same query
📘 Reading Lesson

Lesson Notes

Read through the key concepts before you try the challenge.

The language behind the grid

On the job

You maintain the practice database at Lakeside Medical Associates.

A query built by a previous employee returns the wrong patients, and its Design view grid spans four tables with criteria scattered across a dozen columns. Read as SQL it is eight lines, and the error — a filter on the wrong table — is visible in about twenty seconds.

Your task: Learn to read SQL, because it is often the fastest way to understand what a query is doing.

SQL — Structured Query Language — is the standard language for asking questions of a relational database. Access's Design view is a visual builder that writes SQL for you. Every query you build in the grid exists as a SQL statement, viewable at any time through View > SQL View.

Learning to read it is worth the effort for two reasons. Complex queries are far easier to understand as text than as a sprawling grid, and SQL transfers: the same fundamentals work in SQL Server, MySQL, PostgreSQL, and every other relational database, so this is one of the most portable skills in the course.

Key terms

SELECT
Names which fields you want returned.
FROM
Names the table or tables the data comes from.
WHERE
Filters which rows are returned.
ORDER BY
Sorts the results. ASC is ascending, DESC descending.
JOIN
Combines rows from two tables using a matching value, normally a primary key and its foreign key.
GROUP BY
Collapses rows into groups so aggregate functions like COUNT and SUM can be applied per group.
SELECT LastName, FirstName, DateOfBirth
FROM Patients
WHERE City = 'Brooklyn'
ORDER BY LastName ASC;

Read that as a sentence: return the last name, first name, and date of birth, from the Patients table, for rows where the city is Brooklyn, sorted by last name. The order of the clauses is fixed — SELECT, FROM, WHERE, ORDER BY — and Access will reject a statement that puts them in another order.

OperatorMeansExample
=Equal toWHERE City = 'Brooklyn'
<> Not equal toWHERE Status <> 'Inactive'
> < >= <=ComparisonWHERE Quantity < 10
BETWEENWithin a range, inclusiveWHERE VisitDate BETWEEN #1/1/2026# AND #3/31/2026#
LIKEPattern matchWHERE LastName LIKE 'Sm*'
INMatches any value in a listWHERE Category IN ('Medical','Nursing')
IS NULLField is emptyWHERE Phone IS NULL
AND / ORCombine conditionsWHERE City = 'Brooklyn' AND Status = 'Active'
Operators used in WHERE
Microsoft 365 & Office 2024Access uses the asterisk as its wildcard in LIKE, where most other SQL databases use the percent sign. Access also wraps dates in hash marks (#1/1/2026#) where standard SQL uses quotes. If you move to SQL Server or MySQL later, these are the two differences most likely to trip you up first.
Worked example

Finding the query's actual bug

A query is meant to list active Brooklyn patients with appointments this quarter, but returns patients from every city.

  1. 1

    Open View > SQL View instead of studying the Design grid.

    Eight lines of text are far easier to reason about than a grid spanning four tables. The grid hides the logical structure in its layout; SQL states it directly.

  2. 2

    Read the WHERE clause and check which table each condition applies to.

    This is where filter bugs live. A condition written against the Appointments table's city field rather than the Patients table's will filter the wrong thing — and in the grid, the two look nearly identical because the column header shows only the field name.

  3. 3

    Check how AND and OR are combined, and whether parentheses are present.

    WHERE A AND B OR C does not mean what most people assume, because AND binds more tightly than OR. Missing parentheses around an OR group is the second most common query bug, and it produces exactly this symptom — far more rows than expected.

  4. 4

    Fix it in SQL view, then switch back to Design view to confirm.

    The two views are the same query. Editing the SQL and seeing the grid update confirms your change did what you meant, and it is a good way to build fluency in reading between the two.

Result: The filter is corrected to reference the Patients table, and the OR group is parenthesized.

When a query returns the wrong rows, read its SQL. Filter bugs and operator precedence are visible in the text and nearly invisible in the grid.

Check your understanding

Which SQL clause determines which rows are returned by a query?

Challenge

Apply what you've learned in this lesson.

Write SQL by hand before letting the grid write it for you.

  1. In your Access database, create a query in Design view, then open SQL View and copy the statement it generated. Annotate each clause with what it does.
  2. Write a SELECT statement by hand, in SQL View, that returns all supply items with a quantity below their reorder level, sorted by category. Run it.
  3. Write a statement using LIKE to find every item whose name begins with 'Glove'. Then rewrite it using IN to match three specific categories.
  4. Write a statement combining AND and OR with parentheses, then remove the parentheses and run it again. Record how many rows each returns and explain the difference.

Finished this lesson?

Progress is saved in this browser only. It is not a grade — official progress lives in Brightspace.