0

I'm new and trying to learn query! I wonder if it possible to get result/ return value of the where condition if none of the conditions are exist in the database, so I can differentiate which conditions exist and not in database!

For example:

 Select *
 From Student
 Where Student.id IN ("1","2","3")

The Result I expect is if the id of 2 is not exist in database, then I will get the result of 2. Hence, i will know that id 2 is not yet exist in database.

Thank you!

Popo
  • 15
  • 3

1 Answers1

0

You use a technique similar to finding a row in a table that doesn't have a match in another table, as described in Return row only if value doesn't exist

But since you're comparing with a list rather than a table, you have to synthesize a table from that list. You can do that with a subquery or CTE.

WITH id_list AS (
    SELECT "1" AS id
    UNION
    SELECT "2"
    UNION
    SELECT "3"
)
SELECT id_list.id 
FROM id_list
LEFT JOIN Student ON id_list.id = Student.id
WHERE Student.id IS NULL
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Hello, thank you for the reply! What if i have lot of ids? Is there a way for that, or it would be better to put all those ids in a table and compare it between table? Thanks – Popo Jul 13 '23 at 02:08
  • Yes, create a temporary table with this information. – Barmar Jul 13 '23 at 02:12