Recently we had some sessions on SQL as part of our Data School training. It was a language I really enjoyed picking up, and this blog will go over the fundamentals of what we learnt. This blog will aim to help readers in starting their SQL journey and will be the first of many where each blog gets increasingly more complex.
To start with we will cover 6 key clauses for querying in SQL:
1) SELECT
2) FROM
3) WHERE
4) GROUP BY
5) HAVING
6) ORDERBY
SELECT + FROM
Select and From are used to obtain data for a particular query.
- Select is used to chose the columns you want to use.
- From is used to select the table you want to use.
An example of how they can be implemented is shown below:
![](https://www.thedataschool.co.uk/content/images/2025/01/image-81.png)
In this example, a user will obtain the column ORDER_ID from the table ORDERS.
To select all columns a * can be used:
![](https://www.thedataschool.co.uk/content/images/2025/01/image-82.png)
To select multiple columns, each field can be separated by a comma:
![](https://www.thedataschool.co.uk/content/images/2025/01/image-83.png)
WHERE
WHERE acts very similarly to a filter. You can specify a condition and only outputs that match that condition will be returned.
In this example only products with a profit of over 50 will be returned.
![](https://www.thedataschool.co.uk/content/images/2025/01/image-84.png)
GROUP BY + HAVING
GROUP BY allows you to change the aggregation of the data you are working with. If you GROUP BY a column, then each value within that column will become unique.
For example if you have a list of products and want to see how much profit they make at a Category level, you can group by CATEGORY. Table 1 and 2 show the change this query would make.
![](https://www.thedataschool.co.uk/content/images/2025/01/image-85.png)
![](https://www.thedataschool.co.uk/content/images/2025/01/image-86.png)
![](https://www.thedataschool.co.uk/content/images/2025/01/image-87.png)
When trying to filter a query which contains an aggregation you must use HAVING instead of WHERE. Because our example uses the SUM() function, this makes it an aggregation and, therefore HAVING is needed in this query.
![](https://www.thedataschool.co.uk/content/images/2025/01/image-88.png)
ORDER BY
Order By lives up to its name, and helps sort your data based on a specified column. This can be done in either Ascending or Descending order.
![](https://www.thedataschool.co.uk/content/images/2025/01/image-89.png)
![](https://www.thedataschool.co.uk/content/images/2025/01/image-90.png)