No-SQL Databases

20
Sep

SQL. How to choose the thing which doesn’t exist.

Posted by eugene as No-SQL Databases

I’ve coped with a problem. Let’s think we have DUMMY_TABLE. How should I choose a field from the table by id or the ‘NOT EXISTS’ line if there is no such a record? By the same time you can use only SQL, no procedure programming.

As the result I’ve got the following query:

SELECT ID from DUMMY_TABLE
  where id=1
UNION
SELECT
 'NOT EXISTS' from DUAL WHERE NOT EXISTS(
    select ID from DUMMY_TABLE WHERE ID=1)

It works! And how would it look like in a more elegant way?