William Mora

SQL: Exists Condition

The SQL EXISTS condition is a boolean function that returns true if the condition is met. The syntax is pretty simple:

SELECT * FROM TABLE WHERE EXISTS(_subquery_);

Alternatively, you can use NOT EXISTS(_subquery_). An example of the function is the following:

SELECT utc.table_name    
 FROM user_tab_cols utc     
 WHERE utc.column_name='ACCOUNTID'
 AND NOT EXISTS(SELECT uc.table_name     
   FROM user_constraints uc     
   WHERE uc.table_name=utc.table_name     
   AND uc.r_constraint_name='SYS_C00229824')     
   ORDER BY utc.table_name;
 

The query looks for all the tables that contain the column ACCOUNTID and from those tables, only get the ones that don’t make reference to the constraint SYS_C00229824.