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

  • SEARCH
  • Home
  • 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 1063193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:44:50+00:00 2026-05-16T18:44:50+00:00

I have a table linking customers to previous purchases: RecID CustID ProdID 1 20

  • 0

I have a table linking customers to previous purchases:

RecID   CustID   ProdID
  1       20      105
  2       20      300
  3       31      105
  4       45      105
  5       45      300
  6       45      312

I’d like to get a list of CustIDs that bought Item 105,
but did NOT but Item 300.

In this case, CustID 31.

I can’t seem to do it with selects and joins. I am stumped!

I sure would appreciate some help from experienced SQL folks.

Thank you!


Thanks!

I am the original author of the question.

Mark Byers second example with NOT IN works great! (I did not try others after this one worked for me).

His first example with LEFT JOIN did not return any CustIDs…I think I copied it correctly and used the proper table names and column names. So I don’t know why it did not work for me.

Thanks again to all who were so kind as to take the time to write out some SQL for me.

I had to create a new account to leave a comment (I could not login with the account where I created this yesterday, and the password recovery said it couldn’t find me)

  • 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-16T18:44:51+00:00Added an answer on May 16, 2026 at 6:44 pm

    There are three common approaches. Here is a LEFT JOIN approach:

    SELECT T1.CustID
    FROM yourtable T1
    LEFT JOIN yourtable T2 ON T1.CustID = T2.CustID AND T2.ProdId = 300
    WHERE T1.ProdId = 105
    AND T2.ProdId IS NULL
    

    Here is NOT IN:

    SELECT CustID
    FROM yourtable 
    WHERE ProdId = 105
    AND CustID NOT IN
    (
        SELECT CustID
        FROM yourtable 
        WHERE ProdId = 300
    )
    

    Or you could use NOT EXISTS:

    SELECT CustID
    FROM yourtable T1
    WHERE ProdId = 105
    AND NOT EXISTS
    (
        SELECT NULL
        FROM yourtable T2
        WHERE T2.ProdId = 300
        AND T2.CustID = T1.CustID
    )
    

    Which has better performance depends on which database and version you are using.


    For SQL Server the best is to use either NOT IN or NOT EXISTS:

    In SQL Server, NOT EXISTS and NOT IN predicates are the best way to search for missing values, as long as both columns in question are NOT NULL. They produce the safe efficient plans with some kind of an Anti Join.

    LEFT JOIN / IS NULL is less efficient, since it makes no attempt to skip the already matched values in the right table, returning all results and filtering them out instead.

    Source:

    • NOT IN vs. NOT EXISTS vs. LEFT JOIN / IS NULL: SQL Server
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table with 300 000 records (MyISAM). I get record from this table
I have two tables with a linking table describing their many-to-many relationship. TABLE buildings
I have a database two tables and a linking table that I need a
I have table View with grouped style with three sections. Get information from dictionary
I have the following tables: Persons, Person_Categories and Persons_PersonCategories which is a linking table
I'm building a web app that displays a list of customers in a table.
I have a lookup table linking employees to positions held whilst in the company.
I have a list of stories defined and, in another table, a set of
I have a table containing 2 entries. Something like CREATE TABLE `db`.`main` ( `id`
I have 3 tables Student, Course, and the linking table StudentCourse, how do i

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.