SQL Server Interview Questions
SQL Server Interview Questions
SQL Server Interview Questions
1. Explain the use of WITH ENCRYPTION keyword. Create a store procedure with
encryption.
It is a way to convert the original text of the stored procedure into encrypted form.
The stored procedure gets modified and once it is encrypted then it is not possible to get
original text of the stored procedure from stored procedure itself.
CREATE PROCEDURE abc
WITH ENCRYPTION
AS
<< SELECT statement>>
GO
Analysis Services is a middle tier server of OLAP (online analytical processing) and Data
mining.
Every analysis server has a repository to store metadata for the objects like cubes, data
sources etc.
Service broker allows internal and external processes to send and receive guaranteed
asynchronous messages.
Messages can also be sent to remote servers that are hosting databases as well.
The concept of queues is used by the broker to put a message in a queue and continue
with other applications asynchronously.
This enables client applications to process messages at their leisure without blocking the
broker.
Service Broker uses the concepts of message ordering, coordination, multithreading and
receiver management to solve some major message queuing problems.
6. What are the user defined data types and when are they used?
User defined data types are based on the system data types.
They should be used when multiple tables need to store the same type of data in a column
and you need to ensure that all these columns are exactly the same including length, and
nullability.
A bit data type is an integer data type which can store either a 0 or 1 or null value.
8. Describe the XML support SQL server extends.
The SQL Server (server-side) support has 3 major elements are as follows:
Creation of XML fragments: This is done from the relational data using FOR XML to the
select query.
Ability to shared XML data to be stored in the database.
Finally, storing the XML data.
Client-side XML support in SQL Server is in the form of SQLXML. It can be described in
terms of:
XML Views: Providing bidirectional mapping between XML schemas and relational
tables.
Creation of XML Templates: It allows creation of dynamic sections in XML.
English query allows accessing the relational databases through English Query
applications. Such applications permit the users to ask the database to fetch the data
which is based on simple English instead of using SQL statements.
SQL profiler is a tool which monitors the performance of various stored procedures.
It is used to debug the queries and procedure which is based on the performance.
If there is any problem, then it captures the events on the production environment so that
they can be solved.
It allows the navigation on the XML document to the straight element where we need to
reach and access the attributes.
13. Explain Data Definition Language (DDL), Data Control Language (DCL) and Data
Manipulation Language (DML).
1. Data Definition Language (DDL) - It defines the database structure.
Example:
CREATE: It is used to create a table in a database.
ALTER: It is used to add, delete or modify columns in an existing table.
DROP: It is used to remove a table definition and all the data, indexes, triggers,
constraints, and permission specifications for that table. (Note: You have to be careful
while using this command because once a table is deleted then all the information
available in the table would also be lost forever.)
TRUNCATE: This command is used to delete complete data from an existing table.
COMMENT: It adds comment to the table, view or column-level data dictionary objects.
RENAME: It is used to rename a table.
2. Data Manipulation Language (DML) It is used for manipulate or edit the data.
Example:
SELECT Retrieve data from the database.
INSERT Insert data into the table.
UPDATE Modify the existing data within a table.
DELETE It is used to delete data from a table. This command can also be used
with condition to delete a particular row.
MERGE It is used to select rows from one or more sources for update or insertion
into a table or view. You can specify conditions to determine whether to update or
insert into the target table or view.
CALL It calls a PL/SQL or Java subprogram.
EXPLAIN PLAN These statements are supported in PL/SQL only when executed
dynamically.
LOCK TABLE Control concurrency.
3. Data Control Language (DCL) - These statements to take care of the security and
authorization. It is used to create privileges to allow users access and manipulation of the
database.
Examples:
GRANT To grant a privilege to a user.
A sequence tree is built and it is based on the keywords and expressions in the form of the
logical units.
Query optimizer checks for various permutations and combinations to figure out the
fastest way of using minimum resources to access the source tables. The best found way
is called as an execution plan.
It is a signal command which is used to execute the entire batch of SQL statements after
previous Go command.
16. What is the significance of NULL value and why should it be avoided for permitting
null values?
NULL value means that no entry has been made into the column.
It states that the corresponding value is either unknown or undefined and it is different
from zero (0) or " ".
They should be avoided for the complexity in select & update queries and also because
columns which have constraints like primary or foreign key constraints which cannot
contain a NULL value.
DBCC (Database Consistency Checker) acts as a database console commands for SQL
Server to check database consistency.
Maintenance
Miscellaneous
Informational
Validation
Miscellaneous: Miscellaneous tasks such as enabling trace flags or removing DLL file
from memory.
Miscellaneous Commands:
DBCC dllname
DBCC HELP
DBCC FREESESSIONCACHE
DBCC TRACEOFF
DBCC FREESYSTEMCACHE
DBCC TRACEON
Informational: Informational tasks that gather and display various types of information.
Informational Commands:
DBCC INPUTBUFFER
DBCC SHOWCONTIG
DBCC OPENTRAN
DBCC SQLPERF
DBCC OUTPUTBUFFER
DBCC TRACESTATUS
DBCC PROCCACHE
DBCC USEROPTIONS
DBCC SHOW_STATISTICS
Validation: Validation operations on a database, index, table, catalog etc.
Validation Commands:
DBCC CHECKALLOC
DBCC CHECKFILEGROUP
DBCC CHECKCATALOG
DBCC CHECKIDENT
DBCC CHECKCONSTRAINTS
DBCC CHECKTABLE
DBCC CHECKDB
19. What is Log shipping?
Log shipping defines the process for automatically taking backup of the database and
transaction files on a SQL Server and then restoring them on a standby/backup server.
This keeps the two SQL Server instances in sync with each other.
In case the production server fails, user simply needs to be pointed to the standby/backup
server.
20. What is the difference between a Local and a Global temporary table?
Temporary tables are used to allow short term use of the data in SQL Server.
21. What is the STUFF? And how does it differ from the REPLACE function?
STUFF function:
STUFF Syntax:
STUFF (character_expression, start, length, replaceWith_expression)
REPLACE function:
The REPLACE function replaces all occurrences of a specified string value with another
string value.
Both STUFF and REPLACE function are used to replace the characters in a string.
REPLACE Syntax:
REPLACE (string_expression, string_pattern, string_replacement)
22. What are the rules to use the ROWGUIDCOL property to define a globally unique
identifier column?
Only one column can exist per table that is attached with ROWGUIDCOL property. One
can then use $ROWGUID instead of column name in select list.
23. What are the actions used to prevent once the referential integrity is enforced?
Actions prevented are:
Breaking of the relationship is used to prevent once the referential integrity is enforced on
a database.
Cannot delete a row from primary table if there are related rows in a secondary table.
Cannot update primary tables primary key if row being modified has related rows in
secondary table.
Cannot insert a new row in the secondary table if there are not related rows in the primary
table.
Cannot update secondary tables foreign key if there is no related row in the primary
table.
24. What are the commands available for summarizing the data in the SQL Server?
Commands for summarizing data in SQL Server:
Command
SUM
GROUP BY
Description
Returns the total sum of a
numeric value.
Returns the average value of a
numeric column.
Returns the number of rows of
a resultset.
Returns the maximum value
from a resultset.
Returns the minimum value
from a resultset.
Arrange resultset in groups.
ORDER BY
AVG
COUNT
MAX
MIN
Syntax/Example
SELECT SUM(Sal) as Tot
from Table1;
SELECT AVG(Sal) as
Avg_Sal from Table1;
SELECT COUNT(*) from
Table1;
SELECT MAX(Sal) from
Table1;
SELECT MIN(Sal) from
Table1;
SELECT ZIP, City FROM
Emp GROUP BY ZIP
SELECT ZIP, City FROM
Emp ORDER BY City
25. List out the difference between CUBE operator and ROLLUP operator
CUBE Operator
It is an additional switch to GROUP BY
ROLLUP Operator
It is an extension to GROUP BY clause. It is
26. What are the guidelines to use bulk copy program (BCP) utility of SQL Server?
BCP utility is an API that allows interacting with SQL Server to export/import data in one
of the two data formats.
To import data into a table, you must either use a format file created for that table or
understand the structure of the table and the types of data that are valid for its columns.
2. Cursor behaviors: The keywords such as SCROLL and INSENSITIVE along with the
cursor declaration define scrollability and sensitivity of the cursor.
29. What are the advantages of stored procedures?
Advantages of stored procedures:
31. What are the differences among batches, stored procedures, and triggers?
Batch
Collection or group of SQL
statements. All statements of a
batch are compiled into one
executional unit called
execution plan.
Stored Procedure
It is a collection or group of
SQL statements that is
compiled once but used many
times.
Triggers
It is a type of Stored procedure
that cannot be called directly.
Instead it fires when a row is
updated, deleted or inserted.
Grant user permission to execute a stored procedure irrespective of the related tables.
Grant user permission to work with a stored procedure to access a restricted set of data
yet no give them permissions to update or select underlying data.
Stored procedures can be granted and execute the permissions rather than setting
permissions on data itself.
It provides more granular security control through the stored procedures rather than
complete the control on underlying data in tables.
TRUNCATE
This is also a logged operation but in terms of
deallocation of data pages.
Cannot TRUNCATE a table that has foreign
key constraints.
It resets identity column to the default starting
value.
It removes all rows from a table.
Cannot be Rolled back.
DDL command
DELETE
This is a logged operation for every row.
Any row not violating a constraint can be
deleted.
It does not reset the identity column. Starts
where it left from last.
It is used to delete all or selected rows from a
table based on WHERE clause.
Need to Commit or Rollback
DML command