๐Ÿ“Š We use cookies & analytics to improve your experience. Learn more
Latest

Data Analytics with SQL & Excel Module 4 ยท Lesson 12 of 15

0% Exit to course

Module 4 ยท Dates, windows and time Lesson 12 of 15

Pivot a table in SQL with CASE

Learning objective

Not every database has PIVOT, but CASE works everywhere: turn one column of values into several columns of counts, the same shape a spreadsheet pivot gives you.

Worked example

SUM(CASE WHEN type = 'remote' THEN 1 ELSE 0 END) AS remote counts the remote rows as a column, and the same trick gives onsite and hybrid in one grouped query.

Your practice task

Write one query showing city as rows and remote, onsite and hybrid as columns, with a count in each cell.

Do this before moving on. Reading a solution teaches the shape of it; typing it teaches you.

Key takeaway

Not every database has PIVOT, but CASE works everywhere: turn one column of values into several columns of counts, the same shape a spreadsheet pivot gives you.

Create free account or sign in

Almost there

15 lessons left of 15

Finish every lesson to unlock the certificate. Each one ends in a practice task โ€” do them and the final project brief writes itself.

Next recommended course Interview DSA Crash Course