How to write SQL queries which are easier to read?
Tags: sql, database, programming, development
Published: 2019-09-19 12:58:20

---
title: How to write SQL queries which are easier to read?
published: true
description: One simple tip to write more readable and maintainable SQL query. 
tags: sql, database, programming, development
cover_image: https://thepracticaldev.s3.amazonaws.com/i/guuz7ob1zkdxcql8a48g.png
---
*Disclosure: This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.*

There is no doubt that writing code is more art than science and every coder cannot write beautiful code which is both readable and maintainable, even with the experience. Yes, it's blunt and hard but it's mostly true. 

In general, coding improves with experience but only when you learn the art of coding like favoring [**composition over inheritance**](http://javarevisited.blogspot.sg/2013/06/why-favor-composition-over-inheritance-java-oops-design.html) or coding for[**interface than implementati**on](http://javarevisited.blogspot.sg/2012/03/10-object-oriented-design-principles.html), but, unfortunately 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, DBA and even yourself after a few months.

Whenever I see SQL queries on emails from different developers, I can see the stark difference in their writing style. Some developers and DBAs write it so neatly and indent their query such that you can easily spot key details like which columns you are extracting, and from which table, and what are joining or filtering conditions.

Since in real-life projects, SQL queries are hardly one-liner, learning the right way to write [complex SQL query](http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-database.html) makes a lot of difference; especially when you share that query to someone for review or execution. It also helps when you read it yourself later as I said, after a few months.

The problem is there are many books and courses to teach you SQL like what is a table, different SQL commands but there are very few (like [The Complete SQL Bootcamp] (https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fthe-complete-sql-bootcamp%2F) by Josh Portilla) which focus on writing proper SQL queries.

In this article, I am going to show you a couple of styles which I have tried in 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.

Btw, I expect that you are familiar with SQL and definitely know how to write query. I expect that you have used different SQL clauses like SELECT, INSERT, UPDATE, DELETEand understand their meaning in a SQL query. If you are not, it's better you gain some experience with SQL by joining some of my recommended courses like:

1.  [Learn SQL ](
https://click.linksynergy.com/deeplink?id=JVFxdTr9V80&mid=44188&murl=https%3A%2F%2Fwww.codecademy.com%2Flearn%2Flearn-sql&LSNSUBSITE=TEST) by CodeCademy
2.  [Introduction to SQL](https://pluralsight.pxf.io/c/1193463/424552/7490?u=https%3A%2F%2Fwww.pluralsight.com%2Fcourses%2Fintroduction-to-sql) by Jon Flanders
3. [The Complete SQL Bootcamp] (https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fthe-complete-sql-bootcamp%2F) by Josh Portilla, a Data Scientist,  on Udemy or 
4. [SQL for Newbs: Data Analysis for Beginners](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fsql-for-newbs%2F) by David Kim and Peter Sefton's course on Udemy.

They all are great courses and teach you SQL basics, but, if you need some free alternatives you can also checkout this list of [free SQL courses](https://javarevisited.blogspot.com/2019/01/top-6-free-database-and-sql-courses-to-learn-online.html) for programmers and developers. 


Anyway, let's examing a couple of ways to write SQL query and find out which one is the best way to express intent in quick time:

## SQL Query Version 1.0
[![How to write sql query which is easier to read](https://thepracticaldev.s3.amazonaws.com/i/h3r9j5ekzk1i55rj7x4k.png)](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fsql-for-newbs%2F)

###Pros:
The mixed case was introduced to separate keyword from column and table names like writing [SELECT](http://javarevisited.blogspot.sg/2011/10/selct-command-sql-query-example.html) in a capital case and writing Employee in as it is, but given you are **not consistent** like ``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


## SQL Query Version 2.0
[![SQL query best practices](https://thepracticaldev.s3.amazonaws.com/i/a09kvoj6s9e6d4htdo3r.png)](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fthe-complete-sql-bootcamp%2F)

###Improvement:
1) SQL query is divided into multiple lines which make it more readable, yes, that small thing makes a huge difference.


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



##SQL Query Version 3.0 (Best)
![most readable sql query](https://thepracticaldev.s3.amazonaws.com/i/tviqzrqlq4ub81c7rm2r.png)


###Pros
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](http://www.java67.com/2017/12/difference-between-table-scan-index.html) and [joins](http://javarevisited.blogspot.sg/2013/05/difference-between-left-and-right-outer-join-sql-mysql.html)
3) Having conditions on separate lines allow you to run the query by commenting one of the conditions as shown below:

[![best sql query for programmers](https://thepracticaldev.s3.amazonaws.com/i/hfaf5s601wudmwje9jzv.png)](
https://click.linksynergy.com/deeplink?id=JVFxdTr9V80&mid=44188&murl=https%3A%2F%2Fwww.codecademy.com%2Flearn%2Flearn-sql&LSNSUBSITE=TEST) 

Btw, if you notice there is a subtle differnece, I have added WHERE 1=1 which means you can comment any condition without commenting WHERE clause which was not possible in earlier version of query.

If you are a fan of CAPITAL case for keywords, you can also write the same [SQL query](http://www.java67.com/2018/02/5-free-database-and-sql-query-courses-programmers.html) as shown below, the rules are same but just capital letters for keywords.

[![](https://miro.medium.com/proxy/0*eYRWRBEHLgO-9F81.png)](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fthe-complete-sql-bootcamp%2F)

That's all about **how to write a SQL query** which is readable and more maintainable. Feel free to give your opinion on what do you think of this indentation or styling of SQL queries.

It's a simpler technique but very powerful and goes a long way on improving the readability of your complex [**SQL queries involving multiple joins**](https://javarevisited.blogspot.sg/2012/11/how-to-join-three-tables-in-sql-query-mysql-sqlserver.html) as shown in my earlier example.

If you like you can also use various SQL formatters online but I suggest 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](http://www.java67.com/2018/02/5-free-database-and-sql-query-courses-programmers.html) and [books](http://www.java67.com/2016/09/sql-5-best-books-to-learn-and-master.html) helpful.

So, what's your thoughts? Does these points make sense? Which SQL style are you using? And, can you make it even more readable and maintainable?

**Further Learning**
[Introduction to SQL](https://pluralsight.pxf.io/c/1193463/424552/7490?u=https%3A%2F%2Fwww.pluralsight.com%2Fcourses%2Fintroduction-to-sql)
[The Complete SQL Bootcamp](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fthe-complete-sql-bootcamp%2F)
[SQL for Newbs: Data Analysis for Beginners](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fsql-for-newbs%2F)

Other **SQL and Database Articles** you may like

-   5 Websites to learn SQL for FREE ([websites](https://javarevisited.blogspot.com/2015/06/5-websites-to-learn-sql-online-for-free.html))
-   5 Free Courses to Learn MySQL database ([courses](https://javarevisited.blogspot.com/2018/05/top-5-mysql-courses-to-learn-online.html))
-   5 Free Courses to learn Database and SQL ([courses](http://www.java67.com/2018/02/5-free-database-and-sql-query-courses-programmers.html))
-   5 Books to Learn SQL Better ([books](http://www.java67.com/2016/09/sql-5-best-books-to-learn-and-master.html))
-   How to join more than two tables in a single query ([article](https://javarevisited.blogspot.com/2012/11/how-to-join-three-tables-in-sql-query-mysql-sqlserver.html))
-   Difference between WHERE and HAVING clause ([answer](https://javarevisited.blogspot.com/2013/08/difference-between-where-vs-having-clause-SQL-databse-group-by-comparision.html))
-   10 SQL queries from Interviews ([queries](http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-database.html))
-   Top 5 SQL books for Advanced Programmers ([books](https://javarevisited.blogspot.com/2018/07/top-5-advanced-sql-books-for.html))
-   Difference between SQL, T-SQL, and PL/SQL? ([answer](https://javarevisited.blogspot.com/2017/03/difference-between-sql-tsql-and-plsql.html))
-   Top 5 Online Courses to Learn SQL and Database ([courses](https://hackernoon.com/top-5-sql-and-database-courses-to-learn-online-48424533ac61))

Thanks for reading this article and let me know how do you write SQL queries? which style you use, or you have your own style?


**P. S. -** If you are looking for a free course to start learning SQL and Database basics then I suggest you go through **[Introduction to Databases and SQL Querying](https://click.linksynergy.com/deeplink?id=JVFxdTr9V80&mid=39197&murl=https%3A%2F%2Fwww.udemy.com%2Fintroduction-to-databases-and-sql-querying%2F)** course on Udemy. It's completely free, all you have to do is create a Udemy account and you can access whole course.