SQL Server Interview Questions for DBA/Developer

• What is a DDL, DML, DCL, TCL and DSPL concept in RDBMS world?
The Data Definition Language (DDL) includes,

CREATE TABLE – creates new database table
ALTER TABLE – alters or changes the database table
DROP TABLE – deletes the database table
CREATE INDEX – creates an index or used as a search key
DROP INDEX – deletes an index

The Data Manipulation Language (DML) includes,

SELECT – extracts data from the database
UPDATE – updates data in the database
DELETE – deletes data from the database
INSERT INTO – inserts new data into the database

The Data Control Language (DCL) includes,

GRANT – gives access privileges to users for database
REVOKE – withdraws access privileges to users for database

The Transaction Control (TCL) includes,

COMMIT – saves the work done
ROLLBACK – restore the database to original since the last COMMIT

DSPL – Database Stored Procedure Language came to relational databases relatively late in the game – and thus the languages used for triggers, event handlers, and stored procedures are completely different among the database vendors. Oracle’s PL/SQL is quite different even in statement syntax from SQL Server’s Transact SQL which in turn differs again from DB2’s Stored Procedure language. And of course given the underlying differences in DDL, DML, and DCL it is inevitable that the stored procedure languages would vary in content as well as syntax.

Define candidate key, alternate key, composite key.


How to Reset the Identity Values?

You can set the identity values using 1. DBCC CHECKIND(TABLENAME,RESEED,0) 2. Truncate table.

What are “GRANT”, “REVOKE’ and “DENY’ statements?

Creates an entry in the security system that allows a user in the current database to work with data in the current database or execute specific Transact-SQL statements.
Statement permissions:

GRANT { ALL | statement [ ,…n ] }
TO security_account [ ,…n ]

Object permissions:

{ ALL [ PRIVILEGES ] | permission [ ,…n ] }
[ ( column [ ,…n ] ) ] ON { table | view }
| ON { table | view } [ ( column [ ,…n ] ) ]
| ON { stored_procedure | extended_procedure }
| ON { user_defined_function }
TO security_account [ ,…n ]
[ AS { group | role } ]

Removes a previously granted or denied permission from a user in the current database.

Statement permissions:

REVOKE { ALL | statement [ ,…n ] }
FROM security_account [ ,…n ]

Object permissions:

| ON { stored_procedure | extended_procedure }
| ON { user_defined_function }
{ TO | FROM }
security_account [ ,…n ]
[ AS { group | role } ]

Creates an entry in the security system that denies a permission from a security account in the current database and prevents the security account from inheriting the permission through its group or role memberships.

Statement permissions:

DENY { ALL | statement [ ,…n ] }
TO security_account [ ,…n ]

Object permissions:

| ON { stored_procedure | extended_procedure }
| ON { user_defined_function }
TO security_account [ ,…n ]

• What is Stored Procedure?
A stored procedure is a collection of T-SQL statements. The stored procedure stored in the system tables of the User Database in SQL Server. The system tables used in stored procedure is sysObjects, sysDepends and sysComments.

Stored procedures accept input parameters so that a single procedure can be used over the network by several clients using different input data and stored procedure also returns the output parameter. Stored procedures reduce network traffic and improve performance. Stored procedures can be used to help ensure the integrity of the database.
e.g. sp_help,sp_helpdb (Alt + F1), sp_renamedb, sp_depends etc.

Stored Procedure can takes 1024 input and returns the 1024 output parameters.

• What is Trigger?
Trigger are used to enforce the business rules in the RDBMS. A trigger is a SQL procedure that initiates an action when an event (INSERT, DELETE or UPDATE) occurs. Triggers are stored in and managed by the RDBMS. Triggers are used to maintain the referential integrity of data by changing the data in a systematic fashion. A trigger cannot be called or executed. The RDBMS automatically fires the trigger as a result of a data modification to the associated table.
Triggers can be viewed as similar to stored procedures in that both consist of procedural logic that is stored at the database level. Stored procedures, however, are not event-drive and are not attached to a specific table as triggers are. Stored procedures are explicitly executed by invoking a CALL to the procedure while triggers are implicitly executed. In addition, triggers can also execute stored procedures.

There are two types of trigger in SQL Server 1. After Trigger 2. Instead of Trigger

Nested Trigger: Like the stored procedure trigger can also be nested upto 32 levels. A trigger can also contain INSERT, UPDATE and DELETE logic within itself, so when the trigger is fired because of data modification it can also cause another data modification, thereby firing another trigger. A trigger that contains data modification logic within itself is called a nested trigger. For the nested trigger user has to define the execution order of the trigger.

The trigger will create two magic tables inserted and deleted which contains the structure of table on which it excutes.

