SQL Query Builder

Build SELECT, INSERT, UPDATE and DELETE statements from form fields, with WHERE, JOIN, ORDER BY and LIMIT. Quotes and escapes every value for you.

The interactive tool loads here when JavaScript runs.

About this tool

What is the SQL Query Builder?

The SQL Query Builder turns a few form fields into a runnable statement. Pick SELECT, INSERT, UPDATE or DELETE, type the table name, add columns and WHERE conditions, and click Generate Query. For SELECT you can also add one JOIN, an ORDER BY and a LIMIT.

Every value you type is written as a standard SQL string literal, with single quotes doubled, so a name like O'Brien becomes 'O''Brien' instead of breaking the statement. IN and NOT IN take a comma-separated list and produce a proper parenthesised list. IS NULL and IS NOT NULL need no value. UPDATE and DELETE without a condition get a warning comment on top, because they touch every row.

Table and column names are written exactly as you type them; the builder does not check them against a real database. The output uses LIMIT, which MySQL, PostgreSQL and SQLite understand. SQL Server and Oracle need TOP or FETCH FIRST instead.

  • SELECT, INSERT, UPDATE and DELETE
  • Standard single-quote escaping of every value
  • IN lists, IS NULL and IS NOT NULL handled correctly
  • One JOIN, ORDER BY and LIMIT for SELECT
  • Warning comment on UPDATE or DELETE without WHERE
  • Runs in your browser; nothing is sent to a database
Read the guide: Quotes, NULLs and LIMIT: Writing SQL and Seed Data That Run Everywhere

Updated

How to use the SQL Query Builder

  1. Choose a Query Type (SELECT, INSERT, UPDATE or DELETE) and type the Table Name.
  2. For SELECT, click Add Field for each column you want and give it an alias if you like; leave the list empty for SELECT *. For INSERT and UPDATE, the same rows hold a column and its value; type NULL for a real NULL.
  3. Click Add Condition to build the WHERE clause. Pick an operator; for IN and NOT IN, separate values with commas.
  4. For SELECT, optionally fill in JOIN Type, JOIN Table and ON, then ORDER BY and LIMIT under Additional Options.
  5. Click Generate Query, then Copy or Download the result as query.sql.

Worked examples

Filtering on a name with an apostrophe

Input

SELECT, table users, fields id and email (alias contact), conditions last_name = O'Brien, status IN active, trial, deleted_at IS NULL, ORDER BY created_at DESC, LIMIT 20

Output

SELECT id, email AS contact
FROM users
WHERE last_name = 'O''Brien'
  AND status IN ('active', 'trial')
  AND deleted_at IS NULL
ORDER BY created_at DESC
LIMIT 20;

The apostrophe is doubled, the IN list is split and quoted item by item, and the IS NULL condition is kept even though it has no value. Conditions are always joined with AND.

A DELETE with no WHERE clause

Input

DELETE, table sessions, no conditions

Output

-- Warning: no WHERE clause, this affects every row in the table
DELETE FROM sessions;

The statement is valid SQL, so the builder still writes it, but the comment makes the blast radius obvious before anyone pastes it into a console.

Background and insight

SQL started at IBM in the 1970s as SEQUEL and became an ANSI standard in 1986. The core of SELECT, INSERT, UPDATE and DELETE has barely changed since, but each database added its own way of limiting rows. LIMIT came from MySQL and PostgreSQL; SQL Server uses SELECT TOP n or, since 2012, OFFSET ... FETCH NEXT n ROWS ONLY; Oracle added FETCH FIRST n ROWS ONLY in 12c and before that relied on ROWNUM. The standard form is FETCH FIRST, which is why it is the most portable choice when you need one query to run everywhere.

The SQL standard escapes a single quote inside a string by writing it twice. MySQL also accepts a backslash escape unless the NO_BACKSLASH_ESCAPES mode is on, which is one reason hand-rolled escaping goes wrong. In application code the right answer is a parameterised query, where the driver sends values separately from the SQL text. A builder like this one is for writing ad hoc queries, migrations and examples, not for assembling SQL from user input at runtime.

