cannot write beautiful code which is both readable and maintainable,
even with experience.
developer.
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.
tried in past, their pros and cons and what I think is the best way to write SQL query.
overrules everything) there is no reason not to use it.
Btw, I expect that you are familiar with SQL and know different clauses and
their meaning in a SQL query. If you are not, it’s better you gain some
experience with SQL by joining a good course like:
- From 0 To 1:SQL And Databases — Heavy Lifting by Loony Corn
- Introduction to SQL by Jon Flanders
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;
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;
1) SQL 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.
3rd way to write SQL query (Best)
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 one of the conditions e.g.
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%';
add e.salary > 5000
you learn a style and stick with it, rather relying on formatters.
Thanks for reading this article and let us know how do you write SQL queries? which style you use, or you have your own style? If you are a beginner and learning SQL, you may also find my list of free SQL courses and books helpful.
So, what’s your thoughts? Does these points make sense? Which SQL style are you using?