PGoCareerGoCareer prep tools
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

SQL · Theory

Pivot Table

← All stacks

Theory

428/439

Pivot Table

MySQL has no PIVOT keyword. You fake a pivot with SUM(CASE WHEN subject='Maths' THEN score END) AS maths, … GROUP BY roll. That turns rows into columns.

Fine for a few known subjects. Pain if subjects grow every week — keep a normalised marks table and pivot in the app.

Trap — inventing PIVOT syntax from SQL Server in a MySQL viva.

On the example next to this theory: Pivot Table: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

Exam tip

SUM(CASE WHEN subject='Maths' THEN score END) GROUP BY roll.

Pivot Table — sample query

-- Pivot Table
CREATE TABLE demo (
  id INTEGER PRIMARY KEY,
  label TEXT NOT NULL,
  qty INTEGER DEFAULT 0
);
INSERT INTO demo (id, label, qty) VALUES
  (1, 'alpha', 2),
  (2, 'beta', 5);
SELECT label, qty FROM demo WHERE qty >= 2 ORDER BY qty DESC;

Pivot Table: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

Short notes

  • DefPivot = rows to columns. MySQL: CASE + GROUP BY.
  • RuleKnown list of columns. No PIVOT keyword.
  • TrapSQL Server PIVOT on MySQL.

Questions

1

Does MySQL have PIVOT?

428 / 439

P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.