Member-only story
Advanced SQL Tutorial Part 5 — SQL PIVOTING 🔨
Pivoting involves rotating a table by converting unique values from a single column into multiple columns. This rearrangement turns rows into column values, often involving aggregations on remaining columns.
On the other hand, unpivoting reverses this operation by transforming columns into row values.
Database Support for Pivoting and Unpivoting:
🟠SQL Server:
- Supports both PIVOT and UNPIVOT operators.
🟠Oracle:
- Supports PIVOT and UNPIVOT (from Oracle 11g).
🟠PostgreSQL:
- Does not have built-in PIVOT/UNPIVOT, but can simulate using
CASE/FILTERfor pivoting andUNION ALLorLATERALfor unpivoting.
🟠MySQL:
- No built-in PIVOT/UNPIVOT. Use
CASEandGROUP BYfor pivoting, andUNION ALLfor unpivoting.
🟠SQLite:
- No built-in PIVOT/UNPIVOT. Similar to MySQL, use
CASEandGROUP BYfor pivoting, andUNION ALLfor unpivoting.
🟠IBM Db2:
- Supports PIVOT and UNPIVOT (as of Db2 11.1).
🟠Snowflake:
- Supports PIVOT and UNPIVOT.









