Developer & tech
SQL Query Generator
Fill in what you want back and read the query it produces. The explanation matters more than the SQL — a query you cannot read you cannot trust.
Your query
SELECT id, customer_id, amount, created_at FROM orders WHERE status = 'paid' ORDER BY created_at DESC LIMIT 50;
What it does
- 1. Selects 4 named columns from the table.
- 2. Filters rows before grouping.
- 3. Sorts highest first.
- 4. Returns at most 50 rows.
Before you run it
- Postgres folds unquoted identifiers to lowercase; anything created with mixed case must stay double-quoted.
Habits that keep queries safe
Run every new query as a SELECT first, even when the goal is an UPDATE or DELETE. Confirm the row count is what you expect, then swap the verb. Wrapping the change in a transaction you can roll back costs nothing and saves the occasional catastrophe.
Never build a WHERE clause by joining strings together with user input. Bound parameters are supported by every driver and remove an entire class of vulnerability. This page is a scaffold for a query you will parameterise, not a runtime query builder.
Check the plan before shipping. EXPLAIN on a table with production-scale data tells you whether your filter uses an index; a query that returns instantly on a thousand dev rows can time out on a million real ones.