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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:21:20+00:00 2026-06-10T02:21:20+00:00

I have a fairly horrible query using 2 IN statements that needs running on

  • 0

I have a fairly horrible query using 2 “IN” statements that needs running on our database. Firstly the schema (Simplified for this example):

CREATE TABLE [dbo].[SystemUser]
(
    [SystemUserID] [int] IDENTITY(1,1) NOT NULL,
    [FirstName] [nvarchar](50) NULL,
    [Surname] [nvarchar](50) NULL
    CONSTRAINT [PK_ApplicationUser] PRIMARY KEY CLUSTERED 
    (
        [SystemUserID] ASC
    )
)
GO

CREATE TABLE [dbo].[Group]
(
    [GroupID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NULL
    CONSTRAINT [PK_Group] PRIMARY KEY CLUSTERED 
    (
        [GroupID] ASC
    )
)
GO

CREATE TABLE [dbo].[GroupMembership]
(
    [SystemUserID] [int] NOT NULL,
    [GroupID] [int] NOT NULL
    CONSTRAINT [PK_GroupMembership] PRIMARY KEY CLUSTERED 
    (
        [SystemUserID] ASC,
        [GroupID] ASC
    )
)
GO

What I want to do is find all “SystemUser” records that match a list of SystemUserIDs that do NOT have membership to a “Group” that is in a list of GroupIDs.

So 2 seperate lists of IDs beng compared in one query. The fastest way I can think of doing this currently is below:

SELECT SU.SystemUserID
FROM [dbo].[SystemUser] SU 
LEFT JOIN
(
    SELECT GM.SystemUserID
    FROM [dbo].[GroupMembership] GM
    WHERE GM.GroupID IN
    (
        1, 7, 8, 10, 32
    )
) GM ON GM.SystemUserID = SU.SystemUserID
WHERE SU.SystemUserID IN
(
    10, 61, 80, 93, 98
)
AND GM.SystemUserID IS NULL /* Not matched */

Is there anything I’m missing; would a “WHERE NOT EXISTS” check be more efficient? Or can you think of a better way of processing and filtering by the two lists?

  • 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-10T02:21:22+00:00Added an answer on June 10, 2026 at 2:21 am

    Assuming SQL Server 2005 or later,

    SELECT SU.SystemUserID
    FROM [dbo].[SystemUser] SU 
    WHERE SU.SystemUserID IN
    (
        10, 61, 80, 93, 98
    )
    EXCEPT
    SELECT GM.SystemUserID
    FROM [dbo].[GroupMembership] GM
    WHERE GM.GroupID IN
    (
        1, 7, 8, 10, 32
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly simple database that I inherited. For the purposes of this
I have a fairly standard OLTP normalised database and I have realised that I
I have a fairly complex query in SQL that performs a count across two
we have a fairly huge svn repository that i access using git. so far
I have a fairly small MySQL database (a Textpattern install) on a server that
I have a fairly simple query: SELECT invoice_id WHERE employee_id = 'XXXX' AND customer_id
I have a fairly large Excel file. In this file there is a column
I have a fairly complex Core Data database with many entities, attributes and relationships.
I have a fairly simple console app that monitors an exchange mailbox, picks particular
I have a fairly large chunk of code that produces/returns an arraylist of search

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.