0

Say for some reason I have employees in two separate tables, employee1 and employee2

I just want to add them together, as if they are stacked on top of each other.

something like:

select all from employee1 and employee2 where name = bubba

i know im generalizing, this will be in postgres eventually so if there are any specifics there i should watch for thanks

1
  • 1
    fastest gun in the west - observe. Oct 5, 2009 at 15:01

10 Answers 10

11
SELECT field1, field2, field2 FROM tableA WHERE field1='x'
UNION
SELECT field1, field2, field2 FROM tableB WHERE field1='x'

Use UNION ALL if you want every record, even repeats.

2
  • 1
    +1 for writing out column names, this is important as the columns may not exist in the same order in each table, and bonus credit for explaining use of UNION ALL. Oct 5, 2009 at 15:05
  • 1
    +1 about column names too. Its to generic and open for failure if just an "*" from a table. Especially if different column names, sizes, sequences, etc will cause the UNION to crash. Better to be explicit about what is desired than generic and fail
    – DRapp
    Oct 5, 2009 at 15:13
4

You'll just want to do a union

select * from Employee1 where name = 'bubba'
union
select * from Employee2 where name = 'bubba'
1
3

You will want to use the UNION keyword

select * from employee1 where name = 'bubba'
union
select * from employee2 where name = 'bubba'
5
  • Why was this downvoted? Was it edited? As it is, it's correct. Oct 5, 2009 at 15:03
  • No edits, my guess is someone with an answer is down voting other answers so theirs appear on top.
    – Bob
    Oct 5, 2009 at 15:06
  • I'll upvote you, since I hate when people downvote correct answers
    – CaffGeek
    Oct 5, 2009 at 15:08
  • You should never use select * in a union statement. Always explicitly state the column names. Other wise things will break if one table is changed t and the other is not or even worse if some idiot decides to rearrange the column order on a table.
    – HLGEM
    Oct 5, 2009 at 17:19
  • UNION ALL should be used as dupes are wanted
    – jle
    Nov 2, 2009 at 2:13
2

In most databases what you are requesting is called a UNION and written like this:

select all from employee1 where name = bubba

UNION

select all from employee2 where name = bubba

This comes from Relational Algebra's "union" operator, one of its primitives.

Please note that UNION follows set unions, namely, it will, for any rows which are duplicate between E1 and E2 tables, only select ONE copy of the row. If you wish to select all copies, use "UNION ALL" operator.

1

I think you refer to the UNION operation.

1

If the table have the same schema then

SELECT * FROM employee1 UNION SELECT * FROM employee2

Both tables must have the same number of columns and the columns must be of a similar type.

1

Its a union I guess

Select * from employee 1 where name = 'bubba'

union

select * from employee2 where name = 'bubba'

Use union all if you want duplicates as well.

1

What you want is a "union all":

select * from employee1
union all
select * from employee2;

Column types and order must match, or you'll need to provide column lists rather than "*" in the select list. A "where" clause can be added to either or both "select" statements.

Without "all", any duplicate rows between the two queries will be collapsed into a single row. If that's what you want instead, just remove "all".

0

As others have mentioned, you want UNION. However if you truely want the results stacked, you should use UNION ALL. UNION will remove the dupes, UNION ALL with include them. See http://www.postgresql.org/docs/8.2/interactive/queries-union.html

0

For what it's worth, union all is quicker to execute as it doesn't have to sort to get rid of duplicates in the two sets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.