Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 What is the purpose of subquery and nesting a subq
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

rv498
Yak Posting Veteran

USA
60 Posts

Posted - 03/04/2014 :  17:30:38  Show Profile  Reply with Quote
Can you show me an example of subquery nesting? Thanks

rv498
Yak Posting Veteran

USA
60 Posts

Posted - 03/04/2014 :  18:43:21  Show Profile  Reply with Quote
So I came up with the code below, is this a legit nested subquery?
It returns no rows but I get no error messages.

SELECT DISTINCT c.ResellerName
FROM dbo.DimReseller c JOIN dbo.FactResellerSales e
ON e.ResellerKey = c.ResellerKey WHERE ProductLine IN
(SELECT ProductLine
FROM dbo.DimProduct
WHERE StandardCost IN
(SELECT ProductStandardCost
FROM dbo.FactInternetSales
WHERE ProductStandardCost > 1000))
Go to Top of Page

jackv
Flowing Fount of Yak Knowledge

United Kingdom
2179 Posts

Posted - 03/05/2014 :  01:47:52  Show Profile  Visit jackv's Homepage  Reply with Quote
A Subquery is a query within another SQL query , embedded within the WHERE clause. So , yes it looks like a legit query. There are some subquery rules which need to be followed

They are useful for completing more complex queries

Jack Vamvas
--------------------
http://www.sqlserver-dba.com

Edited by - jackv on 03/05/2014 01:50:01
Go to Top of Page

rv498
Yak Posting Veteran

USA
60 Posts

Posted - 03/05/2014 :  08:27:06  Show Profile  Reply with Quote
Thanks jacky, can you take a look at above code and see if it's qualified as nested subquery?
Go to Top of Page

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1054 Posts

Posted - 03/05/2014 :  08:37:04  Show Profile  Reply with Quote
I believe so - a query with a subquery.

Cheers
MIK
Go to Top of Page

rv498
Yak Posting Veteran

USA
60 Posts

Posted - 03/05/2014 :  08:41:27  Show Profile  Reply with Quote
Thanks MIK
Go to Top of Page

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1054 Posts

Posted - 03/05/2014 :  09:44:18  Show Profile  Reply with Quote
you're welcome!

Cheers
MIK
Go to Top of Page

maunishq
Yak Posting Veteran

Canada
71 Posts

Posted - 03/05/2014 :  10:26:27  Show Profile  Reply with Quote
It returns error message when there is error in your syntax or when the object you referring does not exist in that particular schema/db/table. It pulls 0 rows means your query is right but there is nothing that satisfy your logic.

=======================
Not an Expert, Just a learner.
!_(M)_!
Go to Top of Page

rv498
Yak Posting Veteran

USA
60 Posts

Posted - 03/05/2014 :  10:45:44  Show Profile  Reply with Quote
thanks maunishq
Go to Top of Page

jackv
Flowing Fount of Yak Knowledge

United Kingdom
2179 Posts

Posted - 03/07/2014 :  01:48:09  Show Profile  Visit jackv's Homepage  Reply with Quote
quote:
Originally posted by rv498

Thanks jacky, can you take a look at above code and see if it's qualified as nested subquery?



Yes ,it does as a nested query - based on the definition I posted

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000