CodeRang
Where to start

SQL Query Examples and Structure

SQL query examples are easiest to understand when each clause solves a visible part of a problem. In this example, we connect customers to orders, keep paid orders, calculate totals, filter grouped results, and sort the final rows.

Updated · SQL Arena team

What you will practice

  • Selecting columns with SELECT
  • Choosing data sources with FROM
  • Filtering rows with WHERE
  • Grouping and filtering aggregates
  • Sorting deterministic results

A practice problem in the trainer

This is how a problem looks in SQL Arena: the task, the editor, the result and the database schema.

PrevNextRandom
TaskSubmissionsAI Mentor
Find customers with at least 1400 in paid orders

Use the customers and orders tables. For each customer, count paid orders and calculate their total value. Return the customer name as name, the count as paid_orders, and the sum as total_amount. Keep totals of at least 1400 and sort from the highest total to the lowest.

Query 1PostgreSQLv16
SELECT c.name,
       COUNT(o.id) AS paid_orders,
       SUM(o.amount) AS total_amount
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'paid'
GROUP BY c.id, c.name
HAVING SUM(o.amount) >= 1400
ORDER BY total_amount DESC;
Submit
Resultcustomersorders
Answer accepted

How to build this SQL query

Start with the output rather than the syntax. We need a customer name, a count, and a sum, so SELECT contains c.name, COUNT(o.id), and SUM(o.amount). The values come from two tables. JOIN connects an order to its customer with o.customer_id = c.id. A join condition matters because without it, unrelated customer and order rows can be combined.

WHERE removes cancelled orders before aggregation. After that, GROUP BY creates one group per customer. HAVING works on those groups and keeps only customers whose SUM(o.amount) reaches 1400. This is the practical distinction between WHERE and HAVING: WHERE filters source rows, while HAVING filters grouped results.

SQL is written as SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY, but that is not its logical execution order. A useful simplified model is FROM and JOIN first, then WHERE, GROUP BY, HAVING, SELECT, and ORDER BY. Understanding this sequence explains why an aggregate such as SUM cannot normally be used directly in WHERE.

SQLPostgreSQL v16
SELECT c.name,
       COUNT(o.id) AS paid_orders,
       SUM(o.amount) AS total_amount
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'paid'
GROUP BY c.id, c.name
HAVING SUM(o.amount) >= 1400
ORDER BY total_amount DESC;
Query result
namepaid_orderstotal_amount
Mark13500.00
Elena12700.00
Anna22000.00
Nina11600.00
Oleg21400.00

Common SQL query mistakes

01

Using an aggregate in WHERE

WHERE SUM(o.amount) >= 1400 is conceptually too early because the groups have not been calculated yet. Conditions on aggregate results belong in HAVING.

02

Joining without the relationship

A missing or incorrect ON condition can multiply rows and produce inflated counts and sums. The query may still run, which makes this bug easy to miss unless you inspect the data.

03

Memorizing syntax without testing it

Reading SQL query examples helps, but it does not show whether you can adapt the pattern to different data. Practice is more useful when the same logic is checked against unseen rows.

Practice problems on this topic

SQL from scratch course

If you want to learn SQL queries, move from reading examples to writing them. SQL Arena contains 1,192 auto-checked tasks with separate tables for each problem. Solutions are checked against hidden data rather than a fixed query template, so a query must express the correct logic. Practice is available in PostgreSQL, MySQL, ClickHouse, and BigQuery directly in the browser. The free tier includes 292 tasks, mostly Easy, across all four dialects. The query collection ranges from straightforward filtering and expressions to harder data-processing problems, so you can start with basic SQL exercises and gradually increase the difficulty instead of jumping directly into long queries.

Frequently asked questions

How do I learn SQL queries?

Learn a small set of clauses first: SELECT, FROM, WHERE, and ORDER BY. Then add joins and aggregation when a task requires them. Writing and checking many small queries is usually more effective than memorizing large examples.

How can I practice SQL queries online?

Use a browser-based SQL sandbox where you can execute real queries against task-specific tables. SQL Arena provides separate PostgreSQL, MySQL, ClickHouse, and BigQuery environments and checks task solutions automatically.

What is the execution order of a SQL query?

A useful simplified order is FROM and JOIN, WHERE, GROUP BY, HAVING, SELECT, and ORDER BY. It differs from the order in which the clauses are written. That difference explains many errors involving aggregates and aliases.

Are SQL query examples enough to learn SQL?

Examples are useful for seeing syntax and patterns, but they are only the first step. To know whether you understand a query, change the task or data and reproduce the logic yourself.

Practice SQL queries in the browser

Start with free tasks, write a query, run it, and see whether the logic also passes hidden-data checks. No local database setup is required.

Practice SQL