SQL Server Programmer

What is a subquery in SQL?

SubQuery is a query within another query. The outer query is called as main query and inner query is called as SubQuery.

The SubQuery generally executes first, and its output is used to complete the query condition for the main or outer query. Subquery must be enclosed in parentheses.

Subqueries are on the right side of the comparison operator. You can place the Subquery in a number of SQL clausesWHERE clauseHAVING clauseFROM clause.

Subqueries can be used with SELECTUPDATEINSERTDELETE statements along with expression operator. It could be equality operator or comparison operator such as =, >, =, <= and Like operator.

ORDER BY command cannot be used in a SubQueryGROUPBY command can be used to perform same function as ORDER BY command.

A Sample Syntax Example for SubQuery:

SELECT column_name

FROM table_name

WHERE column_name expression operator

( SELECT COLUMN_NAME from TABLE_NAME WHERE … );

SQL Server Online Training