<?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>Solomon &#187; Interview Questions &#8211; SQL Server</title>
	<atom:link href="http://www.ms.oyangudi.com/blog/tag/interview-questions-sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ms.oyangudi.com/blog</link>
	<description>from Oyangudi...</description>
	<lastBuildDate>Tue, 18 Oct 2011 02:38:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>SQL Server &#8211; Interview Questions #2</title>
		<link>http://www.ms.oyangudi.com/blog/interview-questions-sql-server/sql-server-interview-questions-2/</link>
		<comments>http://www.ms.oyangudi.com/blog/interview-questions-sql-server/sql-server-interview-questions-2/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 03:37:39 +0000</pubDate>
		<dc:creator>Solomon</dc:creator>
				<category><![CDATA[Interview Questions - SQL Server]]></category>

		<guid isPermaLink="false">http://www.ms.oyangudi.com/blog/?p=10</guid>
		<description><![CDATA[Database design What is denormalization and when would you go for it? As the name indicates, denormalization is the reverse process of normalization. It&#8217;s the controlled introduction of redundancy in to the database design. It helps improve the query performance as the number of joins could be reduced. How do you implement one-to-one, one-to-many and [...]]]></description>
			<content:encoded><![CDATA[<div class="simplesocialbuttons">
<div class="simplesocialbutton ssb-button-googleplus"><!-- Google Plus One--><g:plusone size="medium" count="true" href="http://www.ms.oyangudi.com/blog/interview-questions-sql-server/sql-server-interview-questions-2/"></g:plusone></div>
<div class="simplesocialbutton ssb-button-fblike"><!-- Facebook like--><div id="fb-root"></div><fb:like href="http://www.ms.oyangudi.com/blog/interview-questions-sql-server/sql-server-interview-questions-2/" send="false" layout="button_count" width="100" show_faces="false" action="like" font=""></fb:like></div>
<div class="simplesocialbutton ssb-buttom-twitter"><!-- Twitter--><a name="twitter_share" data-count="horizontal" href="http://twitter.com/share" data-text="SQL Server &ndash; Interview Questions #2" data-url="http://www.ms.oyangudi.com/blog/interview-questions-sql-server/sql-server-interview-questions-2/" class="twitter-share-button" rel="nofollow"></a></div>
</div>
<h2><strong>Database design</strong></h2>
<p><strong>What is denormalization and when would you go for it?      <br /></strong>As the name indicates, denormalization is the reverse process of normalization. It&#8217;s the controlled introduction of redundancy in to the database design. It helps improve the query performance as the number of joins could be reduced.     </p>
<p><strong>How do you implement one-to-one, one-to-many and many-to-many relationships while designing tables?      <br /></strong>One-to-One relationship can be implemented as a single table and rarely as two tables with primary and foreign key relationships.     <br />One-to-Many relationships are implemented by splitting the data into two tables with primary key and foreign key relationships.     <br />Many-to-Many relationships are implemented using a junction table with the keys from both the tables forming the composite primary key of the junction table. </p>
<p>&#160;</p>
<p><strong>What&#8217;s the difference between a primary key and a unique key?      <br /></strong>Both primary key and unique enforce uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a nonclustered index by default. Another major difference is that, primary key doesn&#8217;t allow NULLs, but unique key allows one NULL only.     </p>
<p><strong>What are user defined datatypes and when you should go for them?      <br /></strong>User defined datatypes let you extend the base SQL Server datatypes by providing a descriptive name, and format to the database. Take for example, in your database, there is a column called Flight_Num which appears in many tables. In all these tables it should be varchar(8). In this case you could create a user defined datatype called Flight_num_type of varchar(8) and use it across all your tables. </p>
<p>&#160;</p>
<p><strong>What is bit datatype and what&#8217;s the information that can be stored inside a bit column?      <br /></strong>Bit datatype is used to store boolean information like 1 or 0 (true or false). Untill SQL Server 6.5 bit datatype could hold either a 1 or 0 and there was no support for NULL. But from SQL Server 7.0 onwards, bit datatype can represent a third state, which is NULL.     </p>
<p><strong>Define candidate key, alternate key, composite key.      <br /></strong>A candidate key is one that can identify each row of a table uniquely. Generally a candidate key becomes the primary key of the table. If the table has more than one candidate key, one of them will become the primary key, and the rest are called alternate keys. </p>
<p>A key formed by combining at least two or more columns is called composite key.    </p>
<p><strong>What are defaults? Is there a column to which a default can&#8217;t be bound?      <br /></strong>A default is a value that will be used by a column, if no value is supplied to that column while inserting data. IDENTITY columns and timestamp columns can&#8217;t have defaults bound to them. </p>
<p>&#160;</p>
<h2>SQL Server architecture&#160;&#160;&#160; </h2>
<p> 
<p><strong>What is a transaction and what are ACID properties?      <br /></strong>A transaction is a logical unit of work in which, all the steps must be performed or none. ACID stands for Atomicity, Consistency, Isolation, and Durability. These are the properties of a transaction. For more information and explanation of these properties, see SQL Server books online or any RDBMS fundamentals text book.     <br />Explain different isolation levels     <br />An isolation level determines the degree of isolation of data between concurrent transactions. The default SQL Server isolation level is Read Committed. Here are the other isolation levels (in the ascending order of isolation): Read Uncommitted, Read Committed, Repeatable Read, Serializable. See SQL Server books online for an explanation of the isolation levels. Be sure to read about SET TRANSACTION ISOLATION LEVEL, which lets you customize the isolation level at the connection level.</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">CREATE</span> <span style="color: #0000ff">INDEX</span> myIndex <span style="color: #0000ff">ON</span> myTable(myColumn) </pre>
</p></div>
</div>
<p>&#160;</p>
<p><strong>What type of Index will get created after executing the above statement?<br />
    <br /></strong>Non-clustered index. Important thing to note: By default a clustered index gets created on the primary key, unless specified otherwise. </p>
<p></p>
<p><strong>What&#8217;s the maximum size of a row?<br />
    <br /></strong>8060 bytes. Don&#8217;t be surprised with questions like &#8216;what is the maximum number of columns per table&#8217;. Check out SQL Server books online for the page titled: &quot;Maximum Capacity Specifications&quot;. </p>
<p></p>
<p><strong>Explain Active/Active and Active/Passive cluster configurations<br />
    <br /></strong>Hopefully you have experience setting up cluster servers. But if you don&#8217;t, at least be familiar with the way clustering works and the two clustering configurations Active/Active and Active/Passive. SQL Server books online has enough information on this topic and there is a good white paper available on Microsoft site. </p>
<p>Explain the architecture of SQL Server </p>
<p>This is a very important question and you better be able to answer it if consider yourself a DBA. SQL Server books online is the best place to read about SQL Server architecture. Read up the chapter dedicated to SQL Server Architecture. </p>
<p></p>
<p><strong>What is lock escalation?<br />
    <br /></strong>Lock escalation is the process of converting a lot of low level locks (like row locks, page locks) into higher level locks (like table locks). Every lock is a memory structure too many locks would mean, more memory being occupied by locks. To prevent this from happening, SQL Server escalates the many fine-grain locks to fewer coarse-grain locks. Lock escalation threshold was definable in SQL Server 6.5, but from SQL Server 7.0 onwards it&#8217;s dynamically managed by SQL Server. </p>
<p></p>
<p><strong>What&#8217;s the difference between DELETE TABLE and TRUNCATE TABLE commands?<br />
    <br /></strong>DELETE TABLE is a logged operation, so the deletion of each row gets logged in the transaction log, which makes it slow. TRUNCATE TABLE also deletes all the rows in a table, but it won&#8217;t log the deletion of each row, instead it logs the deallocation of the data pages of the table, which makes it faster. Of course, TRUNCATE TABLE can be rolled back. </p>
<p></p>
<p><strong>Explain the storage models of OLAP<br />
    <br /></strong>Check out MOLAP, ROLAP and HOLAP in SQL Server books online for more information. </p>
<p></p>
<p><strong>What are the new features introduced in SQL Server 2000 (or the latest release of SQL Server at the time of your interview)? What changed between the previous version of SQL Server and the current version?<br />
    <br /></strong>This question is generally asked to see how current is your knowledge. Generally there is a section in the beginning of the books online titled &quot;What&#8217;s New&quot;, which has all such information. Of course, reading just that is not enough, you should have tried those things to better answer the questions. Also check out the section titled &quot;Backward Compatibility&quot; in books online which talks about the changes that have taken place in the new version. </p>
<p></p>
<p><strong>What are constraints? Explain different types of constraints.<br />
    <br /></strong>Constraints enable the RDBMS enforce the integrity of the database automatically, without needing you to create triggers, rule or defaults. </p>
<p>Types of constraints: NOT NULL, CHECK, UNIQUE, PRIMARY KEY, FOREIGN KEY </p>
<p>For an explanation of these constraints see books online for the pages titled: &quot;Constraints&quot; and &quot;CREATE TABLE&quot;, &quot;ALTER TABLE&quot;<br />
  </p>
<p><strong>What is an index? What are the types of indexes? How many clustered indexes can be created on a table? I create a separate index on each column of a table. What are the advantages and disadvantages of this approach?<br />
    <br /></strong>Indexes in SQL Server are similar to the indexes in books. They help SQL Server retrieve the data quicker. </p>
<p>Indexes are of two types. Clustered indexes and non-clustered indexes. When you create a clustered index on a table, all the rows in the table are stored in the order of the clustered index key. So, there can be only one clustered index per table. Non-clustered indexes have their own storage separate from the table data storage. Non-clustered indexes are stored as B-tree structures (so do clustered indexes), with the leaf level nodes having the index key and it&#8217;s row locater. The row located could be the RID or the Clustered index key, depending up on the absence or presence of clustered index on the table. </p>
<p>If you create an index on each column of a table, it improves the query performance, as the query optimizer can choose from all the existing indexes to come up with an efficient execution plan. At the same time, data modification operations (such as INSERT, UPDATE, DELETE) will become slow, as every time data changes in the table, all the indexes need to be updated. Another disadvantage is that, indexes need disk space, the more indexes you have, more disk space is used.<br />
  </p>
<h2>Database administration&#160;&#160;&#160; </h2>
<p><strong></strong></p>
<p>
  <br /><strong>What is RAID and what are different types of RAID configurations?<br />
    <br /></strong>RAID stands for Redundant Array of Inexpensive Disks, used to provide fault tolerance to database servers. There are six RAID levels 0 through 5 offering different levels of performance, fault tolerance. MSDN has some information about RAID levels and for detailed information, check out the RAID advisory board&#8217;s homepage </p>
<p></p>
<p><strong>What are the steps you will take to improve performance of a poor performing query?<br />
    <br /></strong>This is a very open ended question and there could be a lot of reasons behind the poor performance of a query. But some general issues that you could talk about would be: No indexes, table scans, missing or out of date statistics, blocking, excess recompilations of stored procedures, procedures and triggers without SET NOCOUNT ON, poorly written query with unnecessarily complicated joins, too much normalization, excess usage of cursors and temporary tables. </p>
<p>Some of the tools/ways that help you troubleshooting performance problems are: SET SHOWPLAN_ALL ON, SET SHOWPLAN_TEXT ON, SET STATISTICS IO ON, SQL Server Profiler, Windows NT /2000 Performance monitor, Graphical execution plan in Query Analyzer. </p>
<p><strong></strong></p>
<p><strong>What are the steps you will take, if you are tasked with securing an SQL Server?<br />
    <br /></strong>Again this is another open ended question. Here are some things you could talk about: Preferring NT authentication, using server, database and application roles to control access to the data, securing the physical database files using NTFS permissions, using an unguessable SA password, restricting physical access to the SQL Server, renaming the Administrator account on the SQL Server computer, disabling the Guest account, enabling auditing, using multiprotocol encryption, setting up SSL, setting up firewalls, isolating SQL Server from the web server etc. </p>
<p><strong></strong></p>
<p><strong>What is a deadlock and what is a live lock? How will you go about resolving deadlocks?<br />
    <br /></strong>Deadlock is a situation when two processes, each having a lock on one piece of data, attempt to acquire a lock on the other&#8217;s piece. Each process&#160; would wait indefinitely for the other to release the lock, unless one of the user processes is terminated. SQL Server detects deadlocks and terminates one user&#8217;s process. </p>
<p>A livelock is one, where a&#160; request for an exclusive lock is repeatedly denied because a series of overlapping shared locks keeps interfering. SQL Server detects the situation after four denials and refuses further shared locks. A livelock also occurs when read transactions monopolize a table or page, forcing a write transaction to wait indefinitely. </p>
<p><strong></strong></p>
<p><strong>What is blocking and how would you troubleshoot it?<br />
    <br /></strong>Blocking happens when one connection from an application holds a lock and a second connection requires a conflicting lock type. This forces the second connection to wait, blocked on the first. </p>
<p><strong></strong></p>
<p><strong>Explain CREATE DATABASE syntax<br />
    <br /></strong>Many of us are used to craeting databases from the Enterprise Manager or by just issuing the command: CREATE DATABAE MyDB. But what if you have to create a database with two filegroups, one on drive C and the other on drive D with log on drive E with an initial size of 600 MB and with a growth factor of 15%? That&#8217;s why being a DBA you should be familiar with the CREATE DATABASE syntax. Check out SQL Server books online for more information. </p>
<p></p>
<p><strong>How to restart SQL Server in single user mode? How to start SQL Server in minimal configuration mode?<br />
    <br /></strong>SQL Server can be started from command line, using the SQLSERVR.EXE. This EXE has some very important parameters with which a DBA should be familiar with. -m is used for starting SQL Server in single user mode and -f is used to start the SQL Server in minimal confuguration mode. Check out SQL Server books online for more parameters and their explanations. </p>
<p></p>
<p><strong>As a part of your job, what are the DBCC commands that you commonly use for database maintenance?<br />
    <br /></strong>DBCC CHECKDB, DBCC CHECKTABLE, DBCC CHECKCATALOG, DBCC CHECKALLOC, DBCC SHOWCONTIG, DBCC SHRINKDATABASE, DBCC SHRINKFILE etc. But there are a whole load of DBCC commands which are very useful for DBAs. Check out SQL Server books online for more information. </p>
<p></p>
<p><strong>What are statistics, under what circumstances they go out of date, how do you update them?<br />
    <br /></strong>Statistics determine the selectivity of the indexes. If an indexed column has unique values then the selectivity of that index is more, as opposed to an index with non-unique values. Query optimizer uses these indexes in determining whether to choose an index or not while executing a query. </p>
<p>Some situations under which you should update statistics:<br />
  <br />1) If there is significant change in the key values in the index </p>
<p>2) If a large amount of data in an indexed column has been added, changed, or removed (that is, if the distribution of key values has changed), or the table has been truncated using the TRUNCATE TABLE statement and then repopulated </p>
<p>3) Database is upgraded from a previous version </p>
<p>Look up SQL Server books online for the following commands: UPDATE STATISTICS, STATS_DATE, DBCC SHOW_STATISTICS, CREATE STATISTICS, DROP STATISTICS, sp_autostats, sp_createstats, sp_updatestats<br />
  </p>
<p><strong>What are the different ways of moving data/databases between servers and databases in SQL Server?<br />
    <br /></strong>There are lots of options available; you have to choose your option depending upon your requirements. Some of the options you have are: BACKUP/RESTORE, detaching and attaching databases, replication, DTS, BCP, logshipping, INSERT&#8230;SELECT, SELECT&#8230;INTO, creating INSERT scripts to generate data. </p>
<p></p>
<p><strong>Explian different types of BACKUPs avaialabe in SQL Server? Given a particular scenario, how would you go about choosing a backup plan?<br />
    <br /></strong>Types of backups you can create in SQL Sever 7.0+ are Full database backup, differential database backup, transaction log backup, filegroup backup. Check out the BACKUP and RESTORE commands in SQL Server books online. Be prepared to write the commands in your interview. Books online also has information on detailed backup/restore architecture and when one should go for a particular kind of backup. </p>
<p></p>
<p><strong>What is database replication? What are the different types of replication you can set up in SQL Server?<br />
    <br /></strong>Replication is the process of copying/moving data between databases on the same or different servers. SQL Server supports the following types of replication scenarios: </p>
<p>•&#160;&#160;&#160; Snapshot replication </p>
<p>•&#160;&#160;&#160; Transactional replication (with immediate updating subscribers, with queued updating subscribers) </p>
<p>•&#160;&#160;&#160; Merge replication </p>
<p></p>
<p><strong>How to determine the service pack currently installed on SQL Server?<br />
    <br /></strong>The global variable @@Version stores the build number of the sqlservr.exe, which is used to determine the service pack installed. To know more about this process visit SQL Server service packs and versions. </p>
<p></p>
<h2>Database programming&#160;&#160;&#160; </h2>
<p></p>
<p><strong>What are cursors? Explain different types of cursors. What are the disadvantages of cursors? How can you avoid cursors?<br />
    <br /></strong>Cursors allow row-by-row prcessing of the resultsets. </p>
<p>Types of cursors: Static, Dynamic, Forward-only, Keyset-driven. See books online for more information. </p>
<p>Disadvantages of cursors: Each time you fetch a row from the cursor, it results in a network roundtrip, where as a normal SELECT query makes only one roundtrip, however large the resultset is. Cursors are also costly because they require more resources and temporary storage (results in more IO operations). Furthere, there are restrictions on the SELECT statements that can be used with some types of cursors. </p>
<p>Most of the times, set based operations can be used instead of cursors. Here is an example: </p>
<p>If you have to give a flat hike to your employees using the following criteria: </p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> Salary <span style="color: #0000ff">between</span> 30000 <span style="color: #0000ff">and</span> 40000 <span style="color: #008000">-- 5000 hike</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   2:</span> Salary <span style="color: #0000ff">between</span> 40000 <span style="color: #0000ff">and</span> 55000 <span style="color: #008000">-- 7000 hike</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   3:</span> Salary <span style="color: #0000ff">between</span> 55000 <span style="color: #0000ff">and</span> 65000 -- 9000 hike </pre>
</p></div>
</div>
<p>In this situation many developers tend to use a cursor, determine each employee&#8217;s salary and update his salary according to the above formula. But the same can be achieved by multiple update statements or can be combined in a single UPDATE statement as shown below: </p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">UPDATE</span> tbl_emp <span style="color: #0000ff">SET</span> salary = </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   2:</span>     <span style="color: #0000ff">CASE</span> <span style="color: #0000ff">WHEN</span> salary <span style="color: #0000ff">BETWEEN</span> 30000 <span style="color: #0000ff">AND</span> 40000 <span style="color: #0000ff">THEN</span> salary + 5000</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   3:</span>         <span style="color: #0000ff">WHEN</span> salary <span style="color: #0000ff">BETWEEN</span> 40000 <span style="color: #0000ff">AND</span> 55000 <span style="color: #0000ff">THEN</span> salary + 7000</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   4:</span>         <span style="color: #0000ff">WHEN</span> salary <span style="color: #0000ff">BETWEEN</span> 55000 <span style="color: #0000ff">AND</span> 65000 <span style="color: #0000ff">THEN</span> salary + 10000</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">END</span> </pre>
</p></div>
</div>
<p>Another situation in which developers tend to use cursors: You need to call a stored procedure when a column in a particular row meets certain condition. You don&#8217;t have to use cursors for this. This can be achieved using WHILE loop, as long as there is a unique key to identify each row. For examples of using WHILE loop for row by row processing, check out the &#8216;My code library&#8217; section of my site or search for WHILE.<br />
  </p>
<p><strong>Write down the general syntax for a SELECT statement covering all the options.<br />
    <br /></strong>Here&#8217;s the basic syntax: (Also checkout SELECT in books online for advanced syntax). </p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">SELECT</span> select_list</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   2:</span> [<span style="color: #0000ff">INTO</span> new_table_]</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   3:</span> <span style="color: #0000ff">FROM</span> table_source</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   4:</span> [<span style="color: #0000ff">WHERE</span> search_condition]</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   5:</span> [<span style="color: #0000ff">GROUP</span> <span style="color: #0000ff">BY</span> group_by_expression]</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   6:</span> [<span style="color: #0000ff">HAVING</span> search_condition]</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   7:</span> [<span style="color: #0000ff">ORDER</span> <span style="color: #0000ff">BY</span> order_expression [<span style="color: #0000ff">ASC</span> | <span style="color: #0000ff">DESC</span>] ]</pre>
</p></div>
</div>
<p><strong>What is a join and explain different types of joins.<br />
    <br /></strong>Joins are used in queries to explain how different tables are related. Joins also let you select data from a table depending upon data from another table. </p>
<p>Types of joins: INNER JOINs, OUTER JOINs, CROSS JOINs. OUTER JOINs are further classified as LEFT OUTER JOINS, RIGHT OUTER JOINS and FULL OUTER JOINS. </p>
<p><strong></strong></p>
<p><strong>Can you have a nested transaction?<br />
    <br /></strong>Yes, very much. Check out BEGIN TRAN, COMMIT, ROLLBACK, SAVE TRAN and @@TRANCOUNT </p>
<p></p>
<p><strong>What is an extended stored procedure? Can you instantiate a COM object by using T-SQL?<br />
    <br /></strong>An extended stored procedure is a function within a DLL (written in a programming language like C, C++ using Open Data Services (ODS) API) that can be called from T-SQL, just the way we call normal stored procedures using the EXEC statement. See books online to learn how to create extended stored procedures and how to add them to SQL Server. </p>
<p>Yes, you can instantiate a COM (written in languages like VB, VC++) object from T-SQL by using sp_OACreate stored procedure. Also see books online for sp_OAMethod, sp_OAGetProperty, sp_OASetProperty, sp_OADestroy. For an example of creating a COM object in VB and calling it from T-SQL, see &#8216;My code library&#8217; section of this site.<br />
  </p>
<p><strong>What is the system function to get the current user&#8217;s user id?<br />
    <br /></strong>USER_ID(). Also check out other system functions like USER_NAME(), SYSTEM_USER, SESSION_USER, CURRENT_USER, USER, SUSER_SID(), HOST_NAME(). </p>
<p></p>
<p><strong>What are triggers? How many triggers you can have on a table? How to invoke a trigger on demand?<br />
    <br /></strong>Triggers are special kind of stored procedures that get executed automatically when an INSERT, UPDATE or DELETE operation takes place on a table. </p>
<p>In SQL Server 6.5 you could define only 3 triggers per table, one for INSERT, one for UPDATE and one for DELETE. From SQL Server 7.0 onwards, this restriction is gone, and you could create multiple triggers per each action. But in 7.0 there&#8217;s no way to control the order in which the triggers fire. In SQL Server 2000 you could specify which trigger fires first or fires last using sp_settriggerorder </p>
<p>Triggers can&#8217;t be invoked on demand. They get triggered only when an associated action (INSERT, UPDATE, DELETE) happens on the table on which they are defined. </p>
<p>Triggers are generally used to implement business rules, auditing. Triggers can also be used to extend the referential integrity checks, but wherever possible, use constraints for this purpose, instead of triggers, as constraints are much faster. </p>
<p>Till SQL Server 7.0, triggers fire only after the data modification operation happens. So in a way, they are called post triggers. But in SQL Server 2000 you could create pre triggers also. </p>
<p>There is a trigger defined for INSERT operations on a table, in an OLTP system. The trigger is written to instantiate a COM object and pass the newly inserted rows to it for some custom processing. What do you think of this implementation? Can this be implemented better?<br />
  <br />Instantiating COM objects is a time consuming process and since you are doing it from within a trigger, it slows down the data insertion process. Same is the case with sending emails from triggers. This scenario can be better implemented by logging all the necessary data into a separate table, and have a job which periodically checks this table and does the needful. </p>
<p></p>
<p><strong>What is a self join? Explain it with an example.<br />
    <br /></strong>Self join is just like any other join, except that two instances of the same table will be joined in the query. Here is an example: Employees table which contains rows for normal employees as well as managers. So, to find out the managers of all the employees, you need a self join. </p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">CREATE</span> <span style="color: #0000ff">TABLE</span> emp </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   2:</span> (</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   3:</span>     empid <span style="color: #0000ff">int</span>,</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   4:</span>     mgrid <span style="color: #0000ff">int</span>,</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   5:</span>     empname <span style="color: #0000ff">char</span>(10)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   6:</span> ) </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   7:</span>&#160; </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   8:</span> INSERT emp <span style="color: #0000ff">SELECT</span> 1,2,<span style="color: #006080">'Vyas'</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   9:</span> INSERT emp <span style="color: #0000ff">SELECT</span> 2,3,<span style="color: #006080">'Mohan'</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  10:</span> INSERT emp <span style="color: #0000ff">SELECT</span> 3,<span style="color: #0000ff">NULL</span>,<span style="color: #006080">'Shobha'</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  11:</span> INSERT emp <span style="color: #0000ff">SELECT</span> 4,2,<span style="color: #006080">'Shridhar'</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  12:</span> INSERT emp <span style="color: #0000ff">SELECT</span> 5,2,<span style="color: #006080">'Sourabh'</span> </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  13:</span>&#160; </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  14:</span> <span style="color: #0000ff">SELECT</span> t1.empname [Employee], t2.empname [Manager]</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  15:</span>     <span style="color: #0000ff">FROM</span> emp t1, emp t2</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  16:</span>     <span style="color: #0000ff">WHERE</span> t1.mgrid = t2.empid</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  17:</span>&#160; </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  18:</span> <span style="color: #008000">-- Here's an advanced query using a LEFT OUTER JOIN </span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  19:</span> <span style="color: #008000">-- that even returns the employees without managers (super bosses) </span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  20:</span>&#160; </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  21:</span> <span style="color: #0000ff">SELECT</span> t1.empname [Employee], <span style="color: #0000ff">COALESCE</span>(t2.empname, <span style="color: #006080">'No manager'</span>) [Manager]</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  22:</span>     <span style="color: #0000ff">FROM</span> emp t1 <span style="color: #0000ff">LEFT</span> <span style="color: #0000ff">OUTER</span> <span style="color: #0000ff">JOIN</span>  emp t2</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  23:</span>         <span style="color: #0000ff">ON</span> t1.mgrid = t2.empid</pre>
</p></div>
</div>
<div class="simplesocialbuttons">
<div class="simplesocialbutton ssb-button-googleplus"><!-- Google Plus One--><g:plusone size="medium" count="true" href="http://www.ms.oyangudi.com/blog/interview-questions-sql-server/sql-server-interview-questions-2/"></g:plusone></div>
<div class="simplesocialbutton ssb-button-fblike"><!-- Facebook like--><div id="fb-root"></div><fb:like href="http://www.ms.oyangudi.com/blog/interview-questions-sql-server/sql-server-interview-questions-2/" send="false" layout="button_count" width="100" show_faces="false" action="like" font=""></fb:like></div>
<div class="simplesocialbutton ssb-buttom-twitter"><!-- Twitter--><a name="twitter_share" data-count="horizontal" href="http://twitter.com/share" data-text="SQL Server &ndash; Interview Questions #2" data-url="http://www.ms.oyangudi.com/blog/interview-questions-sql-server/sql-server-interview-questions-2/" class="twitter-share-button" rel="nofollow"></a></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.ms.oyangudi.com/blog/interview-questions-sql-server/sql-server-interview-questions-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server &#8211; Interview Questions #1</title>
		<link>http://www.ms.oyangudi.com/blog/interview-questions-sql-server/sql-server-interview-questions-1/</link>
		<comments>http://www.ms.oyangudi.com/blog/interview-questions-sql-server/sql-server-interview-questions-1/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 03:35:35 +0000</pubDate>
		<dc:creator>Solomon</dc:creator>
				<category><![CDATA[Interview Questions - SQL Server]]></category>

		<guid isPermaLink="false">http://www.ms.oyangudi.com/blog/?p=9</guid>
		<description><![CDATA[1. What are the different types of Joins? Ans. Joins as used to combine the contents of two or more tables and produce a result set that incorporates rows and columns from each table. Tables are typically joined using data that they have in common Join conditions can be specified in either the FROM or [...]]]></description>
			<content:encoded><![CDATA[<div class="simplesocialbuttons">
<div class="simplesocialbutton ssb-button-googleplus"><!-- Google Plus One--><g:plusone size="medium" count="true" href="http://www.ms.oyangudi.com/blog/interview-questions-sql-server/sql-server-interview-questions-1/"></g:plusone></div>
<div class="simplesocialbutton ssb-button-fblike"><!-- Facebook like--><div id="fb-root"></div><fb:like href="http://www.ms.oyangudi.com/blog/interview-questions-sql-server/sql-server-interview-questions-1/" send="false" layout="button_count" width="100" show_faces="false" action="like" font=""></fb:like></div>
<div class="simplesocialbutton ssb-buttom-twitter"><!-- Twitter--><a name="twitter_share" data-count="horizontal" href="http://twitter.com/share" data-text="SQL Server &ndash; Interview Questions #1" data-url="http://www.ms.oyangudi.com/blog/interview-questions-sql-server/sql-server-interview-questions-1/" class="twitter-share-button" rel="nofollow"></a></div>
</div>
<p><b>1. What are the different types of Joins?      <br />Ans.</b> <b>Joins</b> as used to combine the contents of two or more tables and produce a result set that incorporates rows and columns from each table. Tables are typically joined using data that they have in common Join conditions can be specified in either the FROM or WHERE clauses; specifying them in the FROM clause is recommended. WHERE and HAVING clauses can also contain search conditions to further filter the rows selected by the join conditions.</p>
<p>Joins can be categorized as: </p>
<p><b>Inner joins</b> &#8211; (the typical join operation, which uses some comparison operator like = or &lt;&gt;). An inner join is a join in which the values in the columns being joined are compared using a comparison operator Inner joins use a comparison operator to match rows from two tables based on the values in common columns from each table. </p>
<p><b>Equi Join</b> &#8211; It returns all the columns in both tables, and returns only the rows for which there is an equal value in the join column</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> ( <span style="color: #0000ff">SELECT</span> * <span style="color: #0000ff">FROM</span> authors <span style="color: #0000ff">AS</span> a </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   2:</span>     <span style="color: #0000ff">INNER</span> <span style="color: #0000ff">JOIN</span> publishers <span style="color: #0000ff">AS</span> p </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">ON</span> a.city = p.city </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">ORDER</span> <span style="color: #0000ff">BY</span> a.au_lname <span style="color: #0000ff">DESC</span> )</pre>
</p></div>
</div>
<p><b>Self Join</b> &#8211; A table can be joined to itself in a self-join</p>
<p><b>Outer joins</b> &#8211; Outer joins can be a left, right, or full outer join. Outer joins are specified with one of the following sets of keywords when they are specified in the FROM clause: </p>
<p><b>LEFT JOIN or LEFT OUTER JOIN</b> &#8211; The result set of a left outer join includes all the rows from the left table specified in the LEFT OUTER clause, not just the ones in which the joined columns match. When a row in the left table has no matching rows in the right table, the associated result set row contains null values for all select list columns coming from the right table. </p>
<p><b>RIGHT JOIN or RIGHT OUTER JOIN</b> -&#160; A right outer join is the reverse of a left outer join. All rows from the right table are returned. Null values are returned for the left table any time a right table row has no matching row in the left table. </p>
<p><b>FULL JOIN or FULL OUTER JOIN</b> -&#160;&#160; A full outer join returns all rows in both the left and right tables. Any time a row </p>
<p>has no match in the other table, the select list columns from the other table contain null values. When there is a match between the tables,the entire result set row contains data values from the base tables. </p>
<p><b>Cross joins</b> &#8211; Cross joins return all rows from the left table, each row from the left table is combined with all rows from the right table. Cross joins are also called Cartesian products.</p>
<p><b></b></p>
<p><b>2. What is diff. between left and Right outer Join?<br />
    <br />Ans.</b> The result set of a left outer join includes all the rows from the left table specified in the LEFT OUTER clause , while in case of Right outer join all the rows from the right table are returned in the result set.</p>
<p><b></b></p>
<p><b>3. Why we use Unicode In Sql server?<br />
    <br />Ans.</b>Using Unicode data types, a column can store any character that is defined by the Unicode Standard, which includes all of the characters that are defined in the various character sets. Unicode data types take twice as much storage space as non-Unicode data types.</p>
<p>Unicode data is stored using the nchar, nvarchar, and ntext data types in SQL Server. Use these data types for columns that store characters from more than one character set. The SQL Server Unicode data types are based on the National Character data types in the SQL-92 standard</p>
<p><b></b></p>
<p><b>4.What is Fill Factor and its value when the index is created.<br />
    <br />Ans.</b>An option used when creating an index to reserve free space on each page of the index. FILLFACTOR accommodates future expansion of table data and reduces the potential for page splits. FILLFACTOR is a value from 1 through 100 that specifies the percentage of the index page to be left empty.Default is 0.</p>
<p>A fill factor value of 0 does not mean that pages are 0 percent full. It is treated similarly to a fill factor value of 100 in that SQL Server creates clustered indexes with full data pages and non clustered indexes with full leaf pages.If you set fill factor to 100, SQL Server creates both clustered and nonclustered indexes with each page 100 percent full. Setting fill factor to 100 is suitable only for read-only tables, to which additional data is never added.</p>
<p><b></b></p>
<p><b>5.What are the different types of File backup options?<br />
    <br />Ans.</b> All data and objects in the database, such as tables, stored procedures, triggers, and views, are stored only within the following operating system files: </p>
<p><b>Primary<br />
    <br /></b>This file contains the startup information for the database and is used to store data. Every database has one primary data file. </p>
<p><b>Secondary<br />
    <br /></b>These files hold all of the data that does not fit in the primary data file. If the primary file can hold all of the data in the database, databases do not need to have secondary data files. Some databases may be large enough to need multiple secondary data files or to use secondary files on separate disk drives to spread data across multiple disks. </p>
<p><b>Transaction Log<br />
    <br /></b>These files hold the log information used to recover the database. There must be at least one log file for each database. File groups allow files to be grouped together for administrative and data allocation/placement purposes</p>
<p><b>Rules of Files and Filegroups<br />
    <br /></b>Rules for designing files and filegroups include: </p>
<p>• A file or filegroup cannot be used by more than one database. For example, file sales.mdf and sales.ndf, which contain data and objects from the sales database, cannot be used by any other database. </p>
<p>• A file can be a member of only one filegroup. </p>
<p>• Data and transaction log information cannot be part of the same file or filegroup. </p>
<p>• Transaction log files are never part of any filegroups. </p>
<p><b>Types of BackUP<br />
    <br /></b>Database </p>
<p>Transaction log </p>
<p>Differential </p>
<p>Filegroup &amp; Backup</p>
<p><b></b></p>
<p><b>6.What is Differential File group backup?<br />
    <br />Ans.</b>Differential database backup records only those data changes made to the database after the last full database backup. A differential database backup is smaller and takes less time to complete than a database backup. By creating differential database backups more frequently than database backups, you can decrease the amount of data you risk losing.</p>
<p><b></b></p>
<p><b>7.What is Collate?</b> </p>
<p><b>Ans.</b>A clause that can be applied to a database definition or a column definition to define the collation, or to a character string expression to apply a collation cast. The COLLATE clause can be applied only for the char, varchar, text, nchar, nvarchar, and ntext data types. The physical storage of character strings in Microsoft® SQL Server™ 2000 is controlled by collations. A collation specifies the bit patterns that represent each character and the rules by which characters are sorted and compared</p>
<p><b></b></p>
<p><b>8. What is Code Page? </b></p>
<p><b>Ans.</b>A character set that a computer uses to interpret and display characters, often to handle international characters. Essentially, it is a table of characters and corresponding numbers in memory that the computer uses to display data properly. Different languages and locales may use different code pages.</p>
<p><b></b></p>
<p><b>9.When we should go for with Recompile option in Stored Procedures?<br />
    <br />Ans.</b>When stored procedures take parameters whose values differ widely between executions of the stored procedure, resulting in different execution plans to be created each time.&#160; If the parameter you are supplying is atypical or if the data has significantly changed since the stored procedure was created. Use of this option is uncommon, and causes the stored procedure to execute more slowly because the stored procedure must be recompiled each time it is executed. Creating a stored procedure that specifies the WITH RECOMPILE option in its definition indicates that SQL Server does not cache a plan for this stored procedure; the stored procedure is recompiled each time it is executed.</p>
<p><b></b></p>
<p><b>10.What is difference between Having and Group by Clause?<br />
    <br />Ans.</b> The HAVING clause sets conditions on the GROUP BY clause similar to the way WHERE interacts with SELECT. The WHERE search condition is applied before the grouping operation occurs; the HAVING search condition is applied after the grouping operation occurs. The HAVING syntax is exactly like the WHERE syntax, except HAVING can contain aggregate functions. </p>
<p>HAVING clauses can reference any of the items that appear in the select list. </p>
<p>• The WHERE clause is used to filter the rows that result from the operations specified in the FROM clause. </p>
<p>• The GROUP BY clause is used to group the output of the WHERE clause. </p>
<p>• The HAVING clause is used to filter rows from the grouped result</p>
<p>Where – Groupby – Having – Order By</p>
<p><b></b></p>
<p><b>11.What are Isolation levels?</b> </p>
<p><b>Ans. </b>An isolation level determines the degree to which data is isolated for use by one process and guarded against interference from other processes.</p>
<p><b>Read Committed </b>- SQL Server acquires a share lock while reading a row into a cursor but frees the lock immediately after reading the row. Because a shared lock request is blocked by an exclusive lock, a cursor is prevented from reading a row that another task has updated but not yet committed. Read committed is the default isolation level setting for both SQL Server and ODBC. </p>
<p><b>Read Uncommitted </b>- SQL Server requests no locks while reading a row into a cursor and honors no exclusive locks. Cursors can be populated with values that have already been updated but not yet&#160; committed. The user is bypassing all of SQL Server&#8217;s locking transaction control mechanisms. </p>
<p><b>Repeatable Read or Serializable</b> &#8211; SQL Server requests a shared lock on each row as it is read into the cursor as in READ COMMITTED, but if the cursor is opened within a transaction, the shared locks are held until the end of the transaction instead of being freed after the row is read. This has the same effect as specifying HOLDLOCK on a SELECT statement. </p>
<p><b>12.When should we go for NOLOCK hint?<br />
    <br />Ans. </b>A range of table-level locking hints can be specified using the SELECT, INSERT, UPDATE, and DELETE statements to direct Microsoft® SQL Server™ 2000 to the type of locks to be used. Table-level locking hints can be used when a finer control of the types of locks acquired on an object is required. These locking hints override the current transaction isolation level for the session.</p>
<p><b>NOLOCK </b>Do not issue shared locks and do not honor exclusive locks. When this option is in effect, it is possible to read an uncommitted transaction or a set of pages that are rolled back in the middle of a read. Dirty reads are possible. Only applies to the SELECT statement.</p>
<p><b></b></p>
<p><b>13. What are diff. types of Database import?<br />
    <br />Ans.</b>BCP, DTS, BulkCopy Command</p>
<p><b></b></p>
<p><b>14.Diff. between BCP and DTS, which one is advantageous?<br />
    <br />Ans.</b> BCP is a command prompt utility. bcp provides for running bulk copies in .bat and .cmd scripts. bcp is used to bulk copy large files into tables or views in SQL Server databases. BCP is non logged operation.</p>
<p>DTS allows the user to program transformations through two different interfaces.</p>
<p>BCP is extremely fast and has a minimal overhead, but it also has a very rigid (and sometimes unforgiving) syntax. Then along came Data Transformation Services (DTS), an improved method for importing and exporting data between heterogeneous data sources. Whether you want to move data from a legacy system on a onetime basis or continually move data back and forth for data warehousing, DTS should be your first choice. With DTS you don&#8217;t need to struggle with BCP anymore. DTS is extremely flexible and surprisingly fast, and you can use the technology to copy and transform data to or from any OLE DB or ODBC data source.</p>
<p>BCP is designed to work with flat files, while DTS can work with &quot;any&quot; database. using BCP, you have a much more mature tool, and clearer error messages than with DTS.</p>
<p><b></b></p>
<p><b>15.What are DBCC COMMANDS?</b> </p>
<p><b>Ans. </b>The Transact-SQL programming language provides DBCC statements that act as the &quot;database consistency checker&quot; for Microsoft® SQL Server™. These statements check the physical and logical consistency of a database. Many DBCC statements can fix detected problems. These database consistency-checking statements are grouped into these categories.</p>
<p>Maintenance Statements:<br />
  <br />Maintenance tasks on a database, index, or filegroup. </p>
<p>DBCC DBREPAIR<br />
  <br />DBCC SHRINKFILE </p>
<p>DBCC DBREINDEX </p>
<p>DBCC UPDATEUSAGE </p>
<p>DBCC SHRINKDATABASE&#160;&#160;&#160; </p>
<p>Miscellaneous Statements:<br />
  <br />Miscellaneous tasks such as enabling row-level locking or removing a dynamic-link library (DLL) from memory</p>
<p>DBCC dllname (FREE)<br />
  <br />DBCC TRACEOFF </p>
<p>DBCC HELP </p>
<p>DBCC TRACEON </p>
<p>DBCC PINTABLE </p>
<p>DBCC UNPINTABLE </p>
<p>DBCC ROWLOCK&#160;&#160; </p>
<p>Status Statements:<br />
  <br />Status Checks</p>
<p>DBCC INPUTBUFFER<br />
  <br />DBCC SHOW_STATISTICS </p>
<p>DBCC OPENTRAN </p>
<p>DBCC SQLPERF </p>
<p>DBCC OUTPUTBUFFER </p>
<p>DBCC TRACESTATUS </p>
<p>DBCC PROCCACHE </p>
<p>DBCC USEROPTIONS </p>
<p>DBCC SHOWCONTIG&#160;&#160; </p>
<p>Validation Statements:<br />
  <br />Validation operations on a database, table, index, catalog, filegroup, system tables, or allocation of database pages.</p>
<p>DBCC CHECKALLOC<br />
  <br />DBCC CHECKTABLE </p>
<p>DBCC CHECKCATALOG </p>
<p>DBCC NEWALLOC </p>
<p>DBCC CHECKDB </p>
<p>DBCC TEXTALL </p>
<p>DBCC CHECKFILEGROUP </p>
<p>DBCC TEXTALLOC </p>
<p>DBCC CHECKIDENT&#160;&#160; </p>
<p><b></b></p>
<p><b>16. What are System Stored Procedures?<br />
    <br /></b>SQL Server-supplied, precompiled collection of Transact-SQL statements. System stored procedures are provided as shortcuts Ans.for retrieving information from system tables or mechanisms for accomplishing database administration and other tasks that involve updating system tables. The names of all system stored procedures begin with sp_. System stored procedures are located in the master database and are owned by the system administrator, but many of them can be run from any database. If a system stored procedure is executed in a database other than master, it operates on the system tables in the database from which it is executed. </p>
<p><b></b></p>
<p><strong>17. Can we prefix user defined SP&#8217;s with sp_, if so what will happen if u call that SP?<br />
    <br /></strong>Ans. SQL Server always looks for stored procedures beginning with sp_ in this order: </p>
<p>i. Look for the stored procedure in the master database first. </p>
<p>ii. Look for the stored procedure based on any qualifiers provided (database name or owner). </p>
<p>iii. Look for the stored procedure using dbo as the owner, if one is not specified. </p>
<p>Therefore, although the user-created stored procedure prefixed with sp_ may exist in the current database, the master database is always checked first, even if the stored procedure is qualified with the database name Important : If any user-created stored procedure has the same name as a system stored procedure, the user-created stored procedure will never be executed</p>
<p><strong>18. What are Indexes?</strong> </p>
<p><b>Ans.</b>Microsoft SQL Server index is a structure associated with a table that speeds retrieval of the rows in the table. An index contains keys built from one or more columns in the table. These keys are stored in a structure that allows SQL Server to find the row or rows associated with the key values quickly and efficiently. If a table is created with no indexes, the data rows are not stored in any particular order. This structure is called a heap.</p>
<p>The two types of SQL Server indexes are:<br />
  <br /><strong>• Clustered<br />
    <br /></strong>Clustered indexes sort and store the data rows in the table based on their key values. Because the data rows are stored in sorted order on the clustered index key, clustered indexes are efficient for finding rows. There can only be one clustered index per table, because the data rows themselves can only be sorted in one order. The data rows themselves form the lowest level of the clustered index. </p>
<p>The only time the data rows in a table are stored in sorted order is when the table contains a clustered index. If a table has no clustered index, its data rows are stored in a heap. </p>
<p><strong>• Non clustered<br />
    <br /></strong>Nonclustered indexes have a structure that is completely separate from the data rows. The lowest rows of a nonclustered index contain the nonclustered index key values and each key value entry has pointers to the data rows containing the key value. The data rows are not stored in order based on the nonclustered key. The pointer from an index row in a nonclustered index to a data row is called a row locator. The structure of the row locator depends on whether the data pages are stored in a heap or are clustered. For a heap, a row locator is a pointer to the row. For a table with a clustered index, the row locator is the clustered index key </p>
<p><strong>19.When will we go for clustered and Non-clustered Indexes?<br />
    <br />Ans.</strong> </p>
<p>Indexes assist when a query: </p>
<p>• Searches for rows that match a specific search key value (an exact match query). An exact match comparison is one in which the query uses the WHERE statement to specify a column entry with a given value. </p>
<p>For example: WHERE emp_id = &#8216;VPA30890F&#8217; </p>
<p>• Searches for rows with search key values in a range of values (a range query). A range query is one in which the query specifies any entry whose value is between two values.<br />
  <br />For example: WHERE job_lvl BETWEEN 9 and 12 </p>
<p>or, WHERE job_lvl &gt;= 9 and job_lvl &lt;= 12 </p>
<p>• Searches for rows in a table T1 that match, based on a join predicate, a row in another table T2 (an index nested loops join). </p>
<p>• Produces sorted query output without an explicit sort operation, in particular for sorted dynamic cursors. </p>
<p>• Scans rows in a sorted order to permit an order-based operation, such as merge join and stream aggregation, without an explicit sort operation. </p>
<p>• Scans all rows in a table with better performance than a table scan, due to the reduced column set and overall data volume to be scanned (a covering index for the query at hand).<br />
  <br />• Searches for duplicates of new search key values in insert and update operations, to enforce PRIMARY KEY and UNIQUE constraints. </p>
<p>• Searches for matching rows between two tables for which a FOREIGN KEY constraint is defined. Queries using LIKE comparisons can benefit from an index if the pattern starts with a specific character string, for example &#8216;abc%&#8217;, but not if the pattern starts with a wildcard search, for example &#8216;%xyz&#8217;.</p>
<p>• Columns that contain a high number of distinct values, such as a combination of last name and first name (if a clustered index is used for other columns). If there are very few distinct values, such as only 1 and 0, no index should be created. </p>
<p>• Queries that do not return large result sets. </p>
<p>• Columns frequently involved in search conditions of a query (WHERE clause) that return exact matches. </p>
<p>• Decision Support System applications for which joins and grouping are frequently required. Create multiple nonclustered indexes on columns involved in join and grouping operations, and a clustered index on any foreign key columns. </p>
<p>• Columns that contain a limited number of distinct values, such as a state column that contains only 50 distinct state codes. However, if there are very few distinct values, such as only 1 and 0, no index should be created. </p>
<p>• Queries that return a range of values using operators such as BETWEEN, &gt;, &gt;=, &lt;, and &lt;=. </p>
<p>• Columns that are accessed sequentially. </p>
<p>• Queries that return large result sets. </p>
<p>• Columns that are frequently accessed by queries involving join or GROUP BY clauses; typically these are foreign key columns. An index on the column(s)<br />
  <br />specified in the ORDER BY or GROUP BY clause eliminates the need for SQL Server to sort the data because the rows are already sorted. This improves query </p>
<p>performance. </p>
<p>• OLTP-type applications where very fast single row lookup is required, typically by means of the primary key. Create a clustered index on the primary key. </p>
<p>Clustered indexes are not a good choice for:<br />
  <br />• Columns that undergo frequent changes because this results in the entire row moving (because SQL Server must keep the row&#8217;s data values in physical order). This is an important consideration in high-volume transaction processing systems where data tends to be volatile. </p>
<p>• Covered queries. The more columns within the search key, the greater the chance for the data in the indexed column to change, resulting in additional I/O.</p>
<p><b></b></p>
<p><strong>20. What is Referential Integrity?<br />
    <br />Ans.</strong>Referential integrity preserves the defined relationships between tables when records are entered or deleted. In SQL Server, referential integrity is based on relationships between foreign keys and primary keys or between foreign keys and unique keys. Referential integrity ensures that key values are consistent across tables. Such consistency requires that there be no references to nonexistent values and that if a key value changes, all references to it change consistently throughout the database.When you enforce referential integrity, SQL Server </p>
<p>prevents users from: </p>
<p>Adding records to a related table if there is no associated record in the primary table. </p>
<p>Changing values in a primary table that result in orphaned records in a related table. </p>
<p>Deleting records from a primary table if there are matching related records.</p>
<p><b></b></p>
<p><strong>21.What are the Features of Normalized and De-normalized Databases?<br />
    <br />Ans.</strong>Normalizing a logical database design involves using formal methods to separate the data into multiple, related tables. A greater number of narrow tables (with fewer columns) is characteristic of a normalized database.&#160; A few wide tables (with more columns) is characteristic of an unnormalized database. Some of the benefits of normalization include: </p>
<p>• Faster sorting and index creation. </p>
<p>• A larger number of clustered indexes. </p>
<p>• Narrower and more compact indexes. </p>
<p>• Fewer indexes per table, which improves the performance of INSERT, UPDATE, and DELETE statements. </p>
<p>• Fewer NULL values and less opportunity for inconsistency, which increase database compactness. </p>
<p></p>
<p><b></b></p>
<p><strong>22. When will you go for De-normalization?<br />
    <br />Ans.</strong>To introduce redundancy into a table in order to incorporate data from a related table. The related table can then be eliminated. Denormalization can improve efficiency and performance by reducing complexity in a data warehouse schema.</p>
<p><strong></strong></p>
<p><strong>23.Union and Union ALL, which will give better performance and why?<br />
    <br /></strong>Ans.The UNION operator allows you to combine the results of two or more SELECT statements into a single result set. The result sets combined using UNION must all have the same structure. They must have the same number of columns, and the corresponding result set columns must have compatible data types. </p>
<p>The result set column names of a UNION are the same as the column names in the result set of the first SELECT statement in the UNION. The result set column names of the other SELECT statements are ignored. </p>
<p>By default, the UNION operator removes duplicate rows from the result set. If you use ALL, all rows are included in the results and duplicates are not removed </p>
<p>UNION ALL is faster because it doesn&#8217;t try to eliminate the duplicates. In fact, as a &quot;duplicate&quot; is only determined by comparing the whole row (all the columns returned), you should use UNION ALL all the time, except if you WANT to eliminate the duplicates by using UNION</p>
<p><b></b></p>
<p><strong>24.What is the Diff. between drop and detach database?<br />
    <br />Ans.</strong> Drop Database: Removes one or more databases from SQL Server. Removing a database deletes the database and the disk files used by the database. </p>
<p>A database that has been dropped can be re-created only by restoring a backup. You cannot drop a database currently in use (open for reading or writing by any user). Whenever a database is dropped, the master database should be backed up .</p>
<p>Detach Database<br />
  <br />Detaches a database from a server and, optionally, runs UPDATE STATISTICS on all tables before detaching.Detaching a database removes the database from SQL Server, but leaves the database intact within the data and transaction log files that compose the database. These data and transaction log files can then be used to attach the database to any computer running SQL Server, including the server from which the database was detached. This makes the database available in exactly the same state it was in when it was detached.</p>
<p><strong></strong></p>
<p><strong>25.What are Instead of Triggers?</strong> </p>
<p><b>Ans.</b>INSTEAD OF triggers are executed instead of the triggering action (for example, INSERT, UPDATE, DELETE). They can also be defined on views, in which case they greatly extend the types of updates a view can support. The trigger executes in place of the triggering action. INSTEAD OF triggers can be specified on both tables and views. You can define only one INSTEAD OF trigger for each triggering action (INSERT, UPDATE, and DELETE). INSTEAD OF triggers can be used to perform enhance integrity checks on the data values supplied in INSERT </p>
<p>and UPDATE statements. INSTEAD OF triggers also let you specify actions that allow views, which would normally not support updates, to be updatable.</p>
<p><b></b></p>
<p><strong>26. Solve the Query: Customer Table</strong></p>
<p><b>Name</b> <b>Phoneno<br />
    <br /></b>Abc&#160;&#160;&#160;&#160;&#160;&#160; 123 </p>
<p>Pqr&#160;&#160;&#160;&#160;&#160;&#160;&#160; 234 </p>
<p>Rst&#160;&#160;&#160;&#160;&#160;&#160;&#160; 235 </p>
<p>Xyz&#160;&#160;&#160;&#160;&#160;&#160;&#160; 997</p>
<p><b>a.</b>Write a query to find out customers and count of phno with more than one phnumber</p>
<p><b>Ans. </b></p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">Select</span>  name, <span style="color: #0000ff">Count</span>(Phoneno) <span style="color: #0000ff">from</span> customer <span style="color: #0000ff">having</span> <span style="color: #0000ff">count</span>(Phoneno) &gt; 1</pre>
</p></div>
</div>
<p><b>b.</b>Add a row pqr&#160; phno 234. Now write a query to get the count of phno and name with no duplicate values</p>
<p><b>Ans.</b> </p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">Select</span> [Name],phoneno,<span style="color: #0000ff">count</span>(phoneno) <span style="color: #0000ff">as</span> countofPNo <span style="color: #0000ff">from</span> customer <span style="color: #0000ff">group</span> <span style="color: #0000ff">by</span> [Name], phoneno</pre>
</p></div>
</div>
<p><strong></strong></p>
<p><strong>27.Explain about DTS.</strong> </p>
<p><b>Ans.</b> Data Transformation Services (DTS) provides the functionality to import, export, and transform data between SQL Server and any OLE DB, ODBC, or text file format. Using DTS, it is possible to: </p>
<p>Build data warehouses and data marts in Microsoft SQL Server by importing and transferring data from multiple heterogeneous sources interactively or automatically on a regularly scheduled basis. </p>
<p>Create custom transformation objects that can be integrated into third-party products. </p>
<p>Access applications using third-party OLE DB providers.<br />
  <br />This allows applications, for which an OLE DB provider exists, to be used as sources and destinations of data. DTS also provides support for: </p>
<p>• High-speed nonlogged inserts (bcp) into SQL Server version 7.0. </p>
<p>• Creating customized transformations. </p>
<p>• Transferring complete database objects between source and destination SQL Server 7.0 data sources. The Transfer SQL Server Objects task can be used to transfer all of the metadata and data for some or all of the objects in one SQL </p>
<p>Server 7.0 database to another SQL Server 7.0 database. </p>
<p>For example, the Transfer SQL Server Objects task can be used to move a table with all of its associated index, constraint, rule, default, and trigger definitions and the existing rows in the table. The Transfer SQL Server Objects task also can be used to transfer the definitions of objects such as views and stored procedures. </p>
<p><b></b></p>
<p><strong>28.What is OpenRowset?<br />
    <br />Ans.</strong>It includes all connection information necessary to access remote data from an OLE DB data source. </p>
<p>This method is an alternative to accessing tables in a linked server and is a one-time, ad hoc method of connecting and accessing remote data using OLE DB. The OPENROWSET function can be referenced in the FROM clause of a query as though it is a table name. The OPENROWSET function can also be referenced as the target table of an INSERT, UPDATE, or DELETE statement, subject to the capabilities of the OLE DB provider. Although the query may return multiple result sets, OPENROWSET returns only the first one.<br />
  <br />Ex:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">SELECT</span> a.*  <span style="color: #0000ff">FROM</span> <span style="color: #0000ff">OPENROWSET</span>   (<span style="color: #006080">'SQLOLEDB'</span>,<span style="color: #006080">'seattle1'</span>;<span style="color: #006080">'sa'</span>;<span style="color: #006080">'MyPass'</span>,</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   2:</span> <span style="color: #006080">'SELECT * FROM pubs.dbo.authors ORDER BY au_lname, au_fname'</span>) <span style="color: #0000ff">AS</span> a</pre>
</p></div>
</div>
<p><b></b></p>
<p><strong>29.What are stored procedures?<br />
    <br />Ans.</strong>When you use Transact-SQL programs, two methods are available for storing and executing the programs. You can store the programs locally and create applications that send the commands to SQL Server and process the results, or you can store the programs as stored procedures in SQL Server and create applications that execute the stored procedures and process the results. Stored procedures in SQL Server are similar to procedures in other programming languages in that they can: </p>
<p>Accept input parameters and return multiple values in the form of output parameters to the calling procedure or batch. </p>
<p>Contain programming statements that perform operations in the database, including calling other procedures. </p>
<p>Return a status value to a calling procedure or batch to indicate success or failure (and the reason for failure).<br />
  <br />You can use the Transact-SQL EXECUTE statement to run a stored procedure. Stored procedures are different from functions in that they do not return values in place of their names and they cannot be used directly in an expression.The benefits of using stored procedures in SQL Server rather than Transact-SQL programs stored locally on client computers are: </p>
<p>They allow modular programming.<br />
  <br />You can create the procedure once, store it in the database, and call it any number of times in your program. Stored procedures can be created by a person who specializes in database programming, and they can be modified independently of the program source code. </p>
<p>They allow faster execution. If the operation requires a large amount of Transact-SQL code or is performed repetitively, stored procedures can be faster than batches of Transact-SQL code. They are parsed and optimized when they are created, and an in-memory version of the procedure can be used after the procedure is executed the first time. Transact-SQL statements repeatedly sent from the client each time they run are compiled and optimized every time they are executed by SQL Server. </p>
<p>They can reduce network traffic. An operation requiring hundreds of lines of Transact-SQL code can be performed through a single statement that executes the code in a procedure, rather than by sending hundreds of lines of code over the network. </p>
<p>They can be used as a security mechanism. Users can be granted permission to execute a stored procedure even if they do not have permission to execute the procedure&#8217;s statements directly. </p>
<p>A SQL Server stored procedure is created with the Transact-SQL CREATE PROCEDURE statement and can be modified with the ALTER PROCEDURE statement. The stored procedure definition contains two primary components: the specification of the procedure name and its parameters, and the body of the procedure, which contains Transact-SQL statements that perform the procedure&#8217;s operations.<br />
  <br /><b>Extended Stored Procedures<br />
    <br /></b>Extended stored procedures allow you to create your own external routines in a programming language such as C. The extended stored procedures appear to users as normal stored procedures and are executed in the same way. Parameters can be passed to extended stored procedures, and they can return results and return status. Extended stored procedures can be used to extend the capabilities of Microsoft® SQL Server™. Extended stored procedures are dynamic link libraries (DLLs) that SQL Server can dynamically load and execute. Extended stored procedures run directly in the address space of SQL Server and are programmed using the SQL Server Open Data Services API. After an extended stored procedure has been written, members of the sysadmin fixed server role can register the extended stored procedure with SQL Server and then grant permission to other users to execute the procedure. Extended stored procedures can be added only to the master database</p>
<p><b>Remote Stored Procedure<br />
    <br /></b>A collection of SQL statements and optional control-of-flow statements stored under a name on a remote server. Remote stored procedures can be called by clients or SQL Server </p>
<p><b>Distributed Queries</b> </p>
<p>Distributed queries access data from multiple heterogeneous data sources, which can be stored in either the same or different computers. SQL Server supports distributed queries </p>
<p>by using OLE DB, the Microsoft specification of an application programming interface (API) for universal data access.</p>
<p>Distributed queries provide SQL Server users with access to:<br />
  <br />• Distributed data stored in multiple computers that are running SQL Server. </p>
<p>• Heterogeneous data stored in various relational and non-relational data sources that can be accessed using an OLE DB provider. OLE DB providers expose their data in tabular objects called rowsets. SQL Server version 7.0 allows rowsets from OLE DB providers to be referenced in Transact-SQL statements as if they were a SQL Server table.</p>
<p><b></b></p>
<p><b>30. Solve the Query</b></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; Name&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Tel<br />
  <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; ABC&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 100 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; DEF&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 200 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; HIG&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 300 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; ABC&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 100 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; DEF&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 400 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; PQR&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 500 </p>
<p>Write a query to list all the customers and the no. of telephones they have, only if they have more than one unique telephone no&#160;&#160;&#160; </p>
<p><b>Ans. </b></p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">Select</span> Name, <span style="color: #0000ff">count</span>(phoneno) <span style="color: #0000ff">as</span> noofPhoneno <span style="color: #0000ff">from</span> customers <span style="color: #0000ff">group</span> <span style="color: #0000ff">by</span> Name <span style="color: #0000ff">having</span> noofPhoneno &gt; 1</pre>
</p></div>
</div>
<p><b></b></p>
<p><b>31.</b> <b>Solve Query. </b>Consider the following table: </p>
<p>&#160;&#160;&#160;&#160; Sernum&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Day&#160; Temp. </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; 1&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Mon&#160;&#160;&#160;&#160;&#160; 10 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; 2&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Tue&#160;&#160;&#160;&#160;&#160;&#160; 12 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; 3&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Wed&#160;&#160;&#160;&#160;&#160;&#160;&#160; 9 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; 4&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Thurs&#160;&#160;&#160;&#160; 15 </p>
<p>Write a query to list a new column with the difference in temp of the days Mon and Tue,Tue and Wed and soon. (Don&#8217;t use cursors)</p>
<p><b>Ans.</b> </p>
<p></p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">Select</span> a.srno,b.srno,a.[<span style="color: #0000ff">day</span>],b.[<span style="color: #0000ff">day</span>],a.[temp],b.[temp], </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   2:</span>     (a.[temp]-b.[temp])<span style="color: #0000ff">AS</span> DiffinTemp <span style="color: #0000ff">from</span> diff a </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">inner</span> <span style="color: #0000ff">join</span> diff b <span style="color: #0000ff">on</span>  a.srno=b.srno-1</pre>
</p></div>
</div>
<p><b></b></p>
<p><b>32.</b> Consider the tables Team Table </p>
<p>TeamId&#160;&#160;&#160; TeamName </p>
<p>&#160;&#160;&#160;&#160; 1&#160;&#160;&#160;&#160;&#160;&#160; Team1 </p>
<p>&#160;&#160;&#160;&#160; 2&#160;&#160;&#160;&#160;&#160;&#160;&#160; Team2</p>
<p>PlayerTable&#160;&#160;&#160;&#160;&#160;&#160;&#160; </p>
<p>PlayId&#160;&#160; Name<br />
  <br />&#160;&#160;&#160;&#160;&#160; 1&#160;&#160; Name1 </p>
<p>&#160;&#160;&#160;&#160;&#160; 2&#160;&#160; Name2</p>
<p>TeamPlayer</p>
<p>TeamId&#160;&#160; PlayerId<br />
  <br />1&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 1 </p>
<p>2&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 1 </p>
<p>1&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 2 </p>
<p>Write a query to get Team name and Player name</p>
<p><b>Ans. </b></p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">Select</span> c.teamname,d.playername <span style="color: #0000ff">from</span> </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   2:</span>  (<span style="color: #0000ff">Select</span> a.teamid,a.teamname,b.playerid <span style="color: #0000ff">from</span> </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   3:</span>    team a <span style="color: #0000ff">inner</span> <span style="color: #0000ff">join</span> teamplayer b <span style="color: #0000ff">on</span> a.teamid=b.teamid) </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">as</span> c <span style="color: #0000ff">inner</span> <span style="color: #0000ff">join</span> player d <span style="color: #0000ff">on</span> </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   5:</span>      c.playerid=d.playerid</pre>
</p></div>
</div>
<p><b></b></p>
<p><strong>33.Different ways of getting count of rows from a table in queryanalyser *******</strong> </p>
<p><b>Ans.</b> Select distinct count(*)</p>
<p><b></b></p>
<p><strong>34. What is sp_addlinkedserver<br />
    <br />Ans.</strong> Creates a linked server, which allows access to distributed, heterogeneous queries against OLE DB data sources. After creating a linked server with sp_addlinkedserver, this server can then execute distributed queries. If the linked server is defined as SQL Server, remote stored procedures can be executed.</p>
<p>A linked server configuration allows SQL Server to execute commands against OLE DB data sources on different servers. </p>
<p>Linked servers offer these advantages:<br />
  <br />Remote server access. </p>
<p>The ability to issue distributed queries, updates, commands, and transactions on heterogeneous data sources across the enterprise. Freedom from the need to address diverse data </p>
<p>sources differently.</p>
<p><strong></strong></p>
<p><strong>35. What are the different types of Locks? </strong></p>
<p><b>Ans.</b> There are three main types of locks that SQL Server </p>
<p>6.5 uses: </p>
<p>Shared locks </p>
<p>Update locks </p>
<p>Exclusive locks </p>
<p><b>Shared locks </b>are used for operations that do not change or update data, such as a SELECT statement.</p>
<p><b>Update locks</b> are used when SQL Server intends to modify a page, and later promotes the update page lock to an exclusive page lock before actually making the changes.</p>
<p><b>Exclusive locks</b> are used for the data modification operations,&#160; such as UPDATE, INSERT, or DELETE. Shared locks are compatible with other Shared locks or Update locks.</p>
<p>Update locks are compatible with Shared locks only.</p>
<p>Exclusive locks are not compatible with other lock types.</p>
<p>Let me to describe it on the real example. There are four processes, which attempt to lock the same page of the same table. These processes start one after another, so Process1 is the first process, Process2 is the second process and so on. </p>
<p>Process1 : SELECT<br />
  <br />Process2 : SELECT </p>
<p>Process3 : UPDATE </p>
<p>Process4 : SELECT</p>
<p><i>Process1</i> sets the Shared lock on the page, because there are no another locks on this page. </p>
<p><i>Process2</i> sets the Shared lock on the page, because Shared locks are compatible with other Shared locks. </p>
<p><i>Process3 </i>wants to modify data and wants to set Exclusive lock, but it cannot make it before Process1 and Process2 will be finished, because Exclusive lock is not compatible with other lock types. So, Process3 sets Update lock. </p>
<p><i>Process4</i> cannot set Shared lock on the page before Process3 will be finished. So, there is no Lock starvation. Lock starvation occurs when read transactions can monopolize a table or page, forcing a write transaction to wait indefinitely. So, Process4 waits before Process3 will be finished.After Process1 and Process2 were finished, Process3 transfer Update lock into Exclusive lock to modify data. After Process3 was finished, Process4 sets the Shared lock on the page to select data.</p>
<p><b>Locking optimizer hints<br />
    <br /></b>There are six Locking optimizer hints in SQL Server 7.0:</p>
<p>NOLOCK<br />
  <br />HOLDLOCK </p>
<p>UPDLOCK </p>
<p>TABLOCK </p>
<p>PAGLOCK </p>
<p>TABLOCKX </p>
<p>READCOMMITTED </p>
<p>READUNCOMMITTED </p>
<p>REPEATABLEREAD </p>
<p>SERIALIZABLE </p>
<p>READPAST </p>
<p>ROWLOCK</p>
<p><b>NOLOCK </b>is also known as &quot;dirty reads&quot;. This option directs SQL Server not to issue shared locks and not to honor exclusive locks. So, if this option is specified, it is possible to read an uncommitted transaction. This results in higher concurrency and in lower consistency.</p>
<p><b>HOLDLOCK</b> directs SQL Server to hold a shared lock until </p>
<p>completion of the transaction in which HOLDLOCK is used. </p>
<p>You cannot use HOLDLOCK in a SELECT statement that includes the FOR BROWSE option. HOLDLOCK is equivalent to <b>SERIALIZABLE</b>. </p>
<p><b>UPDLOCK</b> instructs SQL Server to use update locks instead of shared locks while reading a table and holds them until the </p>
<p>end of the command or transaction.</p>
<p><b>TABLOCK</b> takes a shared lock on the table that is held until </p>
<p>the end of the command. If you also specify HOLDLOCK, the lock is held until the end of the transaction.</p>
<p><b>PAGELOCK</b> is used by default. Directs SQL Server to use shared page locks.</p>
<p><b>TABLOCKX</b> takes an exclusive lock on the table that is held </p>
<p>until the end of the command or transaction.</p>
<p><b>READCOMMITTED<br />
    <br /></b>Perform a scan with the same locking semantics as a transaction running at the READ COMMITTED isolation level. By default, SQL Server operates at this isolation level. </p>
<p><b>READUNCOMMITTED<br />
    <br /></b>Equivalent to NOLOCK. </p>
<p><b>REPEATABLEREAD<br />
    <br /></b>Perform a scan with the same locking semantics as a transaction running at the REPEATABLE READ isolation level.&#160; </p>
<p><b>SERIALIZABLE<br />
    <br /></b>Perform a scan with the same locking semantics as a transaction running at the SERIALIZABLE isolation level. Equivalent to HOLDLOCK. </p>
<p><b>READPAST<br />
    <br /></b>Skip locked rows. This option causes a transaction to skip over rows locked by other transactions that would ordinarily appear in the result set, rather than block the transaction waiting for the other transactions to release their locks on these rows. The READPAST lock hint applies only to transactions operating at READ COMMITTED isolation and will read only past row-level locks. Applies only to the SELECT statement.&#160; You can only specify the READPAST lock in the READ COMMITTED or REPEATABLE READ isolation levels.</p>
<p><b>ROWLOCK</b> </p>
<p>Use row-level locks rather than use the coarser-grained page and table-level locks. </p>
<p>You can specify one of these locking options in a SELECT statement.</p>
<p>This is the example:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">SELECT</span> au_fname <span style="color: #0000ff">FROM</span> pubs..authors (<span style="color: #0000ff">holdlock</span>)</pre>
</p></div>
</div>
<p><b>Lock Escalation<br />
    <br /></b>You can customize locking by setting Lock Escalation level. The Lock Escalation level determines, when SQL Server applies table locks instead of page locks, and it affects all users of SQL Server. So it&#8217;s escalation from the page to the table level locking. </p>
<p>There are three Lock Escalation options:<br />
  <br />LE threshold maximum </p>
<p>LE threshold minimum </p>
<p>LE threshold percent</p>
<p><b>LE threshold maximum</b> is the maximum number of page locks to hold before escalating to a table lock. The default value is 200.</p>
<p><b>LE threshold minimum</b> is the minimum number of page locks required before escalating to a table lock. The default value is 20.</p>
<p><b>LE threshold percent</b> is the percentage of page locks needed on a table before escalating to a table lock. The default value is 0, it means that a table lock will be occur only when the LE threshold maximum will be exceeded.</p>
<p>You can configure Lock Escalation levels by using the sp_configure system stored procedure. This is the example to set LE threshold maximum to 250:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">EXEC</span> sp_configure <span style="color: #006080">'LE threshold maximum'</span>, 250</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">RECONFIGURE</span> <span style="color: #0000ff">WITH</span> OVERRIDE</pre>
</p></div>
</div>
<p><b>Deadlocks<br />
    <br /></b>Deadlock occurs when two users have locks on separate objects and each user wants a lock on the other&#8217;s object. </p>
<p>For example, User1 has a lock on object &quot;A&quot; and wants a lock on object &quot;B&quot; and User2 has a lock on object &quot;B&quot; and wants a lock on object &quot;A&quot;. </p>
<p>You can decide which connection will be the candidate for deadlock victim by using SET DEADLOCK_PRIORITY. In other case, SQL Server selects the deadlock victim by choosing the process that completes the circular chain of locks. So, in a multiuser situation, your application should check the message 1205 to indicate that the transaction was rolled back, and if it&#8217;s so, restart the transaction.</p>
<p><b>Note.</b> To reduce the chance of a deadlock, you should minimize the size of transactions and transaction times.</p>
<p><strong></strong></p>
<p><strong>36. What&#8217;s a Primary Key?</strong> </p>
<p><b>Ans.</b>The column or combination of columns that uniquely identifies one row from any other row in a table. A primary key (PK) must be nonnull and must have a unique index. </p>
<p>A primary key is commonly used for joins with foreign keys (matching nonprimary keys) in other tables. Primary Key creates Cluster Index.</p>
<div class="simplesocialbuttons">
<div class="simplesocialbutton ssb-button-googleplus"><!-- Google Plus One--><g:plusone size="medium" count="true" href="http://www.ms.oyangudi.com/blog/interview-questions-sql-server/sql-server-interview-questions-1/"></g:plusone></div>
<div class="simplesocialbutton ssb-button-fblike"><!-- Facebook like--><div id="fb-root"></div><fb:like href="http://www.ms.oyangudi.com/blog/interview-questions-sql-server/sql-server-interview-questions-1/" send="false" layout="button_count" width="100" show_faces="false" action="like" font=""></fb:like></div>
<div class="simplesocialbutton ssb-buttom-twitter"><!-- Twitter--><a name="twitter_share" data-count="horizontal" href="http://twitter.com/share" data-text="SQL Server &ndash; Interview Questions #1" data-url="http://www.ms.oyangudi.com/blog/interview-questions-sql-server/sql-server-interview-questions-1/" class="twitter-share-button" rel="nofollow"></a></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.ms.oyangudi.com/blog/interview-questions-sql-server/sql-server-interview-questions-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

