DEV Community

What's the difference between the SUB SELECT and LEFT JION SQL query?

In the following example, every query clause has the same result, so what is the difference between the two clauses of the query in terms of speed in returning data from database?

Alt Text

Top comments (5)

Collapse
ย 
adron profile image
Adron Hall โ€ข

Title mispelled + content seems to be missing. Was this supposed to be posted?

Collapse
ย 
alqaqaaben profile image
๐“๐“ต๐“ ๐“ช๐“บ๐“ช๐“ช ๐“‘๐“ฎ๐“ท๐“ฐ๐“ฑ๐“พ๐”ƒ๐“ฒ โ˜ช โ€ข

The post has been modified

Collapse
ย 
adron profile image
Adron Hall โ€ข

Ah, yup. BTW join is mispelled in the title.

The first thing is, the first query is a single query, the second is actually two. Having a nested query requires the inside query being executed and then the query being executed on that data. Depending on how those perform it's pretty standard to see slower performance running two queries instead of one, and in a situation like this where things are being executed in memory this could become substantial.

There are other differences, but that's the first and most notable one.

Thread Thread
ย 
alqaqaaben profile image
๐“๐“ต๐“ ๐“ช๐“บ๐“ช๐“ช ๐“‘๐“ฎ๐“ท๐“ฐ๐“ฑ๐“พ๐”ƒ๐“ฒ โ˜ช โ€ข

This means that the first query will be faster with big databases!

Thread Thread
ย 
adron profile image
Adron Hall โ€ข

Yup.