87
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_IdGross | Deduction | NetPay | |
---|---|---|---|
1001 | 40,000 | 4,000 | 36,000 |
1002 | 80,000 | 6,000 | 74,000 |
1003 | 90,000 | 7,000 | 83,000 |
1004 | 75,000 | 5,000 | 70,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
Next TopicTeradata Substring