There is no doubt that writing code is more art than science, every coder cannot write beautiful code which is both readable and maintainable, even with experience. In general, coding improves with experience when you learn the art of coding 

 

e.g. favoring composition over inheritance or coding for interface than implementation, but only a few developers able to master these techniques.  Same applies to SQL queries. The way you structure your query, the way you write it goes a long way to communicate your intent to the fellow developer. When I see SQL queries on emails from multiple developers, I can see the stark difference in their writing style.

Some developers write it so neatly and indent their query properly, which makes it easy to spot the key details e.g. which columns you are extracting from which table and what are conditions.

Since in real life projects, SQL queries are hardly one-liner, learning the right way to write SQL query makes a lot of difference when you read it yourself later or you share that query to someone for review or execution.

In this article, I am going to show you a couple of styles which I have tried in the past, their pros and cons and what I think is the best way to write SQL query. Unless you have a good reason not to use my style e.g. you have a better style or you want to stick with the style used in your project (consistency overrules everything) there is no reason not to use it.

 

1st way to write SQL query

SELECT e.emp_id, e.emp_name, d.dept_name, p.project_name from Employee e 
INNER JOIN Department d ON e.dept_id = d.dept_id INNER JOIN Projects p 
ON e.project_id = p.project_id Where d.dept_name="finance" and e.emp_name 
like '%A%' and e.salary > 5000;



Pros:
1) The mixed case was introduced to separate keyword from column and table names e.g. writing SELECT in a capital case and writing Employee in as it is, but given you are not consistent e.g. SELECT is in caps but from is in small, there is no benefit of using that style.

Cons:
1) Mixed case
2) The whole query is written on one line which gets unreadable as soon the number of tables and columns increases
3) No flexibility in adding a new condition or running without an existing condition


 

2nd way to write SQL query

SELECT e.emp_id, e.emp_name, d.dept_name, p.project_name
from Employee e
INNER JOIN Department d ON e.dept_id = d.dept_id
INNER JOIN Projects p ON e.project_id = p.project_id
Where d.dept_name="finance" and e.emp_name like '%A%' and e.salary > 500;


Improvement:
1) query is divided into multiple lines which make it more readable

Problems
1) Mixed case
2) All conditions on where clause is on the same line, which means excluding them by commenting is not that easy.
 

A Better way to write SQL queries


 

3rd way to write SQL query

select e.emp_id, e.emp_name, d.dept_name
from Employee e
inner join Department d on e.dept_id = d.dept_id
where d.dept_name = 'finance'
and e.emp_name like '%A%'
and e.salary > 500;


1) Dividing SQL queries into multiple lines makes it more readable
2) Using proper indentation makes it easy to spot the source of data i.e. tables and joins
3) Having conditions on separate lines allow you to run the query by commenting on one of the conditions



Related blog:

 

Angularjs training in chennai

創作者介紹
創作者 johndemorses 的頭像
johndemorses

johndemorses

johndemorses 發表在 痞客邦 留言(0) 人氣( 81 )