Home » SQL

SQL Query to merge N columns as single column from a table

Here, we will learn how to merge N columns as a single column from a table using SQL Query?
Submitted by Preeti Jain, on December 30, 2017

Given a table and we have to merge its N elements as a single column using SQL Query.

TABLE: EMPLOYEE

    ID	NAME	CONTACT	SALARY
    1	PREETI	123456	2000
    2	ARPIT	123789	3000
    3	ADI	147258	5000
    4	APOORV	789632	10000
    5	RIDDHI	148796	20000
    6	REX	148965	30000
    7	WENDY	128659	40000
    8	ANIKET	123489	50000

Case 1: Write a query to merge two columns as single column from a table.

mysql> select  concat(id,'_',name) AS concatenate_two_column from EMPLOYEE;

Output/Result

concate_two_column
1_PREETI
2_ARPIT
3_ADI
4_APOORV
5_RIDDHI
6_REX
7_WENDY
8_ANIKET

Case 2: Write a query to merge three columns as single column from a table.

mysql> select  concat(id,'_',name,'_','salary') AS
 concatenate_three_column from EMPLOYEE;

Output/Result

concate_three_column
1_PREETI_2000
2_ARPIT_3000
3_ADI_5000
4_APOORV_10000
5_RIDDHI_20000
6_REX_30000
7_WENDY_40000
8_ANIKET_50000



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.