GITTA-Logo
PDF Version of this document Search Help

Lesson Navigation IconStructured Query Language SQL

Unit Navigation IconSQL overview

Unit Navigation IconCreation and modification of tables

Unit Navigation IconBasic database queries

LO Navigation IconSelect-From-Where

LO Navigation IconMultiple conditions

LO Navigation IconComparison operators

LO Navigation IconArithmetical operators

LO Navigation IconNested queries

LO Navigation IconJoin

LO Navigation IconNon-relational constructs

LO Navigation IconSet operators

LO Navigation IconSummary

LO Navigation IconDatabase queries

Unit Navigation IconSQL Insert, Delete and Update

Unit Navigation IconUsage of SQL

Unit Navigation IconSummary

Unit Navigation IconRecommended Reading

Unit Navigation IconBibliography

Unit Navigation IconMetadata


GITTA/CartouCHe news:


Go to previous page Go to next page

Nested queries

Nested queries

Conditions are usually made of an attribute, a value and an operator that forms the condition (eg. Name="John"). But the values themselves don't have to be constants, they can be the result of another sub-query. We then talk about nested queries. Using the IN-operator nested queries can be as deep as necessary.

There are three types of sub-queries that differ in their result:

  • Sub-queries that return a value (one column and one row)
  • Sub-queries that return a row
  • Sub-queries that return several rows

If only one value or row is returned the normal comparison operators can be used.

If more than one row is returned special operators are used:

  • IN examines whether the value exists in the sub-query
  • <Comparison operator> ALL the condition must return TRUE for all rows in the sub-query
  • <Comparison operator> ANY (SOME) the condition must return TRUE for at least one row in the sub-query
Nested queries
remark Sub-queries can also be used in the FROM part of a query. Thus, a relation can be compiled specifically for a request. The sub-query must be assigned a name with AS: (<Sub-query>) AS <Name>
Top Go to previous page Go to next page