Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SQL query not working with "not in"

5301a04d-c4ab-45c5-a875-276bc7ed9282Sep 14 2015 — edited Sep 14 2015

Hi all, how are you?
I'm Jaime from Spain and I write here because I do not understand why 2 queries that, in my opinion, should return the same result do not return the same. I will expose the case:

 1.- If I use the next query:

           select a.business\_email, a.status from

           (

               SELECT USER\_EMPLOYEE\_REF.BUSINESS\_EMAIL, user\_employee\_ref.status

               FROM SWITCH\_PERMITTED, USER\_EMPLOYEE\_REF

               WHERE USER\_EMPLOYEE\_REF.switch\_date is null

               and (SWITCH\_PERMITTED.EMAIL1= USER\_EMPLOYEE\_REF.BUSINESS\_EMAIL or SWITCH\_PERMITTED.EMAIL2= USER\_EMPLOYEE\_REF.BUSINESS\_EMAIL

               or SWITCH\_PERMITTED.EMAIL3= USER\_EMPLOYEE\_REF.BUSINESS\_EMAIL)

          ) a

 I obtain the next result:

           BUSINESS\_EMAIL                              STATUS

           "[JAMES.CORTES@a.COM](mailto:JAMES.CORTES@TELEFONICA.COM)"          "INACTIVE"

           "[JAMES.CORTES@a.COM](mailto:JAMES.CORTES@TELEFONICA.COM)"          "INACTIVE"

           "[JOSE.CORONEL@a.COM](mailto:JOSE.CORONEL@TELEFONICA.COM)"          "ACTIVE"

           "[JOSE.CORONEL@a.COM](mailto:JOSE.CORONEL@TELEFONICA.COM)"          "INACTIVE"

           "[JOSE.CORONEL@a.COM](mailto:JOSE.CORONEL@TELEFONICA.COM)"          "ACTIVE"

    2.- From this set of rows, I want to obtain the rows of the email that it is always INACTIVE. With that purpouse, I create the next query BUT IT RETURNS 0 rows (WHY????):

           select business\_email, status from

                (

                    SELECT USER\_EMPLOYEE\_REF.BUSINESS\_EMAIL, user\_employee\_ref.status

                    FROM SWITCH\_PERMITTED, USER\_EMPLOYEE\_REF

                    WHERE USER\_EMPLOYEE\_REF.switch\_date is null

                    and (SWITCH\_PERMITTED.EMAIL1= USER\_EMPLOYEE\_REF.BUSINESS\_EMAIL or SWITCH\_PERMITTED.EMAIL2= USER\_EMPLOYEE\_REF.BUSINESS\_EMAIL

                    or SWITCH\_PERMITTED.EMAIL3= USER\_EMPLOYEE\_REF.BUSINESS\_EMAIL)

                )a

           WHERE business\_email not in (

                   select business\_email from USER\_EMPLOYEE\_REF where STATUS='ACTIVE'

           )

 but if I add one extra condition it returns me the correct result (2 rows, the ones of JAMES):

      select a.business\_email, a.status from

      (

          SELECT USER\_EMPLOYEE\_REF.BUSINESS\_EMAIL, user\_employee\_ref.status

          FROM SWITCH\_PERMITTED, USER\_EMPLOYEE\_REF

          WHERE USER\_EMPLOYEE\_REF.switch\_date is null

          and (SWITCH\_PERMITTED.EMAIL1= USER\_EMPLOYEE\_REF.BUSINESS\_EMAIL or SWITCH\_PERMITTED.EMAIL2= USER\_EMPLOYEE\_REF.BUSINESS\_EMAIL

          or SWITCH\_PERMITTED.EMAIL3= USER\_EMPLOYEE\_REF.BUSINESS\_EMAIL)

      ) a

      WHERE a.business\_email not in (

         select business\_email from USER\_EMPLOYEE\_REF  where STATUS='ACTIVE' and **business\_email=a.business\_email**

      )

Does anyone know why this is happening? To have more knowledge I can tell that USER_EMPLOYEE_REF has 109044 rows (and only 20 are INACTIVE), the SWITCH_PERMITTED has 3 rows and the query plans are:

      ![Captura.JPG](https://objectstorage.us-phoenix-1.oraclecloud.com/p/BqK85Rn1zA5MP0vYiqbAdPgs7Z6OmMxw8SD3WCFVm5kY8uReidZ1KPIKkgJ1hCkG/n/axciphqpnohg/b/forums-legacy/o/uploads/jive_attachments/4/2/0/42004154egami.jpeg)

Many thanks in advance and sorry for the inconveniences.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 12 2015
Added on Sep 14 2015
8 comments
296 views