SQL PIVOT Generator: Build Cross-Tab Queries Without Writing Code
🚀 Ready to try it? Build a Pivot Query — free, browser-based, no sign-up.
Open Tool →Table of Contents
Pivot queries rotate row data into columns — turning a list of transactions into a comparison table. They are powerful and frequently needed, but the syntax differs significantly between database engines and is notoriously hard to write by hand. The SQL PIVOT generator handles the syntax for you and outputs ready-to-run queries for SQL Server, PostgreSQL, MySQL, and Generic SQL.
What Is a SQL PIVOT Query?
A PIVOT query transforms row values into column headers. Instead of rows like East, Q1, 12400 and West, Q2, 11500, a pivot produces a table with Region as rows and Q1, Q2, Q3, Q4 as columns with aggregated values in each cell. This wide format is ideal for reports and comparisons.
Step-by-Step: Using the PIVOT Generator
- Enter your table name.
- Set the Row Field — the dimension that becomes row labels (Region, Product, Department).
- Set the Column Field — whose distinct values become column headers (Quarter, Month, Status).
- Set Value Field and Aggregate — the numeric field and function: SUM, COUNT, AVG, MIN, MAX.
- Enter column values — the distinct values for the column field (Q1, Q2, Q3, Q4).
- Select SQL dialect — SQL Server, PostgreSQL, MySQL, or Generic SQL.
- Generate and copy.
SQL Dialects: How Each Database Handles PIVOT
SQL Server T-SQL — Native PIVOT Operator
SELECT Region, [Q1], [Q2], [Q3], [Q4]
FROM (SELECT Region, Quarter, Revenue FROM sales) AS src
PIVOT (SUM(Revenue) FOR Quarter IN ([Q1],[Q2],[Q3],[Q4])) AS pvt;
PostgreSQL and MySQL — Conditional Aggregation
SELECT Region,
SUM(CASE WHEN Quarter = 'Q1' THEN Revenue END) AS "Q1",
SUM(CASE WHEN Quarter = 'Q2' THEN Revenue END) AS "Q2",
SUM(CASE WHEN Quarter = 'Q3' THEN Revenue END) AS "Q3",
SUM(CASE WHEN Quarter = 'Q4' THEN Revenue END) AS "Q4"
FROM sales GROUP BY Region ORDER BY Region;
This standard ANSI SQL approach works in any relational database. PostgreSQL uses double-quoted identifiers; MySQL uses backticks — the generator handles this automatically.
Static vs Dynamic Pivots
The generator produces static pivots — column values are hardcoded. If new values appear in your data, you update the query. A dynamic pivot generates columns at runtime from the actual data, requiring dynamic SQL. Use static pivots for fixed dimensions (quarters, months, status codes); use dynamic pivots for variable or growing column sets. See the Dynamic PIVOT generator for runtime column generation.
Worked Examples
Headcount by Department and Level
SELECT department,
COUNT(CASE WHEN level = 'Junior' THEN employee_id END) AS Junior,
COUNT(CASE WHEN level = 'Mid' THEN employee_id END) AS Mid,
COUNT(CASE WHEN level = 'Senior' THEN employee_id END) AS Senior
FROM employees GROUP BY department;
Average Score by Student and Subject
SELECT student_name,
AVG(CASE WHEN subject = 'Maths' THEN score END) AS Maths,
AVG(CASE WHEN subject = 'English' THEN score END) AS English,
AVG(CASE WHEN subject = 'Science' THEN score END) AS Science
FROM grades GROUP BY student_name ORDER BY student_name;
Frequently Asked Questions
What if a cell has no data?
Empty cells return NULL. The generator wraps each expression in COALESCE(..., 0) to return zero instead of NULL — more useful for arithmetic and reporting.
Can I pivot on more than one row field?
Yes — add multiple fields to SELECT and GROUP BY. For a visual interface with multi-field row grouping, use the Visual Pivot Table Builder.
How do I handle NULL values in the column field?
Add WHERE quarter IS NOT NULL to your query. CASE expressions never match NULL, so NULL column values simply produce empty cells.
🚀 Build a Pivot Query — free, browser-based, no sign-up required.
Open Tool →Related Tools & Guides
Further reading: Microsoft — T-SQL Reference
