SQL Server Developer Interview Question
SQL Server Developer Interview Question
Question 2. What Is The Meaning Of Lock Escalation And Why/how To Stop This?
Answer :
understand that whole table would be locked for the processing thenn this is better to use
TABLOCK hint and get complete table blocked. This is a nice way to avoid the wastage of sql
server DB engine processing for lock escalation. Somewhere you may also need to use
TABLOCKX when you want an exclusive lock on the table in the query.
Question 4. What Changes In The Front End Code Is Needed If Mirroring Is Implemented For
The High Availability?
Answer :
You need to add only FAILOVER PARTNER information in your front end code. “Data
Source=ServerA;Failover Partner=ServerB;Initial Catalog=AdventureWorks;Integrated
Security=True;”.
Question 5. Where Does The Copy Job Runs In The Log Shipping Primary Or Secondary?
Answer :
Secondary server. This question is basically asked to find out whether you have a hands on work
on log shipping or not.
Question 6. What Are The Ways To Find What Code Is Running For Any Spid?
Answer :
Well there are many ways to do this.
find the spid which you want to analyze. An spid is assigned as soon as a client connection is
established with the SQL server. To find the spid you can run any of the following command:
SP_WHO2 ‘ACTIVE’ — This will give you only active spids.
SELECT * FROM sys.dm_exec_requests
Get the spid from above two queries and use any of the following query to get what is
happening behind that spid.
dbcc inputbuffer()
sql2005 and sql2008 – SELECT * FROM sys.dm_exec_sql_text()
sql2005 and sql2008 – SELECT * FROM fn_get_sql()
Question 7. When You Get Following Error? Error 3154: The Backup Set Holds A Backup Of A
Database Other Than The Existing Database?
Answer :
The error comes when you are trying to restore the DB which already exists. Use WITH
REPLACE option to restore the DB with a different name
Question 9. How To View The Error Log For Any Specific Instance?
Answer :
There are many ways but I prefer following method. Take a scenario when you want to find the
error log when the DB was put in a single user mode.
CREATE TABLE #Errorlog (Logdate Datetime, Processinfo
VARCHAR(20),Text VARCHAR(2000))
INSERT INTO #Errorlog
EXEC xp_readerrorlog
SELECT * FROM #Errorlog
WHERE Text Like ‘%SINGLE%USER%’
Question 10. According To You What Goes Into Making The Best Database Administrator?
Answer :
The primary job of DBAs is to secure the data. They should be able to keep it safe as well as
reproduce it efficiently, whenever required. So as per my view, a Database Administrator who can
fulfill the requirements of Securing Data and Retrieving Data is the best DBA.
When I hire a DBA I always ask them questions about backup strategies and efficient restoring
methodologies.
Question 11. I Have All The Primary Data Files, Secondary Data Files As Well As Logs. Now,
Tell Me Can I Still Restore The Database Without Having A Full Backup?
Answer :
You cannot restore the database without having a full database backup. However, if you have the
copy of all the data files (.mdf and .ndf) and logs (.ldf) when database was in working condition
(or your desired state) it is possible to attach the database using sp_attach_db.
Question 12. As Per Your Opinion What Are The Five Top Responsibilities Of A Dba?
Answer :
I rate the following five tasks as the key responsibilities of a DBA.
Securing database from physical and logical integrity damage.
Restoring database from backup as a part of disaster management plan.
Optimizing queries performance by appropriate indexing and optimizing joins, where conditions,
select clause etc.
Designing new schema, support legacy schema, and legacy database systems.
Helping developers improve their SQL-related code writing skill.
Question 13. One Of The Developers In My Company Moved One Of The Columns From One
Table To Some Other Table In The Same Database. How Can I Find The Name Of The New
Table Where The Column Has Been Moved?
Answer :
This question can be answered by querying system views.
For SQL Server 2005 run the following code:
SELECT OBJECT_NAME(OBJECT_ID) TableName
FROM sys.columns
WHERE name = 'YourColumnName'
The previous query will return all the tables that use the column name specified in the WHERE
condition. This is a very small but a very handy script.
Question 14. What Is The Difference Between Sql Server 2000 Object Owner And Sql Server
2005 Schema?
Answer :
Let us first see the fully qualified query name to access a table for SQL Server 2000 and SQL
Server 2005.
SQL Server 2000: [DataBaseServer].[DataBaseName].[ObjectOwner].[Table]
SQL Server 2005: [DataBaseServer].[DataBaseName].[Schema].[Table]
SQL Server 2008: [DataBaseServer].[DataBaseName].[Schema].[Table]
In SQL Server 2000, prior to dropping the user who owns database objects, all the objects
belonging to that user either need to be dropped or their owner has to be changed. Every time a
user is dropped or modified, system admin has to undergo this inconvenient process.
In SQL Server 2005 and the later versions, instead of accessing a database through database
owner, it can be accessed through a schema. Users are assigned to schemas, and by using this
schema a user can access database objects. Multiple users can be assigned to a single schema,
and they all can automatically receive the same permissions and credentials as the schema to
which they are assigned. Because of the same reason in SQL Server 2005 and the later versions
– when a user is dropped from database – there is no negative effect on the database itself.
Question 15. What Is Bi? I Have Heard This Term Before But I Have No Idea About It?
Answer :
BI stands for Business Intelligence. Microsoft started to promote the acronym BI since the launch
of SQL Server 2005. However, it has been in use for a long time. The basic idea of BI is quite
similar to Data Warehousing. Business intelligence is a method for storing and presenting accurate
and timely key enterprise data to CXO, IT Managers, Business Consultants, and distributed teams
of a company, to provide them with up-to-date information to drive intelligent decisions for
business success, which ultimately leads to enhanced revenue, reduced risk, decreased cost, and
better operational control for business agility and competitiveness. An effective BI empowers end
users to use data to understand the cause that led to a particular business result, to decide on the
course of action based on past data, and to accurately forecast future results.
Question 16. What Is Your Recommendation For A Query Running Very Slow?
Answer :
Well, your question is very difficult to answer without looking at the code, application and
physical server. In such situations, there are a few things that must be paid attention to right away.
Restart Server
Upgrade Hardware
Check Indexes on Tables and Create Indexes if necessary Make sure SQL Server has priority over
other operating system processes in SQL Server settings.
Update statistics on the database tables.
Question 17. What Should Be The Fill Factor For Indexes Created On Tables?
Answer :
Fill factor specifies a percentage that indicates how full the Database Engine should make the leaf
level of each index page during index creation or alteration. Fill factor must be an integer value
from 1 to 100. The default is 0. I prefer to keep my servers default fill factor as 90.
Question 18. Which Feature In Sql Server 2008 Has Surprised You? You Can Name Just One.
Answer :
Plan Freezing is a new feature I never thought of. I find it very interesting! It is included in SQL
Server 2008 CTP5. SQL Server 2008 enables greater query performance stability and
predictability by providing new functionality to lock down query plans. This empowers
organizations to promote stable query plans across hardware server replacements, server upgrades,
and production deployments.
Question 19. How Do You Test Your Database?
Answer :
This is a very generic question. I would like to describe my generic database testing method as
well as stored procedure testing methods.
Testing Databases:
Table Column data type and data value validation.
Index implementation and performance improvement.
Constraints and Rules should be validated for data integrity.
Application field length and type should match the corresponding database field.
Database objects like stored procedures, triggers, functions should be tested using different
kinds of input values and checking the expected output variables.
Testing Stored Procedures:
Understand the requirements in terms of Business Logic.
Check if the code follows all the coding standards.
Compare the fields’ requirements of application to the fields retrieved by a stored procedure.
They must match.
Repeatedly run the stored procedures several times with different input parameters and then
compare the output with the expected results.
Pass invalid input parameters and see if a stored procedure has good error handling.
Question 20. What Are System Databases Into Sql Server (2005/2008)
Answer :
TEMPDB, MSDEB, MASTER, MSDB, mssqlsystemresource.
Question 21. What Stored By The Tempdb ?
Answer :Row versions, cursor, temp objects.
Question 22. What Stored By The Model?
Answer :
Templates of new database objects, like tables and column.
Question 23. What Stored By The Master?
Answer :
Server’s configurations and logins.
Question 45. What Are The Uses Of Standby Mode In Log Shipping?
Answer :
If the Restore Jobs is scheduled / delayed for longer intervals this option can be used. As uses
needs to be disconnected every time Restore Job runs(to ensure timely shipping of Transaction
Logs) its recommended not to use this option for regular, less interval Log Shipping
configurations. Using this option gives us advantage of Load Balancing for READONLY
connections and Reporting purposes at the cost of timely disconnections to help success of
Restore Jobs.
Question 48. Where Can You Find The Error Log Information?
Answer :
We can make use of SQL Profiler , SQL Server Log or use xp_readerrorlog extended Stored
Procedure to retrieve the error log information.
Question 49. What Is The Status Of Services On Passive Node For Failover Cluster In Sql
Server?
Answer :
SQL services will be in manual and stopped. Cluster service will be in automatic and started
mode on both the nodes.
Question 50. Can You Move The Resources After Pausing The Node?
Answer :
Yes resources can be moved after pausing the node. But we can't move them back till the node
is paused.
Question 51. What Happens If We Start The Full Text Service On Passive Node.
Answer :
This can be started on both the nodes as this doesn't have any dependency on SQL service or
any resource which is possessed by active node.
Question 53. What Are The Basic Functions For Master, Msdb, Model, Tempdb And Resource
System Databases?
Answer :
The master database holds information for all databases located on the SQL Server instance.
As SQL Server cannot start without a functioning master database, we must administer this
database with care and monitor Startup Parameters in Configuration Manager.
The msdb database stores information regarding database backups, SQL Agent information,
DTS packages, SQL Server jobs, and some replication information such as for log shipping.
The tempdb holds temporary objects such as global and local temporary tables and stored
procedures.
The model is essentially a template database used in the creation of any new user database
created in the instance.
The resource Database is a read-only database that contains all the 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.
Question 55. Where Sql Server User Names And Passwords Are Stored In Sql Server?
Answer :
They get stored in System Catalog Views sys.server_principals and sys.sql_logins.
Question 61. What Is Blocking And How Would You Troubleshoot It?
Answer :
Blocking occurs when a process has acquired lock on a set of rows, and another process is
trying to acquire a lock on the same set of rows. In such a case, the other process has to wait
until the first process finishes its job and releases the lock on the above said rows.
Use sp_lock procedure to see type of locks acquired by various sessions on the server to find
the cause of blocking.
Problem is hinted by the WAIT status is a lot of rows that are returned as an output of
sp_lock stored procedure execution.
Use sp_who and sp_who2 to return more columns to get more information around the
blocking.
Use DBCC INPUTBUFFER (spid).This will show the last 128 characters of the last T-SQL
statement executed from connection referred through spid. This way one can identify the
stored procedure or application module that caused blocking.
To resolve blocking, you can disconnect the connection causing the blocking using KILL
command. If this does not solve the problem permanently, then rewrite the stored
procedure/module causing the block more efficiently.
Question 62. Explain The Different Types Of Backups Available In Sql Server?
Answer :
Types of backups available in SQL Server:
Complete: This creates a complete stand alone image of the database. This backup is self
dependent and can be restored to either the same or a new database on same or other server.
Differential: This backs up only the modified contents since the last backup. They do not
provide much flexibility.
Transaction log: This backs up all transaction logs since the previous transaction log backup
or the complete transaction log backup if there has not been one in past.
Files and Filegroups backup: This option is suitable when time constraints are high and one
cannot afford to perform a complete database backup. It also needs transaction logs backup
to take place to make it worth choosing this option. After restoring file backup, apply
transaction logs to roll the file contents forward to make it consistent with the database..