﻿<?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 2005 / SQL Server 2005 General Discussion  / can any one explain how this query execute / 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>Sat, 25 May 2013 06:56:34 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: can any one explain how this query execute</title><link>http://www.sqlservercentral.com/Forums/Topic1394934-149-1.aspx</link><description>[quote][b]narendra.kongara (12/11/2012)[/b][hr]1&amp;gt; -- Correlated subquery using the department table in both inner and outer queries2&amp;gt;3&amp;gt; SELECT t1.*4&amp;gt;        FROM department t15&amp;gt;        WHERE t1.location IN6&amp;gt;        (SELECT t2.location7&amp;gt;          FROM department t28&amp;gt;          WHERE t1.dept_no &amp;lt;&amp;gt; t2.dept_no)9&amp;gt; GO[/quote]The correlated subquery works by executing once for each row in the department table aliased as t1.  For each of those records, it sees if the location in the record of t1 exists in the same department table aliased as t2 but with a different dept_no.  Since Dallas is the only location with multiple departments in your sample data, it is the only location that satisfies the WHERE clause.</description><pubDate>Tue, 11 Dec 2012 08:57:08 GMT</pubDate><dc:creator>Chris Harshman</dc:creator></item><item><title>RE: can any one explain how this query execute</title><link>http://www.sqlservercentral.com/Forums/Topic1394934-149-1.aspx</link><description>Not sure what you are askingYou can run the following in a query window in management studio and get the output - but will be left with the table in a databaseCREATE TABLE department(dept_no CHAR(4) NOT NULL,dept_name CHAR(25) NOT NULL,location CHAR(30) NULL)goinsert into department values ('d1', 'developer', 'Dallas')insert into department values ('d2', 'tester', 'Seattle')insert into department values ('d3', 'marketing', 'Dallas')select * from departmentWhat you have there looks like it was the output from something like osql</description><pubDate>Tue, 11 Dec 2012 01:59:31 GMT</pubDate><dc:creator>nigelrivett</dc:creator></item><item><title>RE: can any one explain how this query execute</title><link>http://www.sqlservercentral.com/Forums/Topic1394934-149-1.aspx</link><description>What's the question?</description><pubDate>Tue, 11 Dec 2012 01:59:27 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>can any one explain how this query execute</title><link>http://www.sqlservercentral.com/Forums/Topic1394934-149-1.aspx</link><description>CREATE TABLE department(dept_no   CHAR(4) NOT NULL,3&amp;gt;                         dept_name CHAR(25) NOT NULL,4&amp;gt;                         location  CHAR(30) NULL)5&amp;gt;6&amp;gt; insert into department values ('d1', 'developer',   'Dallas')7&amp;gt; insert into department values ('d2', 'tester',      'Seattle')8&amp;gt; insert into department values ('d3', 'marketing',  'Dallas')9&amp;gt;10&amp;gt; select * from department11&amp;gt; GO(1 rows affected)(1 rows affected)(1 rows affected)dept_no dept_name                 location------- ------------------------- ------------------------------d1      developer                 Dallasd2      tester                    Seattled3      marketing                 Dallas(3 rows affected)1&amp;gt; -- Correlated subquery using the department table in both inner and outer queries2&amp;gt;3&amp;gt; SELECT t1.*4&amp;gt;        FROM department t15&amp;gt;        WHERE t1.location IN6&amp;gt;        (SELECT t2.location7&amp;gt;          FROM department t28&amp;gt;          WHERE t1.dept_no &amp;lt;&amp;gt; t2.dept_no)9&amp;gt; GOdept_no dept_name                 location------- ------------------------- ------------------------------d1      developer                 Dallasd3      marketing                 Dallas(2 rows affected)</description><pubDate>Tue, 11 Dec 2012 01:47:45 GMT</pubDate><dc:creator>narendra.kongara</dc:creator></item></channel></rss>