<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; Interesting Interview Questions &#8211; Part 2 &#8211; Puzzle</title>
	<atom:link href="http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/</link>
	<description>Notes of a SQL Server MVP and Database Administrator</description>
	<lastBuildDate>Sat, 21 Nov 2009 05:54:09 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Rakesh Shrivastava</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-55185</link>
		<dc:creator>Rakesh Shrivastava</dc:creator>
		<pubDate>Tue, 25 Aug 2009 07:25:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-55185</guid>
		<description>select p.name from Personcolors p 
left outer join SelectedColors s  on p.colorcode=s.colorcode 
group by p.name
having count(s.colorcode)=(select count(d.colorcode) from SelectedColors d)</description>
		<content:encoded><![CDATA[<p>select p.name from Personcolors p<br />
left outer join SelectedColors s  on p.colorcode=s.colorcode<br />
group by p.name<br />
having count(s.colorcode)=(select count(d.colorcode) from SelectedColors d)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Atin Srivastava</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-54679</link>
		<dc:creator>Atin Srivastava</dc:creator>
		<pubDate>Mon, 10 Aug 2009 11:02:32 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-54679</guid>
		<description>How about this--
What I noticed some blogs above given by bloggers doenst have dynamic queries means their queries will fail if we insert or update more data in either tables... Queries should be general that goes write no matter what data table contains

select name from personcolors a, selectedcolors b where a.colorcode=b.colorcode
 group by [name] having count(*)= (select  count(*) from selectedcolors)</description>
		<content:encoded><![CDATA[<p>How about this&#8211;<br />
What I noticed some blogs above given by bloggers doenst have dynamic queries means their queries will fail if we insert or update more data in either tables&#8230; Queries should be general that goes write no matter what data table contains</p>
<p>select name from personcolors a, selectedcolors b where a.colorcode=b.colorcode<br />
 group by [name] having count(*)= (select  count(*) from selectedcolors)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JOBY CHERIYAN</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-52874</link>
		<dc:creator>JOBY CHERIYAN</dc:creator>
		<pubDate>Tue, 09 Jun 2009 12:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-52874</guid>
		<description>Guys,

My Advice for you all / Correct me if I&#039;m wrong
---------------------------------------------------------
1. Join Query will have Perfomance issue when data is huge.
2. If you use COUNT(*) this also Affect the Query perfomace.
3. name is already a SQL key word so use Tbl.[name] to improve perfomance insted just use name in a query.

Query can be like this
----------------------------

SELECT P.[name] FROM PersonColors P, SelectedColors C
WHERE P.colorcode = C.colorcode 
GROUP BY P.[name] HAVING count(P.[name]) &gt;= (SELECT count(c2.colorcode) FROM SelectedColors c2)</description>
		<content:encoded><![CDATA[<p>Guys,</p>
<p>My Advice for you all / Correct me if I&#8217;m wrong<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
1. Join Query will have Perfomance issue when data is huge.<br />
2. If you use COUNT(*) this also Affect the Query perfomace.<br />
3. name is already a SQL key word so use Tbl.[name] to improve perfomance insted just use name in a query.</p>
<p>Query can be like this<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>SELECT P.[name] FROM PersonColors P, SelectedColors C<br />
WHERE P.colorcode = C.colorcode<br />
GROUP BY P.[name] HAVING count(P.[name]) &gt;= (SELECT count(c2.colorcode) FROM SelectedColors c2)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JOBY CHERIYAN</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-52873</link>
		<dc:creator>JOBY CHERIYAN</dc:creator>
		<pubDate>Tue, 09 Jun 2009 12:44:55 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-52873</guid>
		<description>SELECT P.[name] FROM PersonColors P, SelectedColors C
WHERE P.colorcode = C.colorcode 
GROUP BY P.[name] HAVING count(P.[name]) &gt;= (SELECT count(c2.colorcode) FROM SelectedColors c2)</description>
		<content:encoded><![CDATA[<p>SELECT P.[name] FROM PersonColors P, SelectedColors C<br />
WHERE P.colorcode = C.colorcode<br />
GROUP BY P.[name] HAVING count(P.[name]) &gt;= (SELECT count(c2.colorcode) FROM SelectedColors c2)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-48536</link>
		<dc:creator>Mike</dc:creator>
		<pubDate>Mon, 09 Mar 2009 15:09:27 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-48536</guid>
		<description>Using the INNER JOIN this one is only processing PersonColor records that meet the SelectedColors criteria.  You don&#039;t necessarily need the DISTINCT keyword, but depending on the source of your data, you never know if you&#039;re going to get duplicates in your table (no constraints on the table DDL).  

SELECT Name FROM PersonColors A
INNER JOIN SelectedColors B ON A.ColorCode = B.ColorCode
GROUP BY Name
HAVING COUNT(DISTINCT A.ColorCode) = (SELECT COUNT(DISTINCT ColorCode) FROM SelectedColors)</description>
		<content:encoded><![CDATA[<p>Using the INNER JOIN this one is only processing PersonColor records that meet the SelectedColors criteria.  You don&#8217;t necessarily need the DISTINCT keyword, but depending on the source of your data, you never know if you&#8217;re going to get duplicates in your table (no constraints on the table DDL).  </p>
<p>SELECT Name FROM PersonColors A<br />
INNER JOIN SelectedColors B ON A.ColorCode = B.ColorCode<br />
GROUP BY Name<br />
HAVING COUNT(DISTINCT A.ColorCode) = (SELECT COUNT(DISTINCT ColorCode) FROM SelectedColors)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Uday Chowdary</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-46658</link>
		<dc:creator>Uday Chowdary</dc:creator>
		<pubDate>Sat, 14 Feb 2009 06:31:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-46658</guid>
		<description>Hi Pinal

Here is my kind of solution:

SELECT  NAME
FROM 
PERSONCOLORS PC,SELECTEDCOLORS SC
WHERE PC.COLORCODE=SC.COLORCODE  GROUP BY PC.NAME HAVING COUNT(DISTINCT(PC.COLORCODE))&gt;=(SELECT COUNT(*) FROM  SELECTEDCOLORS)</description>
		<content:encoded><![CDATA[<p>Hi Pinal</p>
<p>Here is my kind of solution:</p>
<p>SELECT  NAME<br />
FROM<br />
PERSONCOLORS PC,SELECTEDCOLORS SC<br />
WHERE PC.COLORCODE=SC.COLORCODE  GROUP BY PC.NAME HAVING COUNT(DISTINCT(PC.COLORCODE))&gt;=(SELECT COUNT(*) FROM  SELECTEDCOLORS)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: A.A</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-46195</link>
		<dc:creator>A.A</dc:creator>
		<pubDate>Sat, 31 Jan 2009 21:56:22 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-46195</guid>
		<description>select distinct name from personcolors p where not exists ( select colorcode from selectedcolors where colorcode not in
	(select colorcode from personcolors p2 where p.Name=p2.name) 
) 

Another long solution</description>
		<content:encoded><![CDATA[<p>select distinct name from personcolors p where not exists ( select colorcode from selectedcolors where colorcode not in<br />
	(select colorcode from personcolors p2 where p.Name=p2.name)<br />
) </p>
<p>Another long solution</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hema</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-45501</link>
		<dc:creator>Hema</dc:creator>
		<pubDate>Fri, 09 Jan 2009 17:03:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-45501</guid>
		<description>Or even this one

declare @ln_count int
set @ln_count = (select count(*) from #tabb)
select name,  count(*)
from #taba
group by name
having count (*) &gt;= @ln_count</description>
		<content:encoded><![CDATA[<p>Or even this one</p>
<p>declare @ln_count int<br />
set @ln_count = (select count(*) from #tabb)<br />
select name,  count(*)<br />
from #taba<br />
group by name<br />
having count (*) &gt;= @ln_count</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hema</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-45497</link>
		<dc:creator>Hema</dc:creator>
		<pubDate>Fri, 09 Jan 2009 15:14:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-45497</guid>
		<description>Hi Pinal,

How about 

select name, count(*) 
from #personcolors 
group by name having count(*) &gt;= 3

since I know there is a maximum of 3 colors in the table SelectedColors</description>
		<content:encoded><![CDATA[<p>Hi Pinal,</p>
<p>How about </p>
<p>select name, count(*)<br />
from #personcolors<br />
group by name having count(*) &gt;= 3</p>
<p>since I know there is a maximum of 3 colors in the table SelectedColors</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jerry</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-45355</link>
		<dc:creator>Jerry</dc:creator>
		<pubDate>Mon, 05 Jan 2009 09:24:10 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-45355</guid>
		<description>Hi Pinal,

I am a beginner in SQL Server 2005.
Kindly suggest me the way of improving the same as I am very much interested in SQL server.

Regards,
Jerry</description>
		<content:encoded><![CDATA[<p>Hi Pinal,</p>
<p>I am a beginner in SQL Server 2005.<br />
Kindly suggest me the way of improving the same as I am very much interested in SQL server.</p>
<p>Regards,<br />
Jerry</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pallavi</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-45122</link>
		<dc:creator>pallavi</dc:creator>
		<pubDate>Sat, 27 Dec 2008 07:31:45 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-45122</guid>
		<description>select distinct(name) from 
PersonColors a
where (select count(colorcode) from PersonColors b where a.name=b.name)&gt;=(select count(colorcode) from SelectedColors)</description>
		<content:encoded><![CDATA[<p>select distinct(name) from<br />
PersonColors a<br />
where (select count(colorcode) from PersonColors b where a.name=b.name)&gt;=(select count(colorcode) from SelectedColors)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott K</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-44736</link>
		<dc:creator>Scott K</dc:creator>
		<pubDate>Thu, 11 Dec 2008 20:07:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-44736</guid>
		<description>I came up with the following:

SELECT Name
FROM dbo.PersonColors P
LEFT JOIN dbo.SelectedColors S
  ON P.ColorCode = S.ColorCode
GROUP BY Name
HAVING COUNT(*) = 
    (
        SELECT DISTINCT COUNT(*) 
        FROM dbo.SelectedColors
    )

Then I read the below comments. I was happy to note that Tejas Shah was close to your answer.

I will say this took me more than 3 minutes to answer, but I am happy that I completed the exercise.</description>
		<content:encoded><![CDATA[<p>I came up with the following:</p>
<p>SELECT Name<br />
FROM dbo.PersonColors P<br />
LEFT JOIN dbo.SelectedColors S<br />
  ON P.ColorCode = S.ColorCode<br />
GROUP BY Name<br />
HAVING COUNT(*) =<br />
    (<br />
        SELECT DISTINCT COUNT(*)<br />
        FROM dbo.SelectedColors<br />
    )</p>
<p>Then I read the below comments. I was happy to note that Tejas Shah was close to your answer.</p>
<p>I will say this took me more than 3 minutes to answer, but I am happy that I completed the exercise.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: santosh</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-44725</link>
		<dc:creator>santosh</dc:creator>
		<pubDate>Thu, 11 Dec 2008 10:54:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-44725</guid>
		<description>Good post pinal
thanks once again</description>
		<content:encoded><![CDATA[<p>Good post pinal<br />
thanks once again</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gajanan</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-44719</link>
		<dc:creator>Gajanan</dc:creator>
		<pubDate>Thu, 11 Dec 2008 05:40:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-44719</guid>
		<description>For duplicate entries we just need to change the having clause in tejas&#039; query

HAVING COUNT(DISTINCT pc.ColorCode) &gt;= (SELECT COUNT(ColorCode) FROM SelectedColors)</description>
		<content:encoded><![CDATA[<p>For duplicate entries we just need to change the having clause in tejas&#8217; query</p>
<p>HAVING COUNT(DISTINCT pc.ColorCode) &gt;= (SELECT COUNT(ColorCode) FROM SelectedColors)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mumu</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-44718</link>
		<dc:creator>mumu</dc:creator>
		<pubDate>Thu, 11 Dec 2008 05:01:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-44718</guid>
		<description>FYI - Tejas&#039;s SQLSvr 2005 script left out a &#039;Tom&#039;, &#039;Blue&#039;.</description>
		<content:encoded><![CDATA[<p>FYI &#8211; Tejas&#8217;s SQLSvr 2005 script left out a &#8216;Tom&#8217;, &#8216;Blue&#8217;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tejas Shah</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-44716</link>
		<dc:creator>Tejas Shah</dc:creator>
		<pubDate>Thu, 11 Dec 2008 04:46:23 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-44716</guid>
		<description>Hi CB,

Thanks for this great solution.

I missed this situation, thanks for this solution. your solution is very correct.


Tejas</description>
		<content:encoded><![CDATA[<p>Hi CB,</p>
<p>Thanks for this great solution.</p>
<p>I missed this situation, thanks for this solution. your solution is very correct.</p>
<p>Tejas</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ajay Sinha</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-44713</link>
		<dc:creator>Ajay Sinha</dc:creator>
		<pubDate>Thu, 11 Dec 2008 04:24:48 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-44713</guid>
		<description>Hi Pinal
   I solved this problem within 5 mins.answer given below
select [Name] ,count(color_code) from
(select [name],color_main1.color_code from color_main inner join color_main1
on color_main.color_code=color_main1.color_code) cat1
group by [Name] 
having count(color_code)=(select count(color_code) from color_main1)</description>
		<content:encoded><![CDATA[<p>Hi Pinal<br />
   I solved this problem within 5 mins.answer given below<br />
select [Name] ,count(color_code) from<br />
(select [name],color_main1.color_code from color_main inner join color_main1<br />
on color_main.color_code=color_main1.color_code) cat1<br />
group by [Name]<br />
having count(color_code)=(select count(color_code) from color_main1)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: CB</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-44708</link>
		<dc:creator>CB</dc:creator>
		<pubDate>Thu, 11 Dec 2008 01:39:15 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-44708</guid>
		<description>The only drawback to Tejas&#039; solution is that you must ensure that duplicate entries in the PersonColor table are prevented. 

See what happens if you add another row to the table with the values &#039;Mike&#039;, &#039;Red&#039;. Mike will now appear in the result query because his count is 3, even though he only has 2 colors. 

To address that issue, this seemed to work:

SELECT [Name]
FROM 
(select distinct [name],[colorcode] from PersonColors) pc
INNER JOIN SelectedColors sc ON pc.ColorCode = sc.ColorCode
GROUP BY [Name]
HAVING COUNT(*) = (SELECT COUNT(*) FROM SelectedColors)</description>
		<content:encoded><![CDATA[<p>The only drawback to Tejas&#8217; solution is that you must ensure that duplicate entries in the PersonColor table are prevented. </p>
<p>See what happens if you add another row to the table with the values &#8216;Mike&#8217;, &#8216;Red&#8217;. Mike will now appear in the result query because his count is 3, even though he only has 2 colors. </p>
<p>To address that issue, this seemed to work:</p>
<p>SELECT [Name]<br />
FROM<br />
(select distinct [name],[colorcode] from PersonColors) pc<br />
INNER JOIN SelectedColors sc ON pc.ColorCode = sc.ColorCode<br />
GROUP BY [Name]<br />
HAVING COUNT(*) = (SELECT COUNT(*) FROM SelectedColors)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER - Interesting Interview Questions - Part 2 - Puzzle - Solution Journey to SQL Authority with Pinal Dave</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-44707</link>
		<dc:creator>SQL SERVER - Interesting Interview Questions - Part 2 - Puzzle - Solution Journey to SQL Authority with Pinal Dave</dc:creator>
		<pubDate>Thu, 11 Dec 2008 01:31:06 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-44707</guid>
		<description>[...] Read Original Interview Question and Puzzle. [...]</description>
		<content:encoded><![CDATA[<p>[...] Read Original Interview Question and Puzzle. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gabriel Rodriguez</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-44706</link>
		<dc:creator>Gabriel Rodriguez</dc:creator>
		<pubDate>Thu, 11 Dec 2008 00:20:28 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1640#comment-44706</guid>
		<description>The ones that use 

COUNT(ColorCode) &gt;= (SELECT COUNT(*) FROM SelectedColors)

are probably wrong. THe reason is because there is no validation to make sure that 3 of the COlorCodes that the person has are all in the SelectedColors table.

I mean, the results are coming up correct, but if you update the ColorCodes for the persons that have all three colors to have ColorCodes that are not in the SelectedColors table, the results will still show and one condition that Pinal specified was that they had to have all the colors in the other table.

again this is a good excercise.</description>
		<content:encoded><![CDATA[<p>The ones that use </p>
<p>COUNT(ColorCode) &gt;= (SELECT COUNT(*) FROM SelectedColors)</p>
<p>are probably wrong. THe reason is because there is no validation to make sure that 3 of the COlorCodes that the person has are all in the SelectedColors table.</p>
<p>I mean, the results are coming up correct, but if you update the ColorCodes for the persons that have all three colors to have ColorCodes that are not in the SelectedColors table, the results will still show and one condition that Pinal specified was that they had to have all the colors in the other table.</p>
<p>again this is a good excercise.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
