Arunraj
Arunraj

Reputation: 568

One of the query in Union returns no results in postgres

I have a union query with 2 select queries.

sample query:

(SELECT column1, column2 FROM table1 where column1 = 1)
UNION
(SELECT column1, column2 FROM table2 WHERE column1 = 100);

Let's say one of the above queries does not return the result (i.e. No rows found).

I just want the result from one of the queries that return data.

Upvotes: 0

Views: 1086

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269853

First, use union all unless you want to incur the overhead of removing duplicates.

Second, the union/union all should be fine, even if one of the subqueries returns no rows. It will returns the rows from the other subquery.

Upvotes: 2

Related Questions