Pivoting is a very useful data manipulation technique. This blog will talk through how rows to columns pivots are achieved in SQL.
The syntax is as below:
![](https://www.thedataschool.co.uk/content/images/2025/01/image-114.png)
Breakdown of syntax:
agg(PIVOT_VALUES1) - specifies the aggregation of the values you want to assign to each new column and which column this data is coming from.
for PIVOT_NAMES1 - specifies the column where each distinct value will become a table header.
in (Header1, ...) - specifies the actual column headers for the pivot result.
Example:
To make this clearer, here is an example of how to pivot table 1:
![](https://www.thedataschool.co.uk/content/images/2025/01/image-118.png)
![](https://www.thedataschool.co.uk/content/images/2025/01/image-119.png)
![](https://www.thedataschool.co.uk/content/images/2025/01/image-120.png)