Skip to content
← All Guides
🔒 No Upload Required ✅ Free Forever 🌐 Browser-Based
SQL Tools

SQL PIVOT Generator: Build Cross-Tab Queries Without Writing Code

By Bill Crawford  ·  February 2026  ·  8 min read  ·  Last updated January 15, 2026

Connect on LinkedIn →

🚀 Ready to try it? Build a Pivot Query — free, browser-based, no sign-up.

Open Tool →

Table of Contents

  1. What Is a PIVOT Query?
  2. Step-by-Step Guide
  3. SQL Dialects Explained
  4. Static vs Dynamic Pivots
  5. Worked Examples
  6. FAQ

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

  1. Enter your table name.
  2. Set the Row Field — the dimension that becomes row labels (Region, Product, Department).
  3. Set the Column Field — whose distinct values become column headers (Quarter, Month, Status).
  4. Set Value Field and Aggregate — the numeric field and function: SUM, COUNT, AVG, MIN, MAX.
  5. Enter column values — the distinct values for the column field (Q1, Q2, Q3, Q4).
  6. Select SQL dialect — SQL Server, PostgreSQL, MySQL, or Generic SQL.
  7. 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

BC
Bill Crawford
Founder, Data Conversion Center

Bill Crawford is a data systems developer and technical founder with over 30 years of professional experience in accounting, finance, and business operations.

He holds a Bachelor's degree in Accounting and has spent more than three decades working within financial and operational environments. Over the past 10 years, he has been heavily involved in the development, implementation, and refinement of financial and enterprise data systems for both Fortune 500 companies and smaller organizations.

His work bridges finance and technology — combining deep domain knowledge in structured reporting and accounting workflows with hands-on SQL development and database architecture experience.

Bill founded DataConversionCenter.com to build practical, browser-based tools that simplify complex data challenges, including:

Rather than focusing on theoretical examples, his tools and articles are informed by real-world challenges encountered in enterprise reporting systems, financial databases, and operational data environments.

Professional Background
  • Bachelor's Degree in Accounting
  • 30+ years in accounting and finance
  • 10+ years deeply involved in financial and enterprise systems development
  • Experience supporting Fortune 500 and small-to-mid-sized organizations
  • Hands-on SQL development across relational database platforms

Bill's mission is to reduce friction in data workflows — particularly for professionals working with structured financial, operational, and reporting data.