Share via

How to replace where exists with inner join ?

ahmed salah 3,216 Reputation points
2021-09-13T10:36:19.893+00:00

I work on sql server 2012 i face issue ican't replace (where exists)
by inner join
so How to do it

SELECT pr.partid

        from 

                 parts.Nop_Part pr with(nolock) 
                inner join extractreports.dbo.RequiredPartsPL rp with(nolock) on rp.partid=pr.partid

                inner join Parts.Nop_PackageAttribute pa WITH(NOLOCK) on pa.packageid=pr.packageid 

                inner JOIN dbo.Core_DataDefinitionDetails dd WITH(NOLOCK) ON pa.[Key] = dd.ColumnNumber --and dd.acceptedvalueid=64


                where     
  exists(select 1 from extractreports.dbo.getrelatedkeyandvaluepackage g where g.Featureid=dd.acceptedvalueid and g.valueid=pa.value  )

                group by pr.partid 

so how to replace statement above by inner join instead of using where exists
statment

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


2 answers

Sort by: Most helpful
  1. MelissaMa-msft 24,246 Reputation points Moderator
    2021-09-14T01:52:55.337+00:00

    Hi @ahmed salah ,

    What is the purpose for replacing exists with inner join?

    We recommend that you post CREATE TABLE statements for your tables together with INSERT statements with sample data and the expected result of the sample after executing your query.

    Actually EXISTS and INNER JOIN have their own uses. If you only would like to return some records from a set, use EXISTS. If you also would like to return the columns in the another table, use INNER JOIN. You only have to notice the situation of one-many.

    I made a test from my side by creating your tables and inserting some sample data and it was working like below:

    SELECT distinct pr.partid    
    from   parts.Nop_Part pr with(nolock)   
    inner join extractreports.dbo.RequiredPartsPL rp with(nolock) on rp.partid=pr.partid                      
    inner join Parts.Nop_PackageAttribute pa WITH(NOLOCK) on pa.packageid=pr.packageid   
    inner JOIN dbo.Core_DataDefinitionDetails dd WITH(NOLOCK) ON pa.[Key] = dd.ColumnNumber  
    inner join extractreports.dbo.getrelatedkeyandvaluepackage g on g.Featureid=dd.acceptedvalueid and g.valueid=pa.value   
    

    Best regards,
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

  2. Viorel 126.9K Reputation points
    2021-09-13T10:46:43.5+00:00

    Try something like this:

    inner join (select top(1) Featureid, valueid from extractreports.dbo.getrelatedkeyandvaluepackage) g on g.Featureid = dd.acceptedvalueid and g.valueid = pa.value 
    

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.