<?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>Nonclustered Index &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/nonclustered-index/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, 28 Feb 2018 19:53:53 +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 Server Interview: Primary Key Column as Non Clustered Index, Non Primary Key Column as Clustered Index</title>
		<link>https://www.dbrnd.com/2018/03/sql-server-interview-primary-key-column-as-non-clustered-index-non-primary-key-column-as-clustered-index/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 10 Mar 2018 16:16:36 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Interview]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[clustered index]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Nonclustered Index]]></category>
		<category><![CDATA[Primary key]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4236</guid>

					<description><![CDATA[In this post, I am sharing an important interview question for SQL Server Database Developer. The question is: Can we apply Non-clustered Index on Primary Key Column, Can we apply Clustered Index on Non-Primary key column? Last year we arranged a walk in interview for SQL Server Developer. I had taken around 19 f2f interviews [&#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%2F2018%2F03%2Fsql-server-interview-primary-key-column-as-non-clustered-index-non-primary-key-column-as-clustered-index%2F&amp;linkname=SQL%20Server%20Interview%3A%20Primary%20Key%20Column%20as%20Non%20Clustered%20Index%2C%20Non%20Primary%20Key%20Column%20as%20Clustered%20Index" 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%2F03%2Fsql-server-interview-primary-key-column-as-non-clustered-index-non-primary-key-column-as-clustered-index%2F&amp;linkname=SQL%20Server%20Interview%3A%20Primary%20Key%20Column%20as%20Non%20Clustered%20Index%2C%20Non%20Primary%20Key%20Column%20as%20Clustered%20Index" 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%2F03%2Fsql-server-interview-primary-key-column-as-non-clustered-index-non-primary-key-column-as-clustered-index%2F&amp;linkname=SQL%20Server%20Interview%3A%20Primary%20Key%20Column%20as%20Non%20Clustered%20Index%2C%20Non%20Primary%20Key%20Column%20as%20Clustered%20Index" 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%2F03%2Fsql-server-interview-primary-key-column-as-non-clustered-index-non-primary-key-column-as-clustered-index%2F&amp;linkname=SQL%20Server%20Interview%3A%20Primary%20Key%20Column%20as%20Non%20Clustered%20Index%2C%20Non%20Primary%20Key%20Column%20as%20Clustered%20Index" 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%2F03%2Fsql-server-interview-primary-key-column-as-non-clustered-index-non-primary-key-column-as-clustered-index%2F&amp;linkname=SQL%20Server%20Interview%3A%20Primary%20Key%20Column%20as%20Non%20Clustered%20Index%2C%20Non%20Primary%20Key%20Column%20as%20Clustered%20Index" 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%2F03%2Fsql-server-interview-primary-key-column-as-non-clustered-index-non-primary-key-column-as-clustered-index%2F&amp;linkname=SQL%20Server%20Interview%3A%20Primary%20Key%20Column%20as%20Non%20Clustered%20Index%2C%20Non%20Primary%20Key%20Column%20as%20Clustered%20Index" 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%2F03%2Fsql-server-interview-primary-key-column-as-non-clustered-index-non-primary-key-column-as-clustered-index%2F&#038;title=SQL%20Server%20Interview%3A%20Primary%20Key%20Column%20as%20Non%20Clustered%20Index%2C%20Non%20Primary%20Key%20Column%20as%20Clustered%20Index" data-a2a-url="https://www.dbrnd.com/2018/03/sql-server-interview-primary-key-column-as-non-clustered-index-non-primary-key-column-as-clustered-index/" data-a2a-title="SQL Server Interview: Primary Key Column as Non Clustered Index, Non Primary Key Column as Clustered Index"></a></p><p>In this post, I am sharing an important interview question for SQL Server Database Developer.</p>
<p><strong>The question is:</strong></p>
<blockquote><p><strong>Can we apply Non-clustered Index on Primary Key Column, Can we apply Clustered Index on Non-Primary key column?</strong></p></blockquote>
<blockquote><p>Last year we arranged a walk in interview for SQL Server Developer. I had taken around 19 f2f interviews and prepared list of questions for another 88 f2f interviews. </p></blockquote>
<blockquote><p>I asked this question to all 19 candidates and 16 candidates said, we cannot create a Non-clustered Index on Primary Key because Primary Key has a default Clustered Index.<br />
This is wrong, You can create Non-clustered Primary key and can apply Clustered Index on Non-Primary key column.</p></blockquote>
<p>The main reason for this confusion is, SQL Server creates default Clustered Index on the Primary Key column. </p>
<p><strong>Our answer should be like:</strong> <em>Yes, we can define Non-Clustered Primary key column and can define Clustered index on other Non-Primary Key column.</em></p>
<blockquote><p>But the best practice is to define a Primary Key as Clustered Index because Unique value + Physical Order of data returns good query performance.</p></blockquote>
<p>In most of the cases, we are joining data using our Primary Key column, but If we require our main joining on other Non-Primary column and also require records basis on that Non-Primary column data order, we should apply Clustered Index on that Non-Primary key column. </p>
<p><strong>T-SQL Script to create NonClustered Primary Key and Clustered Index Non-Primary key column:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_Students
(
	StudID INT 
	,StudName VARCHAR(20)
	,CONSTRAINT pk_tbl_Students_StudID PRIMARY KEY NONCLUSTERED (StudID)
)
GO

CREATE CLUSTERED INDEX idx_tbl_Students_StudName ON tbl_Students (StudName)
GO</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server Interview: A Golden question, Can we disable the Clustered Index?</title>
		<link>https://www.dbrnd.com/2017/02/sql-server-interview-a-golden-question-can-we-disable-the-clustered-index-offline-table/</link>
					<comments>https://www.dbrnd.com/2017/02/sql-server-interview-a-golden-question-can-we-disable-the-clustered-index-offline-table/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 18 Feb 2017 16:16:16 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Interview]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[Clustered Inded]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[heap table]]></category>
		<category><![CDATA[Nonclustered Index]]></category>
		<category><![CDATA[Primary key]]></category>
		<category><![CDATA[SQL Query]]></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=4544</guid>

					<description><![CDATA[<div><img width="448" height="310" src="https://www.dbrnd.com/wp-content/uploads/2017/02/SQLServerSadClusteredIndex.jpg" class="attachment-post-image size-post-image wp-post-image" alt="SQLServerSadClusteredIndex" decoding="async" fetchpriority="high" srcset="https://www.dbrnd.com/wp-content/uploads/2017/02/SQLServerSadClusteredIndex.jpg 448w, https://www.dbrnd.com/wp-content/uploads/2017/02/SQLServerSadClusteredIndex-262x180.jpg 262w" sizes="(max-width: 448px) 100vw, 448px" /></div> Many people asked me, Are you doing an only blogging or consulting? Every time my clear answer was, I am doing full-time Job as DB Lead in one of the leading IT Companies (working for 9 hours to 10 hours) and As you guys know, that I have shared loads of SQL Server Interview questions [&#8230;]]]></description>
										<content:encoded><![CDATA[<div><img width="448" height="310" src="https://www.dbrnd.com/wp-content/uploads/2017/02/SQLServerSadClusteredIndex.jpg" class="attachment-post-image size-post-image wp-post-image" alt="SQLServerSadClusteredIndex" decoding="async" srcset="https://www.dbrnd.com/wp-content/uploads/2017/02/SQLServerSadClusteredIndex.jpg 448w, https://www.dbrnd.com/wp-content/uploads/2017/02/SQLServerSadClusteredIndex-262x180.jpg 262w" sizes="(max-width: 448px) 100vw, 448px" /></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F02%2Fsql-server-interview-a-golden-question-can-we-disable-the-clustered-index-offline-table%2F&amp;linkname=SQL%20Server%20Interview%3A%20A%20Golden%20question%2C%20Can%20we%20disable%20the%20Clustered%20Index%3F" 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%2F02%2Fsql-server-interview-a-golden-question-can-we-disable-the-clustered-index-offline-table%2F&amp;linkname=SQL%20Server%20Interview%3A%20A%20Golden%20question%2C%20Can%20we%20disable%20the%20Clustered%20Index%3F" 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%2F02%2Fsql-server-interview-a-golden-question-can-we-disable-the-clustered-index-offline-table%2F&amp;linkname=SQL%20Server%20Interview%3A%20A%20Golden%20question%2C%20Can%20we%20disable%20the%20Clustered%20Index%3F" 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%2F02%2Fsql-server-interview-a-golden-question-can-we-disable-the-clustered-index-offline-table%2F&amp;linkname=SQL%20Server%20Interview%3A%20A%20Golden%20question%2C%20Can%20we%20disable%20the%20Clustered%20Index%3F" 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%2F02%2Fsql-server-interview-a-golden-question-can-we-disable-the-clustered-index-offline-table%2F&amp;linkname=SQL%20Server%20Interview%3A%20A%20Golden%20question%2C%20Can%20we%20disable%20the%20Clustered%20Index%3F" 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%2F02%2Fsql-server-interview-a-golden-question-can-we-disable-the-clustered-index-offline-table%2F&amp;linkname=SQL%20Server%20Interview%3A%20A%20Golden%20question%2C%20Can%20we%20disable%20the%20Clustered%20Index%3F" 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%2F02%2Fsql-server-interview-a-golden-question-can-we-disable-the-clustered-index-offline-table%2F&#038;title=SQL%20Server%20Interview%3A%20A%20Golden%20question%2C%20Can%20we%20disable%20the%20Clustered%20Index%3F" data-a2a-url="https://www.dbrnd.com/2017/02/sql-server-interview-a-golden-question-can-we-disable-the-clustered-index-offline-table/" data-a2a-title="SQL Server Interview: A Golden question, Can we disable the Clustered Index?"></a></p><p><em> Many people asked me, Are you doing an only blogging or consulting? Every time my clear answer was, I am doing full-time Job as DB Lead in one of the leading IT Companies (working for 9 hours to 10 hours) and </em></p>
<p>As you guys know, that I have shared loads of SQL Server Interview questions and answers.<br />
Let me tell you a story behind this,</p>
<blockquote><p>Whenever I free in a weekend; I always try to attend walk-in interviews which have been arranged by most of the IT Companies so that I can find some real interview questions and can share with you guys.</p></blockquote>
<p><em>Wait wait wait&#8230;.</em> I know most of the people are thinking that I am running for a job&#8230; which is false.</p>
<blockquote><p>I am not going to change my current company, but I am attending most of the interviews because I want to measure myself so that can share something more and can also take superb interviews that I am also doing in my current company. Additionally spending minimum 3 hours to 4 hours every day for blogging&#8230;</p></blockquote>
<p><em>I know, you guys are suffering my blah blah blah&#8230;. from the above two paragraphs, but for a golden question, you guys could suffer sometimes. </em></p>
<p>Last week, I attended a walk-in interview for DBA position at Tech Mahindra Hyderabad, India.<br />
They asked few questions on Clustered Index, and I was empty entirely.</p>
<p><strong>Questions were:</strong></p>
<ul>
<li>Can we disable the Clustered Index?</li>
<li>Have you ever tried to disable Clustered Index?</li>
<li>Once you disable the Clustered Index, Can we access the table?</li>
<li>Your table has Clustered Index + NonClustered Indexes. If you turn off the Clustered Index, Can you access your table using NonClustered Indexes?</li>
<li>Can we take the table in offline mode by disabling the Clustered Index?</li>
<li>Ok, If you disable the Clustered Index, how can you enable it again?</li>
</ul>
<p><strong>My simple answers were:</strong></p>
<ul>
<li>I don&#8217;t know it is possible or not</li>
<li>I never tried to disable the Primary key, and if it is possible</li>
<li>I don&#8217;t know how we can find the data path to access the table</li>
</ul>
<p>Now, I don&#8217;t want to write more blah, blah, blah&#8230; Find the below full demonstration and test it yourself and at the end, you will get all answers to above questions.</p>
<p><code>All the best....</code></p>
<p><em><strong>Now, one more question to you guys. If you have heap table, what ?, Can anyone share their thoughts via putting some comments?</strong></em></p>
<p><strong>Create a sample table:</strong><br />
<pre class="crayon-plain-tag">CREATE TABLE tbl_Students 
(
	StudID INT IDENTITY(1,1)
	,Name VARCHAR(10)
	,CONSTRAINT pk_tbl_Students_StudID PRIMARY KEY(StudID)
)
GO</pre>
<p><strong>Create a NonClustered index on Name column:</strong><br />
<pre class="crayon-plain-tag">CREATE NONCLUSTERED INDEX idx_tbl_Students_Name ON tbl_Students(Name)
GO</pre>
<p><strong>Insert 10000 dummy records:</strong><br />
<pre class="crayon-plain-tag">INSERT INTO tbl_Students(Name) VALUES ('Anvesh')
GO 10000</pre>
<p><strong>Now, Disable the Clustered Index:</strong><br />
It also disables the all Nonclustered Indexes of a table. You can check below warning message***<br />
<pre class="crayon-plain-tag">ALTER INDEX pk_tbl_Students_StudID ON dbo.tbl_Students DISABLE
GO</pre>
<pre class="crayon-plain-tag">Warning: Index 'idx_tbl_Students_Name' on table 'tbl_Students' was disabled as a result of disabling the clustered index on the table.</pre>
<p><strong>Try to select a table:</strong><br />
You will get a below error, You cannot select the table data because of query optimizer unable to find the access path of data.<br />
<pre class="crayon-plain-tag">SELECT *FROM tbl_Students
GO</pre>
<pre class="crayon-plain-tag">Msg 8655, Level 16, State 1, Line 1
The query processor is unable to produce a plan because the index 'pk_tbl_Students_StudID' on table or view 'tbl_Students' is disabled.</pre>
<p><strong>The solution is, Rebuild your Clustered Index and you will get your table back:</strong><br />
<pre class="crayon-plain-tag">ALTER INDEX pk_tbl_Students_StudID ON dbo.tbl_Students REBUILD
GO</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2017/02/sql-server-interview-a-golden-question-can-we-disable-the-clustered-index-offline-table/feed/</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
			</item>
	</channel>
</rss>
