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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:15:36+00:00 2026-05-15T09:15:36+00:00

I am using IN keyword in the query in the middle of a section.

  • 0

I am using IN keyword in the query in the middle of a section. Since I am using nested query and want to replace In with Exists due to performance issues that my seniors have told me might arise.

Am I missing some column, what you are looking for in this query. This query contain some aliases for readibility.

How can I remove it.

SELECT  TX.PK_MAP_ID AS MAP_ID 
        , MG.PK_GUEST_ID AS Guest_Id 
        , MG.FIRST_NAME 
        , H.PK_CATEGORY_ID AS Preference_Id 
        , H.DESCRIPTION AS Preference_Name 
        , H.FK_CATEGORY_ID AS Parent_Id 
        , H.IMMEDIATE_PARENT AS Parent_Name 
        , H.Department_ID 
        , H.Department_Name 
        , H.ID_PATH, H.DESC_PATH 
FROM          
    dbo.M_GUEST AS MG  
        LEFT OUTER JOIN 
    dbo.TX_MAP_GUEST_PREFERENCE AS TX  
            ON  
                (MG.PK_GUEST_ID = TX.FK_GUEST_ID) 
        LEFT OUTER JOIN 
    dbo.GetHierarchy_Table AS H  
            ON  
                (TX.FK_CATEGORY_ID = H.PK_CATEGORY_ID) 
WHERE      
    (MG.IS_ACTIVE = 1)  
        AND  
    (TX.IS_ACTIVE = 1)  
        AND  
    (H.Department_ID IN -----How to remove this IN operator with EXISTS or Count() 
              ( 
                SELECT C.PK_CATEGORY_ID AS DepartmentId 
                FROM  
                    dbo.TX_MAP_DEPARTMENT_OPERATOR AS D  
                        INNER JOIN 
                    dbo.M_OPERATOR AS M  
                            ON   
                                (D.FK_OPERATOR_ID = M.PK_OPERATOR_ID) 
                                    AND  
                                (D.IS_ACTIVE = M.IS_ACTIVE) 
                        INNER JOIN 
                    dbo.L_USER_ROLE AS R  
                            ON   
                                (M.FK_ROLE_ID = R.PK_ROLE_ID) 
                                    AND  
                                (M.IS_ACTIVE = R.IS_ACTIVE) 
                        INNER JOIN 
                    dbo.L_CATEGORY_TYPE AS C  
                            ON  
                                (D.FK_DEPARTMENT_ID = C.PK_CATEGORY_ID) 
                                    AND  
                                (D.IS_ACTIVE = C.IS_ACTIVE) 
                WHERE       
                    (D.IS_ACTIVE = 1)  
                        AND  
                    (M.IS_ACTIVE = 1)  
                        AND  
                    (R.IS_ACTIVE = 1)  
                        AND  
                    (C.IS_ACTIVE = 1)  
            )--END INNER QUERY 
    )--END Condition 

What new problems might I get if I replace IN with EXISTS or COUNT ?

  • 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-15T09:15:37+00:00Added an answer on May 15, 2026 at 9:15 am

    Basically, as I understand your question, you are asking how can I replace this:

    where H.department_id in (select departmentid from...)
    

    with this:

    where exists (select...) 
    

    or this:

    where (select count(*) from ...) > 1
    

    It is fairly straight forward. One method might be this:

    WHERE...
      AND EXISTS (select c.pk_category_id 
                  from tx_map_department_operator d
                    inner join m_operator as m
                      on    d.fk_operator_id = m.pk_operator_id
                    inner join l_user_role l
                      on m.fk_role_id = r.pk_role_id
                    inner join l_category_type c
                      on d.fk_department_id = c.pk_category_id
                   where h.department_id = c.pk_category_id
                     and d.is_active = 1 
                     and m.is_active = 1 
                     and r.is_active = 1 
                     and c.is_active = 1
                  )
    

    I removed the extra joins on is_active because they were redundant. You should test how it runs with your indexes, because that might have been faster. I doubt it though. But it is worth comparing whether it is faster to add the join clause (join on ... and x.is_active=y.is_active) or to check in the where clause (x.is_active=1 and y.is_active=1 and z.is_active=1...)

    And I’d recommend you just use exists, instead of count(*), because I know that exists should stop after finding 1 row, whereas count probably continues to execute until done, and then compares to your reference value (count > 1).

    As an aside, that is a strange column naming standard you have. Do you really have PK prefixes for the primary keys, and FK prefixes for the foreign keys? I have never seen that.

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

Sidebar

Related Questions

I want to write an SQL query using the LIKE keyword. It should search
I recently modified a MYSQL query using the keyword RLIKE however when I ran
I know that C# has the using keyword, but using disposes of the object
This Javascript code is using the 'this' keyword inside a nested function (which is
I want to compare and select a field from DB using Like keyword or
I have established a relation between project.rb and keyword.rb using has_and_belongs_to_many. I now want
I'm using the youtube api to query by keyword for videos. The result has
I have a MS Access query ProductDetailsAll that is queried using an ASP.net web
I'm using the Youtube API to search using a keyword query: import gdata.youtube import
I have a query that returns valid search results using the IRavenQueryable.Search method. However,

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.