Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to figure out how to check if multiple rows exist in multiple tables in one statement.

Given the following tables:

table table_one 

table table_two

This works,

SELECT 1 from table_one where id = 'a'


SELECT 1 from table_two where id = 'aa' 


How can I combine them into one statement?

I tried

 SELECT 1 FROM table_one WHERE id = 'a'
 SELECT 1 FROM  table_two WHERE id = 'aa'

but this just merges the results into one column so I cant tell if a particular table was missing a row.

What i'd like is a result row something this

  table_one  table_two  table_three    table_four
  =========  =========  ===========    ==========
  1          1          [null]         1
share|improve this question
up vote 1 down vote accepted

UNION removes duplicate rows from the overall result, UNION ALL will preserve them.

To know from which table the result came, add a constant value:

SELECT 'one' as source, 
FROM table_one 
WHERE id = 'a'
SELECT 'two',
FROM table_two 
WHERE id = 'aa'
share|improve this answer
works like a charm – sqlnoob Mar 26 '13 at 13:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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