views:

125

answers:

4

What is the term for the select construct in the following select statement that is in bold?

SELECT a.t1 as a, (SELECT b.n as b FROM b WHERE b.x = a.t1), c.t2 as c FROM a,c WHERE a.x = c.x

I was explaining that this can be done in oracle but when asked what it was called, I couldn't think of any term. Is there a term for this? Or is it just selecting a select result?

EDIT: expanded query to make sub-query use clear

+14  A: 

It is a subquery. If b.n refers to a table aliased as b in the outer query, then it could be referred to as a correlated subquery.

As guigui42 notes, it is also a scalar query, since it is returning at most only one column and row. In fact, you must take care to ensure at most only one row is ever returned, or the query may crash at some later date. This is often guarded against by using TOP 1 or equivalent.

RedFilter
*"...because it is referring to a table in the outer query."* It is? I'm missing it. It *could be*, that "..." after the `WHERE` leaves a big question mark...
T.J. Crowder
@TJ: You are right, I was mistakenly assuming `b.n` referred to a table outside the query., which we can't tell from the segment posted. I will update my response.
RedFilter
@RedFilter: Well, whether you're mistaken is unclear. You could well be right. It's a good answer regardless.
T.J. Crowder
to be more precise, i think its a "scalar correlated subquery", since it returns one and only one result per line in the select clause.
guigui42
Looks like "scalar correlated subquery" might be the most accurate term
sal
A scalar subquery can return zero or one rows!
onedaywhen
+4  A: 

A nested sub-query.

...notoriously poor performers if mis-used (which is quite often) as well.

Justin Niessner
Just as am aside, this is being used to replace oracle case statements to replace ID codes with descriptive terms. Its more readable and performance is acceptable.
sal
+1  A: 

This is referred to as a sub-select.

Ryan Tenney
Lots of things are sub-selects. I need something more specific.
sal
+5  A: 

i would say "Scalar subquery"

EDIT : as RedFilter said, it is also a correlated subquery.

so it is a Scalar correlated subquery

guigui42
Well, it *may* be corrolated. It may not, the OP didn't give us enough to work with.
T.J. Crowder
How could it not be a correlated sub-query? The question (unamended) says "(SELECT b.n as b FROM n WHERE ...)"; the column b.n has to come from somewhere other than table 'n', doesn't it? (Unless there is a way for nesting structured types in a table, in which case table N might contain a compound column B which has a component N...does Oracle indulge in that stuff? Some of the ORDBMS do.)
Jonathan Leffler
@Jonathan Leffler: there's no evidence in this case to suggest that a table in the main query's `FROM` clause will appear in the scalar subquery's `WHERE` clause.
onedaywhen
@onedaywhen: what is the `b` in `b.n`?
Jonathan Leffler
@onedaywhen: also, a table from the main query can appear (and appears to appear) in the select-list; that would make it a correlated query, whether the table is mentioned in the WHERE or not.
Jonathan Leffler
@Jonathan Leffler: I'm sure the `b.n` was a typo so I've edited to correct it to `n.b`.
onedaywhen
@Jonathan Leffler: "a table from the main query can appear... in the select-list; that would make it a correlated query, whether the table is mentioned in the WHERE or not" -- I admit I've never seen that done and there must be more readable ways of achieving the same result but I guess your point is valid. Then again, I've never found much value in differentiating a correlated subquery from other types of subqueries :)
onedaywhen