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 8756915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:11:01+00:00 2026-06-13T14:11:01+00:00

I have a SQL that looks like this: SELECT * FROM foo WHERE foo.bar

  • 0

I have a SQL that looks like this:

SELECT * FROM foo WHERE foo.bar IN (SELECT foobar.bar FROM foobar)

This is not good, right? The nested SELECT will cause things to slow down? How should I query something like this?

  • 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-06-13T14:11:02+00:00Added an answer on June 13, 2026 at 2:11 pm

    The IN clause is perfectly valid SQL, but it’s not always the preferred way. I’ve often found that MySQL’s performance on them is pretty poor, even if foo.bar is indexed.

    If you change it to a join, you have to be careful if the second table has multiple rows that match the join condition for each row of the first table, because the join will produce multiple result rows. If this is possible, the join should be:

    SELECT f.*
    FROM foo f
    INNER JOIN (SELECT DISTINCT bar FROM foobar) fb USING (bar);
    

    The ultimate answer is that you should use EXPLAIN to see how different forms of the query will be executed. But if you’re not experiencing a performance problem in the first place, don’t sweat it.

    If you want all the rows where f.bar does not exist in foobar, NOT IN is usually the most succinct way to write it. But it can also be written using a LEFT OUTER JOIN:

    SELECT f.*
    FROM foo f
    LEFT OUTER JOIN foobar fb USING (bar)
    WHERE fb.bar IS NULL
    

    You don’t need the subquery in this case because you’re only reporting the non-matching rows, so there obviously can’t be multiple matches in the result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sql query that looks like this: SELECT SUM(A) AS expr1 FROM
Suppose I have a SQL query that looks like this: SELECT fName from employees
I have an SQL statement that looks something like: SELECT * FROM table1 AS
I have a lump of SQL that looks a little like this IF NOT
I have a SQL statement that looks like: SELECT [Phone] FROM [Table] WHERE (
Hi there I have an SQL table that looks like this: CREATE TABLE IF
I have a SQL query in my ASP.net web app that looks like this:
I have a line in my SQL Stored Procedure that looks like this (works
I have created this table with a sql query that looks like this $result
I have a Pl/Sql procedure signature that look like this foo(param1 IN type1, param2

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.