1

Is there a way to combine two SQL queries into a single SELECT query in PostgreSQL?

My requirements are as follows:

SELECT id FROM table1;
SELECT name FROM table2 WHERE table2.id = table1.id;

I think I need to pass values of table1.id in as some sort of dynamic values (loop values) for use in the SELECT statement executed on table2. What is the easiest way to solve this problem, is it possible to do this with stored procedures or functions in PostgreSQL?

1
  • As @ClodoaldoNeto said, what you're looking for here is a simple INNER JOIN clause, plain and simple. Using functions, triggers or even dynamic SQL in this situation would be exuberant overkill (at best) - please don't do it. Commented Sep 14, 2014 at 17:23

1 Answer 1

3
select t1.id, name
from
    table1 t1
    inner join
    table2 t2 using (id)
where t1.id = 1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.