﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Subquery / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 19 Jun 2013 12:19:30 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Subquery</title><link>http://www.sqlservercentral.com/Forums/Topic1383236-392-1.aspx</link><description>[quote][b]GilaMonster (11/12/2012)[/b][hr][quote][b]SQLSACT (11/12/2012)[/b][hr]When using [i]in[/i] adding a correlation predicate is redundant?[/quote]Written correctly, just about all the time. Please read that blog post and maybe BoL on how the IN function works[quote]Am I on track here?[code="sql"]select T1.Col1 from T1where  T1.Col1   in(select Col1 from T2 where T2.Col1 = T1.Col1)[/code][/quote]No, I gave you the correct form in my previous post.[quote]--2select T1.Col1 from T1where  T1.Col1   in (select Col1 from T2)[/quote]Think about what the IN does with the two columns specified and you should understand why the correlation is completely unnecessary.[/quote]ThanksI see now why it's unnecessaryWhen I asked "am I on track here" followed by the query. I was just confirming that [i]that[/i] query was incorrect based on my Layman's terms.Thanks</description><pubDate>Mon, 12 Nov 2012 12:50:53 GMT</pubDate><dc:creator>SQLSACT</dc:creator></item><item><title>RE: Subquery</title><link>http://www.sqlservercentral.com/Forums/Topic1383236-392-1.aspx</link><description>[quote][b]SQLSACT (11/12/2012)[/b][hr]When using [i]in[/i] adding a correlation predicate is redundant?[/quote]Written correctly, just about all the time. Please read that blog post and maybe BoL on how the IN function works[quote]Am I on track here?[code="sql"]select T1.Col1 from T1where  T1.Col1   in(select Col1 from T2 where T2.Col1 = T1.Col1)[/code][/quote]No, I gave you the correct form in my previous post.[quote]--2select T1.Col1 from T1where  T1.Col1   in (select T2.Col1 from T2)[/quote]Think about what the IN does with the two columns specified and you should understand why the correlation is completely unnecessary.</description><pubDate>Mon, 12 Nov 2012 12:41:00 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: Subquery</title><link>http://www.sqlservercentral.com/Forums/Topic1383236-392-1.aspx</link><description>[quote][b]GilaMonster (11/12/2012)[/b][hr]Well, those aren't written correctly. The correct form would be[code="sql"]--1select T1.Col1 from T1where  exists (select * from T2 where T2.Col1 = T1.Col1)--2select T1.Col1 from T1where  T1.Col1   in(select Col1 from T2)[/code]And those two are completely equivalent.[url]http://sqlinthewild.co.za/index.php/2009/08/17/exists-vs-in/[/url][/quote]Thanks GailI think I'm getting thereWhen using [i]in[/i] 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 [i]select Col1 from table1 where table1 in (select Col1 from table1 )[/i]Am I on track here?[code="sql"]select T1.Col1 from T1where  T1.Col1   in(select Col1 from T2 where T2.Col1 = T1.Col1)[/code]Thanks</description><pubDate>Mon, 12 Nov 2012 11:51:05 GMT</pubDate><dc:creator>SQLSACT</dc:creator></item><item><title>RE: Subquery</title><link>http://www.sqlservercentral.com/Forums/Topic1383236-392-1.aspx</link><description>Well, those aren't written correctly. The correct form would be[code="sql"]--1select T1.Col1 from T1where  exists (select * from T2 where T2.Col1 = T1.Col1)--2select T1.Col1 from T1where  T1.Col1   in(select T2.Col1 from T2)[/code]And those two are completely equivalent.[url]http://sqlinthewild.co.za/index.php/2009/08/17/exists-vs-in/[/url]</description><pubDate>Mon, 12 Nov 2012 10:56:49 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: Subquery</title><link>http://www.sqlservercentral.com/Forums/Topic1383236-392-1.aspx</link><description>[quote][b]Sean Lange (11/9/2012)[/b][hr]I would write that query a little differently. I would instead write it as[code]select t1.Col1 from Tbl1 t1join Tbl2 t2 on t1.Col1 = t2.Col2where t1.Col1 = '52'[/code]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!!![/quote]Thanks SeanI'm just trying to better understand T-SQLWhen dealing with correlated subqueries, are these 2 queries essentially the same?[code="sql"]--1select T1.Col1 from T1where  exists (select Col1 from T2 where T2.Col1 = T1.Col1)--2select T1.Col1 from T1where  T1.Col1   in(select Col1 from T2 where T2.Col1 = T1.Col1)[/code]Thanks</description><pubDate>Sat, 10 Nov 2012 13:34:05 GMT</pubDate><dc:creator>SQLSACT</dc:creator></item><item><title>RE: Subquery</title><link>http://www.sqlservercentral.com/Forums/Topic1383236-392-1.aspx</link><description>I would write that query a little differently. I would instead write it as[code]select t1.Col1 from Tbl1 t1join Tbl2 t2 on t1.Col1 = t2.Col2where t1.Col1 = '52'[/code]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!!!</description><pubDate>Fri, 09 Nov 2012 14:54:55 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Subquery</title><link>http://www.sqlservercentral.com/Forums/Topic1383236-392-1.aspx</link><description>[quote][b]Roland Alexander STL (11/9/2012)[/b][hr]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![/quote]Thanks for the replyI want to be able to completely understand what is fundamentally happening hereThe subquery has its results and the outer query has its own results as well, are these 2 results cross referenced?Thanks</description><pubDate>Fri, 09 Nov 2012 14:31:12 GMT</pubDate><dc:creator>SQLSACT</dc:creator></item><item><title>RE: Subquery</title><link>http://www.sqlservercentral.com/Forums/Topic1383236-392-1.aspx</link><description>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!</description><pubDate>Fri, 09 Nov 2012 14:19:34 GMT</pubDate><dc:creator>Roland Alexander STL</dc:creator></item><item><title>Subquery</title><link>http://www.sqlservercentral.com/Forums/Topic1383236-392-1.aspx</link><description>Hi guysTrying to gear up my T-SQL skillsWhen dealing with subqueries, like below[code="sql"]select Col1 from Tbl1 where Col1 = '52' and Col1 in (select Col2 from Tbl2)[/code]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.Col1Thanks</description><pubDate>Fri, 09 Nov 2012 14:16:13 GMT</pubDate><dc:creator>SQLSACT</dc:creator></item></channel></rss>