Teradata SubQueries

The Teradata SubQuery returns records from one table based on the values from another table.

It is a SELECT query within another query. The SELECT query called the inner query is executed first, and the outer query uses the result. Some of its most important features are:

  • A query can have multiple SubQueries, and SubQueries may contain another SubQuery.
  • SubQueries do not return the duplicate records.
  • If SubQuery returns only one value, then we can use = operator to use it with the outer query. If it returns multiple values, then we can use IN or NOT IN.
  • The SubQueries must be enclosed with parenthesis.

Syntax

Following is the generic syntax of Teradata SubQueries.

Example

Consider the following Salary table of the employees.

Employee_Id GrossDeductionNetPay
100140,0004,00036,000
100280,0006,00074,000
100390,0007,00083,000
100475,0005,00070,000

The following query identifies the employee Id with the highest salary. The inner SELECT performs the aggregation function to return the maximum NetPay value, and the outer SELECT query uses this value to return the employee record with this value.

When this query is executed, it produces the following output.

     /* Query completed. One row found. 2 columns returned  */
     /* Total elapsed time was 1 second */

 Employee_Id        NetPay 
 
     1003            83000 





Latest Courses