473,324 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

' not in ' function syntax.

Hi All,

I am trying to execute this kind of query that uses'Not in ' function but unable to get 'Nulls'

select * from table1 where col1 not in (select col2 from table2).
Here col1 is not primary key in table1 and col2 is not foreign key for col1. They store same kind of data.
There are few nulls for col1 in table 1 and no nulls for col2.
If i execute the above statement , the rows with nulls for col1 are not retrieving.

As these nulls are not in col2 of table2, they should be retrieved right?
Dec 20 '07 #1
1 1446
Hi,

NULL is considered "UNKNOWN". It is never "IN" or "NOT IN" anything. If you want to see the rows where COL1 is NULL, you have to do so explicitly using the "IS NULL" clause in your query.

Example:

Expand|Select|Wrap|Line Numbers
  1. chris@XE> DROP TABLE t1;
  2.  
  3. Table dropped.
  4.  
  5. Elapsed: 00:00:00.03
  6. chris@XE> 
  7. chris@XE> CREATE TABLE t1  (
  8.   2  ID NUMBER(10),
  9.   3  col1 VARCHAR2(1)
  10.   4  );
  11.  
  12. Table created.
  13.  
  14. Elapsed: 00:00:00.00
  15. chris@XE> 
  16. chris@XE> INSERT INTO t1
  17.   2  VALUES      (1, 'a');
  18.  
  19. 1 row created.
  20.  
  21. Elapsed: 00:00:00.01
  22. chris@XE> INSERT INTO t1
  23.   2  VALUES      (2, 'b');
  24.  
  25. 1 row created.
  26.  
  27. Elapsed: 00:00:00.00
  28. chris@XE> INSERT INTO t1
  29.   2  VALUES      (3, 'c');
  30.  
  31. 1 row created.
  32.  
  33. Elapsed: 00:00:00.01
  34. chris@XE> INSERT INTO t1
  35.   2  VALUES      (4, 'd');
  36.  
  37. 1 row created.
  38.  
  39. Elapsed: 00:00:00.00
  40. chris@XE> INSERT INTO t1
  41.   2  VALUES      (5, NULL);
  42.  
  43. 1 row created.
  44.  
  45. Elapsed: 00:00:00.00
  46. chris@XE> INSERT INTO t1
  47.   2  VALUES      (6, 'e');
  48.  
  49. 1 row created.
  50.  
  51. Elapsed: 00:00:00.03
  52. chris@XE> INSERT INTO t1
  53.   2  VALUES      (7, 'f');
  54.  
  55. 1 row created.
  56.  
  57. Elapsed: 00:00:00.03
  58. chris@XE> INSERT INTO t1
  59.   2  VALUES      (8, NULL);
  60.  
  61. 1 row created.
  62.  
  63. Elapsed: 00:00:00.01
  64. chris@XE> INSERT INTO t1
  65.   2  VALUES      (9, 'g');
  66.  
  67. 1 row created.
  68.  
  69. Elapsed: 00:00:00.01
  70. chris@XE> 
  71. chris@XE> COMMIT ;
  72.  
  73. Commit complete.
  74.  
  75. Elapsed: 00:00:00.01
  76. chris@XE> 
  77. chris@XE> DROP TABLE t2;
  78.  
  79. Table dropped.
  80.  
  81. Elapsed: 00:00:00.06
  82. chris@XE> 
  83. chris@XE> CREATE TABLE t2  (
  84.   2  ID NUMBER(10),
  85.   3  col2 VARCHAR2(1) NOT NULL
  86.   4  );
  87.  
  88. Table created.
  89.  
  90. Elapsed: 00:00:00.03
  91. chris@XE> 
  92. chris@XE> INSERT INTO t2
  93.   2  VALUES      (1, 'a');
  94.  
  95. 1 row created.
  96.  
  97. Elapsed: 00:00:00.03
  98. chris@XE> INSERT INTO t2
  99.   2  VALUES      (3, 'c');
  100.  
  101. 1 row created.
  102.  
  103. Elapsed: 00:00:00.03
  104. chris@XE> INSERT INTO t2
  105.   2  VALUES      (7, 'f');
  106.  
  107. 1 row created.
  108.  
  109. Elapsed: 00:00:00.01
  110. chris@XE> 
  111. chris@XE> COMMIT ;
  112.  
  113. Commit complete.
  114.  
  115. Elapsed: 00:00:00.01
  116. chris@XE> 
  117. chris@XE> SELECT *
  118.   2  FROM   t1
  119.   3  WHERE  col1 NOT IN (SELECT col2
  120.   4                      FROM   t2);
  121.  
  122.         ID C
  123. ---------- -
  124.          2 b
  125.          4 d
  126.          6 e
  127.          9 g
  128.  
  129. Elapsed: 00:00:00.06
  130. chris@XE> 
  131. chris@XE> 
  132. chris@XE> SELECT *
  133.   2  FROM   t1
  134.   3  WHERE  col1 NOT IN (SELECT col2
  135.   4                      FROM   t2)
  136.   5  OR     col1 IS NULL;
  137.  
  138.         ID C
  139. ---------- -
  140.          2 b
  141.          4 d
  142.          5
  143.          6 e
  144.          8
  145.          9 g
  146.  
  147. 6 rows selected.
  148.  
  149. Elapsed: 00:00:00.07
  150. chris@XE> 
Dec 20 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
11
by: John Collyer | last post by:
Hi, In assembly language you can use a lookup table to call functions. 1. Lookup function address in table 2. Call the function Like: CALL FUNCTION
4
by: bluedolphin | last post by:
This is my first function in Visual and I'm having a simple syntax syntax issue that I'm hoping someone can help correct for me. I have a function Public Function...
6
by: Stuart McGraw | last post by:
I am looking for a VBA "format" or "template" function, that is, a function that takes a format string and a varying number of arguments, and substitutes the argument values into the format string...
5
by: Cancerbero | last post by:
Hi (first, excuse me for my bad english) As I know, the semantics for typedef is: typedef A B; I think this makes B a synonym of A, where A is an existing data type. Is that right? Based...
21
by: Dmitry Anikin | last post by:
I mean, it's very convenient when default parameters can be in any position, like def a_func(x = 2, y = 1, z): ... (that defaults must go last is really a C++ quirk which is needed for overload...
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
4
by: Tony Lownds | last post by:
(Note: PEPs in the 3xxx number range are intended for Python 3000) PEP: 3107 Title: Function Annotations Version: $Revision: 53169 $ Last-Modified: $Date: 2006-12-27 20:59:16 -0800 (Wed, 27 Dec...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.