Get startedGet started for free

Avoid common mistakes

1. Avoid common mistakes

In the last two lessons you learned about the steps you can take to ensure your code is easy to read and understand. In this lesson you will learn about common SQL mistakes that you want to avoid when writing your scripts.

2. Don't misuse comments

Comments can be incredibly valuable for conveying the intent of your code when the code isn't self explanatory. But this can easily go too far. Let's use the example on the left to learn what not to do. First, avoid using comments to write an essay. This doesn't mean that you shouldn't document your code, but keep it concise and to the point.

3. Don't misuse comments

Looking better already. Next, commenting code is incredibly helpful when you're prototyping your query. However, leaving old comments in the code can make it confusing to read and may be unclear how uncommenting these lines can affect your code.

4. Don't misuse comments

Finally, conveying intent using comments doesn't mean rewriting the same code as English, redundancy here doesn't add any value.

5. Don't misuse comments

Here, you can see how poor use of comments can make a simple query much more difficult to read.

6. Don't SELECT everything

The next mistake to avoid is returning every column in a query. Unless you know that you need every single column from your query you're far better off being specific with your SELECT and ensure that only the columns you need are returned. This not only helps better convey the intent of your code but can also improve performance for downstream processes.

7. Don't use SQL for programming

Finally, do not use SQL as a programming language. Yes, it is possible to add programming language elements to your SQL code. In this example you can see that both loops, conditionals and print statements actually work. But, because something is possible does not mean that it should be done. Remember, SQL is a database management language, not a programming language. There are many great languages that will offer you better clarity and efficiencies for executing the same code.

8. Let's practice!

Remember, the tips covered in this chapter are not rules they are recommendations for how you can write code that both the future you and any colleague who would use your code would be happy to read. With that in mind, let's go improve some queries!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.