Sql Server Interview Questions and Answers (1758) - Page 86

Identify the correct statements

NOTE: This is objective type question, Please click question title for correct answer.
Identify the correct statements abut SEQUENCE objects of Denali

NOTE: This is objective type question, Please click question title for correct answer.
Identify the correct statements

NOTE: This is objective type question, Please click question title for correct answer.
When we cannot use Next Value For Function of Sequence object?

It can never be use in conjunction with

Check constraints

Default objects
Computed columns
Views
User-defined functions
User-defined aggregates
Sub-queries
Common table expressions
Derived tables
Top
Over
Output
On
Where
Group By
Having
Order By
Compute
Compute By

Identify the query for obtaining the complete metadata information about the Sequence

NOTE: This is objective type question, Please click question title for correct answer.
Explain the benefits of using Linked Server.

Linked Servers allows to connect other database instances on the same server or on another machine or remote servers.It allows SQL Server to execute SQL scripts against OLE DB data sources on remote servers using OLE DB providers.The remote servers can be SQL Server, Oracle,PostgreSQL etc. which means databases that support OLE DB can be used for linking servers.

Uses and benefits
In many instances, the data represents 2 different logical sets of data; hence, you have 2 databases. The reason for using linked servers over putting both DBs on the same SQL instance would be to spread the workload out over 2 physical servers for scalability and performance. In another word, it brings the Distributed Queries in place.
Why sp_executesql is better than EXEC for writing dynamic queries?

- sp_executesql allows for statements to be parameterized. Moreover, the parameters are strongly typed. So it mitigates the risk of SQL injection which is very common in case of EXEC since the query prepared by this is an adhoc one.

- For dynamic SQL too, the plan gets re-used while using sp_executesql. The execution plan that gets generated on the first execution of the SQL query is re-used in the subsequent executions. Henceforth, it is faster as compared to EXEC method. In case of EXEC, since adhoc statements are generated on every query execution, SQL Server needs to recompile for every subsequent execution.
Why Covering Index is better than creating several non-clustered index?

Instead of creating several non-clustered index, try using Covering Index because it can satisfy all requested columns in a query without performing a further lookup into the clustered index.If all the columns requested in the select list of query, are available in the index, then the query engine doesn't have to lookup the table again which can significantly increase the performance of the query. Since all the requested columns are available with in the index, the index is covering the query. It enhances the query performance by avoiding unnecessary lookups.
Consider the below queries Query1 --------- Select Col1,Col2,Col3, Count(Col4), Sum(Col5) From Table Group By Col1,Col2,Col3 Query2 ---------- Select Col1,Col2,Col3, COUNT(Col4) OVER (PARTITION BY <Your partition by Columns> ORDER BY (Select 1)), SUM(Col5) OVER (PARTITION BY <Your partition by Columns> ORDER BY (Select 1)) From Table Which is better and why?

The second on e is better.The disadvantage of using the first approach is that we need to group by all other non-aggregate columns which some times is not desirable and yields wrong results/not desired ones. The second approach is better as we have the choice of grouping the columns by our own choice and at a case by case basis.
Why it is not recommended to name a user defined store procedures with "sp_"?

They are system defined and mostly resides under the master db. So if we write a user defined stored procedure by the name "sp_" the query engine will first search the sp indside the master db and if not found then it will search in the current session db. This brings unnecessary round trip. Better to use some other naming convention as "usp_".
Identify the true statements

NOTE: This is objective type question, Please click question title for correct answer.
Which stored procedure is used to iterate through all databases?

NOTE: This is objective type question, Please click question title for correct answer.
What is the maximum number of case level density in SQL Server?

NOTE: This is objective type question, Please click question title for correct answer.
What will be the output? ;with cte as(select rn=1 union all select rn=rn+1 from cte where rn<10) select * from cte

NOTE: This is objective type question, Please click question title for correct answer.
Explain with example the importance of Clustered and Non-Clustered Index

A clustered index alters the way that the rows are stored. When we create a clustered index on a column (or a number of columns), SQL server sorts the table’s rows by that column(s). It is like a dictionary, where all words are sorted in alphabetical order in the entire book.

Way to create a clustered index

USE [tempdb]


GO

-- Create a new table with three columns.

CREATE TABLE [dbo].[TestTable]

(TestCol1 int NOT NULL,

TestCol2 nchar(10) NULL,

TestCol3 nvarchar(50) NULL);

GO

-- Create a clustered index called IX_TestTable_TestCol1

-- on the dbo.TestTable table using the TestCol1 column.

CREATE CLUSTERED INDEX IX_TestTable_TestCol1

ON dbo.TestTable (TestCol1);

GO



--Drop the table

DROP TABLE [dbo].[TestTable]

GO


A non-clustered index , on the other hand, does not alter the way the rows are stored in the table. It creates a completely different object within the table that contains the column(s) selected for indexing and a pointer back to the table’s rows containing the data. It is like an index in the last pages of a book, where keywords are sorted and contain the page number to the material of the book for faster reference.

Way to create a non-clustered index

USE [tempdb]


GO

-- Create a new table with three columns.

CREATE TABLE [dbo].[TestTable]

(TestCol1 int NOT NULL,

TestCol2 nchar(10) NULL,

TestCol3 nvarchar(50) NULL);

GO

-- Create a nonclustered index called IX_TestTable_TestCol1

-- on the [dbo].[TestTable] table using the TestCol1 column.

CREATE NONCLUSTERED INDEX IX_TestTable_TestCol1 ON dbo.TestTable (TestCol1);

GO



--Drop the table

DROP TABLE [dbo].[TestTable]

GO

What will be the output of Select Patindex('%[m|n|o|p]%',REVERSE(REVERSE(REVERSE(REVERSE('56728745MNO')))))?

NOTE: This is objective type question, Please click question title for correct answer.
What is a view? What is the purpose of it?

A view is a logical snapshot based on a table or another view. It is mainly used for -

a) Restricting access to data
b) Ensuring data in dependency
c) Making complex queries simple
d) Data integrity and security
e) Faster performance by materializing
f) Providing different views of same data
How to delete duplicate rows from a table?

Suppose we have a table tblPerson with the below records

PersonName PersonAge 

----------------- ------------------
Person1 30
Person1 30
Person2 50
Person3 40
Person2 50

We need the output as

PersonName PersonAge 

----------------- ------------------
Person1 30
Person2 50
Person3 40


We can achieve it as

;With Cte As(
Select Rn=Row_Number() Over(Partition By PersonName Order by PersonName) ,p.*
From tblPerson)
Delete from Cte
Where Rn >1

The Row_Number() function will make a ranking as

Rn PersonName            PersonAge

---- ------------------ -----------------
1 Person1 30
2 Person1 30
1 Person2 50
1 Person3 40
2 Person2 50


The Delete statement will delete those rows where the Rn > 1.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories