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:

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:

Table 1
Table 2