Utilumo
LightDarkSystem
Guide1 min readUpdated July 2, 2026

How to write a SQL query

Short answer

Start with SELECT to choose columns, FROM to pick the table, then add WHERE to filter, ORDER BY to sort, and GROUP BY to aggregate. Build it up one clause at a time and test against real data.

The clauses, in order

A SQL SELECT reads as a sentence: which columns, from which table, matching which conditions, in which order. You write the clauses in a fixed order, though the database evaluates them differently.

SELECT name, COUNT(*) AS orders
FROM users
JOIN orders ON orders.user_id = users.id
WHERE users.active = 1
GROUP BY name
ORDER BY orders DESC;
A query using the common clauses

Build it up step by step

  1. SELECT + FROMChoose the columns and the table: SELECT name FROM users.
  2. Filter with WHEREKeep only the rows you want: WHERE active = 1.
  3. Join related tablesPull in other data with JOIN ... ON a matching key.
  4. Group and sortAggregate with GROUP BY, then order with ORDER BY.
Try it: SQL Query TesterRun your query against sample tables and see the result instantly.Open tool
Test on a small dataset firstBefore running against real data, try the query on sample rows. It is easy to filter out too much, or to multiply rows with a loose JOIN. See what is a SQL JOIN.

Keep queries readable

  • Uppercase keywords (SELECT, FROM, WHERE) so structure stands out
  • One clause per line; indent joins and conditions
  • Alias tables so joins are easy to follow
  • Format messy queries before sharing or reviewing them

References

Questions

What is the basic structure of a SQL query?

SELECT columns FROM table, optionally followed by WHERE to filter, JOIN to combine tables, GROUP BY to aggregate, and ORDER BY to sort. Only SELECT and FROM are required for a simple query.

What is the difference between WHERE and HAVING?

WHERE filters individual rows before grouping. HAVING filters groups after GROUP BY has aggregated them, so it can use aggregate functions like COUNT and SUM.

Does this send my data anywhere?

No. Utilumo's developer tools parse and transform input inside the browser tab. Nothing is uploaded, stored, or logged.

Keep reading