Menu

How to switch rows and columns in SQL?

How to switch rows and columns in SQL? Let's transpose so the rows become columns and columns as rows.

Written by Selva Prabhakaran | 2 min read

Problem

How to switch rows and columns in SQL so that the rows and columns are interchanged?

Given a table named Students with columns Name, Math, and Science representing student names and their scores in Math and Science. We want to transpose this table such that subjects become rows and student names become columns with their respective scores under them.

Input

idattributevalue
1Height175
2Weight70
3Age30

Try Hands-Om: Fiddle

Create Input Table: Gist

Desired Output

This result represents the switched rows and columns, where the attributes (‘Height’, ‘Weight’, ‘Age’) are now columns, and their corresponding values are in the same row.

HeightWeightAge
1757030

Solution:

sql
    CREATE TEMPORARY TABLE switched_table AS
    SELECT
        MAX(CASE WHEN attribute = 'Height' THEN value END) AS "Height",
        MAX(CASE WHEN attribute = 'Weight' THEN value END) AS "Weight",
        MAX(CASE WHEN attribute = 'Age' THEN value END) AS "Age"
    FROM original_table;

Explanation:

The above query creates a temporary table named switched_table and uses conditional aggregation to switch the rows and columns from the original_table.

It creates columns for each unique value in the attribute column (in this example, ‘Height’, ‘Weight’, and ‘Age’) and pivots the data accordingly. The MAX function is used to aggregate the data for each column.

  1. SQL for Data Science – Level 1
  2. SQL for Data Science – Level 2
  3. SQL for Data Science – Level 3
  1. Introduction to SQL
  2. SQL Window Functons – Made Simple and Easy
  3. SQL Subquery

More SQL Questions

  1. How to select only rows with max value on a column?
  2. How to transpose columns to rows in SQL?
  3. How to select first row in each GROUP BY group?
  4. How to retrieve the last record in each group in SQL?
Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Related Course
Master SQL — Hands-On
Join 5,000+ students at edu.darkorange-mallard-189514.hostingersite.com
Explore Course
Free Callback - Limited Slots
Not Sure Which Course to Start With?
Talk to our AI Counsellors and Practitioners. We'll help you clear all your questions for your background and goals, bridging the gap between your current skills and a career in AI.
10-digit mobile number
📞
Thank You!
We'll Call You Soon!
Our learning advisor will reach out within 24 hours.
(Check your inbox too — we've sent a confirmation)
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science