How to Build a Pivot Table in MariaDB
To pivot product sales in MariaDB, group by company and add a conditional sum for each product, such as SUM(IF(product = 'X', sales, 0)) AS X. The result is one row per company and one column per selected product, with multiple matching sales rows added together. You can also use SUM(CASE ... END).
Prepare sample companies, products, and sales
Select a practice database and run this setup. Companies will be rows, products will be columns, and the cells will hold sums. Alpha has two rows for X and a zero sale for Z. Beta has no row for Y and has a product W outside the fixed pivot columns.
CREATE TEMPORARY TABLE pivot_sales (
company VARCHAR(20) NOT NULL,
product CHAR(1) NOT NULL,
sales INT NOT NULL
);
INSERT INTO pivot_sales (company, product, sales) VALUES
('Alpha', 'X', 10), ('Alpha', 'X', 15),
('Alpha', 'Y', 8), ('Alpha', 'Z', 0),
('Beta', 'X', 4), ('Beta', 'Z', 12),
('Beta', 'Z', 3), ('Beta', 'W', 7);
The temporary table is visible only in the connection that created it. Keep using that connection for the remaining examples. Your practice account needs permission to create temporary tables.
Turn products into columns with SUM and IF
GROUP BY collects the rows for each company. For each product column, IF() supplies the sales amount for a matching row and zero for other products. SUM() adds the resulting values within the company.
SELECT company,
SUM(IF(product = 'X', sales, 0)) AS X,
SUM(IF(product = 'Y', sales, 0)) AS Y,
SUM(IF(product = 'Z', sales, 0)) AS Z
FROM pivot_sales
GROUP BY company
ORDER BY company;
| Company | X | Y | Z |
|---|---|---|---|
| Alpha | 25 | 8 | 0 |
| Beta | 4 | 0 | 15 |
For Alpha, X is 10 + 15 = 25. For Beta, Z is 12 + 3 = 15. Alpha’s Z is a recorded zero, while Beta’s Y is zero because no row matched.
Build the same pivot with CASE
With CASE, put the condition in WHEN, the value to sum in THEN, and the fallback in ELSE. The next query performs the same calculation as the IF() version.
SELECT company,
SUM(CASE WHEN product = 'X' THEN sales ELSE 0 END) AS X,
SUM(CASE WHEN product = 'Y' THEN sales ELSE 0 END) AS Y,
SUM(CASE WHEN product = 'Z' THEN sales ELSE 0 END) AS Z
FROM pivot_sales
GROUP BY company
ORDER BY company;
The two result rows and all three product totals match the table above. If one product column needs an extra condition, add it to that column’s WHEN expression.
Distinguish zero sales from missing rows
To return NULL when no product row matches, omit ELSE 0 from SUM(CASE ... END). To count matching rows, return 1 only on a match and pass the expression to COUNT(), which ignores NULL.
SELECT company,
SUM(CASE WHEN product = 'Y' THEN sales END) AS y_sales_or_null,
COUNT(CASE WHEN product = 'Y' THEN 1 END) AS y_rows,
COUNT(CASE WHEN product = 'Z' THEN 1 END) AS z_rows
FROM pivot_sales
GROUP BY company
ORDER BY company;
For Beta, y_sales_or_null is NULL and y_rows is 0. Alpha’s z_rows is 1, so its zero Z total came from a real row.
The example makes sales NOT NULL. If your data permits missing sales values, SUM() skips them. To tell how many matching rows exist and how many have a known amount, compare COUNT(CASE WHEN ... THEN 1 END) with COUNT(CASE WHEN ... THEN sales END).
Choose an output when products change
A fixed pivot has only the columns defined in its SELECT list. A new product such as W does not add a column automatically. When products change frequently, group by both company and product and let the report or application turn those rows into columns. See the MariaDB guide to pivoting.
SELECT company, product, SUM(sales) AS total_sales
FROM pivot_sales
GROUP BY company, product
ORDER BY company, product;
The grouped rows include Beta’s W sales of 7. The fixed columns total 4 + 0 + 15 = 19 for Beta, while all its products total 26. Add a W column or use the grouped rows when the report must cover all products.


