Teradata – Logical and Conditional Operators

Teradata supports the following logical and conditional operators. These operators are used to perform comparison and combine multiple conditions.

SyntaxMeaning
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
=Equal to
BETWEENIf values within range
INIf values in <expression>
NOT INIf values not in <expression>
IS NULLIf value is NULL
IS NOT NULLIf value is NOT NULL
ANDCombine multiple conditions. Evaluates to true only if all conditions are met
ORCombine multiple conditions. Evaluates to true only if either of the conditions is met.
NOTReverses the meaning of the condition

BETWEEN

BETWEEN command is used to check if a value is within a range of values.

Example

Consider the following employee table.

EmployeeNoFirstNameLastNameJoinedDateDepartmentNoBirthDate
101MikeJames3/27/200511/5/1980
102RobertWilliams4/25/200723/5/1983
103PeterPaul3/21/200724/1/1983
104AlexStuart2/1/2008211/6/1984
105RobertJames1/4/2008312/1/1984

The following example fetches records with employee numbers in the range between 101,102 and 103.

SELECT EmployeeNo, FirstName FROM  
Employee 
WHERE EmployeeNo BETWEEN 101 AND 103;

When the above query is executed, it returns the employee records with employee no between 101 and 103.

*** Query completed. 3 rows found. 2 columns returned. 
*** Total elapsed time was 1 second. 
 EmployeeNo            FirstName 
-----------  ------------------------------ 
   101                   Mike 
   102                   Robert 
   103                   Peter

IN

IN command is used to check the value against a given list of values.

Example

The following example fetches records with employee numbers in 101, 102 and 103.

SELECT EmployeeNo, FirstName FROM  
Employee 
WHERE EmployeeNo in (101,102,103);

The above query returns the following records.

*** Query completed. 3 rows found. 2 columns returned. 
*** Total elapsed time was 1 second.  
 EmployeeNo            FirstName 
-----------  ------------------------------ 
   101                   Mike 
   102                   Robert 
   103                   Peter

NOT IN

NOT IN command reverses the result of IN command. It fetches records with values that don’t match with the given list.

Example

The following example fetches records with employee numbers not in 101, 102 and 103.

SELECT * FROM  
Employee 
WHERE EmployeeNo not in (101,102,103);

The above query returns the following records.

*** Query completed. 2 rows found. 6 columns returned. 
*** Total elapsed time was 1 second. 
 EmployeeNo          FirstName                      LastName 
----------- ------------------------------ -----------------------------    
    104                Alex                          Stuart 
    105                Robert                        James 

Leave a Reply