NULL is the other classic trap. NULL means unknown, so status = NULL is never true, not even for rows where status is NULL. You have to write status IS NULL. That is why IS NULL and IS NOT NULL are separate operators in the dropdown rather than a value you type.

Common use cases

  • Writing a one-off support query

    Put together a SELECT with a few filters and a LIMIT to look something up without typing out quoting by hand.

  • Learning SQL syntax

    See how each form field maps to a clause, and how IN lists, NULL checks and joins are written.

  • Drafting a data fix

    Build the UPDATE with its WHERE clause, read it back, then run it inside a transaction.

  • Examples for documentation

    Produce clean, consistently formatted queries for a runbook, a ticket or a README.

Tips and limitations

  • Values are always quoted, including numbers. MySQL, PostgreSQL and SQLite coerce '42' to a number when the column is numeric; if you need an unquoted expression such as NOW(), edit it in after generating.
  • Run UPDATE and DELETE inside BEGIN ... ROLLBACK first and check the affected row count before you commit.
  • For SQL Server, replace LIMIT n with SELECT TOP n; for Oracle 12c and later, use FETCH FIRST n ROWS ONLY.
  • Prefix columns with a table alias (o.user_id) when you add a JOIN, so ambiguous column names do not fail.
  • Never paste end-user input into generated SQL in an application. Use your driver's parameter placeholders instead.

Sources and further reading

  1. SELECT, PostgreSQL Global Development Group. LIMIT and the standard FETCH FIRST clause
  2. ORDER BY Clause (Transact-SQL), Microsoft. OFFSET ... FETCH NEXT in SQL Server
  3. SELECT (row limiting clause), Oracle. FETCH FIRST n ROWS ONLY in Oracle
  4. Lexical Structure: string constants, PostgreSQL Global Development Group. Escaping a single quote by doubling it
  5. Query Parameterization Cheat Sheet, OWASP. Why application code should use parameterised queries

Frequently asked questions

How do I escape a single quote in a SQL string?

Write it twice: 'O''Brien' stores O'Brien. That is the SQL standard and works in MySQL, PostgreSQL, SQLite, SQL Server and Oracle. MySQL also accepts \' unless NO_BACKSLASH_ESCAPES is set, but doubling is the portable form, and the builder uses it for every value.

Why does WHERE column = NULL return no rows?

Because NULL means unknown, and comparing anything to unknown gives unknown, which WHERE treats as false. Use column IS NULL or IS NOT NULL instead. The builder offers both as operators so you do not have to remember the special case.

Does the SQL Query Builder work for SQL Server and Oracle?

Mostly. SELECT, INSERT, UPDATE and DELETE are standard, but the LIMIT clause is not. SQL Server uses SELECT TOP n or OFFSET ... FETCH NEXT, and Oracle 12c and later uses FETCH FIRST n ROWS ONLY. Leave LIMIT empty and add the right clause yourself for those two.

How do I write an IN condition with several values?

Choose IN or NOT IN and type the values separated by commas, for example active, trial. The builder trims each one, quotes it and wraps the list in parentheses, producing status IN ('active', 'trial').

Can I insert a NULL value with the INSERT option?

Yes. Type NULL (any case) as the value and it is written as the NULL keyword rather than the string 'NULL'. TRUE and FALSE are treated the same way. Everything else is quoted as a string literal.

Is a query builder safe against SQL injection?

It escapes quotes correctly, but generated SQL is still text. In application code use parameterised queries or prepared statements so the database never parses user input as SQL. This tool is meant for writing queries by hand, not for building them from untrusted input.

Is there an in-depth guide to the SQL Query Builder?

Yes. “Quotes, NULLs and LIMIT: Writing SQL and Seed Data That Run Everywhere” covers the background, how it works, common pitfalls and worked examples. Read the full guide.