When Coding in SQL there is a hierarchy that's important to remember.
This may seem backwards in some instances SQL will only read in a specific order.
The order these tools work in is
- Select
- From
- Where
- Group by
- Having
- Order by
Select - this is the function the tells SQL which fields you want to see (told you it was backwards) make sure each of the fields that you want to output are separated by a comma
From - This tells SQL which table the data will be stored in, it is often best practice to put the entire file path to make sure there is no confusion
Where - This tells SQL which filters you want to apply to that fields, eg - Where Contains(Name, "J")
Group by - if we have some sort of aggregation this will tell SQL what to group the aggregation by
eg: Select Name, Sum(Sales)
Group by Name
Having - this is very similar to the where function - however is primarily for those aggregation functions
Finally order By - tells SQL how to sequence the results