SQL Formatter
Paste SQL. Format it, check it as you type. Nothing leaves your browser.
- 0
- Lines
- 0
- Statements
- 0
- Errors
On this page
About SQL Formatter
Updated
What it does
SQL Formatter turns messy SQL into clean, readable code with consistent indents, line breaks and keyword case. It also checks your SQL as you type: when something is wrong, it rings the exact spot, gives the line and column, and says how to fix it.
How to use it
Type or paste your SQL in the editor and press Format. The formatted SQL replaces your text in the editor; the lines Format changed stay marked until you edit them, and Show before shows your text as it was. Minify puts the SQL on one line. Choose your dialect in the Result header so the check uses the right grammar. Press Example to try it with a sample query.
Customization
Options let you choose the dialect, among them MySQL, PostgreSQL and SQL Server, keyword case, indent, a table-like layout, where AND and OR go, and the space between statements. Your choices are saved in this browser.
Common SQL errors
Misspelling keywords
Misspelling is a common error in SQL. It can be a typo or the wrong keyword. For example, the following code results in a 1064 syntax error, because the keyword SLECT is misspelled and the SQL engine does not recognize it:
SLECT * FROM cards WHERE color LIKE '%blue%';Misspelling table or column names
Misspelling table or column names is another common error. It can be a typo or a wrong guess at the name of a table (1146 Table not found) or column (1054 Unknown column) that does not exist in the database. The engine does not recognize the resource you are trying to access. A formatter cannot catch this kind of error, because it does not know your database.
Missing or extra commas
Columns are separated by commas, but there is no comma after the last column. Missing or extra commas usually result in a 1064 syntax error. For example, the following code is invalid because of the comma before FROM:
SELECT
c.name,
c.color,
c.type,
FROM cards c
INNER JOIN decks d ON c.deck_id = d.id
WHERE d.name = 'Joust';About SQL
SQL, or Structured Query Language, is a standardized language for managing and querying databases. It is used to communicate with databases, notably when handling structured data: data that holds relations among entities and variables.
SQL was first developed at IBM by Donald D. Chamberlin and Raymond F. Boyce in the early 1970s. That version, called SEQUEL (Structured English Query Language), was designed to manipulate and retrieve data in IBM's quasi-relational database System R, developed at IBM Research Almaden. SEQUEL later became SQL, because "SEQUEL" was a registered trademark of the aircraft maker Hawker Siddeley.
SQL is standardized by ISO and ANSI.
Proper SQL format
- SQL keywords are not case sensitive.
- SQL statements are made of descriptive English words.
- SQL statements end with a semicolon
;. - White space and new lines do not change the meaning of a statement.
Code examples in SQL
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
age INT
);This creates a table named employees with four columns: id, name, email and age.
INSERT INTO employees (id, name, email, age)
VALUES (1, 'Jacky Bauers', 'jackyb@example.com', 30);This inserts a new row. The values come in the same order as the columns.
SELECT * FROM employees;The SELECT statement returns all rows from employees. The * stands for all columns.
Other SQL resources
- ANSI SQL StandardThe American National Standards Institute, which publishes the SQL standard.
- MySQL DocumentationThe official MySQL documentation, covering all aspects of the MySQL Database Server.
- PostgreSQL DocumentationThe official documentation for PostgreSQL, a powerful, open-source object-relational database.
- Microsoft SQL ServerThe official page for Microsoft SQL Server, the relational database from Microsoft.
- W3Schools SQL TutorialA beginner-friendly tutorial on SQL from W3Schools.
- SQL ShackArticles, discussions and tutorials for SQL Server professionals.
- DB-Engines RankingDatabase systems ranked by their current popularity, many of which use SQL.
- SQLZooAn interactive resource for learning SQL through hands-on exercises.
- SQL on WikipediaAn extensive article on SQL from the online encyclopedia.
- Oracle Database TechnologiesResources related to SQL and Oracle databases.
- SQL: From Traditional Databases to Big DataA research paper on the evolution of SQL and its role in big data.
- The History of SQLWho created SQL and why, and how it all began.
SQL Formatting FAQ
Why use SQL?
SQL is crucial for managing data stored in relational database management systems (RDBMS) or for stream processing in a relational data stream management system (RDSMS). It is particularly useful for handling structured data, including data encompassing relations among entities and variables. Here are some other benefits of using SQL:
- High speed. SQL queries can retrieve large amounts of records from a database quickly and efficiently.
- Well defined standards. Long established as a standard language for database management systems.
- No coding needed. SQL removes the need to write a lot of code to manage database systems.
- Multiple data views. With SQL, you can view the data in many different ways.
How does the SQL Formatter work?
Formatting is done by sql-formatter, an open source library that knows the grammar of many SQL dialects. The check as you type uses node-sql-parser, which reads your SQL with the full grammar of the dialect you choose: MySQL, PostgreSQL, SQL Server, SQLite, MariaDB, BigQuery, Snowflake, Redshift, Db2, Hive, Trino and N1QL. For the other dialects the tool checks brackets and quotes only, and says so.
The editor is CodeMirror with @codemirror/lang-sql for syntax highlighting. Everything runs in your browser. Your SQL is never sent anywhere.
How should I format my SQL?
There is no one correct way to format SQL code. Different developers have different preferences and styles. A good rule of thumb is to ask your team which standard they use, or to choose a standard you like and stick to it.
Experts in SQL have written down a few generally agreed standards and good practices, but there is no hard and fast rule. Stick to the standard used in your organization or team. If you work alone, choose the standard you prefer.
What are the most common SQL formatting rules?
These are guidelines, not strict rules, that make SQL code easier to read and maintain across dialects:
- Capitalization. SQL keywords in uppercase, table and column names in lowercase.
- Indentation. Indent by 2 or 4 spaces, and be consistent.
- Line breaks. At least one line break after each statement; often a blank line between statements.
- Commas. At the end of each line, not at the start of the next line.
- Comments. On their own line, unless the comment is short enough for the end of a line.
- Quotes. Single quotes for strings.
- Aliases. Give tables and columns short aliases, except in short queries.
- Operators. Spaces around operators:
1 = 1, not1=1. - Subqueries. Indent subqueries by at least one level and put them on their own lines.
- Joins. Every JOIN on a new line, often indented one level.
- CASE. Each case on a new line, indented one level; a short CASE can stay on one line.
Where can I find SQL formatting guides?
We recommend the comprehensive SQL Style Guide by Simon Holywell. GitLab also has its own SQL Style Guide, which is easy to understand and comes with the linter SQLFluff that formats SQL code for you.
Why use an SQL formatter?
SQL code can get complex, which makes it harder to read and debug. SQL Formatter formats your code, makes it easier to read, and helps you find mistakes before you run the query. It supports many SQL dialects, which makes it useful for developers and database administrators alike.