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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:28:55+00:00 2026-05-11T18:28:55+00:00

I have a query that I know can be done using a subselect, but

  • 0

I have a query that I know can be done using a subselect, but due to large table sizes (100k+ rows per table) I would like to find an alternative using a join. This is not a homework question, but it’s easier to share an example in such terms.

Suppose there are two tables:
Students
:id :name
1   Tom
2   Sally
3   Ben

Books
:id  :student_id  :book
1    1                  Math 101
2    1                  History
3    2                  NULL
4    3                  Math 101

I want to find all students who don’t have a history book. Working subselect is:
select name from students where id not in (select student_id from books where book = ‘History’);

This returns Sally and Ben.

Thanks for your replies!

  • 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-11T18:28:55+00:00Added an answer on May 11, 2026 at 6:28 pm

    Is performance the problem? Or is this just some theoretical (homework?) question to avoid a subquery? If it’s performance then this:

    SELECT *
    FROM studnets s
    WHERE NOT EXISTS
    (SELECT id FROM books WHERE student_id = s.id AND book = 'History')
    

    will perform a lot better than the IN you’re doing on MySQL (on some other databases, they will perform equivalently). This can also be rephrased as a join:

    SELECT s.*
    FROM studnets s
    LEFT JOIN books b ON s.id = b.student_id AND b.book = 'History'
    WHERE b.id IS NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 98k
  • Answers 98k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The desired XML is not in a valid format, your… May 11, 2026 at 7:35 pm
  • Editorial Team
    Editorial Team added an answer Here is an example of impersonating another use with WindowsIdentity.Impersonate… May 11, 2026 at 7:35 pm
  • Editorial Team
    Editorial Team added an answer Solution, no. Irritating cumbersome workaround, yes: $stored_exc = null; try… May 11, 2026 at 7:35 pm

Related Questions

To illustrate, assume that I have two tables as follows: VehicleID Name 1 Chuck
OK, I know it can be done, I do it quite often, but why
I'm working on a forums system. I'm trying to allow users to see the
I need to query the database to get the column names , not to
Does anybody know how I can get the number of the elements (rows*cols) returned

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.