Rows to Columns Pivots in SQL

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
Author:
Dan Booth
Powered by The Information Lab
1st Floor, 25 Watling Street, London, EC4M 9BR
Subscribe
to our Newsletter
Get the lastest news about The Data School and application tips
Subscribe now
© 2025 The Information Lab