Best Practices For Writing SQL Queries

In this blog, you will learn about the best practices for writing SQL queries.

1. Write SQL keywords in capital letters.

2. Use table aliases with columns when you are joining multiple tables.

3. Never use select *, always mention list of columns in select clause.

4. Add useful comments wherever you write complex logic. Avoid too many comments.

5. Use joins instead of correlated subqueries for better performance.

6. Create CTEs instead of multiple sub queries , it will make your query easy to read.

7. Join tables using JOIN keywords instead of writing join condition in where clause for better readability.

8. Never use order by in sub queries , It will unnecessary increase runtime.

9. If you know there are no duplicates in 2 tables, use UNION ALL instead of UNION for better performance.

Thanks for reading!