41

I have made a draft remote application on top of libpq for PostrgreSQL. It behaves well, but I have profiled the general functioning of the application. For each final business result that I produce, it happens that I call something like 40 select clause (over tcpip).

I have reminiscences from SQL-Server reminding me to minimize the number of interactions between my remote application and the database. Having analyzed my selects, I do think I could reduce this number to 3 SELECT clauses, using joins. But I don't remember the syntax for using the result of a SELECT in another SELECT.

E.g.:

SELECT * FROM individual
INNER JOIN publisher
ON individual.individual_id = publisher.individual_id
WHERE individual.individual_id = 'here I would like to use the results of a another select'

This other SELECT would be simply of the kind:

SELECT identifier FROM another_table WHERE something='something'

Here is the simplified tables layout, declined a number of times for different item_types ... (3 totally different types, hence the 3 SQL queries if optimized).

table passage
 id_passage PK
 business_field_passage bytea
table item
 id_item PK
 id_passage FK
 business_field_item text
table item_detail
 id_item_detail PK
 id_item FK
 business_field_item_detail text
 image_content bytea

There are several id_item for one id_passage.
There are several id_item_detail for one id_item.

How would you write that?
What is the name for describing the action of redirecting one select into another (if any)?

Erwin Brandstetter
186k28 gold badges463 silver badges636 bronze badges
asked Jan 25, 2013 at 15:38
3
  • 1
    postgresql.org/docs/9.1/static/… Commented Jan 25, 2013 at 15:41
  • are you referring to 7.2.1.3. Subqueries ? Commented Jan 25, 2013 at 15:44
  • Possibly yes, together with the JOIN part. Commented Jan 25, 2013 at 15:47

2 Answers 2

53

Is this what you're aiming for? Make sure the fields that are being compared are comparable (i.e. both fields are numeric, text, boolean, etc).

SELECT * FROM Individual
INNER JOIN Publisher
ON Individual.IndividualId = Publisher.IndividualId
WHERE Individual.IndividualId = (SELECT someID FROM table WHERE blahblahblah)

If you wish to select based on multiple values:

SELECT * FROM Individual
INNER JOIN Publisher
ON Individual.IndividualId = Publisher.IndividualId
WHERE Individual.IndividualId IN (SELECT someID FROM table WHERE blahblahblah)
answered Jan 25, 2013 at 16:32
3
  • can it be that straight forward ?? does it still work if SELECT someID FROM table WHERE blahblahblah has multiple records ? I'm gonna check that right now. Commented Jan 25, 2013 at 16:38
  • Which query is selecting multiple records? It can work if you're selecting multiple records, but if you could show us your table layouts that would help us refine the answer. Commented Jan 25, 2013 at 16:40
  • 4
    WHERE Individual.IndividualId IN... looks good. Commented Jan 25, 2013 at 17:00
13

You can just rewrite that as another JOIN. This is typically simplest and fastest:

SELECT i.*, p.*
FROM individual i
JOIN publisher p USING (individualid)
JOIN another_table a ON a.identifier = i.individualid
WHERE a.something = 'something';

Only equivalent if there is at most one match in another_table. (This is true in any case if a.identifier is the PK).

I also simplified somewhat and did away with the gratuitous CaMel-case spelling of identifiers.

answered Jan 26, 2013 at 1:07
5
  • 2
    Yes, this. I die a little inside whenever I see IN (SELECT ..) syntax. Commented Jan 26, 2013 at 12:56
  • @MarkStorey-Smith Do you mean that it's more than simpler and faster: this a standard of sql coding to use another join instead of a in ( select...) In such case I should also attribute the good answer to Erwin. Commented Jan 26, 2013 at 19:27
  • 1
    @StephaneRolland Whether its faster or not will be platform and version dependent. SQL Server 2008+ for example will generate identical execution plans for INNER JOIN and IN (SELECT ...) syntax. No idea on whether same applies to PostgreSql. Performance aside, the IN (SELECT ...) style leaves me wondering if the author has fully grasped the semantics and concepts of SQL. AngrySpartan has answered your original question correctly. ErwinBrandstetter has shown you the way you should do it :). Commented Jan 26, 2013 at 19:41
  • 7
    @MarkStorey-Smith: a JOIN is not always equivalent to an IN condition. The question is not which one is faster, the question is which one is correct. Commented Dec 11, 2015 at 11:48
  • I have only partially grasped SQL semantics/concepts. If the SELECT statement in the IN (SELECT...) portion of the overall query returns more than one row, how can the "IN (SELECT...)" syntax be replaced with JOINs ? Commented Jun 1, 2020 at 0:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.