vivek SB
vivek SB

Reputation: 1

I have multiple queries want to merge all query results into the same table

table1:                     table2
studentname                StudentAge   
 stuart                      18  

I have following queries like 1)

select studentname as sname from table1;
  sname
  Stuart

2)

select StudentAge as age from table2;
  age
  18

I want to display the query results in a table having two columns as:

 category         Values
  sname          Stuart
  age              18

Upvotes: 0

Views: 100

Answers (3)

Yogesh Sharma
Yogesh Sharma

Reputation: 50163

This suggests me union all :

select t.category, t.values
from ( (select t.studentname as values, 'studentname' as category from table1 t) union all
       (select t.stuedentage, 'age' as category from table2 t)
     ) t;

Upvotes: 0

Given the data you've presented, with only a single row in each table, you can use a CROSS JOIN to join them:

select t1.studentname, t2.StudentAge
  from table1 t1
  cross join table2 t2

This will perform a Cartesian join, where each row in each table is joined to every row in the other table, and the number of returned rows is the product of the number of rows in the two tables.

db<>fiddle here

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269493

If your tables have at most one row, you can use subqueries:

select (select studentname from table1) as sname
       (select StudentAge from table2) as age

Upvotes: 1

Related Questions