Remember the first time you saw a large SQL query written by somebody else? I do. Meaningless aliases, no comments, all sub-queries and no common-table-expressions. Almost zero readability. What’s worse is that I had to debug this query because it was resulting in a discrepancy. Where on earth do I start? First - format the query using any one of the million tools available online. I use dpriver.
Second - in the words of my database class professor, “Read the query like the computer reads it.” When executing a query, the database management system follows a specific order of execution -
In other words -
Trying to understand the SELECT clause before understanding which tables are involved in the query is, to the say the least, difficult. Ignore the SELECT and jump straight to the FROM clause. Once you understand this, half the job is done. Note: When dealing with sub-queries, start with the innermost query and move gradually outwards. Once you have a good understanding of the query, and have made the changes you need, be a good samaritan and rewrite parts of the query that you think can be made more readable. Specifically, I’m a huge advocate of using the WITH clause in place of sub-queries. They enforce a sense of “sequence” to the query and make it easier to think through the logic of the query. Let’s say we wanted to identify the customers who have a lifetime revenue of more than $100, and then use this group as an input to our SQL query. Normally, we’d write it as a sub-query and join it in with the rest of the query. While this does the job, the query is much more readable if a WITH clause is used for this purpose. Before the start of the query, define the WITH table: WITH cust_group AS (SELECT customer_id, sum(order_total) FROM orders GROUP BY customer_id HAVING sum(order_total)>100) This temporary table can be joined in to the query just like any other table. Note that this is computationally as efficient as the sub-query method, just a lot more readable. Hopefully, these tips help in reducing the time and frustration when reading a query written by someone else, or even one that you wrote yourself a long time ago. I’d love to hear more tips to easily read SQL or to make SQL more readable. Please feel free to leave your thoughts in the comments below.
12 Comments
ANAND KUMAR
4/28/2020 12:57:57 pm
I was really helpful as I am a beginner in SQl.
Reply
Alex
8/26/2020 08:46:23 pm
Hi! Thanks for insight. Would it be possible for you to help me translate a SQL query?
Reply
Akash Agrawal
8/27/2020 12:37:37 pm
Hey Alex - glad it was helpful, and I can certainly help translate that query! Could you please reach out through the "Contact" page?
Reply
10/14/2022 04:59:31 am
Involve article wrong relationship concern boy leave. Measure design child media mission member.
Reply
Bob
2/7/2023 10:52:17 pm
Don't usually leave comments but I was looking for a resource exactly like this -- super helpful!
Reply
Akash
4/24/2023 09:11:51 pm
Glad it was helpful, Bob!
Reply
Nick
4/24/2023 07:06:54 pm
Great breakdown, especially for new practitioners overwhelmed with extensive subqueries. Thank you for the tips and the clarity - sorry it took me nearly five years to appreciate your writing!
Reply
Akash
4/24/2023 09:12:06 pm
Glad it was helpful, Nick!
Reply
Leave a Reply. |
Archives
June 2018
Categories |