<?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>SQL Tips and Tricks &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/sql-tips-and-tricks/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>Mon, 24 Jun 2019 19:42:24 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Find the Order basis on thier Status and Step</title>
		<link>https://www.dbrnd.com/2019/08/sql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step/</link>
					<comments>https://www.dbrnd.com/2019/08/sql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 12 Aug 2019 16:16:20 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[Anvesh Patel]]></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 Interview]]></category>
		<category><![CDATA[SQL Problem]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5809</guid>

					<description><![CDATA[Check the below input data and require output data to find order which step is 0 with status D and for the same order other status are P. Input Data: [crayon-6a8f5cc795590939791747/] Expected Output: [crayon-6a8f5cc795593110911933/] Create a table with sample data: [crayon-6a8f5cc795594493945876/] Solution 1: Using CTE [crayon-6a8f5cc795595381309027/] Solution 2: Using GROUP BY .. Having [crayon-6a8f5cc795596153595681/] Please [&#8230;]]]></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%2F2019%2F08%2Fsql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Order%20basis%20on%20thier%20Status%20and%20Step" 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%2F2019%2F08%2Fsql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Order%20basis%20on%20thier%20Status%20and%20Step" 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%2F2019%2F08%2Fsql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Order%20basis%20on%20thier%20Status%20and%20Step" 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%2F2019%2F08%2Fsql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Order%20basis%20on%20thier%20Status%20and%20Step" 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%2F2019%2F08%2Fsql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Order%20basis%20on%20thier%20Status%20and%20Step" 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%2F2019%2F08%2Fsql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Order%20basis%20on%20thier%20Status%20and%20Step" 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%2F2019%2F08%2Fsql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Order%20basis%20on%20thier%20Status%20and%20Step" data-a2a-url="https://www.dbrnd.com/2019/08/sql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step/" data-a2a-title="SQL Puzzle: SQL Advance Query – Find the Order basis on thier Status and Step"></a></p><p>Check the below input data and require output data to find order which step is 0 with status D and for the same order other status are P.</p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">OrderID Step        Status
------- ----------- ------
ABC     0           D
ABC     1           P
ABC     2           P
ABC     3           P
XYZ     0           D
XYZ     1           D
EFQ     0           D</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">OrderID
-------
ABC</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_Orders
(
	OrderID CHAR(5) 
	,Step INTEGER 
	,Status CHAR(1) 
)
GO 

INSERT INTO tbl_Orders
VALUES
('ABC', 0, 'D'),
('ABC', 1, 'P'),
('ABC', 2, 'P'),
('ABC', 3, 'P'),
('XYZ', 0, 'D'),
('XYZ', 1, 'D'),
('EFQ', 0, 'D'),
('EFQ', 1, 'D')
GO</pre> </p>
<p><strong>Solution 1: Using CTE</strong></p>
<pre class="crayon-plain-tag">with ctetest as 
( 
SELECT OrderID,Step,Status FROM tbl_Orders WHERE Step = 0 and Status = 'D'
)
select distinct a.OrderID from ctetest a inner join tbl_Orders b on a.OrderID = b.OrderID and b.Status = 'P'</pre> </p>
<p><strong>Solution 2: Using GROUP BY .. Having </strong></p>
<pre class="crayon-plain-tag">SELECT OrderID 
FROM tbl_Orders
GROUP BY OrderID
HAVING COUNT(*) =    
COUNT(CASE WHEN Step &lt;&gt; 0 AND Status = 'P' THEN 1 ELSE NULL END) 
+ COUNT(CASE WHEN Step = 0 AND Status = 'D' THEN 1 ELSE NULL END)</pre> </p>
<p><strong><code>Please try the different solutions for this puzzle and share it via comment...</code></strong></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/08/sql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step/feed/</wfw:commentRss>
			<slash:comments>19</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Generate the group of Sequences</title>
		<link>https://www.dbrnd.com/2019/07/sql-puzzle-sql-advance-query-generate-the-group-of-sequences/</link>
					<comments>https://www.dbrnd.com/2019/07/sql-puzzle-sql-advance-query-generate-the-group-of-sequences/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 29 Jul 2019 16:16:41 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[sequence]]></category>
		<category><![CDATA[SQL Advance Query]]></category>
		<category><![CDATA[SQL Interview]]></category>
		<category><![CDATA[SQL Problem]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5919</guid>

					<description><![CDATA[Check the below input data and expected output to generate the group of sequences. Input Data: [crayon-6a8f5cc7959ec695117417/] Expected Output: [crayon-6a8f5cc7959ee914619062/] Create a table with sample data: [crayon-6a8f5cc7959ef922297662/] Solution: [crayon-6a8f5cc7959f0662817038/] Please try the different solution for this puzzle and share it via comment...]]></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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-group-of-sequences%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20group%20of%20Sequences" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-group-of-sequences%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20group%20of%20Sequences" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-group-of-sequences%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20group%20of%20Sequences" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-group-of-sequences%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20group%20of%20Sequences" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-group-of-sequences%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20group%20of%20Sequences" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-group-of-sequences%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20group%20of%20Sequences" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-group-of-sequences%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20group%20of%20Sequences" data-a2a-url="https://www.dbrnd.com/2019/07/sql-puzzle-sql-advance-query-generate-the-group-of-sequences/" data-a2a-title="SQL Puzzle: SQL Advance Query – Generate the group of Sequences"></a></p><p>Check the below input data and expected output to generate the group of sequences. </p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">Name SeqNo
---- -----------
A    1
A    2
A    3
B    1
B    2
C    1
C    2
C    3
D    1
D    2
D    3
D    4
D    5</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">Name SequenceNo_Groups
---- --------------------
A    11,12,13
A    11,12,13
A    11,12,13
B    21,22
B    21,22
C    31,32,33
C    31,32,33
C    31,32,33
D    41,42,43,44,45
D    41,42,43,44,45
D    41,42,43,44,45
D    41,42,43,44,45
D    41,42,43,44,45</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE TestSequences
(
	Name VARCHAR(3)
	,SeqNo INT
)
GO

INSERT INTO TestSequences
VALUES
('A' ,1)
,('A' ,2)
,('A' ,3)
,('B' ,1)
,('B' ,2)
,('C' ,1)
,('C' ,2)
,('C' ,3)
,('D' ,1)
,('D' ,2)
,('D' ,3)
,('D' ,4)
,('D' ,5)
 GO</pre> </p>
<p><strong>Solution:</strong></p>
<pre class="crayon-plain-tag">;WITH CTE AS
(
    SELECT b.Name , CAST(rnk  * 10 + SeqNo AS VARCHAR(3)) rnk FROM
    (
        SELECT * , ROW_NUMBER() OVER ( ORDER BY (SELECT NULL) )  rnk  FROM
        (
            SELECT DISTINCT Name FROM TestSequences
        ) a
    ) b
    INNER JOIN TestSequences a on b.Name = a.Name
)
SELECT Name, STUFF ((SELECT ',' + rnk FROM CTE w WHERE w.Name = t.Name FOR XML PATH('')) , 1,1,'') SequenceNo_Groups
FROM CTE t</pre> </p>
<p><strong><code>Please try the different solution for this puzzle and share it via comment...</code></strong></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/07/sql-puzzle-sql-advance-query-generate-the-group-of-sequences/feed/</wfw:commentRss>
			<slash:comments>11</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Generate the range of data basis on common combination</title>
		<link>https://www.dbrnd.com/2019/07/sql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination/</link>
					<comments>https://www.dbrnd.com/2019/07/sql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 15 Jul 2019 16:16:14 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[RANGE]]></category>
		<category><![CDATA[SQL Advance Query]]></category>
		<category><![CDATA[SQL Interview]]></category>
		<category><![CDATA[SQL Problem]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5940</guid>

					<description><![CDATA[Check the below input data and expected output to generate the report on range basis on the common combination of SendFlag and ReceiveFlag. Input Data: [crayon-6a8f5cc795bbd036117109/] Expected Output: [crayon-6a8f5cc795bbe985917489/] Create a table with sample data: [crayon-6a8f5cc795bbf771233274/] Solution: [crayon-6a8f5cc795bc0958502066/] Please try the different solution for this puzzle and share it via comment...]]></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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20range%20of%20data%20basis%20on%20common%20combination" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20range%20of%20data%20basis%20on%20common%20combination" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20range%20of%20data%20basis%20on%20common%20combination" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20range%20of%20data%20basis%20on%20common%20combination" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20range%20of%20data%20basis%20on%20common%20combination" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20range%20of%20data%20basis%20on%20common%20combination" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20range%20of%20data%20basis%20on%20common%20combination" data-a2a-url="https://www.dbrnd.com/2019/07/sql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination/" data-a2a-title="SQL Puzzle: SQL Advance Query – Generate the range of data basis on common combination"></a></p><p>Check the below input data and expected output to generate the report on range basis on the common combination of SendFlag and ReceiveFlag.</p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">ID          CreationDate SendFlag ReceiveFlag
----------- ------------ -------- -----------
1           2016-08-08   0        0
2           2016-10-01   0        0
3           2016-11-26   1        0
4           2016-12-26   1        0
5           2017-01-08   1        0
6           2017-03-10   1        1
7           2017-06-20   1        1
8           2017-08-15   0        1</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">StartID     EndID       SendFlag ReceiveFlag
----------- ----------- -------- -----------
1           2           0        0
3           5           1        0
6           7           1        1
8           8           0        1</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE Flags
(
	ID INT 
	,CreationDate DATE
	,SendFlag BIT
	,ReceiveFlag BIT
)
GO
 
INSERT INTO Flags 
VALUES
(1,'20160808',0,0)
,(2,'20161001',0,0)
,(3,'20161126',1,0)
,(4,'20161226',1,0)
,(5,'20170108',1,0)
,(6,'20170310',1,1)
,(7,'20170620',1,1)
,(8,'20170815',0,1)
GO</pre> </p>
<p><strong>Solution:</strong></p>
<pre class="crayon-plain-tag">;WITH CTE1 AS
(
    SELECT f.*, ROW_NUMBER() OVER (ORDER BY f.ID) rnk
    FROM Flags f left JOIN Flags f2 on f.id + 1 = f2.id and f.SendFlag = f2.SendFlag and f.ReceiveFlag  = f2.ReceiveFlag
    WHERE f2.ID IS NULL
),
CTE2 AS
(
    SELECT f2.*,ROW_NUMBER() OVER (ORDER BY f2.ID) rnk
    FROM Flags f RIGHT JOIN Flags f2 on f.id = f2.id - 1 and f.SendFlag = f2.SendFlag and f.ReceiveFlag  = f2.ReceiveFlag
    WHERE f.ID IS NULL
)
SELECT c2.ID as StartID,c1.ID as EndID,c1.SendFlag,c2.ReceiveFlag 
FROM CTE1 c1 INNER JOIN CTE2 c2 on c1.rnk = c2.rnk</pre> </p>
<p><strong><code>Please try the different solution for this puzzle and share it via comment...</code></strong></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/07/sql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; To generate the account balance column for Bank Accounts</title>
		<link>https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts/</link>
					<comments>https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 17 Jun 2019 16:16:59 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[bank]]></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 Interview]]></category>
		<category><![CDATA[SQL Problem]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[transaction]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5860</guid>

					<description><![CDATA[Check the below input data and expected output to prepare the report on bank account transaction like generate the data of AccountBalance column where for CR plus the transaction value and for DR minus the transaction value. Input Data: [crayon-6a8f5cc795d01954468277/] Expected Output: [crayon-6a8f5cc795d02779748701/] Create a table with sample data: [crayon-6a8f5cc795d03905863193/] Solution: [crayon-6a8f5cc795d04295669568/] Please try the [&#8230;]]]></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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" data-a2a-url="https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts/" data-a2a-title="SQL Puzzle: SQL Advance Query – To generate the account balance column for Bank Accounts"></a></p><p>Check the below input data and expected output to prepare the report on bank account transaction like generate the data of AccountBalance column where for CR plus the transaction value and for DR minus the transaction value.</p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">TransactionDate AccName    Type Amount
--------------- ---------- ---- -------------
2017-01-01      Anvesh     CR   60000.00
2017-02-01      Anvesh     DB   8000.00
2017-03-01      Anvesh     CR   8000.00
2017-04-01      Anvesh     DB   5000.00
2017-01-01      Nupur      CR   10000.00
2017-02-02      Nupur      CR   8000.00
2017-03-03      Nupur      DB   8000.00</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">TransactionDate AccName    Type Amount                AccountBalance
--------------- ---------- ---- --------------------- ---------------
2017-01-01      Anvesh     CR   60000.00              60000.00
2017-02-01      Anvesh     DB   8000.00               52000.00
2017-03-01      Anvesh     CR   8000.00               60000.00
2017-04-01      Anvesh     DB   5000.00               55000.00
2017-01-01      Nupur      CR   10000.00              10000.00
2017-02-02      Nupur      CR   8000.00               18000.00
2017-03-03      Nupur      DB   8000.00               10000.00</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE AccountBalance
(
	TransactionDate DATE
	,AccName VARCHAR(10)
	,Type VARCHAR(2)
	,Amount MONEY
)
GO

INSERT INTO AccountBalance
VALUES
('2017-01-01','Anvesh','CR','60000')
,('2017-02-01','Anvesh','DB','8000')
,('2017-03-01','Anvesh','CR','8000')
,('2017-04-01','Anvesh','DB','5000')
,('2017-01-01','Nupur','CR','10000')
,('2017-02-02','Nupur','CR','8000')
,('2017-03-03','Nupur','DB','8000')
GO</pre> </p>
<p><strong>Solution:</strong></p>
<pre class="crayon-plain-tag">;WITH CTE AS
(
    SELECT TransactionDate , AccName , Type 
            , CASE WHEN Type = 'DB' THEN AMOUNT * -1 ELSE AMOUNT END Amount, Amount Amt
            ,ROW_NUMBER() OVER (PARTITION BY AccName ORDER BY (SELECT NULL)) rnk
    FROM AccountBalance
)
SELECT  TransactionDate , AccName , Type , Amt Amount
, (SELECT SUM(Amount) FROM CTE c2 WHERE c2.AccName = c1.AccName AND c2.rnk &lt;= c1.rnk ) AccountBalance
FROM CTE c1</pre> </p>
<p><strong><code>Please try the different solution for this puzzle and share it via comment...</code></strong></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Place NULL for repeating values</title>
		<link>https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-place-null-for-repeating-values/</link>
					<comments>https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-place-null-for-repeating-values/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 03 Jun 2019 16:16:54 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Repeat]]></category>
		<category><![CDATA[SQL Advance Query]]></category>
		<category><![CDATA[SQL Interview]]></category>
		<category><![CDATA[SQL Problem]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5838</guid>

					<description><![CDATA[Check the below input data and expected output to place NULL for repeating values. Input Data: [crayon-6a8f5cc7961c2305109572/] Expected Output: [crayon-6a8f5cc7961c3235011536/] Create a table with sample data: [crayon-6a8f5cc7961c4169918647/] Solution: [crayon-6a8f5cc7961c5760733464/] Please try the different solutions for this puzzle and share it via comment...]]></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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-place-null-for-repeating-values%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Place%20NULL%20for%20repeating%20values" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-place-null-for-repeating-values%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Place%20NULL%20for%20repeating%20values" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-place-null-for-repeating-values%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Place%20NULL%20for%20repeating%20values" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-place-null-for-repeating-values%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Place%20NULL%20for%20repeating%20values" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-place-null-for-repeating-values%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Place%20NULL%20for%20repeating%20values" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-place-null-for-repeating-values%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Place%20NULL%20for%20repeating%20values" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-place-null-for-repeating-values%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Place%20NULL%20for%20repeating%20values" data-a2a-url="https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-place-null-for-repeating-values/" data-a2a-title="SQL Puzzle: SQL Advance Query – Place NULL for repeating values"></a></p><p>Check the below input data and expected output to place NULL for repeating values.<br />
<strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">A           B           C           Code
----------- ----------- ----------- ----
1           1           1           A
1           1           2           A
1           1           3           A
1           1           4           A
2           2           1           C
2           2           2           C
2           2           3           C</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">A           B           C           Code
----------- ----------- ----------- ----
1           1           1           A
NULL        NULL        2           NULL
NULL        NULL        3           NULL
NULL        NULL        4           NULL
2           2           1           C
NULL        NULL        2           NULL
NULL        NULL        3           NULL</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE Code
(
	A int
	,B Int
	,C int
	,Code CHAR(1) 	
)
GO 
 
INSERT INTO Code
VALUES
(1,1,1,'A')
,(1,1,2,'A')
,(1,1,3,'A')
,(1,1,4,'A')
,(2,2,1,'C')
,(2,2,2,'C')
,(2,2,3,'C')
GO</pre> </p>
<p><strong>Solution:</strong></p>
<pre class="crayon-plain-tag">;with ctetest as 
(
select *, row_number()over (partition by Code,A,B order by A) as rnk from Code
) 
select
	case when rnk = 1 then A else NULL end as A
	,case when rnk = 1 then B else NULL end AS B
	,C
	,case when rnk = 1 then Code else NULL end AS Code
from ctetest</pre> </p>
<p><strong><code>Please try the different solutions for this puzzle and share it via comment...</code></strong></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-place-null-for-repeating-values/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Report on Manager &#038; Employee Nth level hierarchy</title>
		<link>https://www.dbrnd.com/2019/05/sql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy/</link>
					<comments>https://www.dbrnd.com/2019/05/sql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 06 May 2019 16:16:43 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[hierarchy]]></category>
		<category><![CDATA[SQL Advance Query]]></category>
		<category><![CDATA[SQL Interview]]></category>
		<category><![CDATA[SQL Problem]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5865</guid>

					<description><![CDATA[Check the below input data and expected output to prepare the report on Manager -> Employee Nth level hierarchy basis on the ManagerID column. Input Data: [crayon-6a8f5cc7962db586947936/] Expected Output: [crayon-6a8f5cc7962dc091936980/] Create a table with sample data: [crayon-6a8f5cc7962dd707552638/] Solution: [crayon-6a8f5cc7962de227447886/] Please try the different solution for this puzzle and share it via comment...]]></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%2F2019%2F05%2Fsql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Report%20on%20Manager%20%26%20Employee%20Nth%20level%20hierarchy" 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%2F2019%2F05%2Fsql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Report%20on%20Manager%20%26%20Employee%20Nth%20level%20hierarchy" 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%2F2019%2F05%2Fsql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Report%20on%20Manager%20%26%20Employee%20Nth%20level%20hierarchy" 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%2F2019%2F05%2Fsql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Report%20on%20Manager%20%26%20Employee%20Nth%20level%20hierarchy" 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%2F2019%2F05%2Fsql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Report%20on%20Manager%20%26%20Employee%20Nth%20level%20hierarchy" 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%2F2019%2F05%2Fsql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Report%20on%20Manager%20%26%20Employee%20Nth%20level%20hierarchy" 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%2F2019%2F05%2Fsql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Report%20on%20Manager%20%26%20Employee%20Nth%20level%20hierarchy" data-a2a-url="https://www.dbrnd.com/2019/05/sql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy/" data-a2a-title="SQL Puzzle: SQL Advance Query – Report on Manager &amp; Employee Nth level hierarchy"></a></p><p>Check the below input data and expected output to prepare the report on Manager -> Employee Nth level hierarchy basis on the ManagerID column.</p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">EmpID       EmpName    ManagerID
----------- ---------- -----------
1           Anvesh     NULL
2           Neevan     NULL
3           Mukesh     1
4           Rajesh     3
5           Nupur      2
6           Roy        5
7           Martin     6
8           Manish     1
9           Eric       2
10          Purv       9</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">EmpName    Hierarchy FullHierarchyName
---------- --------- --------------------------
Anvesh     1         Anvesh.
Manish     8         Anvesh..Manish.
Mukesh     3         Anvesh..Mukesh.
Rajesh     4         Anvesh..Mukesh..Rajesh.
Neevan     2         Neevan.
Eric       9         Neevan..Eric.
Purv       10        Neevan..Eric..Purv.
Nupur      5         Neevan..Nupur.
Roy        6         Neevan..Nupur..Roy.
Martin     7         Neevan..Nupur..Roy..Martin.</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE Employees
(
	EmpID INT
	,EmpName VARCHAR(10)
	,ManagerID INT
)
 
INSERT INTO Employees 
VALUES
(1,'Anvesh',NULL)
,(2,'Neevan',NULL)
,(3,'Mukesh',1)
,(4,'Rajesh',3)
,(5,'Nupur',2)
,(6,'Roy',5)
,(7,'Martin',6)
,(8,'Manish',1)
,(9,'Eric',2)
,(10,'Purv',9)</pre> </p>
<p><strong>Solution:</strong></p>
<pre class="crayon-plain-tag">;WITH CTE(EmpName , EmpId, Level,FullHierarchyName) AS (
     Select E.EmpName, E.EmpID, 0 Level
     ,Cast(E.EmpName+'.' as Varchar(MAX)) FullHierarchyName         
     From Employees E Where E.ManagerID IS NULL
     UNION ALL
     Select E.EmpName, E.EmpID, c.Level + 1 , c.FullHierarchyName+'.'+E.EmpName+'.' FullHierarchyName  
     From Employees E INNER JOIN CTE c on c.EmpID = e.ManagerID 
)
SELECT H.EmpName ,CAST(H.EmpID AS VARCHAR(2)) Hierarchy , FullHierarchyName FROM CTE H 
ORDER BY H.FullHierarchyName</pre> </p>
<p><strong><code>Please try the different solution for this puzzle and share it via comment...</code></strong></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/05/sql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Find the Week start day and Week end day for given Month</title>
		<link>https://www.dbrnd.com/2019/04/sql-puzzle-sql-advance-query-find-the-week-start-day-and-week-end-day-for-given-month/</link>
					<comments>https://www.dbrnd.com/2019/04/sql-puzzle-sql-advance-query-find-the-week-start-day-and-week-end-day-for-given-month/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 22 Apr 2019 16:16:42 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[Anvesh Patel]]></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 Interview]]></category>
		<category><![CDATA[SQL Problem]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=6181</guid>

					<description><![CDATA[Check the below input data and expected output to find the week start day and week end day of given Month and Year. Input data: [crayon-6a8f5cc796687305377600/] Expected Output: [crayon-6a8f5cc796688812363644/] Solution: [crayon-6a8f5cc796689772890968/] Please try the different solution for this puzzle and share it via comment...]]></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%2F2019%2F04%2Fsql-puzzle-sql-advance-query-find-the-week-start-day-and-week-end-day-for-given-month%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Week%20start%20day%20and%20Week%20end%20day%20for%20given%20Month" 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%2F2019%2F04%2Fsql-puzzle-sql-advance-query-find-the-week-start-day-and-week-end-day-for-given-month%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Week%20start%20day%20and%20Week%20end%20day%20for%20given%20Month" 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%2F2019%2F04%2Fsql-puzzle-sql-advance-query-find-the-week-start-day-and-week-end-day-for-given-month%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Week%20start%20day%20and%20Week%20end%20day%20for%20given%20Month" 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%2F2019%2F04%2Fsql-puzzle-sql-advance-query-find-the-week-start-day-and-week-end-day-for-given-month%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Week%20start%20day%20and%20Week%20end%20day%20for%20given%20Month" 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%2F2019%2F04%2Fsql-puzzle-sql-advance-query-find-the-week-start-day-and-week-end-day-for-given-month%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Week%20start%20day%20and%20Week%20end%20day%20for%20given%20Month" 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%2F2019%2F04%2Fsql-puzzle-sql-advance-query-find-the-week-start-day-and-week-end-day-for-given-month%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Week%20start%20day%20and%20Week%20end%20day%20for%20given%20Month" 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%2F2019%2F04%2Fsql-puzzle-sql-advance-query-find-the-week-start-day-and-week-end-day-for-given-month%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Week%20start%20day%20and%20Week%20end%20day%20for%20given%20Month" data-a2a-url="https://www.dbrnd.com/2019/04/sql-puzzle-sql-advance-query-find-the-week-start-day-and-week-end-day-for-given-month/" data-a2a-title="SQL Puzzle: SQL Advance Query – Find the Week start day and Week end day for given Month"></a></p><p>Check the below input data and expected output to find the week start day and week end day of given Month and Year.</p>
<p><strong>Input data:</strong></p>
<pre class="crayon-plain-tag">Month=8
Year=2017</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">WeekStart  WeekEnd
---------- ----------
2017-08-01 2017-08-06
2017-08-07 2017-08-13
2017-08-14 2017-08-20
2017-08-21 2017-08-27
2017-08-28 2017-08-31</pre> </p>
<p><strong>Solution:</strong></p>
<pre class="crayon-plain-tag">DECLARE 
	@Month AS INT = 8
	,@Year AS INT = 2017

;WITH CTE AS
(
     SELECT DISTINCT 
		number
		,DATEFROMPARTS(@Year,@Month,Number) DateValue
		 ,DATEADD(d, (8 - datepart(WEEKDAY, DATEFROMPARTS(@Year,@Month,Number))), DATEFROMPARTS(@Year,@Month,Number)) WeekNumber
     FROM MASTER..SPT_Values
     WHERE Number &gt; 0 AND number &lt; DAY(EOMONTH(CONCAT(@Year,'/',@Month,'/','01'))) 
)
,CTE1 AS 
(
	SELECT 
		CASE WHEN number = 1 THEN DATEADD(d,-1,DateValue) ELSE DateValue End DateValue 
		,CASE WHEN WeekNumber &gt; EOMONTH(DateValue) THEN EOMONTH(DateValue) ELSE WeekNumber END WeekNumber         
     FROM CTE
)
SELECT 
	MIN(DATEADD(d,1,DateValue)) as WeekStart 
	,WeekNumber as WeekEnd 
FROM CTE1
GROUP BY WeekNumber
ORDER BY WeekStart</pre> </p>
<p><strong><code>Please try the different solution for this puzzle and share it via comment...</code></strong></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/04/sql-puzzle-sql-advance-query-find-the-week-start-day-and-week-end-day-for-given-month/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Replace NULL with Previous Non Null value</title>
		<link>https://www.dbrnd.com/2019/03/sql-puzzle-sql-advance-query-replace-null-with-previous-non-null-value/</link>
					<comments>https://www.dbrnd.com/2019/03/sql-puzzle-sql-advance-query-replace-null-with-previous-non-null-value/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 18 Mar 2019 16:16:07 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[Anvesh Patel]]></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 Interview]]></category>
		<category><![CDATA[SQL Problem]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=6105</guid>

					<description><![CDATA[Check the below input data and expected output to replace the NULL value with the previous NON-Null value. Input Data: [crayon-6a8f5cc79674b547016433/] Expected Output: [crayon-6a8f5cc79674c116764510/] Create a table with data: [crayon-6a8f5cc79674d901788866/] Solution: [crayon-6a8f5cc79674e309928701/] Please try the different solution for this puzzle and share it via comment...]]></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%2F2019%2F03%2Fsql-puzzle-sql-advance-query-replace-null-with-previous-non-null-value%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Replace%20NULL%20with%20Previous%20Non%20Null%20value" 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%2F2019%2F03%2Fsql-puzzle-sql-advance-query-replace-null-with-previous-non-null-value%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Replace%20NULL%20with%20Previous%20Non%20Null%20value" 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%2F2019%2F03%2Fsql-puzzle-sql-advance-query-replace-null-with-previous-non-null-value%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Replace%20NULL%20with%20Previous%20Non%20Null%20value" 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%2F2019%2F03%2Fsql-puzzle-sql-advance-query-replace-null-with-previous-non-null-value%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Replace%20NULL%20with%20Previous%20Non%20Null%20value" 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%2F2019%2F03%2Fsql-puzzle-sql-advance-query-replace-null-with-previous-non-null-value%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Replace%20NULL%20with%20Previous%20Non%20Null%20value" 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%2F2019%2F03%2Fsql-puzzle-sql-advance-query-replace-null-with-previous-non-null-value%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Replace%20NULL%20with%20Previous%20Non%20Null%20value" 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%2F2019%2F03%2Fsql-puzzle-sql-advance-query-replace-null-with-previous-non-null-value%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Replace%20NULL%20with%20Previous%20Non%20Null%20value" data-a2a-url="https://www.dbrnd.com/2019/03/sql-puzzle-sql-advance-query-replace-null-with-previous-non-null-value/" data-a2a-title="SQL Puzzle: SQL Advance Query – Replace NULL with Previous Non Null value"></a></p><p>Check the below input data and expected output to replace the NULL value with the previous NON-Null value.</p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">Id          Code
----------- ----
1           ABC
2           XYZ
3           NULL
4           NULL
5           PQR
6           NULL
7           ERT
8           NULL</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">Id          Code Previous_NonNullCode
----------- ---- --------------------
1           ABC  ABC
2           XYZ  XYZ
3           NULL XYZ
4           NULL XYZ
5           PQR  PQR
6           NULL PQR
7           ERT  ERT
8           NULL ERT</pre> </p>
<p><strong>Create a table with data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_Values
(
   Id INT
  ,Code VARCHAR(3)
)
GO

INSERT INTO tbl_Values 
VALUES
(1,'ABC'),(2,'XYZ'),(3,NULL),(4,NULL)
,(5,'PQR'),(6,NULL),(7,'ERT'),(8,NULL)
GO</pre> </p>
<p><strong>Solution:</strong></p>
<pre class="crayon-plain-tag">;WITH CTE AS
(
    SELECT *,ROW_NUMBER() OVER (ORDER BY Id) rnk 
    FROM tbl_Values
)
SELECT 
	Id
	,Code
	,(SELECT TOP 1 Code FROM CTE a WHERE a.rnk = 
	(SELECT MAX(b.rnk) FROM CTE b WHERE b.rnk &lt;= c.rnk AND b.Code IS NOT NULL)) Previous_NonNullCode         
FROM CTE c</pre> </p>
<p><strong><code>Please try the different solution for this puzzle and share it via comment...</code></strong></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/03/sql-puzzle-sql-advance-query-replace-null-with-previous-non-null-value/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Generate the data in the RANGE format</title>
		<link>https://www.dbrnd.com/2019/03/sql-puzzle-sql-advance-query-generate-the-data-in-the-range-format/</link>
					<comments>https://www.dbrnd.com/2019/03/sql-puzzle-sql-advance-query-generate-the-data-in-the-range-format/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 11 Mar 2019 16:16:08 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[RANGE]]></category>
		<category><![CDATA[SQL Advance Query]]></category>
		<category><![CDATA[SQL Interview]]></category>
		<category><![CDATA[SQL Problem]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5897</guid>

					<description><![CDATA[Check the below input data and expected output to start the range from 0 to first value and the first value to second value so on. Input Data: [crayon-6a8f5cc796945349975087/] Expected Data: [crayon-6a8f5cc796946891589454/] Create a table with sample data: [crayon-6a8f5cc796947136214436/] Solution 1: [crayon-6a8f5cc796948159356589/] Solution 2: [crayon-6a8f5cc796949182577626/] Please try the different solution for this puzzle and share [&#8230;]]]></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%2F2019%2F03%2Fsql-puzzle-sql-advance-query-generate-the-data-in-the-range-format%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20data%20in%20the%20RANGE%20format" 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%2F2019%2F03%2Fsql-puzzle-sql-advance-query-generate-the-data-in-the-range-format%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20data%20in%20the%20RANGE%20format" 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%2F2019%2F03%2Fsql-puzzle-sql-advance-query-generate-the-data-in-the-range-format%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20data%20in%20the%20RANGE%20format" 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%2F2019%2F03%2Fsql-puzzle-sql-advance-query-generate-the-data-in-the-range-format%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20data%20in%20the%20RANGE%20format" 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%2F2019%2F03%2Fsql-puzzle-sql-advance-query-generate-the-data-in-the-range-format%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20data%20in%20the%20RANGE%20format" 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%2F2019%2F03%2Fsql-puzzle-sql-advance-query-generate-the-data-in-the-range-format%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20data%20in%20the%20RANGE%20format" 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%2F2019%2F03%2Fsql-puzzle-sql-advance-query-generate-the-data-in-the-range-format%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20data%20in%20the%20RANGE%20format" data-a2a-url="https://www.dbrnd.com/2019/03/sql-puzzle-sql-advance-query-generate-the-data-in-the-range-format/" data-a2a-title="SQL Puzzle: SQL Advance Query – Generate the data in the RANGE format"></a></p><p>Check the below input data and expected output to start the range from 0 to first value and the first value to second value so on.</p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">Val
-----------
108
116
226
308
416
426</pre> </p>
<p><strong>Expected Data:</strong></p>
<pre class="crayon-plain-tag">LowerRange  UpperRange
----------- -----------
0           108
108         116
116         226
226         308
308         416
416         426
426         NULL</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE Ranges
(Val INT)
GO
 
INSERT INTO Ranges(Val) 
VALUES 
(108),(116),
(226),(308),
(416),(426)</pre> </p>
<p><strong>Solution 1:</strong></p>
<pre class="crayon-plain-tag">;with CTE as 
(
select *,row_number()over (order by Val) as rnk from (select 0 Val union select Val from Ranges) AS T
)
select a.Val as LowerRange,b.Val as UpperRange 
from CTE a 
left outer join CTE b on a.rnk = b.rnk-1</pre> </p>
<p><strong>Solution 2:</strong></p>
<pre class="crayon-plain-tag">;WITH CTE AS
(
SELECT Val , ROW_NUMBER() OVER (ORDER BY Val ASC) rnk FROM
(     
       SELECT 0 Val 
       UNION ALL
       SELECT Val from Ranges
)a
)
SELECT c.Val LowerRange , (SELECT TOP 1 Val FROM CTE c1 WHERE c1.rnk &gt; c.rnk ) UpperRange
FROM CTE c</pre> </p>
<p><strong><code>Please try the different solution for this puzzle and share it via comment...</code></strong></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/03/sql-puzzle-sql-advance-query-generate-the-data-in-the-range-format/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Do basic validation of Email Address</title>
		<link>https://www.dbrnd.com/2018/12/sql-puzzle-sql-advance-query-do-basic-validation-of-email-address/</link>
					<comments>https://www.dbrnd.com/2018/12/sql-puzzle-sql-advance-query-do-basic-validation-of-email-address/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 17 Dec 2018 16:16:38 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Email Validation]]></category>
		<category><![CDATA[SQL Advance Query]]></category>
		<category><![CDATA[SQL Interview]]></category>
		<category><![CDATA[SQL Problem]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7287</guid>

					<description><![CDATA[Check the below input data and expected output and perform the basic Email Validation using simple LIKE predicate. Input Data: [crayon-6a8f5cc796a36483020452/] Expected Output: [crayon-6a8f5cc796a37450609186/] Create a table with sample data: [crayon-6a8f5cc796a38367370251/] Solution: [crayon-6a8f5cc796a39684027306/] Please try the different solution for this puzzle and share it via comment...]]></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%2F2018%2F12%2Fsql-puzzle-sql-advance-query-do-basic-validation-of-email-address%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Do%20basic%20validation%20of%20Email%20Address" 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%2F2018%2F12%2Fsql-puzzle-sql-advance-query-do-basic-validation-of-email-address%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Do%20basic%20validation%20of%20Email%20Address" 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%2F2018%2F12%2Fsql-puzzle-sql-advance-query-do-basic-validation-of-email-address%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Do%20basic%20validation%20of%20Email%20Address" 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%2F2018%2F12%2Fsql-puzzle-sql-advance-query-do-basic-validation-of-email-address%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Do%20basic%20validation%20of%20Email%20Address" 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%2F2018%2F12%2Fsql-puzzle-sql-advance-query-do-basic-validation-of-email-address%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Do%20basic%20validation%20of%20Email%20Address" 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%2F2018%2F12%2Fsql-puzzle-sql-advance-query-do-basic-validation-of-email-address%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Do%20basic%20validation%20of%20Email%20Address" 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%2F2018%2F12%2Fsql-puzzle-sql-advance-query-do-basic-validation-of-email-address%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Do%20basic%20validation%20of%20Email%20Address" data-a2a-url="https://www.dbrnd.com/2018/12/sql-puzzle-sql-advance-query-do-basic-validation-of-email-address/" data-a2a-title="SQL Puzzle: SQL Advance Query – Do basic validation of Email Address"></a></p><p>Check the below input data and expected output and perform the basic Email Validation using simple LIKE predicate. </p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">emailname
---------------------
abc@gmail.com
@gmail.com
abcxyz.gmail.com
xyz@yahoo.com</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">emailname                   IsCorrectEmail
--------------------------  --------------
abc@gmail.com                   True
@gmail.com                      False
abcxyz.gmail.com                False
xyz@yahoo.com                   True</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_emails (emailname VARCHAR(MAX))
INSERT INTO tbl_emails VALUES ('abc@gmail.com'),('@gmail.com'),('abcxyz.gmail.com'),('xyz@yahoo.com')
GO</pre> </p>
<p><strong>Solution:</strong></p>
<pre class="crayon-plain-tag">SELECT
  emailname, 
  CASE WHEN emailname LIKE '%_@_%_.__%' 
       THEN 'True' 
  ELSE 'False' 
  END IsCorrectEmail
FROM tbl_emails</pre> </p>
<p><strong><code>Please try the different solution for this puzzle and share it via comment...</code></strong></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2018/12/sql-puzzle-sql-advance-query-do-basic-validation-of-email-address/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
