©  Rahul Batra 2018
Rahul BatraSQL Primerhttps://doi.org/10.1007/978-1-4842-3576-8_12

12. Subqueries

Rahul Batra1 
(1)
Gurgaon, Haryana, India
 

A subquery, simply put, is a query written as a part of a bigger statement. Think of it as a SELECT statement inside another one. The result of the inner SELECT can then be used in the outer query. Let us take a simple example to illustrate this.

Consider the same source tables as the ones in the joins chapter – authors_tbl and newlang_tbl. We will try to write a query (and a subquery) to display the author of a particular language (Listing 12-1).

SELECT author FROM authors_tbl
 WHERE language_id IN
 ( SELECT id FROM newlang_tbl
   WHERE language="Tcl");
Listing 12-1

A simple subquery example

The subquery SELECT ...

Get SQL Primer: An Accelerated Introduction to SQL Basics now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.