Vous êtes sur la page 1sur 12

THE EXPERTS VOICE IN SQL SERVER

LEARN MORE ABOUT SQL INTERVIEW


QUESTIONS-II
Gives you the knowledge about SQL Interview Questions




A free SQL Interview Questions-II eBook by,

Venkatesan Prabhu Jayakantham,
Microsoft Most Valuable professional




ABOUT THE AUTHOR
WWW.KAASHIVINFOTECH.COM
[SQL INTERVIEW QUESTIUONS-PART II]


2
Venkatesan Prabhu Jayakantham (venkat) has more than 8 years experience in
the Microsoft Technologies such as VB.Net, ASP.Net, C#.net, SSIS, SSAS, ADO.Net, etc.
He is the Managing Director of KAASHIV INFOTECH ( http://www.kaashivinfotech.com/),
a software company in Chennai. Before that, he worked in HCL Technologies (India and
Australia) for six years as Project Lead.
As a service motive, Venkat contributed more than 700 articles which is read by the
developers in 170 countries (400 developers per day)
(http://venkattechnicalblog.blogspot.com/ ). Aligned with KaaShiv InfoTechs mission,
he met more than 20,000 young minds and spreaded Microsoft Technologies / Career
guidance programs.
Venkat won many awards in his career, which includes Prestigious Microsoft
award MVP (Most Valuable Professional) award for the years 2008,2009,2010,2011
and won many awards.
List of other awards in his career,
Microsoft certified Smart .Net Candidate in 2004
Most valuable member for dotnetspider site in 2007
HCL SQL Subject Matter expert (SME - SQL Server) for the year (2008,2009)
HCL Special contribution award winner on Dotnet skills for year(2008)
HCL SQL Knowledge Champion for the year 2009
MindCracker MVP on SQLServer 2010 for the year 2010,2011
INETA champion - Gold Member 2010 for the year 2010
HCL Service Contribution Award for the year 2010
Leading Lights "Rising Star" award from Common Wealth Bank, Australia for the year
2010
Technical certifications
Cisco certified Network Associate (CCNA) 2004
Microsoft Certified Application Developer (MCAD) 2005


ACKNOWLEDGEMENT
WWW.KAASHIVINFOTECH.COM
[SQL INTERVIEW QUESTIUONS-PART II]


3
I would like to thank to my family members for their support and
encouragement. Without them it would impossible for me to publish this e-book.
I would also like to thank my KaaShiv InfoTech team for their support to publish
this e-book.









DISCLAIMER
All rights reserved. No part of this book may be copied, adapted, abridged
or translated, stored in any retrieval system, computer system, photographic or other
system or transmitted in any form or by any means without the prior written permission
of the copyright holders. Any breach will entail legal action and permission without
further notice.

WWW.KAASHIVINFOTECH.COM
[SQL INTERVIEW QUESTIUONS-PART II]


4
1. Deleting backup and restore history :

Usually, backup and restore history will be available in the MSDB database(Microsoft Database).
Its advisable to cleanup the database after archiving the databases.
Scenario: Considering, am having daily backups and after two years the database will be archived. In
that case, the backup and restore information which is happened before two years need not be
maintained and we can go for cleanup. In that case, we are using the below command to cleanup the
database. You need to specify the exact dates. So that, data before that dates will be cleaned up.
USE msdb
EXEC sp_delete_backuphistory '1/31/2011'
2. Avoiding prefix 'sp_' in the name of user stored procedure?

By nature, if the stored procedure is created with the prefix sp_. SQL Server will start search for the
stored procedure in the master database. After wards, it will search for the local database which in turn
a major problem. To avoid this,
We need to access the stored procedure using Fully Qualified Name,
DatabaseName.SchemaName.ProcedureName
3. Find the execution time of a stored procedure?

The best way is to use SQL profiler. Just choose the required events.Else if you dont have the permission
to access SQL Profiler or want to determine that programatically, then try:

4. Programmatically find out when SQL Server service is started?

1. First Option is to check when the tempdb database is created. This is the exact time at which
the services were restarted.
WWW.KAASHIVINFOTECH.COM
[SQL INTERVIEW QUESTIUONS-PART II]


5


2. Second Option is the information about the database startup will be available while executing
the command,
exec xp_readerrorlog

5. Join tables from different databases

This can be achieved by joining the tables or indicating the tables using Fully Qualified names.



WWW.KAASHIVINFOTECH.COM
[SQL INTERVIEW QUESTIUONS-PART II]


6
6. Difference between Stored Procedure (SP) and User Defined Function (UDF)?

User Defined Function can be executed using the SELECT clause whereas Stored Procedure
cannot be Executed..
User Defined Function does not return any output parameters while Stored Procedure will
return output parameters.
User Defined Function cannot make permanent changes to server environments while SPs can
change some of the server environment.
If there is an error in UDF its stops executing. But in SPs it just ignores the error and moves to
the next statement.

7. SET or SELECT -- is there any performance difference?

It doesnt major performance difference between the Set and Select statement. The only thing is the
usability nature of the commands.
SET @variable = (SELECT datacolumn FROM tablename)
The above statement should return a single value which will be assigned to the variable. Here, if
the results of the subquery expression is empty then @variable has a value of NULL but if the results of
the subquery are more than one row then you get a error.
And thus it makes sense while,
SELECT @variable = columnvalue FROM dbo.tablename
returns a value from a number of rows and we are not sure whats the criteria in selecting that
row.
One major difference in assigning using SET and SELECT is:
SET @variable1 = 'somevalue', @variable2 = 'someothervalue'
This is NOT possible but the same is possible with SELECT as in:
SELECT @variable1 = 'somevalue', @variable2 = 'someothervalue'
8. How to use dynamic sql for a nvarchar of more than 4000 characters?

Usually, its a typical problem we will face while executing the Dynamic SQL the length of the string
is really huge when compared to the max length of the string. To overcome this major issue. Here is a small
alternative. We can have part of the execution string in one variable and remaining part in the other string.



WWW.KAASHIVINFOTECH.COM
[SQL INTERVIEW QUESTIUONS-PART II]


7
A small example

find columns having index?
sp_helpindex tblUserDetails

sp_help tblUserDetails

EXEC sp_msforeachtable @command1= "PRINT '?' EXEC
sp_helpindex @objname ='?'"


www.jobsanddumps.com
Get all latest job Updates.
All Company Aptitude/Technical
Questions with Solutions
Latest Dumps for Certifications.
www.catch2learn.com
Get Educational videos on all
technologies, all degrees and all
Interviews for free of cost.

WWW.KAASHIVINFOTECH.COM
[SQL INTERVIEW QUESTIUONS-PART II]


8



9. Get the Top Data from SQL Server


WWW.KAASHIVINFOTECH.COM
[SQL INTERVIEW QUESTIUONS-PART II]


9
www.jobsanddumps.com
Get all latest job Updates.
All Company Aptitude/Technical
Questions with Solutions
Latest Dumps for Certifications.
www.catch2learn.com
Get Educational videos on all
technologies, all degrees and all
Interviews for free of cost.


10. Get the Top Data with Ties from SQL Server (If there are same data.Ties in data will happen.
If we need all the data.

In this scenario you need to use With Ties option

In the second query, Ive given Top 3 data. But the third and fourth data is having a tie up which inturn
enables the query engine to fetch both the data which are in Ties.
WITH TIES OPTION MAY CAUSE MORE THAN THE SPECIFIED NUMBER OF RECORDS TO BE DISPLAYED
BECAUSE IT DISPLAYS ALL OF THE RECORDS MATCHING THE LAST RECORD'S ORDER BY CLAUSE

11. Variable in Top Clause (Dynamic value for the top clause):

Suppose, If we want to pass the arguments for getting top number
of records we can achieve it by declaring the variable and using
the variable as below.




WWW.KAASHIVINFOTECH.COM
[SQL INTERVIEW QUESTIUONS-PART II]


10

12. REBUILDING VS REORGANIZE THE INDEX
o When an index is rebuilt, it is dropped and a new one is created.
o In the process fragmentation is removed and disk space is reclaimed.

ALTER INDEX PK_CustId ON Sales.Customer REBUILD
REORGANIZE THE INDEX
o Minor Level modification, so that rearranging of the tree node is done.
o Small Fragmentation is removed, the index remains.

ALTER INDEX PK_CustId ON Sales.Customer REORGANIZE
13. Query in one of the forum:
using Below sample query(2 times same query with one condition as extra) to get the 2 type of result
set.
IF @Status = 'A'
Select * From TableName where ID = @ID and Value > 200
Else
Select * From TableName where ID = @ID
How can i use Case statement to avoid to create the same select statement 2 times.



WWW.KAASHIVINFOTECH.COM
[SQL INTERVIEW QUESTIUONS-PART II]


11
Here is my answer to achieve the same.




14. How can we force SQL Server to use a specific index when executing a query?




WWW.KAASHIVINFOTECH.COM
[SQL INTERVIEW QUESTIUONS-PART II]


12
www.jobsanddumps.com
Get all latest job Updates.
All Company Aptitude/Technical
Questions with Solutions
Latest Dumps for Certifications.
www.catch2learn.com
Get Educational videos on all
technologies, all degrees and all
Interviews for free of cost.


15. Query to Create comma separated Output.

Vous aimerez peut-être aussi