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.
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.
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;
| # | name | paid_orders | total_amount |
|---|---|---|---|
| 1 | Mark | 1 | 3500.00 |
| 2 | Elena | 1 | 2700.00 |
| 3 | Anna | 2 | 2000.00 |
| 4 | Nina | 1 | 1600.00 |
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.
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;
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.
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;
| name | paid_orders | total_amount |
|---|---|---|
| Mark | 1 | 3500.00 |
| Elena | 1 | 2700.00 |
| Anna | 2 | 2000.00 |
| Nina | 1 | 1600.00 |
| Oleg | 2 | 1400.00 |
Common SQL query mistakes
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.
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.
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 courseIf 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.
- EasyfreeLarge Order or Regularbigquery
- EasyfreeNumeric Codes Without Failuresbigquery
- EasyfreeInside Each Event Sessionbigquery
- EasyfreeSessions That Ran Longbigquery
- MediumPremiumRound Without Rewriting the Listbigquery
- HardPremiumMedian in a Massive Purchase Logbigquery
- ExpertPremiumPlatforms Across the Citiesbigquery
- MediumPremiumPaid Activity at a Glancebigquery
Reference on this topic
In-depth articles with syntax and examples in the SQL Arena reference.
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 SQLMore practice from CodeRang
- Python ArenaPython Arena has 1300+ Python 3.12 problems covering functions, classes, async code and technical interview tasks.1300+ tasks · Python 3.12
- Playwright ArenaPlaywright Arena has 30 JavaScript and E2E problems where you write a test and immediately see what it did in the browser.30 tasks · JavaScript / E2E