What is pivot?
- SQL course
- Advanced
- Lesson 24/81
- 4 min
Turn rows into columns.
What is pivot?
Pivot turns rows into columns. It produces wide-format reports.
-- satır: ay, değer: ciro
-- pivot: her ay bir sütunFrom month rows to month columns.
What is pivot? — details
With pivot you produce a column per category. Done with CASE and aggregates.
For example, you turn a monthly sales table into a report with each category in its own column: rows are months, columns are Books, Electronics, Clothing.
Practice What is pivot?
Create a pivot column for each category with CASE WHEN category = 'Kitap' THEN amount ELSE 0 END.
Starter code
-- write pivot
SELECTSample solution
SELECT
SUM(CASE WHEN category = 'Kitap' THEN amount ELSE 0 END) AS Kitap,
SUM(CASE WHEN category = 'Elektronik' THEN amount ELSE 0 END) AS Elektronik
FROM sales;Expected output
Kitap Elektronik 200 1500
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does pivot turn into columns?
- Triggers
- Tables
- Indexes
- Rows Correct answer
Explanation: Pivot turns rows into columns.
2 Pivot
row → ____
row → column
Explanation: A pivot turns values from rows into columns; each category becomes its own column.
3 What is pivot: match the terms
| Term | Meaning |
|---|---|
pivot |
row→column |
CASE |
condition |
aggregate |
summary |
wide |
columnar |