×

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 CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 minutes read.

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.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 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 Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 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.

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 FULL JOIN

In this section, we will help you understand the concept of the SQL FULL Join clause with a few examples. The SQL FULL Join query is executed to display the integrated...

3 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 minutes read.

SQL DROP Table

In this tutorial, we will help you to understand how to delete the table from the database in SQL with the help of examples. The DROP TABLE query is used to...

4 minutes read.

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 minutes read.

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...

5 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

SQL LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples. The SQL Logical Operator displays the query result in one...

5 minutes read.

SQL Count

Structured Query Language Count() Function is used with Structured Query Language SELECT Statement. SQL Count() function returns the number of items that match the specified criteria in the SELECT statement. Count()...

2 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 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.