Since I joined the data school and looked at our calendar I have been looking forward to learning SQL, I know its a really commonly used language and has so many applications. I wanted to share some of the fundamentals of SQL that we learnt.
SQL stands for Structured Query Language. It is therefore no surprise that we must write our queries in a logical and structured way.
Select
From
Where
Group By
Having
Order By
Select; Selecting the column headers that we would like to retrieve.
From; Deciding the table that we are interested in.
Where; Using where will filter the selected records before they have been aggregated.
Group By ; We must group by certain columns so that we can calculate our aggregations.
Having; Using having will do the filtering after the aggregation of measures selected above.
Order by; Will order the records retrieved. (We can use order by on aggregated fields.
Another really fundamental aspect of writing SQL is making sure that it is clear what steps you have done. Even if you are not handing your work on to someone else. It is still good practice to document your work, You may go back to the code in a years time and need the documentation to remind. Below are 5 useful tips to help better document your SQL Code:
1) 1 line per keyword, column or clause
2) keep cases consistent
3) write meaningful aliases
4) use indentation and commas
5) Write comments
Using these 5 tips will help turn your code from this:
to this!