Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3229598
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:49:46+00:00 2026-05-17T16:49:46+00:00

I have heard joins should be preferred over nested queries. Is it true in

  • 0

I have heard joins should be preferred over nested queries. Is it true in general? Or there might be scenarios where one would be faster than other:

for e.g. which is more efficient way to write a query?:

Select emp.salary 
from employee emp    
where emp.id = (select s.id from sap s where s.id = 111)

OR

Select emp.salary     
from employee emp   
INNER JOIN sap s ON emp.id = s.id   
WHERE s.id = 111
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:49:46+00:00Added an answer on May 17, 2026 at 4:49 pm

    I have heard joins should be preferred over nested queries. Is it true in general?

    It depends on the requirements, and the data.

    Using a JOIN risks duplicating the information in the resultset for the parent table if there are more than one child records related to it, because a JOIN returns the rows that match. Which means if you want unique values from the parent table while using JOINs, you need to look at using either DISTINCT or a GROUP BY clause. But none of this is a concern if a subquery is used.

    Also, subqueries are not all the same. There’s the straight evaluation, like your example:

    where emp.id = (select s.id from sap s where s.id = 111)
    

    …and the IN clause:

    where emp.id IN (select s.id from sap s where s.id = 111)
    

    …which will match any of the value(s) returned by the subquery when the straight evaluation will throw an error if s.id returns more than one value. But there’s also the EXISTS clause…

    WHERE EXISTS(SELECT NULL 
                   FROM SAP s
                  WHERE emp.id = s.id
                    AND s.id = 111)
    

    The EXISTS is different in that:

    • the SELECT clause doesn’t get evaluated – you can change it to SELECT 1/0, which should trigger a divide-by-zero error but won’t
    • it returns true/false; true based on the first instance the criteria is satisfied so it’s faster when dealing with duplicates.
    • unlike the IN clause, EXISTS supports comparing two or more column comparisons at the same time, but some databases do support tuple comparison with the IN.
    • it’s more readable
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have heard over and over that you should add indexes to any foreign
I have heard umpteen times that we 'should not mix business logic with other
I have heard/read the term but don't quite understand what it means. When should
I have heard there is a way to cheat single inheritance and implement multiple
I have heard that there is lots of interesting and useful WSGI middleware around.
I have heard of desktop applications whose code has been open-sourced, but are there
I have heard mixed opinions over the amount of memory that a byte takes
I have heard that I should avoid using 'order by rand()', but I really
I have heard using PDB files can help diagnose where a crash occurred. My
I have heard the 'shard' technique mentioned several times with regard to solving scaling

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.