×

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a better readability, we beautify the SQL statements using SQL Formatter. In view of user’s perspective we format the SQL queries.

The formatting is done by applying indentations, spaces in between, and tabs between the statements.

For example, let’s consider a small SQL query for better understanding.

select * from employee ORDER BY e_salary;

This can be formatted as,

select 
   *
from
   ORDER BY e_salary;

Let’s look another complex example.

select * from employee where EXISTS (select * from employee, department where employee.id = department.id and department_name=’cse’ 

By formatting the above query,

(select 
   *
from
employee
where EXISTS
(
	select 
	   *
           from
           employee, department
	where
	employye.id = department.id
	and
	department_name = ‘cse’

In this way we can understand the code more efficiently.

By this formatting we can understand the query more easily. So, for this purpose we use formatting Tools.

SQL formatter is a beautifying tool for large SQL queries. We have many different tools for formatting SQL queries. Some of the tools are

  1. Atom Beautify SQL Formatter
  2. Poor Man’s T-SQL Formatter
  3. Code Beautify SQL Formatter
  4. Instant SQL Formatter

Now let’s have a look of how these tools work.

Atom Beautify SQL Formatter:

This tool is used for formatting all languages like sql, html, css etc... After installation, one can use command Palette to choose the editor for a specific language. Then we need to select the code which is to be formatted. And after the selection, by clicking on Beautify On Save we can get the results.

Poor Man’s T-SQL Formatter:

This tool helps us in handling complete multi-batch scripts, including object definition scripts such as stored procedures, triggers etc...

We should use this tool by launching SSMS. Then, from tools select format T-SQL code.

Code Beautify SQL Formatter:

This tool is Specially for SQL queries. To Beautify large SELECT statements, we use Code Beautify SQL Formatter. This tool indents the code and spreads the code into multiple rows.

Instant SQL Formatter:

This tool gives out a clean, well-structured script as an output from the unorganised script. This tool not only formats SQL code but also this tool can convert between SQL and C#, java.

Indentation of code:

In this block we have 3 stages of indentation.

  1. None
  2. Block
  3. Smart

None:

If we select None, the cursor points to the starting point of the next line.

Block:

If we opt Block button, the cursor points over the previous line indentation in the next line.

Smart:

This option decides the indent styles automatically. This works on a query by default.

Rules for Formatting the SQL Code:

 Following standard rules for SQL formatting gives best results in greater efficiency.

Object Naming:

  • One should use a singular name for table/column.
  • Do not use SQL Keywords as Table name / column Name. If we use SQL keywords as the table name, then we should enclose the name with quotation marks.
  • Do not start a table/column name with an underscore.
  • Prefer using AS keyword mostly, as this increases the readability of the code.
  • Do not give same name for both table and column to avoid confusion.
  • Do not use special characters like  ‘&’, ‘$’, and ‘*’ .

Alignment:

  • While formatting, every Keyword should start in the next line.

Ex:

select student_name , 
            student_ID
from student;

Comments:

One should not write too many comments to reduce the complexity.

If it is necessary to include comments, then prefer Multi-line comments.

Ex:

select  person_age, 
	person_DOB    
                 /*Age, DOB are the columns in Person Table -- Multi-Line Comments*/
from Person;

Indentation:

One needs to use a new line after each query. It looks good if one uses a new line after every comma also.

Queries of SELECT:

For writing a select statement, if the select statements consists of many columns, then every column should be written in a new line with a comma at the end of the line. comma should not be placed at the starting of next line.

Ex:

select person_ID,
	person_age,
	person_name,
	person_DOB
from Person;

Queries of WHERE:

If a query has a where statement, then it should be written in the next line without any indentation.

Ex:

select person_ID,
	  person_salary
from Person
where person_Name = “Dhruv”;

Queries of INSERT:

For inserting records into a table, we use INSERT keyword. Indentation should be followed.

Ex:

Insert into Person(person_ID, person_Name, person_age) values
	(1, ‘Dhruv’, 12),
	(2, ‘Ayaan’, 17);

Queries of UPDATE:

For Updating the values in the table, we use UPDATE keyword and SET keyword. So, every time when we update, the set keyword should start from a new line.

Ex:

Update Person
Set person_ID=101
Where person_name = ‘Dhruv’;

Long SQL Query:

Long SQL queries contains many sub queries in it. So, formatting is necessary in such cases. Every sub query must be intended on a new line.

Ex:

select person_ID,
	person_Name
	case
	    when  person_age < 18 then ‘below 18’
	    when person_age >= 18 then ’18 or above’
	end as Age
from Person;

Join Statements:

We Join two tables using INNER JOIN, OUTER JOIN, INNER JOIN. Along with these we use ON Operator.

So, ON operator should be written on a new-line.

Ex:

select e.ID,
	e.Salary,
	d.ID
from  Employee as e
JOIN Department as d
     ON e. Name = d.Name;

NOTE:All SQL Keywords like SELECT, WHERE, HAVING, ORDER BY, GROUP BY should be written in a new line.

Usage of SQL formatter tools:

If the SQL queries are small and less complex, we can indent them manually and understand the query.

But In case of large databases, we write complex queries. So, it becomes difficult for us to format/indent the queries manually.

Advantages of SQL Formatter:

  • This tool transforms queries into more flexible way to read and understand them.
  • When a query is formatted with required spaces, it becomes easy to debug the error and to fix them.
  • It increases the efficiency of the Database Transactions.
  • It helps in scaling the Database.

Related Topics

SQL SELECT MAX

The SQL Max() function is an aggregate function in SQL. This function returns the values which are greater in the condition. The condition may be a number, or it may...

5 minutes read.

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

5 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.

SQL Cast Operator

While writing SQL queries, we can see many values which are to be converted into another datatype for accessing them. In SQL this conversion is generally done by CAST function. CAST...

4 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

SQL Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

How to drop a column in SQL?

How to drop a column in SQL Introduction To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause. Syntax: ALTER TABLE tablename...

4 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

4 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

SQL CASE

This page contains all the information about SQL CASE. The CASE is an If-Else type of logical query used in the statement. The CASE in Structured Query Language is similar...

5 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.