SQL Sub-query that Returns Single Value: Best Example

Sub-query returns single or multiple values. If you want to write a Subquery to return single value, then you need to include comparison operators. The list is <,>, =, >=<,<= and <>

Both numeric and strings you can compare. If you want to retrieve data from the RETAIL_PRICES table, when R_PRICE values equal to the maximum price listed in the S_PRICE column of the SALES_PRICES table, then below is the query.

Sub-query

 
Listen Audio:

 
You do not have to use an aggregate function (such as MAX) to return a single value in a subquery.

 

For example, the subquery’s WHERE clause might include a condition that will return only one value.

The important point to remember is ensure your subquery returns single value.

Otherwise, you will receive an error when using a comparison operator.

Related posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.