SQL Server Interview Questions Part 2

What is a UNIQUE constraint?

“A UNIQUE constraint ensures that all values in a column are different. This provides uniqueness for the column(s) and helps identify each row uniquely. Unlike primary key, there can be multiple unique constraints defined per table. The code syntax for UNIQUE is quite similar to that of PRIMARY KEY and can be used interchangeably.

CREATE TABLE Students ( /* Create table with a single field as unique */
Name VARCHAR(255)

CREATE TABLE Students ( /* Create table with multiple fields as unique */
LastName VARCHAR(255)
UNIQUE (ID, FirstName)

ALTER TABLE Students /* Set a column as unique */

ALTER TABLE Students /* Set multiple columns as unique / ADD CONSTRAINT PK_Student / Naming a unique constraint */
UNIQUE (ID, FirstName);”

What is the SELECT statement?

“SELECT operator in SQL is used to select data from a database. The data returned is stored in a result table, called the result-set.

SELECT * FROM myDB.students;”

What are some common clauses used with SELECT query in SQL?

“Some common SQL clauses used in conjuction with a SELECT query are as follows:

WHERE clause in SQL is used to filter records that are necessary, based on specific conditions.
ORDER BY clause in SQL is used to sort the records based on some field(s) in ascending (ASC) or descending order (DESC).
FROM myDB.students
WHERE graduation_year = 2019
GROUP BY clause in SQL is used to group records with identical data and can be used in conjuction with some aggregation functions to produce summarized results from the database.
HAVING clause in SQL is used to filter records in combination with the GROUP BY clause. It is different from WHERE, since WHERE clause cannot filter aggregated records.
SELECT COUNT(studentId), country
FROM myDB.students
WHERE country != “”INDIA””
GROUP BY country
HAVING COUNT(studentID) > 5;”

SQL Server Interview Questions

What are Entities and ?

“Entity: An entity can be a real-world object, either tangible or intangible, that can be easily identifiable. For example, in a college database, students, professors, workers, departments, and projects can be referred to as entities. Each entity has some associated properties that provide it an identity.

What is the difference between DROP and TRUNCATE statements?

If a table is dropped, all things associated with the tables are dropped as well. This includes – the relationships defined on the table with other tables, the integrity checks and constraints, access privileges and other grants that the table has. To create and use the table again in its original form, all these relations, checks, constraints, privileges and relationships need to be redefined. However, if a table is truncated, none of the above problems exist and the table retains its original structure.

What are the differences between OLTP and OLAP?

“OLTP stands for Online Transaction Processing, is a class of software applications capable of supporting transaction-oriented programs. An important attribute of an OLTP system is its ability to maintain concurrency. OLTP systems often follow a decentralized architecture to avoid single points of failure. These systems are generally designed for a large audience of end users who conduct short transactions. Queries involved in such databases are generally simple, need fast response times and return relatively few records. Number of transactions per second acts as an effective measure for such systems.

OLAP stands for Online Analytical Processing, a class of software programs which are characterized by relatively low frequency of online transactions. Queries are often too complex and involve a bunch of aggregations. For OLAP systems, the effectiveness measure relies highly on response time. Such systems are widely used for data mining or maintaining aggregated, historical data, usually in multi-dimensional schemas.

Advance SQL Server Interview Questions

What is Collation? What are the different types of Collation Sensitivity?

“Collation refers to a set of rules that determine how data is sorted and compared. Rules defining the correct character sequence are used to sort the character data. It incorporates options for specifying case-sensitivity, accent marks, kana character types and character width. Below are the different types of collation sensitivity:

Case sensitivity: A and a are treated differently.
Accent sensitivity: a and á are treated differently.
Kana sensitivity: Japanese kana characters Hiragana and Katakana are treated differently.
Width sensitivity: Same character represented in single-byte (half-width) and double-byte (full-width) are treated differently.”

What is CHECK constraint?

A CHECK constraint can be applied to a column in a table to limit the values that can be placed in a column. Check constraint is to enforce integrity.

Can SQL servers linked to other servers?

SQL server can be connected to any database which has OLE-DB provider to give a link. Example: Oracle has OLE-DB provider which has link to connect with the SQL server group.

SQL Server Interview Questions

What is sub query and its properties?

“A sub-query is a query which can be nested inside a main query like Select, Update, Insert or Delete statements. This can be used when expression is allowed. Properties of sub query can be defined as

A sub query should not have order by clause
A sub query should be placed in the right hand side of the comparison operator of the main query
A sub query should be enclosed in parenthesis because it needs to be executed first before the main query
More than one sub query can be included”

What are the types of sub query?

“There are three types of sub query

Single row sub query which returns only one row
Multiple row sub query which returns multiple rows
Multiple column sub query which returns multiple columns to the main query. With that sub query result, Main query will be executed.”

What is SQL server agent?

The SQL Server agent plays a vital role in day to day tasks of SQL server administrator(DBA). Server agent’s purpose is to implement the tasks easily with the scheduler engine which allows our jobs to run at scheduled date and time.

Advance SQL Server Interview Questions

What are scheduled tasks in SQL Server?

Scheduled tasks or jobs are used to automate processes that can be run on a scheduled time at a regular interval. This scheduling of tasks helps to reduce human intervention during night time and feed can be done at a particular time. User can also order the tasks in which it has to be generated.

What is COALESCE in SQL Server?

“COALESCE is used to return first non-null expression within the arguments. This function is used to return a non-null from more than one column in the arguments.

Example –

Select COALESCE(empno, empname, salary) from employee;”

How exceptions can be handled in SQL Server Programming?

Exceptions are handled using TRY—-CATCH constructs and it is handles by writing scripts inside the TRY block and error handling in the CATCH block.

SQL Server Interview Questions

Can we check locks in database? If so, how can we do this lock check?

Yes, we can check locks in the database. It can be achieved by using in-built stored procedure called sp_lock.

What are the types of Triggers?

“There are four types of triggers and they are:

Instead of”

What is an IDENTITY column in insert statements?

IDENTITY column is used in table columns to make that column as Auto incremental number or a surrogate key.

Advance SQL Server Interview Questions

What is Bulk copy in SQL?

Bulk copy is a tool used to copy large amount of data from Tables. This tool is used to load large amount of data in SQL Server.

What will be query used to get the list of triggers in a database?

“Query to get the list of triggers in database-

Select * from sys.objects where type=’tr'”

What is the difference between UNION and UNION ALL?

“UNION: To select related information from two tables UNION command is used. It is similar to JOIN command.
UNION All: The UNION ALL command is equal to the UNION command, except that UNION ALL selects all values. It will not remove duplicate rows, instead it will retrieve all rows from all tables.”

SQL Server Interview Questions

What is Filtered Index?

Filtered Index is used to filter some portion of rows in a table to improve query performance, index maintenance and reduces index storage costs. When the index is created with WHERE clause, then it is called Filtered Index

What are the methods used to protect against SQL injection attack?

“Following are the methods used to protect against SQL injection attack:

Use Parameters for Stored Procedures
Filtering input parameters
Use Parameter collection with Dynamic SQL
In like clause, user escape characters”

What is SQL injection?

“SQL injection is an attack by malicious users in which malicious code can be inserted into strings that can be passed to an instance of SQL server for parsing and execution. All statements have to checked for vulnerabilities as it executes all syntactically valid queries that it receives.

Even parameters can be manipulated by the skilled and experienced attackers.”

Advance SQL Server Interview Questions

What is CDC?

CDC is abbreviated as Change Data Capture which is used to capture the data that has been changed recently. This feature is present in SQL Server 2008.

What do mean by XML Datatype?

XML data type is used to store XML documents in the SQL Server database. Columns and variables are created and store XML instances in the database.

Which command is used for user defined error messages?

RAISEERROR is the command used to generate and initiates error processing for a given session. Those user defined messages are stored in sys.messages table.

SQL Server Interview Questions


TABLESAMPLE is used to extract sample of rows randomly that are all necessary for the application. The sample rows taken are based on the percentage of rows.

How data can be copied from one table to another table?


This command is used to insert data into a table which is already created.


This command is used to create a new table and its structure and data can be copied from existing table.”

What is the difference between GETDATE and SYSDATETIME?

Both are same but GETDATE can give time till milliseconds and SYSDATETIME can give precision till nanoseconds. SYSDATE TIME is more accurate than GETDATE.

Advance SQL Server Interview Questions

Where are SQL Server user names and passwords stored in SQL Server?

User Names and Passwords are stored in sys.server_principals and sys.sql_logins. But passwords are not stored in normal text.

How to delete duplicate rows in SQL Server?

Duplicate rows can be deleted using CTE and ROW NUMER feature of SQL Server.

What is the command used to Recompile the stored procedure at run time?

“Stored Procedure can be executed with the help of keyword called RECOMPILE.



SQL Server Interview Questions

What is the use of @@SPID?

A @@SPID returns the session ID of the current user process.

What is the difference between varchar and nvarchar types?

Varchar and nvarchar are same but the only difference is that nvarchar can be used to store Unicode characters for multiple languages and it also takes more space when compared with varchar.

What is the difference between COMMIT and ROLLBACK?

Every statement between BEGIN and COMMIT becomes persistent to database when the COMMIT is executed. Every statement between BEGIN and ROOLBACK are reverted to the state when the ROLLBACK was executed.

Advance SQL Server Interview Questions

What will be the maximum number of index per table?

“For SQL Server 2008 100 Index can be used as maximum number per table. 1 Clustered Index and 999 Non-clustered indexes per table can be used in SQL Server.

1000 Index can be used as maximum number per table. 1 Clustered Index and 999 Non-clustered indexes per table can be used in SQL Server.

1 Clustered Index and 999 Non-clustered indexes per table can be used in SQL Server.”

What is the use of FOR Clause?

FOR clause is mainly used for XML and browser options. This clause is mainly used to display the query results in XML format or in browser.

What is ISNULL() operator?

ISNULL function is used to check whether value given is NULL or not NULL in sql server. This function also provides to replace a value with the NULL.

SQL Server Interview Questions

How can you create a login?

“You can use the following command to create a login


What is the difference between SUBSTR and CHARINDEX in the SQL Server?

“The SUBSTR function is used to return specific portion of string in a given string. But, CHARINDEX function gives character position in a given specified string.

Gives result as Smi

CHARINDEX(‘i’, ‘Smiley’,1)
Gives 3 as result as I appears in 3rd position of the string”

What are Magic Tables in SQL Server?

During DML operations like Insert, Delete, and Update, SQL Server creates magic tables to hold the values during the DML operations. These magic tables are used inside the triggers for data transaction.

Advance SQL Server Interview Questions

Which SQL server table is used to hold the stored procedure scripts?

Sys.SQL_Modules is a SQL Server table used to store the script of stored procedure. Name of the stored procedure is saved in the table called Sys.Procedures.

SQL server Part 1SQL server Part 3
Back to top