Example: EXISTS/NOT EXISTS Functions - Teradata Vantage

Teradata® VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Vantage
Published
January 2023
Language
English (United States)
Last Update
2024-04-03
dita:mapPath
phg1621910019905.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
phg1621910019905

To select rows of t1 whose values in column x1 are equal to the value in column x2 of t2, one of the following queries can be used:

   SELECT * 
   FROM t1 
   WHERE x1 IN 
    (SELECT x2 
     FROM t2);
   SELECT * 
   FROM t1 
   WHERE EXISTS 
    (SELECT * 
     FROM t2 
     WHERE t1.x1=t2.x2);

To select rows of t1 whose values in column x1 are not equal to any value in column x2 of t2, you can use any one of the following queries:

   SELECT * 
   FROM t1 
   WHERE x1 NOT IN 
    (SELECT x2 
     FROM t2);
   
   SELECT * 
   FROM t1 
   WHERE NOT EXISTS 
    (SELECT * 
     FROM t2 
     WHERE t1.x1=t2.x2);
   
   SELECT 'T1 is not empty' 
   WHERE EXISTS 
    (SELECT * 
     FROM t1); 
   
   SELECT 'T1 is empty' 
   WHERE NOT EXISTS 
    (SELECT * 
     FROM t1);