How to Create Pivot Table in MySQL

Last updated on May 17th, 2020 at 06:38 am

Pivot tables are useful for data analysis, allow you to display row values as columns to easily get insights. However, there is no function to create a pivot table in MySQL. So, you need to write SQL query to create pivot table in MySQL. Luckily there are many ways to create Pivot Table in MySQL. Let’s look at each one of them quickly.

 

How to Create Pivot Table in MySQL

 

Here are the steps to create pivot table in MySQL. Let’s say you have the following table

CREATE TABLE exams (
id int(11) NOT NULL auto_increment,
name varchar(15),
exam int,
score int,
PRIMARY KEY (id)
);

insert into exams (name,exam,score) values ('Bob',1,70);
insert into exams (name,exam,score) values ('Bob',2,77);
insert into exams (name,exam,score) values ('Bob',3,71);
insert into exams (name,exam,score) values ('Bob',4,70);

insert into exams (name,exam,score) values ('Sue',1,89);
insert into exams (name,exam,score) values ('Sue',2,87);
insert into exams (name,exam,score) values ('Sue',3,88);
insert into exams (name,exam,score) values ('Sue',4,89);

mysql> select * from exams;
+------+------+------+-------+
| id   | name | exam | score |
+------+------+------+-------+
|   1  |  Bob |   1  |   70  |
|   2  |  Bob |   2  |   77  |
|   3  |  Bob |   3  |   71  |
|   4  |  Bob |   4  |   70  |
|   5  |  Sue |   1  |   89  |
|   6  |  Sue |   2  |   87  |
|   7  |  Sue |   3  |   88  |
|   8  |  Sue |   4  |   89  |
+------+------+------+-------+


Let’s say you want to pivot the table by exam column so as to create 1 row for each student and 1 column for each exam, as shown below.

+--------+-----------+-----------+-----------+------------+
|   name |    exam1  |    exam2  |    exam3  |    exam4   |
+--------+-----------+-----------+-----------+------------+
|   Bob  |     70    |     77    |     71    |     70     |
|   Sue  |     89    |     87    |     88    |     89     |
+--------+-----------+-----------+-----------+------------+

You can create a pivot table in MySQL using IF or CASE statement.

Bonus read: How to Calculate Median in MySQL

Create Pivot Table in MySQL using IF statement

Here’s the SQL query transpose rows to columns using IF statement.

SELECT name,
sum(IF(exam=1, score, NULL)) AS exam1,
sum(IF(exam=2, score, NULL)) AS exam2,
sum(IF(exam=3, score, NULL)) AS exam3,
sum(IF(exam=4, score, NULL)) AS exam4
FROM exams
GROUP BY name;
+--------+-----------+-----------+-----------+------------+
|   name |    exam1  |    exam2  |    exam3  |    exam4   |
+--------+-----------+-----------+-----------+------------+
|   Bob  |     70    |     77    |     71    |     70     |
|   Sue  |     89    |     87    |     88    |     89     |
+--------+-----------+-----------+-----------+------------+

In the above query, you need to group by name column since you want 1 row for each student. Also, you need to provide 1 condition for each column you need to create, that is, 1 condition for each exam

 

Create Pivot Table in MySQL using CASE statement

Here’s the SQL query to convert rows to columns using CASE statement.

SELECT name,
sum(CASE WHEN exam=1 THEN score ELSE NULL END) AS exam1,
sum(CASE WHEN exam=2 THEN score ELSE NULL END) AS exam2,
sum(CASE WHEN exam=3 THEN score ELSE NULL END) AS exam3,
sum(CASE WHEN exam=4 THEN score ELSE NULL END) AS exam4
FROM exams
GROUP BY name;
+--------+-----------+-----------+-----------+------------+
|   name |    exam1  |    exam2  |    exam3  |    exam4   |
+--------+-----------+-----------+-----------+------------+
|   Bob  |     70    |     77    |     71    |     70     |
|   Sue  |     89    |     87    |     88    |     89     |
+--------+-----------+-----------+-----------+------------+

The above query works similar to the one that uses IF condition above. You need to group by the column by which you want to pivot your data, that is, name. Also, you need to define 1 CASE statement for each exam number since you want to create separate columns for each exam.

 

You can also combine exam scores in your pivot table. For example, if you want to add up scores of exam1 and exam2 and show them in same column, you can use the following query.

SELECT name,
sum(CASE WHEN exam=1 or exam=2 THEN score ELSE NULL END) AS exam12,
sum(CASE WHEN exam=3 THEN score ELSE NULL END) AS exam3,
sum(CASE WHEN exam=4 THEN score ELSE NULL END) AS exam4
FROM exams
GROUP BY name;
+--------+------------+-----------+-----------+------------+
|   name |    exam12  |    exam2  |    exam3  |    exam4   |
+--------+------------+-----------+-----------+------------+
|   Bob  |     147    |     77    |     71    |     70     |
|   Sue  |     176    |     87    |     88    |     89     |
+--------+------------+-----------+-----------+------------+

In the above table, the scores of exam 1 and exam 2 have been added and displayed in a single column exam12, by simply modifying the condition of 1st CASE statement, and removing the 2nd CASE statement.

 

You can customize the above queries as per your requirement to create pivot table in MySQL. You can also use a reporting tool to plot the result in a table. Here’s an example of table created using Ubiq.

create pivot table in mysql

 

Did you know you can create pivot tables in Ubiq using just drag & drop?

If you want to create pivot tables, charts & dashboards from MySQL database, you can try Ubiq. We offer a 14-day free trial.