About the SQL Query Generator
The awkward part of writing SQL from memory is rarely the logic, it is the dialect. Row limiting is LIMIT in PostgreSQL, MySQL and SQLite, TOP (n) right after SELECT in SQL Server, and FETCH FIRST n ROWS ONLY in the standard. Identifier quoting uses backticks, double quotes or square brackets depending on the engine. This generator applies the right form for the dialect you pick, so the statement runs where you paste it.
Parameter placeholders follow the same rule. An INSERT or UPDATE is written with the marker the driver expects: ? for MySQL and SQLite, $1 and $2 for PostgreSQL, @p1 for SQL Server. Parameterised statements are what you want anyway, because a value pasted into the text is how injection bugs start.
The upsert option shows how far the dialects diverge: PostgreSQL and SQLite use ON CONFLICT ... DO UPDATE with the first listed column as the key, MySQL uses ON DUPLICATE KEY UPDATE, and SQL Server gets a comment pointing at MERGE because it has no equivalent clause. An UPDATE or DELETE with an empty filter is generated with a warning comment on top rather than quietly producing a statement that would rewrite the whole table.
Column names are checked as plain identifiers, so a stray fragment is refused instead of producing broken SQL. Run the result through the SQL Formatter for house style, or build the schema first with the SQL Table Generator.
How to use
- Choose the statement type, then type the table and the column list.
- Add a filter under Where and an ordering if you need them.
- Pick the dialect so quoting, row limits and placeholders match your database.
- Copy the statement, or download
query.sql.
Common questions
- Why are there question marks instead of values?
- Writing statements with placeholders is the safe habit. Bind the values through your driver and injection stops being possible.
- Which column becomes the conflict key in an upsert?
- The first column in your list. Put the primary key or the unique column first, then the columns that should be overwritten.
- What does the warning comment on a DELETE mean?
- You left the filter empty, so the statement would remove every row. The comment is a reminder to add a condition before running it.
- Can I turn off the quoting?
- Yes. Untick Quote identifiers for bare names, which is fine when nothing clashes with a reserved word.