SQL “select where not in subquery” returns no results

后端 未结 11 1854
滥情空心
滥情空心 2020-11-29 14:46

Disclaimer: I have figured out the problem (I think), but I wanted to add this issue to Stack Overflow since I couldn\'t (easily) find it anywhere. Also, someone might

相关标签:
11条回答
  • 2020-11-29 15:25

    this worked for me :)

    select * from Common

    where

    common_id not in (select ISNULL(common_id,'dummy-data') from Table1)

    and common_id not in (select ISNULL(common_id,'dummy-data') from Table2)

    0 讨论(0)
  • 2020-11-29 15:29
    select *
    from Common c
    where not exists (select t1.commonid from table1 t1 where t1.commonid = c.commonid)
    and not exists (select t2.commonid from table2 t2 where t2.commonid = c.commonid)
    
    0 讨论(0)
  • 2020-11-29 15:33
    select *,
    (select COUNT(ID)  from ProductMaster where ProductMaster.CatID = CategoryMaster.ID) as coun 
    from CategoryMaster
    
    0 讨论(0)
  • 2020-11-29 15:33

    Please follow the below example to understand the above topic:

    Also you can visit the following link to know Anti join

    select department_name,department_id from hr.departments dep
    where not exists 
        (select 1 from hr.employees emp
        where emp.department_id=dep.department_id
        )
    order by dep.department_name;
    
    DEPARTMENT_NAME DEPARTMENT_ID
    Benefits    160
    Construction    180
    Contracting 190
    .......
    

    But if we use NOT IN in that case we do not get any data.

    select Department_name,department_id from hr.departments dep 
    where department_id not in (select department_id from hr.employees );
    

    no data found

    This is happening as (select department_id from hr.employees) is returning a null value and the entire query is evaluated as false. We can see it if we change the SQL slightly like below and handle null values with NVL function.

    select Department_name,department_id from hr.departments dep 
    where department_id not in (select NVL(department_id,0) from hr.employees )
    

    Now we are getting data:

    DEPARTMENT_NAME DEPARTMENT_ID
    Treasury    120
    Corporate Tax   130
    Control And Credit  140
    Shareholder Services    150
    Benefits    160
    ....
    

    Again we are getting data as we have handled the null value with NVL function.

    0 讨论(0)
  • 2020-11-29 15:37

    I had an example where I was looking up and because one table held the value as a double, the other as a string, they would not match (or not match without a cast). But only NOT IN. As SELECT ... IN ... worked. Weird, but thought I would share in case anyone else encounters this simple fix.

    0 讨论(0)
  • 2020-11-29 15:38

    Table1 or Table2 has some null values for common_id. Use this query instead:

    select *
    from Common
    where common_id not in (select common_id from Table1 where common_id is not null)
    and common_id not in (select common_id from Table2 where common_id is not null)
    
    0 讨论(0)
提交回复
热议问题