Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SubQuery


SubQuery

Author
Message
pchirags
pchirags
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2261 Visits: 615
Comments posted to this topic are about the item SubQuery
demonfox
demonfox
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1367 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
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1367 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
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1812 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
SSC-Addicted
SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)

Group: General Forum Members
Points: 455 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
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1335 Visits: 1619
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-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24001 Visits: 18292
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
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1367 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
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1460 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
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4785 Visits: 4611
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 question

There 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