n4mi n4mi - 3 months ago 19
SQL Question

Need help in nested sql queries

Please help me with this query

SELECT *
FROM documents
WHERE doc_id <> (SELECT doc_id
FROM doc_submitted
WHERE student_IDNUM = 131009685)


What I want is to get all documents that are not in the
doc_submitted
.

Answer

I generally recommend NOT EXISTS over NOT IN, because NOT IN returns zero rows if even one row in the subquery has a NULL value:

SELECT d.*
FROM documents d
WHERE NOT EXISTS (SELECT 1
                  FROM doc_submitted ds
                  WHERE ds.doc_id = d.docid AND ds.student_IDNUM = 131009685
                 )
Comments