SQL Query to Convert Rows to Columns in SQL Server
Last Updated :
16 Dec, 2021
In this article we will see, how to convert Rows to Column in SQL Server. In a table where many columns have the have same data for many entries in the table, it is advisable to convert the rows to column. This will help to reduce the table and make the table more readable.
For example, Suppose we have a table given below:
NAME | COLLEGE | ROLL NUMBER | SUBJECT | MARKS |
ROMY | BVP | 0261150 | DBMS | 90 |
ROMY | BVP | 0261150 | NETWORKING | 87 |
ROMY | BVP | 0261150 | GRAPHICS | 95 |
PUSHKAR | MSIT | 0898888 | DBMS | 91 |
PUSHKAR | MSIT | 0898888 | NETWORKING | 90 |
PUSHKAR | MSIT | 0898888 | GRAPHICS | 78 |
It is better if we store the data of this table as:
NAME | COLLEGE | ROLL NUMBER | DBMS | NETWORKING | GRAPHICS |
ROMY | BVP | 0261150 | 90 | 87 | 95 |
PUSHKAR | MSIT | 0898888 | 91 | 90 | 78 |
We can convert rows into column using PIVOT function in SQL.
Syntax:
SELECT (ColumnNames)
FROM (TableName)
PIVOT
(
AggregateFunction(ColumnToBeAggregated)
FOR PivotColumn IN (PivotColumnValues)
) AS (Alias);
//Alias is a temporary name for a table
For the purpose of the demonstration, we will be creating a demo_table in a database called “geeks“.
Step 1: Creating the Database
Use the below SQL statement to create a database called geeks.
Query:
CREATE DATABASE geeks;
Step 2: Using the Database
Use the below SQL statement to switch the database context to geeks.
Query:
USE geeks;
Step 3: Table definition
We have the following demo_table in our geek's database.
Query:
CREATE TABLE demo_table(
NAME varchar(30),
COLLEGE varchar(30),
EXAM_DATE DATE,
SUBJECTS varchar(30),
MARKS int);
Step 4: Insert data into the table
Query:
INSERT INTO demo_table VALUES ('ROMY', 'BVCOE',
'12-OCT-2021', 'DBMS', 90),
('ROMY', 'BVCOE', '12-OCT-2021', 'NETWORKING', 90),
('ROMY', 'BVCOE', '12-OCT-2021', 'GRAPHICS', 100),
('ROMY', 'BVCOE', '12-OCT-2021', 'CHEMISTRY', 98),
('ROMY', 'BVCOE', '12-OCT-2021', 'MATHEMATICS', 78),
('PUSHKAR', 'MSIT', '14-OCT-2021', 'NETWORKING' , 97),
('PUSHKAR', 'MSIT', '14-OCT-2021', 'GRAPHICS', 98),
('PUSHKAR', 'MSIT', '14-OCT-2021', 'CHEMISTRY', 79),
('PUSHKAR', 'MSIT', '14-OCT-2021', 'MATHEMATICS', 79),
('PUSHKAR', 'MSIT', '14-OCT-2021', 'DBMS', 79);
Step 5: See the content of the table
Use the below command to see the content of the demo_table:
Query:
SELECT * FROM demo_table;
Output:

Step 6: Using pivot function in order to convert row into column.
Query:
SELECT * FROM demo_table
PIVOT
(AVG(MARKS) FOR SUBJECTS IN (DBMS,NETWORKING,
GRAPHICS, CHEMISTRY, MATHEMATICS)) AS PivotTable;
We have used AVERAGE aggregate function because average of one value is the value itself.
Output:

We can see that rows get transformed to column.
Similar Reads
SQL Query to Convert DateTime to Date in SQL Server
In SQL Server, working with DateTime data types can be a bit complex for beginners. This is because DateTime includes both the date and time components, while many scenarios only require the date. Whether you're working with large datasets, performing data analysis, or generating reports where time
5 min read
How to Efficiently Convert Rows to Columns in SQL?
In SQL, rows and columns are the fundamental building blocks of a database. Rows represent individual records, while columns represent the attributes or characteristics of those records. However, there may be instances where we need to convert rows to columns in order to better analyze and manipulat
5 min read
How to Efficiently Convert Rows to Columns in PostgreSQL?
Converting rows to columns, often referred to as pivoting or transposing, is a crucial aspect of data transformation in SQL. This technique is useful for improving data readability, facilitating analysis, aligning data formats with the requirements of reporting tools, and optimizing queries. In Post
5 min read
How to Efficiently Convert Rows to Columns in PL/SQL?
In Oracle PL/SQL, converting rows into columns is a common operation, especially useful for reporting, data analysis, and reformatting data for easy visualization. PL/SQL, or Procedural Language/Structured Query Language, is a powerful procedural extension to SQL, created by Oracle, that integrates
5 min read
SQL Server | Convert Tables in T-SQL into XML
XML (Extensible Markup Language) is a widely-used markup language designed to store and transfer structured data between different systems and platforms. While HTML focuses on the visual representation of dataOverviewXML is similar to HTML which is designed to structure and store data for sharing ac
3 min read
How to Set a Column Value to NULL in SQL Server
In the world of database management, SQL Server is a leading and extensively utilized system. A fundamental task within SQL Server is manipulating data within tables, and setting a column value to NULL is a common operation. Whether it's for maintaining data integrity, performing updates, or meeting
4 min read
How to Get the Data Type of Columns in SQL Server?
SQL Server is a widely used Relational Database Management System (RDBMS) that allows users to create and manage databases effectively. SQL Server offers the SQL Server Management Studio which defines the database development and administration. In this article, we will learn how to retrieve the dat
4 min read
How to Convert Rows into Columns in MySQL?
Converting rows into columns, also known as pivoting or transposing, is a common operation in DBMS, and MySQL provides robust functionality for achieving this transformation. This process is useful to reshape data for better analysis or reporting. This guide will explore the syntax, usage, and examp
3 min read
Combine Rows into String in SQL Server
To combine rows into a string in SQL Server, use the SQL COALESCE() function or the SQL CONCAT() function. COALESCE() function in SQL is used to handle null values. It returns non-null values from a row, which can be concatenated into string. CONCAT() function in SQL is used to concatenate two or mo
2 min read
How to Concatenate Text From Multiple Rows in SQL Server
When we fetch data from a table, there may be requirements to concatenate the text value of a table column in multiple rows into a single row. There are many ways we can concatenate multiple rows into single row SQL Server. We can use different ways based on need and convenience. In this article, we
6 min read