3

I have a query

SELECT * 
FROM table1
WHERE
documentId in
(
   --select items from a second table where a third column happens to be null
   select documentId from table2 t2 inner join table1 t1
   on t1.documentId = t2.documentId and t1.itemId = t2.ItemId
   WHERE t1.someOtherColumn is null
)
and itemId in
(
   --similar query as above, just selecting itemId now
   select itemId from table2 t2 inner join table1 t1
   on t1.documentId = t2.documentId and t1.itemId = t2.ItemId
   WHERE t1.someOtherColumn is null
)
order by 1

Given that table1 has composite PK = documentId + itemId, is this sufficient to select only values from table1 that are unique? My fear is that a situation may occur where documentId exists and itemId exists, but since they are not looked at together then incorrect selections can be made.

For example,

Let's assume that there exists a value

documentId = 1 and itemId = 1.

Let's assume there is no composite key

documentId = 1 and itemId = 1.

I do not want composite key

[documentId = 1 and itemId = 3]

to be included.

I also do not want composite key

[documentId = 2 and itemId = 1]

to be included.

If there were a composite key added later (it's not there now)

[documentId = 1 and itemId = 1]

then it should be included.

3 Answers 3

5

This would do the same without having to use a join nor two INs

SELECT *
FROM table1
WHERE exists
(
    SELECT 1
    FROM table2
    WHERE someOtherColumn is null
        AND table1.documentId = table2.documentId
        AND table1.itemId = table2.ItemId
)
ORDER BY 1
0
5

You are correct assuming that it may lead to erroneous results. You could use this:

SELECT table1.* 
FROM table1
WHERE someOtherColumn IS NULL
  AND (documentId, itemId ) IN
   (
   SELECT documentId, itemId 
   FROM table2 t2                  --- no need for table1 again here 
   )
ORDER BY 1

Update: I think the above does not wotk in SQL-Server, only in Postgres (and Oracle?). Anyway, this should work in most systems:

SELECT table1.* 
FROM table1
WHERE someOtherColumn IS NULL
  AND EXISTS
   (
   SELECT documentId, itemId FROM table2 t2     
   WHERE table2.documentId = table1.documentId
     AND table2.itemId = table1.itemId
   )
ORDER BY 1
3
  • This looks very promising. I'm unable to get it to work. Error Incorrect syntax near ',' referring to the where (documentId, Dec 14, 2011 at 22:54
  • It's WHERE t1.someOtherColumn so t1 is required. Sorry for not making that clear in the original post. I updated it. Dec 14, 2011 at 22:57
  • "I think the [first query] does not wotk in SQL-Server" -- correct but it is an oft asked for feature that can be voted for here.
    – onedaywhen
    Dec 15, 2011 at 9:30
2

how about this:

SELECT * 
FROM table1,
(
   select documentId, itemid from table2 t2 inner join table1 t1
   on t1.documentId = t2.documentId and t1.itemId = t2.ItemId
   WHERE someOtherColumn is null
) t
WHERE
table1.documentId = t.documentid
and table1.itemId = t.itemid
order by 1
3
  • This is a really bad solution, sorry for saying. If there is not a 1 to 1 relation between table1 and table2, you will get alot of extra rows. Example: if there are 3 rows in table1 joined with 1 row in table2, you get 3 rows for the subquery resulting in 9 rows for the whole query. Dec 15, 2011 at 8:19
  • i think a simple DISTINCT would mitigate that.. the OP didn't specify anything about the select list.
    – Randy
    Dec 16, 2011 at 16:53
  • You should never use distinct to compensate for a badly written query. Dec 19, 2011 at 14:43

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.