Oracle sql return true if exists question

Matt picture Matt · Nov 4, 2010 · Viewed 95.3k times · Source

How do I check if a particular element exists in a table - how can I return true or false?

I have a table that has

  • user_id
  • user_password
  • user_secretQ

Verbally, I want to do this: If a particular user_id exists in the user_id column, then return true -- otherwise return false.

Answer

DCookie picture DCookie · Nov 4, 2010

There is no Boolean type in Oracle SQL. You will need to return a 1 or 0, or some such and act accordingly:

SELECT CASE WHEN MAX(user_id) IS NULL THEN 'NO' ELSE 'YES' END User_exists
  FROM user_id_table
 WHERE user_id = 'some_user';