Forset1
Forset1

Reputation: 117

Merge two columns from different tables

I have one table "teachers" with a column called 'email', and another table "students" with a column also called 'email'.

I need to show all the e-mails both of the students and the teachers in one single column. That is, one list of all the existing e-mails no matter the position of the owner.

Upvotes: 0

Views: 509

Answers (2)

Patrick Hofman
Patrick Hofman

Reputation: 157136

Use a union:

select email
from   teachers
union
select email
from   students

It concatenates the two results, and shows the overall distinct values. (In contrary to union all that can result in duplicate values since all row values are shown, not only the distinct values)

Just a little extra, if you do want to know the origin of the email address, you could do this:

select 'teacher' origin
,      id
,      email
from   teachers
union
select 'student' origin
,      id
,      email
from   students

Upvotes: 1

Nicola Ferraro
Nicola Ferraro

Reputation: 4189

Use union:

select email from teachers
union
select email from students

Upvotes: 1

Related Questions