• What is View?
A view is one type of virtual tables which only stores the SELECT query without data. User can perform the Insert/Update/Delete operation on the view. View can give us the better security.

User can define the index on views. The Instead of trigger can fire on the view.

• What is Index?
Indexes in SQL Server are similar to the indexes in books. They help SQL Server retrieve the data quicker.

Indexes are of two types. Clustered indexes and non-clustered indexes. When you craete 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’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.

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 t ime, 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.
• What is the difference between clustered and a non-clustered index?


A nonclustered index is a special type of index in which the logical order of the index does not match the physical stored order of the rows on disk. The leaf node of a nonclustered index does not consist of the data pages. Instead, the leaf nodes contain index rows. SQL Server can create 249 Non-clustered index per table.

• What is cursors?
Cursors allow row-by-row prcessing of the resultsets. The system table used in the cursor operation is sysCursors. The cursor can be local or global.

Types of cursors: Static, Dynamic, Forward-only, Keyset-driven. See books online for more information.

Disadvantages of cursors: Each time you fetch a row from the cursor, it results in a network round trip, where as a normal SELECT query makes only one round trip, however large the result set is. Cursors are also costly because they require more resources and temporary storage (results in more IO operations). Further, there are restrictions on the SELECT statements that can be used with some types of cursors.

What is the use of DBCC commands?
E.g. DBCC CHECKDB – Ensures that tables in the db and the indexes are correctly linked.
DBCC CHECKALLOC – To check that all pages in a db are correctly allocated.
DBCC CHECKFILEGROUP – Checks all tables file group for any damage.

• What is a Linked Server?
Think of a Linked Server as an alias on your local SQL server that points to an external data source. This external data source can be Access, Oracle, Excel or almost any other data system that can be accessed by OLE or ODBC–including other MS SQL servers. An MS SQL linked server is similar to the MS Access feature of creating a “Link Table.”


• What’s the difference between a primary key and a unique key?

• What is a NOLOCK?

• What is difference between DELETE & TRUNCATE commands?
Delete: Delete is logged operation. Trigger can fire on the delete operation. Delete can use the where clause. Can be used in foreign key relationship tables and remove the data from the child table if the ON DELETE CASCADE is specified.
DELETE Can be Rolled back.
DELETE is DML Command.
DELETE does not reset identity of the table.

TRUNCATE removes all rows from a table, but the table structure and its columns, constraints, indexes and so on remain. Truncate resets the identity value.

We cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint.
TRUNCATE can be Rolled back if it is used between Begin/Commit/Rollback transactions.
TRUNCATE is DDL Command.
TRUNCATE Resets identity of the table.

Difference between Function and Stored Procedure?
UDF can be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section whereas Stored procedures cannot be.
UDFs can return the table variable.


We can not write the configuration statements in UDFs. UDs can return only one value whereas SPs can return 1024 output parameters.

When is the use of UPDATE_STATISTICS command?

• What types of Joins are possible with Sql Server?
Joins are used in queries to explain how different tables are related. Joins also let us select data from a table depending upon data from another table.

• What is the difference between a HAVING CLAUSE and a WHERE CLAUSE?
Specifies a search condition for a group or an aggregate. HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause. Having Clause is basically used only with the GROUP BY function in a query. WHERE Clause is applied to each row before they are part of the GROUP BY function in a query. It is the good practice to use WHERE clause with the Group By for the better performance result.

• What is SQL Profiler?

It is a tool which help us to profiling the activities at the database level. It is the good practice to use the profiler from the different machine rather than the production machine.

SQL Profiler is a graphical tool that allows system administrators to monitor events in an instance of Microsoft SQL Server. We can capture and save data about each event to a file or SQL Server table to analyze later. For example, you can monitor a production environment to see which stored procedures are hampering performance by executing too slowly.

• What is User Defined Functions?

• Which TCP/IP port does SQL Server run on? How can it be changed?

SQL Server runs on port 1433. It can be changed from the Network Utility TCP/IP properties –> Port number.both on client and the server.

• What are the authentication modes in SQL Server? How can it be changed?
Windows mode and mixed mode (SQL & Windows).
• Where are SQL server users names and passwords are stored in sql server?
They get stored in master db in the sysxlogins table.

SELECT SERVERPROPERTY(‘productversion’), SERVERPROPERTY (‘productlevel’), SERVERPROPERTY(‘edition’), SELECT @@version

• What is SQL server agent?

It is one of the services provided by SQL Server. Used for the scheduling purpose. To start the this service from the dos prompt you can write net start sqlserveragent
• What is @@ERROR?

• What is Raiseerror?

• What is log shipping?

