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

The Archive Base Latest Questions

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

I have a SELECT statement that works, and runs fast enough on my tables

  • 0

I have a SELECT statement that works, and runs fast enough on my tables (<0.01sec on 50k+ products, 3k+ categories). But in my mind it’s not very elegant and would like to hear any suggestions on making it better.

There are 3 tables of interest:

  • products – key productID
  • categories – key categoryID
  • products_tree – link table (categories contain many products, products can belong to many categories)

I have a list of excluded categoryIDs [e.g. 1040,1050,1168] I want to select all the productIDs that belong to one of these excluded categories only if the product doesn’t belong to another NON-excluded category

My Query looks like this:

SELECT DISTINCT productID  FROM products_tree  WHERE    categoryID IN (1040,1050,1168)    AND productID NOT IN     ( SELECT DISTINCT productID        FROM products_tree        WHERE        categoryID NOT IN (1040,1050,1168)     ); 
  • 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. 2026-05-11T05:55:55+00:00Added an answer on May 11, 2026 at 5:55 am

    I can think of a few methods, each of which perform differently depending on indexes and your particular database implementation. Some that may look slow can be optimised in ways you may not have imagined and so it’s worth trialling them all and comparing execution plans to see what is happening…

    Note1: I use GROUP BY rather than DISTINCT, this is because it allows the omptimiser to make use of indexes. I’ve seen implementations work out that they can turn the DISTINCT in to a GROUP BY, but it’s highly worth using GROUP BY in the fist place to be sure. It also gets you thinking about indexes, which is never a bad thing.

    Note2: Some queries like this take a while to optimise, as there are many options for the optimiser to evaluate. It is therefore often worth compiling all the different options in to stored procedures and comparing the execution of those stored procedures. This ensures your compare actually Query Time and not different Compile Times.

    SELECT    [tree].productID FROM    products_tree AS [tree] WHERE    [tree].productID IN (1040,1050,1168)    AND NOT EXISTS (SELECT * FROM products_tree WHERE productID = [tree].productID AND categoryID NOT IN (1040,1050,1168))  GROUP BY    [tree].productID 

    SELECT    [tree].productID FROM    products_tree AS [tree] LEFT OUTER JOIN    (       SELECT          productID       FROM          product_tree       WHERE          productID NOT IN (1040,1050,1168)       GROUP BY          productID     )     AS [ok_products]        ON [ok_products].productID = [tree].productID WHERE    [tree].productID IN (1040,1050,1168)    AND [ok_products].productID IS NULL  GROUP BY    [tree].productID 

    SELECT    [tree].productID FROM    products_tree AS [tree] GROUP BY    [tree].productID HAVING        MAX(CASE WHEN [tree].productID     IN (1040,1050,1168) THEN 1 ELSE 0 END) = 1    AND MAX(CASE WHEN [tree].productID NOT IN (1040,1050,1168) THEN 1 ELSE 0 END) = 0 

    There are others, and variations of each, but this should give you a very good start. But I really would stress the use of GROUP BY and the consideration to INDEXES 🙂

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

Sidebar

Related Questions

I have this SQL statement that works but takes a while to execute I
I have an SQL statement that works SELECT * FROM eventsTable WHERE columnName='Business' I
I have a SQL Statement that works as I want. select COUNT(*), MIN(emailed_to)from email.email_archive
I have A Script that has a Select statement to go to multiple sub
I have to following SQL Statement that I want to conver to LINQ Select
I have a query that makes several calls within a SELECT statement to a
I have created a procedure and used the below statement inside that . select
I have a batch file that runs some SELECT queries using sqlcmd, puts the
I have a simple SQL Statement that works perfectly in SQL Server: DECLARE @ID
I have SQL SELECT statement that returns: Error: ORA-00600: internal error code, arguments: [qerpfAllocateR],

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.