# SELECT query Structure

A SELECT query is built from keywords that must appear in a fixed order: SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY and LIMIT.

Novice · SQL · MySQL · Updated November 16, 2026

By Gregor Dzierzon

SQL is a declarative language: you describe what you need, and the database works out how to get it. The ANSI standard defines the structure of a SELECT statement. It consists of keywords that define how you can query data.

The template below shows the keywords and the order they must appear in. This chapter uses SELECT, FROM, WHERE and ORDER BY. JOIN, GROUP BY and HAVING come in later chapters, and LIMIT keeps only the first few rows of the result.

```sql
SELECT <column names>
FROM <table name>
JOIN <table name> ON <linked columns>
WHERE <filter for rows>
GROUP BY <column names>
HAVING <filter for groups>
ORDER BY <column names>
LIMIT <number of rows>
```

## Do it: put the clauses in order

A teammate wrote a query for the price list: the products that cost more than $20, most expensive first. The four clauses are all right, but they're in the wrong order, so the database refuses to run it. (`DESC` after a column in `ORDER BY` sorts from the highest value down.)

The `products` table (the **Tables** tab shows the SQL that makes it) has these rows:

| product_name | unit_price |
|---|---|
| Chai | 18 |
| Chang | 19 |
| Aniseed Syrup | 10 |
| Chef Anton's Cajun Seasoning | 22 |
| Grandma's Boysenberry Spread | 25 |
| Uncle Bob's Organic Dried Pears | 30 |
| Northwoods Cranberry Sauce | 40 |
| Mishi Kobe Niku | 97 |

Once the clauses are in order, the query gives exactly these rows, in this order:

| product_name | unit_price |
|---|---|
| Mishi Kobe Niku | 97 |
| Northwoods Cranberry Sauce | 40 |
| Uncle Bob's Organic Dried Pears | 30 |
| Grandma's Boysenberry Spread | 25 |
| Chef Anton's Cajun Seasoning | 22 |

**Your job:** put the query's four clauses in the order SQL needs, so it runs.

- Keep each clause as it is: `SELECT product_name, unit_price`, `FROM products`, `WHERE unit_price > 20` and `ORDER BY unit_price DESC`.
- Only their order changes.

Practiced live on the lesson page: write the SQL right there and check it.

## Cheat sheets

- [The SELECT Statement](https://www.ask-codey.com/cheat-sheets/the-select-statement-an-overview-cheat-sheet)

Source: https://www.ask-codey.com/lessons/the-select-statement-select-query-structure