• What is the difference between a local and a global variable?
User can create local temporary table using Single(#) and global temporary table using (##)A local temporary table exists only for the duration of a connection or, if defined inside a compound statement, for the duration of the compound statement.
What command do we use to rename a db?
sp_renamedb ‘oldname’ , ‘newname’
If someone is using db it will not accept sp_renmaedb. In that case first bring db to single user using sp_dboptions. Use sp_renamedb to rename database. Use sp_dboptions to bring database to multi user mode.

• What are the different types of replication? Explain.
The SQL Server 2000-supported replication types are as follows:
· Transactional
· Snapshot
· Merge
Snapshot replication distributes data exactly as it appears at a specific moment in time and does not monitor for updates to the data. Snapshot replication is best used as a method for replicating data that changes infrequently or where the most up-to-date values (low latency) are not a requirement. When synchronization occurs, the entire snapshot is generated and sent to Subscribers.

Transactional replication, an initial snapshot of data is applied at Subscribers, and then when data modifications are made at the Publisher, the individual transactions are captured and applied to Subscribers.

Merge replication is the process of distributing data from Publisher to Subscribers, allowing the
Publisher and Subscribers to make updates while connected or disconnected, and then merging the updates between sites when they are connected.
What are the OS services that the SQL Server installation adds?


• What is the STUFF function and how does it differ from the REPLACE function?
REPLACE function to replace existing characters of all occurance. Using this syntax
search_string found in the string_expression will be replaced with replacement_string.

• Using query analyzer, name 3 ways to get an accurate count of the number of records in a table?

The Master database stores the information about the sql server configuration, databases, users etc.

The msdb database stores information regarding database backups, SQL Agent information, DTS packages, backup and restore history, SQL Server jobs, and some replication information such as for log shipping.

• What are primary keys and foreign keys?

• What is data integrity? Explain constraints?
Data integrity is an important feature in SQL Server. When used properly, it ensures that data is accurate, correct, and valid. It also acts as a trap for otherwise undetectable bugs within applications.
A UNIQUE constraint enforces the uniqueness of the values in a set of columns, so no duplicate values are entered.The unique key constraints are used to enforce entity integrity as the primary key constraints.


are used to enforce domain integrity, as the check constraints.

• What is Identity?

• What is BCP? When does it used?

How do you load large data to the SQL server database?
BulkCopy is a tool used to copy huge amount of data from tables. BULK INSERT command helps to Imports a data file into a database table or view in a user-specified format.

• How to know which index a table is using?
SELECT table_name,index_name FROM user_constraints

• How to copy the tables, schema and views from one SQL server to another?
Microsoft SQL Server 2000 Data Transformation Services (DTS) is a set of graphical tools and
programmable objects that lets user extract, transform, and consolidate data from disparate sources into single or multiple destinations.

• What is Self Join?
This is a particular case when one table joins to itself, with one or two aliases to avoid confusion. A self join can be of any type, as long as the joined tables are the same. A self join is rather unique in that it involves a relationship with only one table. The common example is when company have a hierarchal reporting structure whereby one member of staff reports to another.

• What is Cross Join?

• Which virtual table/Magic Tables does a trigger use?
Inserted and Deleted.

• List few advantages of Stored Procedure.
· Stored procedure can reduced network traffic and latency, boosting application performance.
· Stored procedure execution plans can be reused, staying cached in SQL Server’s memory,
reducing server overhead.
· Stored procedures help promote code reuse.
· Stored procedures can encapsulate logic. You can change stored procedure code without
affecting clients.
· Stored procedures provide better security to your data.


16 thoughts on “SQL Server Interview Questions for DBA/Developer

  1. Laxmi

    Hi Jshah,
    Thanks in Advance.
    What is the diffrence between Remote Server and Linked server in SQL Server ?


    1. JShah Post author

      Remote Server you can think as Remote desktop which you can connect through TCP/IP or OSQL or by regestiring in your Management Studion.

      Linked Server is used to connect SQL Server or other databases like (Oracle, Access, MySQL) any db server which is support my Microsoft ODBC/OLEDB.

  2. Abhilash

    Hi Jugal,
    I never ever seen such a huge information under single page.
    I think for a beginer it would be more than enough to know about the SQL.


  3. keatichai

    1 AS1 01/01/2008 00:00:00 44PLCC .5
    2 AS1 01/01/2008 00:00:00 68PLCC 2
    3 AS1 01/01/2008 00:00:00 84PLCC .5
    4 AS1 09/07/2009 00:00:00 84PLCC 3
    5 AS1 09/09/2009 00:00:00 84PLCC 5

    please help create sql statement need result only line 1,2,5

  4. Pravesh

    hi sir ,
    this pravesh tyagi…iam the MCA student and make my career in Oracle………so, as a fresher what the job opposites in oracle…..eg: DBA, Develpoer


Leave a Reply to Kanan Cancel reply


WordPress.com Logo


Google photo


Twitter picture


Facebook photo


Connecting to %s