Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 148

Pivot Table in MySQL

In this lesson: Convert rows to columns in MySQL using CASE + SUM/GROUP BY — month-wise sales report example with output. In Hindi & English.

What is a pivot

A pivot table turns rows into columns to make a cross-tab summary — like sales totals with each month as its own column. MySQL has no PIVOT keyword, but you can build one easily using a CASE inside SUM. This is called conditional aggregation.

💡 No PIVOT in MySQL — use SUM(CASE WHEN ... THEN value ELSE 0 END) per column.

The starting data

-- sales table (rows)
+--------+-------+--------+
| region | month | amount |
+--------+-------+--------+
| North  | Jan   |   100  |
| North  | Feb   |   150  |
| South  | Jan   |   200  |
+--------+-------+--------+

We want one row per region, with Jan and Feb as columns showing the totals. That is a pivot.

CASE inside SUM

The trick is a CASE that returns the amount only for the matching month, else 0:

SUM(CASE WHEN month = 'Jan' THEN amount ELSE 0 END) AS Jan

For each region, this adds up only the January amounts. Repeat one such expression per column you want.

The pivot query

SELECT region,
    SUM(CASE WHEN month = 'Jan' THEN amount ELSE 0 END) AS Jan,
    SUM(CASE WHEN month = 'Feb' THEN amount ELSE 0 END) AS Feb
FROM sales
GROUP BY region;
+--------+-----+-----+ | region | Jan | Feb | +--------+-----+-----+ | North | 100 | 150 | | South | 200 | 0 | +--------+-----+-----+

GROUP BY region makes one row per region, and each CASE-SUM fills the right month column. The rows have become columns.

Pivoting with COUNT

SELECT region,
    SUM(CASE WHEN status = 'paid' THEN 1 ELSE 0 END) AS paid,
    SUM(CASE WHEN status = 'due' THEN 1 ELSE 0 END) AS due
FROM invoices
GROUP BY region;

To count instead of total, put 1 in the THEN. This counts how many rows fall into each category — a count-based cross-tab, like paid vs due invoices per region.

Notes and limits

  • You list columns manually. Each output column needs its own CASE.
  • Fixed categories work best. Great for months or statuses you know in advance.
  • Dynamic columns need generated SQL. For unknown values, build the query in code.

Common mistakes

  • Forgetting GROUP BY. Without it, the pivot does not collapse to one row per category.
  • Using ELSE NULL with SUM unintentionally. Use ELSE 0 for clean totals.
  • Expecting a PIVOT keyword. MySQL has none; use CASE.
  • Mismatched column labels. Name each pivot column with AS.

Summary

  • A pivot turns rows into columns for a cross-tab summary.
  • MySQL has no PIVOT; use SUM(CASE WHEN ... THEN ... ELSE 0 END).
  • GROUP BY the row category to get one row each.
  • Use 1 in the CASE to pivot with counts.
  • You must list each output column, so it suits fixed categories.

Next, learn grouping in GROUP BY and HAVING, or conditional logic in the CASE expression.

Frequently Asked Questions

What is a pivot table in MySQL?
A pivot table turns rows into columns to create a cross-tab summary — for example, showing total sales with months as columns instead of rows. MySQL has no PIVOT keyword, so you build it using CASE expressions inside aggregate functions like SUM.
How do I create a pivot table in MySQL?
Use SUM with a CASE for each output column: SUM(CASE WHEN month = 'Jan' THEN amount ELSE 0 END) AS Jan, repeated per month, grouped by the row category. Each CASE picks the value for that column.
Why does MySQL use CASE for pivoting?
MySQL does not have a built-in PIVOT command like some databases. The standard technique is conditional aggregation: a CASE inside SUM (or COUNT) evaluates each row into the right column, and GROUP BY collapses them into one row per category.
How do I pivot with counts instead of sums?
Use COUNT with a CASE, or SUM of 1: SUM(CASE WHEN status = 'paid' THEN 1 ELSE 0 END) AS paid. This counts how many rows fall into each column category, giving a count-based cross-tab.
What are the limits of CASE-based pivots?
The main limit is that you must list each output column manually, so it does not adapt automatically to new values. For a fixed, known set of categories (months, statuses) it works well; for dynamic columns you need to build the SQL programmatically.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicRecords from Last 7 Days Your progress: 148 / 151 (98%) · Lesson 149 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free