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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 What is the purpose of subquery and nesting a subq

Author  Topic 

rv498
Yak Posting Veteran

60 Posts

Posted - 2014-03-04 : 17:30:38
Can you show me an example of subquery nesting? Thanks

rv498
Yak Posting Veteran

60 Posts

Posted - 2014-03-04 : 18:43:21
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
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2014-03-05 : 01:47:52
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
Go to Top of Page

rv498
Yak Posting Veteran

60 Posts

Posted - 2014-03-05 : 08:27:06
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
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2014-03-05 : 08:37:04
I believe so - a query with a subquery.

Cheers
MIK
Go to Top of Page

rv498
Yak Posting Veteran

60 Posts

Posted - 2014-03-05 : 08:41:27
Thanks MIK
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2014-03-05 : 09:44:18
you're welcome!

Cheers
MIK
Go to Top of Page

maunishq
Yak Posting Veteran

71 Posts

Posted - 2014-03-05 : 10:26:27
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

60 Posts

Posted - 2014-03-05 : 10:45:44
thanks maunishq
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2014-03-07 : 01:48:09
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
   

- Advertisement -