UNPIVOT in SQL

Below example converts rows to columns using UNPIVOT clause.

Assuming you have a table like this:

image

And you want to convert like this:

image

Use the query:

SELECT
     ID, col, val
FROM
     Table_1
UNPIVOT
     (val for col in (col1, col2, col3)) p

You can also apply WHERE clause like this:

SELECT
     ID, col, val
FROM
     Table_1
UNPIVOT
     (val for col in (col1, col2, col3)) p
WHERE id=1

Result:

image