<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Correlated Subquery &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/correlated-subquery/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Wed, 05 Jul 2017 20:36:07 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Find third highest Employee Salary</title>
		<link>https://www.dbrnd.com/2017/07/sql-puzzle-sql-advance-query-find-third-highest-employee-salary-correlated-subquery/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Wed, 19 Jul 2017 16:16:25 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[Correlated Subquery]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[SQL Advance Query]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5642</guid>

					<description><![CDATA[Check below input/output data, and the employee third highest employee salary. Input data: [crayon-6ab68e2a6207d268809717/] Require output: [crayon-6ab68e2a62081561038834/] Create a table with sample data: [crayon-6ab68e2a62082906970355/] Solution 1 (Correlated Subquery): [crayon-6ab68e2a62083495438181/] Solution 2 (DENSE_RANK()): [crayon-6ab68e2a62084094461211/] Solution 3 (MAX() &#038; CROSS APPLY): [crayon-6ab68e2a62085753174822/]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F07%2Fsql-puzzle-sql-advance-query-find-third-highest-employee-salary-correlated-subquery%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20third%20highest%20Employee%20Salary" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F07%2Fsql-puzzle-sql-advance-query-find-third-highest-employee-salary-correlated-subquery%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20third%20highest%20Employee%20Salary" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F07%2Fsql-puzzle-sql-advance-query-find-third-highest-employee-salary-correlated-subquery%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20third%20highest%20Employee%20Salary" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F07%2Fsql-puzzle-sql-advance-query-find-third-highest-employee-salary-correlated-subquery%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20third%20highest%20Employee%20Salary" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F07%2Fsql-puzzle-sql-advance-query-find-third-highest-employee-salary-correlated-subquery%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20third%20highest%20Employee%20Salary" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F07%2Fsql-puzzle-sql-advance-query-find-third-highest-employee-salary-correlated-subquery%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20third%20highest%20Employee%20Salary" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F07%2Fsql-puzzle-sql-advance-query-find-third-highest-employee-salary-correlated-subquery%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20third%20highest%20Employee%20Salary" data-a2a-url="https://www.dbrnd.com/2017/07/sql-puzzle-sql-advance-query-find-third-highest-employee-salary-correlated-subquery/" data-a2a-title="SQL Puzzle: SQL Advance Query – Find third highest Employee Salary"></a></p><p>Check below input/output data, and the employee third highest employee salary.</p>
<p><strong>Input data:</strong></p>
<pre class="crayon-plain-tag">empid       ename      salary
----------- ---------- -----------
1           pqr        25000
2           abc        25000
3           xyz        37000
4           pqr        35200
5           ymf        40000
6           sfs        40000
7           wer        37000
8           cvb        37800</pre> </p>
<p><strong>Require output:</strong></p>
<pre class="crayon-plain-tag">empid       ename      salary
----------- ---------- -----------
3           xyz        37000
7           wer        37000</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">create table tbl_employee 
(
	empid int primary key
	,ename varchar(10)
	,salary int	
)
GO

insert into tbl_employee values 
(1,'pqr',25000)
,(2,'abc',25000)
,(3,'xyz',37000)
,(4,'pqr',35200)
,(5,'ymf',40000)
,(6,'sfs',40000)
,(7,'wer',37000)
,(8,'cvb',37800)
GO</pre> </p>
<p><strong>Solution 1 (Correlated Subquery):</strong></p>
<pre class="crayon-plain-tag">select * from tbl_employee as a
where 3 = (select count(distinct salary) from tbl_employee as b where a.salary&lt;=b.salary)</pre> </p>
<p><strong>Solution 2 (DENSE_RANK()):</strong></p>
<pre class="crayon-plain-tag">with cteEmp as
(
	select
		empid
		,ename
		,salary
		,DENSE_RANK() over (order by salary desc) as rnk
	from tbl_employee
)
select 
	empid
	,ename
	,salary
from cteEmp
where rnk = 3</pre> </p>
<p><strong>Solution 3 (MAX() &#038; CROSS APPLY):</strong></p>
<pre class="crayon-plain-tag">SELECT a.ename, b.salary
FROM 
(
	SELECT MAX(salary) Salary
    FROM tbl_employee d
    WHERE d.Salary !=
	(
		SELECT MAX(salary) Salary
		FROM tbl_employee e 
		WHERE e.Salary != (SELECT MAX(salary) FROM tbl_employee)
	)
	and d.Salary != (SELECT MAX(salary) FROM tbl_employee)
) b
CROSS APPLY (SELECT ename FROM tbl_employee WHERE salary = b.salary ) a</pre> </p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
