SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SubQuery


SubQuery

Author
Message
pchirags
pchirags
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3079 Visits: 616
Comments posted to this topic are about the item SubQuery
demonfox
demonfox
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2975 Visits: 1192
nice and easy for the last day of the week ..

thanks for the question !!!

~ demonfox
___________________________________________________________________
Wondering what I would do next , when I am done with this one Ermm
demonfox
demonfox
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2975 Visits: 1192
The first three items are required for subqueries. There are only 32 nesting levels allowed and if a TOP clause is used, an ORDER BY must be included.


actually, It's otehr way around with ORDER BY ..
An order by cannot be included in a subquery except when a TOP clause is used ..

EDIT :didn't notice that .. I opened the account w00t
Correct answers: 33% (1)
Incorrect answers: 67% (2)

shouldn't have come that early to office Cool

~ demonfox
___________________________________________________________________
Wondering what I would do next , when I am done with this one Ermm
Lokesh Vij
Lokesh Vij
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4742 Visits: 1599
Must include an ORDER BY clause when a TOP clause is specified.


I think this statements should be "Must include an TOP clause when a ORDER BY clause is specified."

~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


db4breakfast
db4breakfast
SSChasing Mays
SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)

Group: General Forum Members
Points: 651 Visits: 389
demonfox (3/7/2013)
The first three items are required for subqueries. There are only 32 nesting levels allowed and if a TOP clause is used, an ORDER BY must be included.


actually, It's otehr way around with ORDER BY ..
An order by cannot be included in a subquery except when a TOP clause is used ..

EDIT :didn't notice that .. I opened the account w00t
Correct answers: 33% (1)
Incorrect answers: 67% (2)

shouldn't have come that early to office Cool


For a subquery, it can't contain an order by unless top is used.


USE AdventureWorks;
GO
--this query run with no error
SELECT ProductID, (SELECT TOP(1) CustomerID FROM Sales.Customer)
FROM Production.Product;

--this query has error msg 1033: The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
SELECT ProductID, (SELECT CustomerID FROM Sales.Customer ORDER BY CustomerID DESC)
FROM Production.Product;



edit: the answer's explanation is not correct.

--------------------------------------------------------------------------------------
Hai Ton
My Db4Breakfast blog.
vinu512
vinu512
SSCertifiable
SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)

Group: General Forum Members
Points: 6409 Visits: 1626
The option is wrong....it is not necessary to have an Order By clause if Top is specified in a query.....it should be other way round.....Order By can only be specified in a query if Select Top is used.

Here's a simple example:


Declare @test Table (a int)

Insert Into @test Values(1);
Insert Into @test Values(2);
Insert Into @test Values(3);
Insert Into @test Values(4);
Insert Into @test Values(5);
Insert Into @test Values(6);
Insert Into @test Values(7);
Insert Into @test Values(8);
Insert Into @test Values(9);
Insert Into @test Values(10);

--Correct SubQueries

Select Top 1 * From @test Where a IN(Select Top 5 * From @test)
Order By a Desc

Select Top 1 * From @test Where a IN(Select Top 5 * From @test Order By a)
Order By a Desc

--Incorrect SubQuery(Error: Msg 1033, Level 15, State 1, Line 24 - The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.)

Select Top 1 * From @test Where a IN(Select * From @test Order By a)
Order By a Desc



Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden ;-)
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (110K reputation)SSC Guru (110K reputation)SSC Guru (110K reputation)SSC Guru (110K reputation)SSC Guru (110K reputation)SSC Guru (110K reputation)SSC Guru (110K reputation)SSC Guru (110K reputation)

Group: General Forum Members
Points: 110504 Visits: 18623
Looks like we may have a fun day with this question.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

demonfox
demonfox
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2975 Visits: 1192
vinu512 (3/7/2013)
The option is wrong....it is not necessary to have an Order By clause if Top is specified in a query.....it should be other way round.....Order By can only be specified in a query if Select Top is used.


well, yes , it should be written the other way round.. but, still, that doesn't make the option incorrect ...

as mentioned in the question
An ORDER BY is not required if a TOP clause is used. -- stated as wrong
Must include an ORDER BY clause when a TOP clause is specified --stated as correct


I don't see anything wrong with the english w00t

SQLRNNR (3/7/2013)
Looks like we may have a fun day with this question.


:-D definitely !!! Hehe

~ demonfox
___________________________________________________________________
Wondering what I would do next , when I am done with this one Ermm
Danny Ocean
Danny Ocean
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2876 Visits: 1549
Oops. Got it wrong due to overlook question Doze

Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
www.GrowWithSql.com
Stuart Davies
Stuart Davies
SSCrazy Eights
SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)

Group: General Forum Members
Points: 9554 Visits: 4856
Damn it! I should have listen to my teachers mantra "read the question through before trying to answer"

-------------------------------Posting Data Etiquette - Jeff Moden Smart way to ask a questionThere are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand (the world). There is no such thing as a dumb question. ― Carl Sagan I would never join a club that would allow me as a member - Groucho Marx
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search