How to use the sql query builder
- Enter the columns you want to retrieve, or type
*for all columns. - Provide the main table name. This field is required to build the query.
- Add optional clauses like joins, WHERE conditions, GROUP BY and ORDER BY.
- Set limit and offset numbers if you need pagination.
- The formatted SQL appears instantly as you type. Copy it directly into your database client.
How the SQL query builder works
This tool assembles a SELECT statement from the fields you provide. It starts with SELECT followed by your column list, then FROM and the table name. Each additional clause you fill in appends the corresponding SQL keyword and expression.
For example, if you enter id, name as columns, users as the table, age > 18 as the WHERE condition and 10 as the limit, the builder produces:
SELECT id, name FROM users WHERE age > 18 LIMIT 10;
The raw string is then passed through a hand-written tokenizer that splits it into keywords, identifiers, strings, numbers, operators and comments. The formatter uses those tokens to insert line breaks before major clauses, making the output readable without changing its meaning.
Tokenizer details and limits
The built-in tokenizer handles standard SQL constructs: single-quoted strings with doubled-quote escaping ('it''s'), double-quoted identifiers ("my column"), backtick-quoted identifiers, numeric literals including decimals, two-character operators like <= and !=, line comments starting with -- and block comments between /* and */.
It does not attempt full dialect parsing. Dialect-specific syntax such as MySQL's /*! ... */ conditional comments, PostgreSQL dollar-quoted strings ($$body$$), T-SQL bracket identifiers ([column]) or Oracle hint comments are treated as generic tokens. The formatter will preserve them but may not place line breaks around them perfectly. If you need strict validation, use a dedicated sql validator instead.
Common SELECT clauses reference
The following table summarizes the clauses this sql query builder supports and when to use each one.
| Clause | Purpose | Example |
|---|---|---|
| SELECT | Columns or expressions to return | id, name, COUNT(*) |
| FROM | Source table or view | users u |
| JOIN | Combine rows from another table | LEFT JOIN orders o ON u.id = o.uid |
| WHERE | Filter rows before grouping | status = 'active' |
| GROUP BY | Aggregate rows by column values | department_id |
| HAVING | Filter groups after aggregation | COUNT(*) > 5 |
| ORDER BY | Sort the final result set | created_at DESC |
| LIMIT / OFFSET | Restrict row count and paginate | LIMIT 20 OFFSET 40 |
Avoiding common mistakes
A frequent error is placing aggregate functions like COUNT() or SUM() in the WHERE clause. Aggregates must be filtered in the HAVING clause after GROUP BY. Another mistake is omitting table aliases when joining tables that share column names, which causes ambiguous-column errors in most databases.
String literals require single quotes, while identifiers with spaces or reserved words need double quotes or backticks depending on your database. Mixing these up is a common source of syntax errors. Also remember that LIMIT and OFFSET syntax varies: MySQL and PostgreSQL support LIMIT n OFFSET m, while SQL Server uses OFFSET m ROWS FETCH NEXT n ROWS ONLY. This builder outputs the LIMIT/OFFSET form.
Worked examples
| Example | Input | Result |
|---|---|---|
| All columns from users | Columns: *; Table: users | SELECT * / FROM users; |
| Filtered and sorted | Columns: id, name, email; Table: customers; WHERE: status = 'active'; ORDER BY: name ASC; Limit: 50 | SELECT id, name, email / FROM customers / WHERE status = 'active' / ORDER BY name ASC / LIMIT 50; |
| Join with aggregation | Columns: u.name, COUNT(o.id) AS order_count; Table: users u; Joins: LEFT JOIN orders o ON u.id = o.user_id; WHERE: u.active = 1; GROUP BY: u.name; HAVING: COUNT(o.id) > 0; ORDER BY: order_count DESC; Limit: 10 | SELECT u.name, COUNT(o.id) AS order_count / FROM users u / LEFT JOIN orders o ON u.id = o.user_id / WHERE u.active = 1 / GROUP BY u.name / HAVING COUNT(o.id) > 0 / ORDER BY order_count DESC / LIMIT 10; |
| Pagination | Columns: id, title; Table: posts; WHERE: published = true; ORDER BY: created_at DESC; Limit: 20; Offset: 40 | SELECT id, title / FROM posts / WHERE published = true / ORDER BY created_at DESC / LIMIT 20 OFFSET 40; |
FAQ
Does this sql query builder execute my query?
No. It only constructs and formats the SQL text. You must copy the output and run it in your own database client or application.
Which SQL dialects are supported?
The builder generates standard ANSI SQL SELECT statements with LIMIT and OFFSET syntax common to MySQL, PostgreSQL and SQLite. Dialect-specific features like TOP or window functions are not automatically added but can be typed manually into the fields.
Can I build INSERT, UPDATE or DELETE queries here?
This tool focuses exclusively on SELECT statements. For INSERT queries, use the dedicated sql insert generator or convert existing data with csv to sql insert.
Is my data sent to a server?
No. The entire sql query builder runs locally in your browser. Nothing you type is transmitted anywhere.
Why does the tokenizer matter?
The tokenizer breaks your raw input into meaningful pieces like keywords, strings and operators. This allows the formatter to add line breaks at the right places without splitting a quoted string or breaking an operator like <= into two lines.
How do I handle special characters in string values?
Use standard SQL escaping. In single-quoted strings, double the quote character: 'O''Brien'. The tokenizer recognizes this pattern and keeps it intact during formatting.