DATA with BARAA

DATA with BARAA

fun with data

SQL Basics

Querying Data

Filtering Data

Joining Tables

SQL Functions

Modifying Data

Defining Data

3 Rules to Style SQL Code

In this tutorial, you will learn why code styling is important and how to style your SQL code using simple 3 rules.

Why Should Style Your Code?

When learning a new programming language, it is not enough to learn how to write a code, you also need to learn many other things, such as:

  • How to solve a task with just a few lines of code.
  • How to write code that delivers high performance.
  • And finally, and most importantly, how to write code that looks professional and is easy to read.

In the many years that I have been working with developers, I have found that everyone has a different opinion when it comes to how the SQL code should be styled or the script formatted.

On the other hand, all developers agree that the code should be readable and follow some style guidelines.

In our IT projects, we usually create a set of coding standards, rules, guidelines, and best practices that we use as a guide for our developers.

You can develop your own coding style, but the most important thing is to stick to it!

You might ask me, do I really need to style my code, is it not enough to write a working code?

Well no, there are two main reasons why you have to pay attention to the code style:

  • Let’s say you are working on a project and writing SQL code. In many projects, your code will be reviewed by other developers.
    They will have a hard time reading your code even if you write a simple code, and others might be forced to rewrite your code to read and review it.
  • Another reason if your code contains errors, you may find it hard to figure out in which line the problem is.

Especially if you are a beginner in SQL, you will often not pay attention to the code style and think you can ignore it at the start.

So my advice to you is don’t develop bad habits at the start, you might have a hard time later breaking them.😇

Example

The following example, it is a short query which selects the data from customers and orders:

				
					select c.customer_id,c.first_name,c.last_name,o.order_id,o.order_date
from customers c join orders o on c.customer_id=o.customer_id 
where c.country='germany' AND o.quantity>100
				
			

Let’s apply 3 simple rules and see the results:

Rule #1 Add new lines for each keyword

  • Add new lines for each keyword and selected column.
  • Adding new Lines works wonderful and helps your code to be readable.
				
					select 
c.customer_id,
c.first_name,
c.last_name,
o.order_id,
o.order_date
from customers c 
join orders o 
on c.customer_id=o.customer_id 
where c.country='germany' 
AND o.quantity>100
				
			

The code looks already better using the first rule, let’s see what else we can do.

.

Rule #2 Uppercasing keywords

Make all keywords in caps to make them easier to find. because each keyword has its function and task.

				
					SELECT 
c.customer_id,
c.first_name,
c.last_name,
o.order_id,
o.order_date
FROM customers c 
JOIN orders o 
ON c.customer_id=o.customer_id 
WHERE c.country='germany' 
AND o.quantity>100
				
			

Rule #3 Add write spaces

  • You can add white space in WHERE and JOIN
  • You can aswell add a table to all columns
				
					SELECT 
	c.customer_id,
	c.first_name,
	c.last_name,
	o.order_id,
	o.order_date
FROM customers c 
JOIN orders o 
ON c.customer_id = o.customer_id 
WHERE c.country = 'germany' 
AND o.quantity > 100
				
			

Before and After ...

With only 3 simple rules you will have a nice readable script that everyone can read.

Now let’s look at both scripts side by side.

Can you see the difference Which one is more readable?

without style

				
					select c.customer_id,c.first_name,c.last_name,o.order_id,o.order_date
from customers c join orders o on c.customer_id=o.customer_id 
where c.country='germany' AND o.quantity>100
				
			

with a style

				
					SELECT 
	c.customer_id,
	c.first_name,
	c.last_name,
	o.order_id,
	o.order_date
FROM customers c 
JOIN orders o 
ON c.customer_id = o.customer_id 
WHERE c.country = 'germany' 
AND o.quantity > 100
				
			

It is straightforward that the script with a style has a proper format that helps you and others to read it easily and to find errors and problems.

Final thoughts

Most importantly, you want to make sure that your programming style is consistent throughout your code. 

As developers we sometimes create something that works and then say “I’ll format it later”, but that later never comes. keep that in mind.

On another hand, don’t make very complicated and time-consuming rules that waste your time, try to find a middle ground.

Me personally, I follow only these 3 rules that I showed you in this tutorial.

.
Share it !

Schema is a way to group objects such as tables. It is a container you can put objects into.