Everything2
Near Matches
Ignore Exact
Full Text
Everything2

subquery

created by novasoy

(idea) by novasoy (1.3 wk) (print)   ?   (I like it!) Tue Jan 08 2002 at 20:36:32

In SQL a subquery is a query within another query. The results of the subquery are used in the predicate of the main, or outer, query to produce the output.

Here is a simple example.

SELECT * FROM movies outer
   WHERE outer.director IN 
     (SELECT inner.name FROM directors inner
      WHERE inner.nationality = "British");

In this query, everything within the parentheses is considered the subquery. What happens first is that the query engine selects the first row in the movies table. It then executes the subquery, which results in a listing of all the British directors in the directors table. The outer query's predicate is then evaluated against this list, and if the candidate row from the outer table has a director that matches the list from the subquery, then that movie is returned as output.

This is how subqueries work in theory. There is some variation from vendor to vendor on how the subquery is actually processed by the query engine. Some vendors, I believe, have optimization features so that the subquery is only executed once instead of once per candidate row. Obviously this results in much faster execution.


printable version
chaos

correlated subquery where exists Predicate
any inner join Subcomandante Marcos Theory
example Everything Query parentheses
SQL
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Drink up!
Vanier Park
Notre Dame Du Haut
Garrison Keillor
pellagra
July 30, 2003
Septuagint
What do you say to someone who has just had an abortion?
Dr. Strangelove, or How I Learned to Stop Worrying and Love the Bomb
Making the Movies
DSL
How to smoke a cigar
The Murder of Kurt Cobain
Genghis Khan
New Writeups
XWiz
Trism(review)
artman2003
Briefcase Full of Souls - Part I(fiction)
Dreamvirus
Alan Ladd(person)
waverider37
Harold Holt(person)
The Debutante
Until death do us part(fiction)
Ysardo
a brother to a sister(personal)
antigravpussy
your warm whispers(personal)
Clarke
Multiculturalism(idea)
aneurin
Earl of Landaff(person)
Heitah
Pseudocide(idea)
XWiz
Google Knol(lede)
Mythi
July 24, 2008(personal)
locke baron
The fall of Earth(fiction)
BookReader
Fear the Cold(dream)
Pavlovna
Kathleen MacInnes(person)
E2 is a by-product of the existence of The Everything Development Company