user3331920
user3331920

Reputation: 41

adding alias row in select query using union

I have a table student(name_std,age_std,address_std). When i write query

select * from Student

it displays

name_std  age_std  address_std
Deepak     22        London
Anjali     16        Srilanka

My requirement is that when I query select * from Student,It should give me output like

name_std  age_std  address_std
**Name       Age       Address**   <<alias row>>
Deepak     22        London
Anjali     16        Srilanka

please suggest me some solution

Upvotes: 0

Views: 3192

Answers (4)

San
San

Reputation: 4538

Assuming age_std as number -

SELECT 'Name'    name_std, 
       'Age'     age_std, 
       'Address' address_std 
FROM   dual 
UNION ALL 
SELECT name_std, 
       To_char(age_std), 
       address_std 
FROM   student; 

Upvotes: 0

Upendra Chaudhari
Upendra Chaudhari

Reputation: 6543

Try UNION like below :

SELECT 'Name' as name_std,'Age' as age_std, 'Address' as address_std FROM dual
UNION
SELECT name_std,to_char(age_std),address_std FROM Student

Upvotes: 1

Incognito
Incognito

Reputation: 3074

You will have to do away with * and use the column names and aliases

SELECT name_Std "Name", age_std "Age", address_std "Address"
FROM   student

Upvotes: 0

Fabio
Fabio

Reputation: 23480

I think you can just use alias to each column to achieve your desired output.

SELECT name_std as Name age_std as Age address_std  as Address
FROM Student

Upvotes: 0

Related Questions