SQL tools for readable queries and portable data
SQL queries tend to arrive in awkward shapes: a single 2,000-character line from an ORM log, a query pasted from a ticket with inconsistent capitalization, or a dump full of INSERT statements when you only wanted a spreadsheet. These SQL tools make queries readable and move data in and out of SQL.
Which SQL tool do you need?
- Read a complex query – the SQL formatter puts each clause on its own line, indents subqueries and joins and normalizes keyword case.
- Compact a query – the SQL minifier removes comments and whitespace, handy for embedding a query in code or a URL.
- Escape values – SQL escape doubles single quotes in string literals, and SQL unescape reverses it.
- Import data – CSV to SQL and JSON to SQL create
CREATE TABLEandINSERTstatements from files. - Export data – SQL to CSV, SQL to JSON and SQL to HTML turn INSERT statements back into tables, as do the SQL to XML and SQL to YAML converters.
Common workflows
Reviewing a slow query: copy it from your database log or ORM debug output, format it, and read the joins and WHERE conditions clause by clause. Formatted SQL makes missing join conditions and accidental cross joins obvious.
Seeding a test database: generate fake rows with the random CSV generator, convert them with CSV to SQL, and run the script against your development database.
Sharing data from a dump: extract the INSERT statements for one table and convert them to CSV for colleagues who work in spreadsheets.
Why format SQL at all?
SQL is often written once and read many times: in code review, during incident analysis and when onboarding new team members. A consistent layout, with keywords in one case and one clause per line, makes those readings faster and reduces mistakes.
Reading ORM output
ORMs such as Eloquent, Hibernate and Entity Framework log generated SQL as a single line with placeholders. Formatting that output is often the fastest way to understand what the framework is actually asking the database, for example spotting an N+1 pattern where the same query appears hundreds of times with different IDs.
SQL tips
- Escaping by hand is fine for one-off scripts, but application code should always use parameterized queries (prepared statements) to prevent SQL injection.
- Dialects differ: MySQL uses backticks for identifiers, PostgreSQL and standard SQL use double quotes, SQL Server uses square brackets.
- Formatting does not change what a query does, so it is safe to format production queries for reading.
- Queries and data are processed in your browser and never sent to a database or server.