Tags

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,


SQL SERVER Interview Questions & Answers – SET 1 (50 Questions & Answers)

Download – Pawan Kumar Khowal – SQL SERVER Interview Questions with Answers

PAGE – 5

PREV

Question 41. What is the best value for MAXDOP value?

Answer-

MAXDOP is Max degree of parallelism. This option controls the number of processors that are used for the execution of a query in a parallel plan.

One can use the max degree of parallelism option to limit the number of processors to use for parallel plan execution and to prevent run-away queries from impacting SQL Server performance by using all available CPUs.

The answer is: It depends. It depends on the hardware, the environment (OLTP vs. OLAP), and the load and so on.

The default value for MAXDOP is 0 (zero) and can be set or viewed using (sp_configure). A value of 0 means that SQL Server will use all processors if a query runs in parallel.

For details please visit –
http://www.mssqltips.com/sqlservertip/2650/what-maxdop-setting-should-be-used-for-sql-server/

Question 42. Which is better “Left Outer” Or “NOT EXIST”?

Answer-

In SQL Server, NOT EXISTS and NOT IN predicates are the best ways to search for missing values, as long as both columns in question are NOT NULL. They produce the safe efficient plans with some kind of an Anti-Join.

LEFT JOIN is less efficient, since it makes no attempt to skip the already matched values in the right table, returning all results and filtering them out instead.
http://explainextended.com/2009/09/15/not-in-vs-not-exists-vs-left-join-is-null-sql-server/

Question 43. How to find the statistics are outdated?

Answer –

You can check statistics by using below command –


DBCC SHOW_STATISTICS('TestRIDInxs','Ix_Index')

Table Name – TestRIDInxs
Index Name - 'Ix_Index

Output of the above query is given below-

Statistics

Question 44. How to find the query running on a given SPID?

Answer-

You can use below query to find query running on an active SPID. Number (52) is the SPID showing in the below example.


DECLARE @sqltext VARBINARY(128)
SELECT @sqltext = sql_handle
FROM sys.dm_exec_requests
WHERE session_id = 52
SELECT TEXT
FROM sys.dm_exec_sql_text(@sqltext);

Question 45. Is “TRUNCATE” DDL or DML command?

Answer- DDL

Question 46. I have written a recursive CTE, it’s been running infinitely and failed with an error. The requirement is that the recursion required for 1000 times. How could you be able to handle the situation?

Answer-

Well if you write a proper recursive CTE then it will not run indefinitely.

MAXRECURSION can be used to break the recursive CTE. MAXRECURSION can be used to prevent a poorly formed recursive CTE from entering into an infinite loop.

The following example intentionally creates an infinite loop and uses the MAXRECURSION hint to limit the number of recursion levels to ten.


;WITH CTE AS
(
	SELECT ParentID FROM Hierarchies WHERE ParentID = 1
	UNION ALL
	SELECT C.ParentID FROM Hierarchies H INNER JOIN CTE C
	ON C.ParentID = H.ParentID
)
SELECT * FROM CTE
OPTION (MAXRECURSION 10);
GO

You will also get this kind of message-

Msg 530, Level 16, State 1, Line 1
The statement terminated. The maximum recursion 10 has been exhausted before statement completion.

Question 47. Which is better a CTE or a subquery? Why?

Answer-

Both CTEs and Sub Queries have pretty much the same performance and function.

CTE’s have an advantage over using a subquery in that you can use recursion in a CTE.

The biggest advantage of using CTE is readability. CTEs can be reference multiple times in the same statement where as sub query cannot.

Question 48. Any alternative to triggers?

Answer –

There are some options available depending exclusively on the requirement. They are
Change Data Capture
Change Tracking
Using output clause (Write the data out to a specific Audit table )
Computed column

Question 49. Why resourceDB introduced?

Answer –

It is a read-only database that contains system objects that are included with SQL Server. SQL Server system objects, such as sys.objects, are physically persisted in the Resource database, but they logically appear in the sys schema of every database. The Resource database does not contain user data or user metadata.

Also Resource database makes upgrading to a new version of SQL Server an easier and faster procedure. In earlier versions of SQL Server, upgrading required dropping and creating system objects. Because the Resource database file contains all system objects, an upgrade is now accomplished simply by copying the single Resource database file to the local server.

For details please visit –

http://blogs.msdn.com/b/vsanil/archive/2012/11/02/resource-database-common-questions.aspx

Question 50. How to create linked server.

Answer –

Please use below T-SQL script or follow the screen shots


USE [master]
GO
EXEC MASTER.dbo.sp_addlinkedserver
    @server = N'SRVR002\ACCTG',
    @srvproduct=N'SQL Server' ;
GO

LinkedServer