Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Subquery Expand / Collapse
Author
Message
Posted Friday, November 09, 2012 2:16 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, April 17, 2014 7:44 AM
Points: 1,371, Visits: 2,597
Hi guys

Trying to gear up my T-SQL skills

When dealing with subqueries, like below

select Col1 from Tbl1 where Col1 = '52' and Col1 in (select Col2 from Tbl2)

What is the order of the execution here? Does SQL first check for the value 52 in Tbl2.Col2 and then check for the value 52 in Tbl1.Col1

Thanks
Post #1383236
Posted Friday, November 09, 2012 2:19 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Thursday, April 10, 2014 7:08 AM
Points: 419, Visits: 326
Maybe, maybe not. Depends on many factors such as indexing, clustering, number of rows in the tables, etc. Check the execution plan to see what's getting processed when. Might surprise you!

Roland Alexander
The Developing World


There are two means of refuge from the miseries of life: music and cats. ~ Albert Schweitzer
Post #1383238
Posted Friday, November 09, 2012 2:31 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, April 17, 2014 7:44 AM
Points: 1,371, Visits: 2,597
Roland Alexander STL (11/9/2012)
Maybe, maybe not. Depends on many factors such as indexing, clustering, number of rows in the tables, etc. Check the execution plan to see what's getting processed when. Might surprise you!


Thanks for the reply

I want to be able to completely understand what is fundamentally happening here

The subquery has its results and the outer query has its own results as well, are these 2 results cross referenced?

Thanks
Post #1383244
Posted Friday, November 09, 2012 2:54 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:49 PM
Points: 12,016, Visits: 11,046
I would write that query a little differently. I would instead write it as

select t1.Col1 
from Tbl1 t1
join Tbl2 t2 on t1.Col1 = t2.Col2
where t1.Col1 = '52'

That to me is a LOT easier to read and understand quickly. Check out the execution plans for both queries. It will really surprise you!!!


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1383260
Posted Saturday, November 10, 2012 1:34 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, April 17, 2014 7:44 AM
Points: 1,371, Visits: 2,597
Sean Lange (11/9/2012)
I would write that query a little differently. I would instead write it as

select t1.Col1 
from Tbl1 t1
join Tbl2 t2 on t1.Col1 = t2.Col2
where t1.Col1 = '52'

That to me is a LOT easier to read and understand quickly. Check out the execution plans for both queries. It will really surprise you!!!


Thanks Sean

I'm just trying to better understand T-SQL

When dealing with correlated subqueries, are these 2 queries essentially the same?

--1
select T1.Col1 from T1
where exists
(select Col1 from T2 where T2.Col1 = T1.Col1)
--2
select T1.Col1 from T1
where T1.Col1 in
(select Col1 from T2 where T2.Col1 = T1.Col1)

Thanks
Post #1383393
Posted Monday, November 12, 2012 10:56 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 1:57 PM
Points: 41,558, Visits: 34,479
Well, those aren't written correctly. The correct form would be

--1
select T1.Col1 from T1
where exists
(select * from T2 where T2.Col1 = T1.Col1)
--2
select T1.Col1 from T1
where T1.Col1 in
(select T2.Col1 from T2)

And those two are completely equivalent.

http://sqlinthewild.co.za/index.php/2009/08/17/exists-vs-in/



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1383785
Posted Monday, November 12, 2012 11:51 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, April 17, 2014 7:44 AM
Points: 1,371, Visits: 2,597
GilaMonster (11/12/2012)
Well, those aren't written correctly. The correct form would be

--1
select T1.Col1 from T1
where exists
(select * from T2 where T2.Col1 = T1.Col1)
--2
select T1.Col1 from T1
where T1.Col1 in
(select Col1 from T2)

And those two are completely equivalent.

http://sqlinthewild.co.za/index.php/2009/08/17/exists-vs-in/


Thanks Gail

I think I'm getting there

When using in adding a correlation predicate is redundant?

In my own words, using the query below. The subquery has already satisfied what I'm looking therefore rendering the outer query unnecessary.
Something like select Col1 from table1 where table1 in (select Col1 from table1 )

Am I on track here?

select T1.Col1 from T1
where T1.Col1 in
(select Col1 from T2 where T2.Col1 = T1.Col1)

Thanks
Post #1383804
Posted Monday, November 12, 2012 12:41 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 1:57 PM
Points: 41,558, Visits: 34,479
SQLSACT (11/12/2012)
When using in adding a correlation predicate is redundant?


Written correctly, just about all the time. Please read that blog post and maybe BoL on how the IN function works

Am I on track here?

select T1.Col1 from T1
where T1.Col1 in
(select Col1 from T2 where T2.Col1 = T1.Col1)



No, I gave you the correct form in my previous post.

--2
select T1.Col1 from T1
where T1.Col1 in (select T2.Col1 from T2)


Think about what the IN does with the two columns specified and you should understand why the correlation is completely unnecessary.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1383823
Posted Monday, November 12, 2012 12:50 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, April 17, 2014 7:44 AM
Points: 1,371, Visits: 2,597
GilaMonster (11/12/2012)
SQLSACT (11/12/2012)
When using in adding a correlation predicate is redundant?


Written correctly, just about all the time. Please read that blog post and maybe BoL on how the IN function works

Am I on track here?

select T1.Col1 from T1
where T1.Col1 in
(select Col1 from T2 where T2.Col1 = T1.Col1)



No, I gave you the correct form in my previous post.

--2
select T1.Col1 from T1
where T1.Col1 in (select Col1 from T2)


Think about what the IN does with the two columns specified and you should understand why the correlation is completely unnecessary.


Thanks

I see now why it's unnecessary

When I asked "am I on track here" followed by the query. I was just confirming that that query was incorrect based on my Layman's terms.

Thanks
Post #1383830
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse