SQL Interview Questions
SQL Interview Questions
When we copy the data from one table to another table we use insert and select
query. Tables always independent objects that mean a table does not depend on other
tables
How to create a new table from an existing table or in how many ways
we can create a new table from an existing table?
If required we can create a new table from an existing table as below.
Syntax1: (with all column from an existing table)
SELECT * INTO <NEW TABLE NAME> FROM <OLD TABLE NAME>
Example: SELECT * INTO NEWEMPLOYEE FROM EMPLOYEE
When we execute the above query it will create a new table with all records from an
existing table.
Syntax2: (with specific columns from an existing table)
SELECT <REQUIREDCOLUMN> INTO <NEW TABLE NAME> FROM <OLD TABLE N
AME>
Example: SELECT EID, SALARY INTO SPECEMP FROM EMPLOYEE
When we execute the above query it will create a new table with the specific column
data from an existing table.
Syntax3: (creating a new table without data)
SELECT * INTO <NEW TABLE NAME> FROM <OLD TABLE NAME> WHERE 1 = 0
Example: SELECT * INTO DUMMYEMP FROM EMPLOYEE WHERE 1 = 0
OR
SELECT <REQUIRED COLUMNS> INTO <NEW TABLE NAME> FROM
<OLD TABLE NAME>
SELECT EID, SALARY INTO TAB1 FROM EMPLOYEE WHERE 1 = 0
When we execute the above query it will create a new table without records from an
existing table.
What is normalization?
Database normalization is a data design and organization process applied to data
structures based on rules that help build relational databases. In relational database
design the process of organizing data to minimize redundancy. Normalization usually
involves dividing a database into two or more tables and defining relationships between
the tables. The objective is to isolate data so that additions, deletions, and modifications
of a field can be made in just one table and then propagated through the rest of the
database via the defined relationships.
What are the different normalization forms?
This is one of the frequently asked SQL Server Interview Questions.
1NF: Eliminate Repeating Groups: Make a separate table for each set of related
attributes, and give each table a primary key. Each field contains at most one value from
its attribute domain.
2NF: Eliminate Redundant Data: If an attribute depends on only part of a multi-valued
key, remove it to a separate table.
3NF: Eliminate Columns Not Dependent On Key: If attributes do not contribute to a
description of the key, remove them to a separate table. All attributes must be directly
dependent on the primary key
BCNF: Boyce-Codd Normal Form: If there are non-trivial dependencies between
candidate key attributes, separate them out into distinct tables.
4NF: Isolate Independent Multiple Relationships: No table may contain two or more
1:n or n:m relationships that are not directly related.
5NF: Isolate Semantically Related Multiple Relationships: There may be practical
constrains on information that justifies separating logically related many-to-many
relationships.
ONF: Optimal Normal Form: A model limited to only simple (elemental) facts, as
expressed in Object Role Model notation.
DKNF: Domain-Key Normal Form: A model free from all modification anomalies.
Remember, these normalization guidelines are cumulative. For a database to be in 3NF,
it must first fulfill all the criteria of a 2NF and 1NF database. Please click here to
learn Database Normalization in detail step by step with some real-time examples.
What is the Cursor?
A cursor is a database object used by applications to manipulate data in a set on a row-
by-row basis, instead of the typical SQL commands that operate on all the rows in the
set at one time.
In order to work with a cursor we need to perform some steps in the following order:
1. Declare cursor
2. Open cursor
3. Fetch row from the cursor Process fetched row Close cursor
4. Deallocate cursor
What is the use of DBCC commands?
DBCC stands for database consistency checker. We use these commands to check the
consistency of the databases, i.e., maintenance, validation task, and status checks. For
example,
1. DBCC CHECKDB – Ensures that tables in the DB and the indexes are correctly
linked.
2. DBCC CHECKALLOC – To check that all pages in a DB are correctly allocated.
3. DBCC CHECKFILEGROUP – Checks all tables file group for any damage.
What is a Linked Server?
Linked Servers is a concept in SQL Server by which we can add other SQL Server to a
Group and query both the SQL Server DBS using T-SQL Statements. With a linked
server, you can create very clean, easy to follow, SQL statements that allow remote data
to be retrieved, joined, and combined with local data. Stored
Procedure sp_addlinkedserver, sp_addlinkedsrvlogin will be used to add a new
Linked Server.
What is Collation?
Collation refers to a set of rules that determine how data is sorted and compared.
Character data is sorted using rules that define the correct character sequence, with
options for specifying case-sensitivity, accent marks, kana character types, and
character width.
What are the different types of Collation Sensitivity?
1. Case sensitivity: A and a, B and b, etc.
2. Accent sensitivity: a and á, o and ó, etc.
3. Kana Sensitivity: When Japanese kana characters Hiragana and Katakana
are treated differently, it is called Kana sensitive.
4. Width sensitivity: When a single-byte character (half-width) and the same
character when represented as a double-byte character (full-width) are treated
differently than it is width sensitive.
How to implement one-to-one, one-to-many and many-to-many
relationships while designing tables?
1. The one-to-one relationship can be implemented as a single table and rarely as
two tables with primary and foreign key relationships.
2. One-to-Many relationships are implemented by splitting the data into two tables
with primary key and foreign key relationships.
3. Many-to-Many relationships are implemented using a junction table with the keys
from both the tables forming the composite primary key of the junction table.
What is a NOLOCK?
This is one of the most frequently asked SQL Interview Questions. Using the NOLOCK
query optimizer hint is generally considered good practice in order to improve
concurrency on a busy system. When the NOLOCK hint is included in a SELECT
statement, no locks are taken when data is read. The result is a Dirty Read, which
means that another process could be updating the data at the exact time you are
reading it. There are no guarantees that your query will retrieve the most recent data.
The advantage of performance is that your reading of data will not block updates from
taking place, and updates will not block your reading of data. SELECT statements take
Shared (Read) locks. This means that multiple SELECT statements are allowed
simultaneous access, but other processes are blocked from modifying the data. The
updates will queue until all the reads have completed, and reads requested after the
update will wait for the updates to complete. The result of your system is the
delay(blocking).
When is the use of the UPDATE_STATISTICS command?
This is one of the most frequently asked SQL Interview Questions. This command is
basically used when a large processing of data has occurred. If a large number of
deletions any modification or Bulk Copy into the tables has occurred, it has to update the
indexes to take these changes into account. UPDATE_STATISTICS updates the
indexes on these tables accordingly.
What is sub-query? Explain the properties of sub-query.
This is one of the frequently asked SQL Server Interview Questions. Sub-queries are
often referred to as sub-selects, as they allow a SELECT statement to be executed
arbitrarily within the body of another SQL statement. A sub-query is executed by
enclosing it in a set of parentheses. Sub-queries are generally used to return a single
row as an atomic value, though they may be used to compare values against multiple
rows with the IN keyword.
A subquery is a SELECT statement that is nested within another T-SQL statement. A
subquery SELECT statement if executed independently of the T-SQL statement, in
which it is nested, will return a result set. Meaning a subquery SELECT statement can
stand alone and is not dependent on the statement in which it is nested. A subquery
SELECT statement can return any number of values and can be found in, the column list
of a SELECT statement, a FROM, GROUP BY, HAVING, and/or ORDER BY clauses of
a T-SQL statement. A Subquery can also be used as a parameter to a function call.
Basically, a subquery can be used anywhere an expression can be used.
Properties of Sub-Query
1. A subquery must be enclosed in the parenthesis.
2. The subquery must be put in the right hand of the comparison operator, and
3. The subquery cannot contain an ORDER-BY clause.
4. A query can contain more than one sub-queries.
What are the types of sub-queries?
1. Single-row subquery, where the subquery returns only one row.
2. Multiple-row subquery, where the subquery returns multiple rows
3. Multiple column subquery, where the subquery returns multiple columns.
What is the SQL Profiler?
This is one of the most frequently asked SQL Server Interview Questions. SQL Profiler is
a graphical tool that allows system administrators to monitor events in an instance of
Microsoft SQL Server. You 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.
Use SQL Profiler to monitor only the events in which you are interested. If traces are
becoming too large, you can filter them based on the information you want, so that only
a subset of the event data is collected. Monitoring too many events adds overhead to the
server and the monitoring process and can cause the trace file or trace table to grow
very large, especially when the monitoring process takes place over a long period of
time.
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 the client and the server.
What are the authentication modes in SQL Server? How can it be
changed?
Windows mode and mixed mode (SQL & Windows). To change authentication mode in
SQL Server click Start, Programs, Microsoft SQL Server, and click SQL Enterprise
Manager to run SQL Enterprise Manager from the Microsoft SQL Server program group.
Select the server then from the Tools menu select SQL Server Configuration Properties,
and choose the Security page.
Where are SQL server users names and passwords are stored in
SQL server?
They get stored in master DB in the sysxlogins table.
Which command using Query Analyzer will give you the version of
SQL server and operating system?
SELECT SERVERPROPERTY(‘productversion’), SERVERPROPERTY (‘productlevel’),
SERVERPROPERTY (‘edition’)
What is the SQL server agent?
This is one of the frequently asked SQL Server Interview Questions. SQL Server agent
plays an important role in the day-to-day tasks of a database administrator (DBA). It is
often overlooked as one of the main tools for SQL Server management. Its purpose is to
ease the implementation of tasks for the DBA, with its full-function scheduling engine,
which allows you to schedule your own jobs and scripts.
What is log shipping?
Log shipping is the process of automating the backup of database and transaction log
files on a production SQL server and then restoring them onto a standby server.
Enterprise Editions only supports log shipping. In log shipping, the transnational log file
from one server is automatically updated into the backup database on the other server. If
one server fails, the other server will have the same DB that can be used as the Disaster
Recovery plan. The key feature of log shipping is that it will automatically backup
transaction logs throughout the day and automatically restore them on the standby
server at a defined interval.
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 the
single user using sp_dboptions. Use sp_renamedb to rename the database. Use
sp_dboptions to bring the database to multi-user mode.
What are sp_configure commands and set commands?
Use sp_configure to display or change server-level settings. To change database-level
settings, use ALTER DATABASE. To change settings that affect only the current user
session, use the SET statement.
What are the different types of replication? Explain.
The SQL Server 2000-supported replication types are as follows:
1. Transactional
2. Snapshot
3. 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.
In 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 propagated 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?
MS SQL SERVER SERVICE, SQL AGENT SERVICE, DTC (Distribution transac co-
ordinator)
What are three SQL keywords used to change or set someone’s
permissions?
GRANT, DENY, and REVOKE.
What does it mean to have quoted_identifier on? What are the
implications of having it off?
When SET QUOTED_IDENTIFIER is ON, identifiers can be delimited by double
quotation marks, and literals must be delimited by single quotation marks. When SET
QUOTED_IDENTIFIER is OFF, identifiers cannot be quoted and must follow all
Transact-SQL rules for identifiers.
What is the STUFF function and how does it differ from the REPLACE
function?
STUFF function to overwrite existing characters. Using this syntax,
STUFF(string_expression, start, length, replacement_characters), string_expression is
the string that will have characters substituted, the start is the starting position, the
length is the number of characters in the string that are substituted, and
replacement_characters are the new characters interjected into the string.
REPLACE function to replace existing characters of all occurrences. Using this syntax
REPLACE(string_expression, search_string, replacement_string), where every
incidence of 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?
SELECT * FROM table1 SELECT COUNT(*) FROM table1
SELECT rows FROM sysindexes WHERE id = OBJECT_ID(table1) AND indid < 2
How to rebuild the Master Database?
This is one of the frequently asked SQL Server Questions. Shutdown Microsoft SQL
Server 2000, and then run Rebuildm.exe. This is located in the Program Files\Microsoft
SQL Server\80\Tools\Binn directory.
In the Rebuild Master dialog box, click Browse. In the Browse for Folder dialog box,
select the \Data folder on the SQL Server 2000 compact disc or in the shared network
directory from which SQL Server 2000 was installed, and then click OK.
Click Settings. In the Collation Settings dialog box, verify or change settings used for the
master database and all other databases.
Initially, the default collation settings are shown, but these may not match the collation
selected during setup. You can select the same settings used during setup or select new
collation settings. When done, click OK.
In the Rebuild Master dialog box, click Rebuild to start the process. The Rebuild Master
utility reinstalls the master database. To continue, you may need to stop a server that is
running.
What are the basic functions for master, MSDB, model, TEMPFB
databases?
This is one of the frequently asked SQL Server Interview
Questions. The Master database holds information for all databases located on the SQL
Server instance and is the glue that holds the engine together. Because SQL Server
cannot start without a functioning master database, you must administer this database
with care.
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.
What is De-normalization?
De-normalization is the process of attempting to optimize the performance of a
database by adding redundant data. It is sometimes necessary because current DBMSs
implement the relational model poorly. A true relational DBMS would allow for a fully
normalized database at the logical level while providing physical storage of data that is
tuned for high performance. De-normalisation is a technique to move from higher to
lower normal forms of database modeling in order to speed up database access.
What is Scheduled Jobs or What is a Scheduled Tasks?
This is one of the frequently asked SQL Server Interview Questions. Scheduled tasks let
users automate processes that run on regular or predictable cycles. Users can schedule
administrative tasks, such as cube processing, to run during times of slow business
activity. Users can also determine the order in which tasks run by creating job steps
within a SQL Server Agent job. E.g. Back up the database, Update the Stats of Tables.
Job steps give the user control over the flow of execution.
If one job fails, the user can configure SQL Server Agent to continue to run the
remaining tasks or to stop the execution.
What is BCP? When does it use?
BulkCopy is a tool used to copy a huge amount of data from tables and views. BCP does
not copy the structures same as source to destination.
How do you load large data to the SQL server database?
BulkCopy is a tool used to copy a 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.
Can we rewrite subqueries into simple select statements or with
joins?
Subqueries can often be re-written to use a standard outer join, resulting in faster
performance. As we may know, an outer join uses the plus sign (+) operator to tell the
database to return all non-matching rows with NULL values. Hence we combine the
outer join with a NULL test in the WHERE clause to reproduce the result set without
using a sub-query.
Can SQL Servers link to other servers like Oracle?
This is one of the most frequently asked SQL Server Interview Questions. SQL Server
can be lined to any server provided it has an OLE-DB provider from Microsoft to allow a
link. E.g. Oracle has an OLE-DB provider for oracle that Microsoft provides to add it as a
linked server to the SQL Server group.
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 Data Warehousing?
Subject-oriented, meaning that the data in the database is organized so that all the
data elements relating to the same real-world event or object are linked together;
Time-variant, meaning that the changes to the data in the database are tracked and
recorded so that reports can be produced showing changes over time;
Non-volatile, meaning that data in the database is never over-written or deleted,
once committed, the data is static, read-only, but retained for future reporting;
Integrated, meaning that the database contains data from most or all of an
organization’s operational applications and that this data is made consistent.
What is OLTP (Online Transaction Processing)?
In OLTP – online transaction processing systems relational database design uses the
discipline of data modeling and generally follows the Codd rules of data normalization in
order to ensure absolute data integrity. Using these rules complex information is broken
down into its most simple structures (a table) where all of the individual atomic level
elements relate to each other and satisfy the normalization rules.
What is Data Integrity?
1. Data integrity means the data contained in the database is accurate and reliable.
2. To provide data integrity, RDBMS provides a set of integrity constraints that
ensures that data entered into the database is accurate, valid, and consistent.
What are the different levels of data integrity in SQL Server?
1. Entity Integrity (uniquely identify- PK, UQ)
2. Domain Integrity (Follow defined rule- Check
3. Referential integrity (relationships- FK)
4. User-defined integrity
What is Entity integrity?
1. Entity integrity ensures each row in a table is a uniquely identifiable entity. We
can achieve entity integrity in a table by using either PRIMARY KEY or UNIQUE
KEY constraints.
2. In simple words, we can say Entity Integrity ensures that there are no duplicate
rows in a table.
What is Referential integrity?
1. Referential integrity ensures that relationships between tables remain preserved
as data is inserted, deleted, and modified. We can apply referential integrity using
a FOREIGN KEY constraint.
2. In simple words, we can say that Referential integrity ensures that rows cannot
be updated or deleted which are used by other records.
What is Domain Integrity?
1. Domain integrity ensures that the values going to store in a column must follow
some defined rules such as range, type, and format. A database can enforce
these rules by using the CHECK KEY constraint.
2. In simple words, we can say that Domain Integrity enforces valid entries for a
given column by restricting the type, the format, or the range of possible values.
What is user-defined integrity?
User-Defined Integrity enforces some specific business rules that do not fall into an
entity, domain, or referential integrity categories. Each of these categories of data
integrity can be enforced by the appropriate constraints.
What is a Constraint in SQL Server?
A constraint is a property that is assigned to a column or set of columns in a table that
is used to enforce data integrity means it ensures that the data is going to store in a
table is valid, consistent, and accurate.
Why do we need Constraints in SQL Server?
1. A constraint is used to restrict the insertion of unwanted data in any columns.
2. We can create constraints on single or multiple columns of any table.
3. It maintains the data integrity i.e. accurate data or original data of the table.
What are the different types of Constraints available in SQL Server?
SQL Server supports five constraints for maintaining data integrity which are
1. UNIQUE KEY constraint
2. NOT NULL constraint
3. CHECK KEY constraint
4. PRIMARY KEY constraint
5. FOREIGN KEY constraint.
Note: Constraints are imposed on columns of a table.
What is Default Constraint in SQL Server?
1. The default constraint is used to fill the column with a default value that is defined
during the creation of a table if the user does not supply any value while inserting
the data.
2. IDENTITY columns and timestamp columns can’t be associated with a default
constraint.
3. In simple words, we can say that Default constraints enable the SQL Server to
write default value to a column when the user doesn’t specify a value.
4. A default allows us to specify a constant value, NULL, or the run-time value of a
system function if no known value exists or if the column is missing in an INSERT
statement.
5. Let us see an example for understanding this concept
The Global Temporary Tables are visible to all the connections of the SQL Server and
are only destroyed when the last connection referencing the table is closed.
Multiple users across multiple connections can have Local temporary tables with the
same name but Global Temporary Table Names should be unique and if we inspect the
name of the Global Temporary Table in the object explorer there will be no random
numbers suffixed at the end of the table name.
What are the differences between Local and Global Temporary
Tables?
1. Local Temp tables are prefixed with a single pound (#) symbol, whereas global
temp tables are prefixed with 2 pounds (##) symbols.
2. SQL Server appends some random numbers at the end of the local temp table
name where this is not done for global temp table names.
3. The Local temporary tables are only visible to that session of the Server which
has created it, whereas Global temporary tables are visible to all the server
sessions.
4. Local temporary tables are automatically dropped; when the session that created,
the temporary tables are closed. Global temporary tables are also automatically
dropped when the session that creates the temporary tables is closed.
Can you please explain when to use a temp table in SQL Server?
When we want to perform many operations on the data in a specific table in the
database, we can load the data into a temporary table and then we can perform
operations on the temporary table. Loading data into a temporary table speeds up the
process because all the operations are performed locally on the client. Use of the
Temporary Tables reduces the load on both the network and server as all the interaction
with temporary tables occurs on the Client.
1. When we are doing a large number of row manipulation in stored procedures.
2. This is useful to replace the cursor. We can store the result set data into a temp
table, then we can manipulate the data from there.
3. When we are having a complex join operation.
4. A Temporary Table variable can be very useful when used with stored
procedures to pass input/output parameters or to store the result of a table-
valued function.
Can you create foreign key constraints on temporary tables?
No
Do you have to manually delete temporary tables?
No, temporary tables are automatically dropped, when the session that created the
temporary tables is closed. But if we maintain a persistent connection or if connection
pooling is enabled, then it is better to explicitly drop the temporary tables we have
created. However, it is generally considered a good coding practice to explicitly drop
every temporary table we create.
In which database, the temporary tables get created?
TEMPDB database.
How can I check for the existence of a temporary table?
IF OBJECT_ID('tempdb..#LocalTempTable') IS NOT NULL
Begin
Drop table #LocalTempTable
Print 'Temporary table #LocalTempTable is deleted'
End
Else
Begin
Print 'Temporary table #LocalTempTable is not Found,
it may have been deleted already'
End
Usually, it is best to create a temp table through the create function. For example, if we
want to check if that temp table existed and then drop/create a new one and insert data
it would go something like this.
IF object_ID('tempdb..##temptable') IS NOT NULL
DROP TABLE ##TEMPTABLE
CREATE TABLE ##TEMPTABLE
INSERT INTO ##TEMPTABLE
SELECT e.EMPLOYEE
FROM Employee e
WHERE e.employeename = 'Pranaya'
This would check for the table and drop it if it exists then would create a new global temp
table (indicated by the ##) and insert all employees with the name Pranaya into this
temp table.
SQL appends random function as at the end of the Local table’s
name. So, we can create a Local table with the same name. But the
Global table’s name has to be unique. Why is that?
It is because a Local table is available to the connection that created it. Other users can
have that name for their local table. But in the case of a global temporary table, it is
available to all users on different sessions. So, its name cannot be different.
How to copy the structure of the temporary table from one table to
another? i.e., copying the only structure and no data?
Select * into #Temp1 from tblEmployee
Select Top 0 * into #Temp2 from #Temp1
--Or
Select * into #Temp1 from tblEmployee where 1=2
What is the difference between a Temporary Table and a Table
variable?
1. A table variable is created in the memory whereas a temporary table is created in
the TempDB. But if there is memory pressure, the pages belonging to a table
variable may be pushed out to tempdb.
2. Table variables cannot be involved in transactions, logging or locking. This
makes the table variable faster than a temporary table.
3. We can pass the table variable as a parameter to functions and stored
procedures, whereas we cannot do the same with a temporary table.
4. The temporary table can have indexes, whereas a table variable can only have a
primary index. If speed is an issue Table variable can be faster, but if there are a
lot of records, or there is a need to search the temporary table based on a
clustered index, then a Temporary Table would be better. If we have less than
100 rows generally use a table variable. Otherwise, use a temporary table. This is
because SQL Server won’t create statistics on table variables.
The problem in the above execution is even if the error occurred in the program, it still
showing the result so there are chances of users being confused.
Note: Whenever an error occurred while executing a program developed by using any
programming language the program terminates abnormally on the line where the error
got occurred but in SQL Server it will still continue the program execution.
What is Exception Handling?
We handle errors of a program both in a programming language as well as databases
also whereas handling errors in a programming language needs stopping the abnormal
termination and allowing the statements which are not related to the error to execute but
handling an error in SQL Server means stopping the execution of the statements which
are related with the error.
With the introduction of Try/Catch blocks in SQL Server 2005, error handling in SQL
Server is now similar to programming languages like C#, and java. Before understanding
error handling using try/catch, let’s step back and understand how error handling was
done in SQL Server 2000, using system function @@Error. Sometimes, system
functions that begin with two at signs (@@), are called global variables. They are not
variables and do not have the same behaviors as variables, instead, they are very
similar to functions.
Now let’s create tblProduct and tblProductSales that we will be using for the rest of
this demo.
--SQL script to create tblProduct
Create Table tblProduct
(
ProductId int NOT NULL primary key,
Name nvarchar(50),
UnitPrice int,
QtyAvailable int
)
Go
-- SQL script to load data into tblProduct
Insert into tblProduct values(1, 'Laptops', 2340, 100)
Insert into tblProduct values(2, 'Desktops', 3467, 50)
Go
-- SQL script to create tblProductSales
Create Table tblProductSales
(
ProductSalesId int primary key,
ProductId int,
QuantitySold int
)
Go
--Create a procedure
Create Procedure spSellProduct
@ProductId int,
@QuantityToSell int
As
Begin
-- Check the stock available, for the product we want to sell
Declare @StockAvailable int
Select @StockAvailable = QtyAvailable
from tblProduct where ProductId = @ProductId
-- Throw an error to the calling application, if enough stock is not available
If(@StockAvailable< @QuantityToSell)
Begin
Raiserror('Not enough stock available',16,1)
End
-- If enough stock available
Else
Begin
Begin Transaction
-- First reduce the quantity available
Update tblProduct set QtyAvailable = (QtyAvailable - @QuantityToSell)
where ProductId = @ProductId
Declare @MaxProductSalesId int
-- Calculate MAX ProductSalesId
Select @MaxProductSalesId = Case When
MAX(ProductSalesId) IS NULL
Then 0 else MAX(ProductSalesId) end
from tblProductSales
-- Increment @MaxProductSalesId by 1, so we don't get a primary key violation
Set @MaxProductSalesId = @MaxProductSalesId + 1
Insert into tblProductSales values(@MaxProductSalesId, @ProductId, @QuantityToSell)
Commit Tran
End
End
Stored procedure – spSellProduct, has 2 parameters
– @ProductId and @QuantityToSell. @ProductId specifies the product that we want to
sell, and @QuantityToSell specifies the quantity we would like to sell.
In the procedure, we are using Raiserror() function to return an error message back to
the calling application, if the stock available is less than the quantity we are trying to sell.
We have to pass at least 3 parameters to the Raiserror() function.
RAISERROR(‘Error Message’, ErrorSeverity, ErrorState)
Severity and State are integers. In most cases, when you are returning custom errors,
the severity level is 16, which indicates general errors that can be corrected by the user.
In this case, the error can be corrected, by adjusting the @QuantityToSell, to be less
than or equal to the stock available. ErrorState is also an integer between 1 and 255.
RAISERROR only generates errors with the state from 1 through 127.
The problem with this procedure is that the transaction is always committed. Even, if
there is an error somewhere, between updating tblProduct and tblProductSales table.
In fact, the main purpose of wrapping these 2 statements (Update tblProduct Statement
& Insert into tblProductSales statement) in a transaction is to ensure that, both of the
statements are treated as a single unit. For example, if we have an error when executing
the second statement, then the first statement should also be rolled back.
In SQL server 2000, to detect errors, we can use @@Error system function. @@Error
returns a NON-ZERO value, if there is an error, otherwise ZERO, indicating that the
previous SQL statement encountered no errors. The stored
procedure spSellProductCorrected, makes use of @@ERROR system function to
detect any errors that may have occurred. If there are errors, roll back the transaction,
else commit the transaction. If you comment the line (Set @MaxProductSalesId =
@MaxProductSalesId + 1) and then execute the stored procedure there will be a primary
key violation error, when trying to insert into tblProductSales. As a result of this, the
entire transaction will be rolled back.
Alter Procedure spSellProductCorrected
@ProductId int,
@QuantityToSell int
As
Begin
-- Check the stock available, for the product we want to sell
Declare @StockAvailable int
Select @StockAvailable = QtyAvailable
From tblProduct where ProductId = @ProductId
-- Throw an error to the calling application, if enough stock is not available
If(@StockAvailable< @QuantityToSell)
Begin
Raiserror('Not enough stock available',16,1)
End
-- If enough stock available
Else
Begin
Begin Tran
-- First reduce the quantity available
Update tblProduct set QtyAvailable = (QtyAvailable - @QuantityToSell)
where ProductId = @ProductId
Declare @MaxProductSalesId int
-- Calculate MAX ProductSalesId
Select @MaxProductSalesId = Case When
MAX(ProductSalesId) IS NULL
Then 0 else MAX(ProductSalesId) end
From tblProductSales
-- Increment @MaxProductSalesId by 1, so we don't get a primary key violation
Set @MaxProductSalesId = @MaxProductSalesId + 1
Insert into tblProductSales values(@MaxProductSalesId, @ProductId, @QuantityToSell)
if(@@ERROR <> 0)
Begin
Rollback Tran
Print 'Rolled Back Transaction'
End
Else
Begin
Commit Tran
Print 'Committed Transaction'
End
End
End
Note:
@@ERROR is cleared and reset on each statement execution. Check it immediately
following the statement being verified, or save it to a local variable that can be checked
later.
In the tblProduct table, we already have a record with ProductId = 2. So the insert
statement causes a primary key violation error. @@ERROR retains the error number,
as we are checking for it immediately after the statement that causes the error.
Insert into tblProduct values(2, 'Mobile Phone', 1500, 100)
if(@@ERROR <> 0)
Print 'Error Occurred'
Else
Print 'No Errors'
On the other hand, when you execute the code below, you get a message ‘No
Errors’ printed. This is because the @@ERROR is cleared and reset on each statement
execution.
Insert into tblProduct values(2, 'Mobile Phone', 1500, 100)
--At this point @@ERROR will have a NON ZERO value
Select * from tblProduct
--At this point, @@ERROR gets reset to ZERO, because of the
--select statement successfully executed
if(@@ERROR <> 0)
Print 'Error Occurred'
Else
Print 'No Errors'
In this example, we are storing the value of the @@Error function to a local variable,
which is then used later.
Declare @Error int
Insert into tblProduct values(2, 'Mobile Phone', 1500, 100)
Set @Error = @@ERROR
Select * from tblProduct
if(@Error <> 0)
Print 'Error Occurred'
Else
Print 'No Errors'
Error handling in SQL Server 2005 and later versions:
Syntax:
BEGIN TRY
{ Any set of SQL statements }
END TRY
BEGIN CATCH
[ Optional: Any set of SQL statements ]
END CATCH
[Optional: Any other SQL Statements]
Any set of SQL statements, that can possibly throw an exception are wrapped
between BEGIN TRY and END TRY blocks. If there is an exception in the TRY block,
the control immediately jumps to the CATCH block. If there is no exception CATCH
block will be skipped and the statements after the CATCH block are executed.
Errors trapped by a CATCH block are not returned to the calling application. If any
part of the error information must be returned to the application the code in the CATCH
block must do so by using RAISERROR() function.
In procedure spSellProduct, Begin Transaction and Commit Transaction statements
are wrapped between Begin Try and End Try block. If there are no errors in the code
that is enclosed in the TRY block, then COMMIT TRANSACTION gets executed and the
changes are made permanent. On the other hand, if there is an error then the control
immediately jumps to the CATCH block. In the CATCH block, we are rolling the
transaction back. So it’s much easier to handle errors with Try/Catch construct than with
@@Error system function.
Also notice that in the scope of the CATCH block, there are several system functions
that are used to retrieve more information about the error that occurred these functions
return NULL if they are executed outside the scope of the CATCH block.
TRY/CATCH cannot be used in a user-defined function.
Create Procedure spSellProduct
@ProductId int,
@QuantityToSell int
As
Begin
-- Check the stock available, for the product we want to sell
Declare @StockAvailable int
Select @StockAvailable = QtyAvailable
From tblProduct where ProductId = @ProductId
-- Throw an error to the calling application, if enough stock is not available
If(@StockAvailable< @QuantityToSell)
Begin
Raiserror('Not enough stock available',16,1)
End
-- If enough stock available
Else
Begin
Begin Try
Begin Transaction
-- First reduce the quantity available
Update tblProduct set QtyAvailable = (QtyAvailable - @QuantityToSell)
where ProductId = @ProductId
Declare @MaxProductSalesId int
-- Calculate MAX ProductSalesId
Select @MaxProductSalesId = Case When
MAX(ProductSalesId) IS NULL
Then 0 else MAX(ProductSalesId) end
From tblProductSales
--Increment @MaxProductSalesId by 1, so we don't get a primary key violation
Set @MaxProductSalesId = @MaxProductSalesId + 1
Insert into tblProductSales values(@MaxProductSalesId, @ProductId, @QuantityToSell)
Commit Transaction
End Try
Begin Catch
Rollback Transaction
Select
ERROR_NUMBER() as ErrorNumber,
ERROR_MESSAGE() as ErrorMessage,
ERROR_PROCEDURE() as ErrorProcedure,
ERROR_STATE() as ErrorState,
ERROR_SEVERITY() as ErrorSeverity,
ERROR_LINE() as ErrorLine
End Catch
End
End
How to handle errors in SQL Server?
From SQL Server 2005 we are provided with a structure error handling mechanism with
the help of TRY and CATCH blocks which should be used as follows
Syntax:
BEGIN TRY
--statements which will cause the error
--statements which are related to the error that should not execute when the error
occurred
END TRY
BEGIN CATCH
--statements which should be executed when the error occurs
END CATCH
To overcome the problem we faced in the dividing procedure rewrite the procedure as
follows.
Example 1: Create a procedure for dividing 2 variables values by using TRY
CATCH implementation with user-defined error statements.
CREATE PROCEDURE SPDIVIDE(@X INT, @Y INT)
AS
BEGIN
DECLARE @Z INT
SET @Z = 0
BEGIN TRY
SET @Z = @X / @Y
PRINT 'RESULT IS :- '+ CAST(@Z AS CHAR)
END TRY
BEGIN CATCH
PRINT 'SECOND NUMBER SHOULD NOT BE ZERO'
END CATCH
END
EXEC SPDIVIDE 10, 2
OUTPUT: RESULT IS:- 5
EXEC SPDIVIDE 10, 0
OUTPUT: SECOND NUMBER SHOULD NOT BE ZERO
When we execute with correct values error will not occur in the program so after
executing all the statements in the try block control directly jumps to the statements
present after catch block without executing the catch block.
If any error occurs in the execution process in such case from the line where the error
got occurred in the try block, control directly jumps to the catch block. So rest of the
statement in try will not execute whereas catch block will execute.
Note: In our above program when the error got occurred we are displaying an error
message “SECOND NUMBER SHOULD NOT BE ZERO”. In place of that error message
we can also display the original error message associated with that data by calling a
function “Error_Message”. To test this rewriting the code inside the catch block as
following
Print Error_Message()
Example 2: Create a procedure for dividing two variables values by using try catch
implementation with system defined error statements.
CREATE PROCEDURE SPDIVIDE(@X INT, @Y INT)
AS
BEGIN
DECLARE @Z INT
BEGIN TRY
SET @Z = @X / @Y
PRINT 'RESULT IS :-' + CAST(@Z ASCHAR)
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
END CATCH
END
EXEC SPDIVIDE 10, 0
OUTPUT: Divide by zero error encountered.
What is ERROR_MESSAGE()?
This method is used to display what type of error has occurred in the try block.
PREDEFINED ERRORS:
Whenever an error occurs under a program like dividing a number by zero, violation of
primary key, violation of Check constraint etc, the system displays an error message
telling us the problem encountered in the code.
Every error that occurs in the program is associated with four attributes
1. Error Number
2. Error Message
3. SEVERITY Level
4. State
Example: Message 8134 (Error Number), Level 16(SEVERITY Level), State 1 (state),
Divide by Zero error encountered (Error Message)
ERROR NUMBER: Error number is a unique identification given for each and every
error that occurs in the program. This value will be below 50,000 for predefined errors
and must be above 50,000 for error defined by the user.
ERROR MESSAGE: It is a brief information describing the error occurred which should
be maxed from 2047 characters.
SEVERITY LEVEL: This tells about the importance of the error which can be ranging
between 0 to 24. In which
0 to 9: are not serves which can be considered as information or status messages.
11 to 16: Indicates these errors can be created by the user.
17 to 19: Indicates these are software errors cannot be corrected by the user must be
reported to the system administrator.
20 to 24: Indicates fatal errors and if these errors occur they can damage the system or
database. So here the connection immediately terminates with the database.
STATE: It is an arbitrary value which is not that important can be ranging from 0 to 127.
We use this whenever the same error has to occur in multiple places.
Note: We can find the information of all predefined errors under the table “Sys
Messages”
How to raise errors explicitly in a program?
Generally, errors raised in a program on predefined reasons like dividing a number by
zero, violation of primary key, violation of check, violation of referential integrity
etc. Whereas if required we can also raise an error in our programs in two different ways
1. Using Raiserror statement
2. Using a throw statement (new feature of SQL Server 2012)
Syntax: Raiserror
Raiserror (errorid/errormsg, SEVERITY, state)[with log]
Syntax: throw
Throw errorid, errormsg, state
What is the difference between the RAISERROR function and throw
statement?
If we use any of the two statements in a program for raising an error without try and
catch blocks, RAISERROR statement after raising the error will still continue the
execution of the program whereas throw statement will terminate the program
abnormally on that line. But if they are used under try block both will behave in the same
way that is will jump directly to catch block from where the error got raised.
RAISERROR statement will give an option of specifying the SEVERITY of the error
message whereas we don’t have these option in case of throw statement where all error
message will have a default SEVERITY of 16.
In case of RAISERROR, there is a chance of recording the error message into the
server log file by using the with log option whereas we cannot do this in case of throw
statement.
In case of throw, we need to specify both errorid and error message to raise the error
whereas in case of RAISERROR we can specify either id or message. If the id is not
specified default error id is 50000 but if we want to specify only error id first we need to
add the error message in the sysmessage table by specifying a unique id to the table.
Example: Write a Procedure for dividing two numbers and raise an error in the
program if the division is 1 by using the RAISERROR statement.
CREATE PROCEDURE DIVIDEBYONE(@X INT, @Y INT)
AS
BEGIN
DECLARE @Z INT
SET @Z = 0
BEGIN TRY
IF @Y = 1
RAISERROR ('DIVISOR CANNOT BE ONE', 16, 1)
SET @Z = @X / @Y
PRINT'THE RESULT IS: '+CAST(@Z AS VARCHAR)
END TRY
BEGIN CATCH
PRINT ERROR_NUMBER()
PRINT ERROR_MESSAGE()
PRINT ERROR_SEVERITY()
PRINT ERROR_STATE()
END CATCH
END
EXEC DIVIDEBYONE 100, 1
The above procedure can also be defined with the help of a throw statement in place of
Raiserror as following.
ALTER PROCEDURE DIVIDEBYONE(@X INT, @Y INT)
AS
BEGIN
DECLARE @Z INT
SET @Z = 0
BEGIN TRY
IF @Y = 1
THROW 50000,'DIVISOR CANNOT BE ONE', 1
SET @Z = @X / @Y
PRINT'THE RESULT IS: '+CAST(@Z AS VARCHAR)
END TRY
BEGIN CATCH
PRINT ERROR_NUMBER()
PRINT ERROR_MESSAGE()
PRINT ERROR_SEVERITY()
PRINT ERROR_STATE()
END CATCH
END
EXEC DIVIDEBYONE 100, 1
What is @@ERROR?
The @@ERROR automatic variable returns the error code of the last Transact-SQL
statement. If there was no error, @@ERROR returns zero. Because @@ERROR is
reset after each Transact-SQL statement, it must be saved to a variable if it is needed to
process it further after checking it.
Stored procedures report errors to client applications via the RAISERROR command.
RAISERROR doesn’t change the flow of a procedure; it merely displays an error
message, sets the @@ERROR automatic variable, and optionally writes the message to
the SQL Server error log and the NT application event log.
How to get @@error and @@rowcount at the same time?
If @@Rowcount is checked after Error checking statement then it will have 0 as the
value of @@Recordcount as it would have been reset.
And if @@Recordcount is checked before the error-checking statement then @@Error
would get reset. To get @@error and @@rowcount at the same time do both in the
same statement and store them in the local variable. SELECT @RC =
@@ROWCOUNT, @ER = @@ERROR
What is SubProgram?
A subprogram in SQL Server is a named block of code that is directly saved on the
database server. Then we can execute this subprogram when and where it is
required. Under the databases, a subprogram is reported as a “stored procedure” or
“stored function” or “database triggers”.
What is a stored procedure or procedure in SQL Server?
In a database management system (DBMS), a stored procedure is a precompiled set of
Structured Query Language (SQL) statements with an assigned name and directly
saved in the database.
The stored procedure in SQL Server can accept both input and output parameters so
that a single stored procedure can be used by several clients over the network by using
different input data. The stored procedure will reduce network traffic and increase
performance. If we modify the body of the stored procedure then all the clients who are
using the stored procedure will get the updated stored procedure.
Why we need the stored procedure?
This is one of the most frequently asked SQL Server Stored Procedure Interview
Questions in the SQL Server Interview. So let’s discuss this question in details
Whenever we want to execute a SQL query from an application the SQL query
(statement) what we send from an application will be first compiled(parsed) for
execution, where the process of compiling(parsing) is time-consuming because
compilation occurs each time when we execute the query or statements.
To overcome the above problem, we write SQL statements or query under the stored
procedure and execute because a stored procedure is a pre-compiled block of code,
without compiling(parsing) the statements get executed whenever the procedures are
called which can increase the performance of database server which ultimately
increases the performance of the application.
If we have a situation where we write the same query again and again, we can save that
specific query as a stored procedure and call it’s just by its name whenever required that
query to execute.
What is the output parameter?
The Parameters of a stored procedure can be of two types
1. Input parameters
2. Output parameters
The Input parameters are basically used for bringing value into the procedure for
execution whereas the output parameters are used for carrying a value out of the
procedure after execution.
If a parameter is declared as output, we only require assigning a value to the parameter
inside the procedure so that the procedure will send that value at the end of procedure
execution.
What are the stored procedure status variables?
Whenever we execute a stored procedure in SQL Server, it always returns an integer
status variable indicating the status, usually, zero indicates success, and non-zero
indicates the failure. To see this yourself, execute any stored procedure from the object
explorer, in SQL Server management studio.
1. Right Click and select ‘Execute Stored Procedure
2. If the procedure, expects parameters, provide the values and click OK
3. Along with the result that you expect, the stored procedure also returns a Return
Value = 0
So, from this point, we understood that, when a stored procedure is executed, it returns
an integer status variable. With this in mind, let’s understand the difference between the
output parameters and RETURN values in SQL Server stored procedure.
We are going to use the following Employee table to understand the Stored Procedure
Output Parameters and Return values in SQL Server.
Instead of using the INNER JOIN keyword we can just use the JOIN keyword as shown
below. JOIN or INNER JOIN means the same.
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyId,
Comp.CompanyName
FROM Candidate Cand
JOIN Company Comp
ON Cand.CompanyId = Comp.CompanyId
What is the Left Outer Join in SQL Server? Explain with an example?
Let us understand Left join in SQL Server with an example. If we want to select all the
rows from the LEFT table (In our example Candidate Table) including the rows that have
a null foreign key value (CompanyId in Candidate Table is the foreign key ) then we
use LEFT OUTER JOIN. A query involving a LEFT OUTER JOIN for the Candidate and
Company Table is shown below.
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyId,
Comp.CompanyName
FROM Candidate Cand
LEFT OUTER JOIN Company Comp
ON Cand.CompanyId = Comp.CompanyId
If we run the above query the output will be as shown below. If we look at the output, we
now got all 7 rows (all the rows from the Candidate Table) including the row that has a
null value for the CompanyId column in the Candidate Table. So, the LEFT OUTER
JOIN would get all the rows from the LEFT Table including the rows that have null
foreign key value.
Instead of using the LEFT OUTER JOIN keyword we can just use the LEFT JOIN
keyword as shown below. LEFT OUTER JOIN or LEFT JOIN means the same.
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyId,
Comp.CompanyName
FROM Candidate Cand
LEFT JOIN Company Comp
ON Cand.CompanyId = Comp.CompanyId
What is the Right Outer Join in SQL Server? Explain with an example?
Let us understand Right Outer join in SQL Server with an example. If we want to
select all the rows from the LEFT Table (In our example Candidate Table) that have non-
null foreign key values plus all the rows from the RIGHT table (In our
example Company Table) including the rows that are not referenced in the LEFT Table,
then we use RIGHT OUTER JOIN. A query involving a RIGHT OUTER JOIN for
the Candidate and Company Table is shown below.
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyId,
Comp.CompanyName
FROM Candidate Cand
RIGHT OUTER JOIN Company Comp
ON Cand.CompanyId = Comp.CompanyId
If we run the above query the output will be as shown below. If we look at the output, we
now got 6 rows. All the rows from the Candidate Table that has non-null foreign key
value plus all the rows from the Company Table including the row that is not referenced
in the Candidate Table.
Instead of using the RIGHT OUTER JOIN keyword we can just use the RIGHT JOIN
keyword as shown below. RIGHT OUTER JOIN or RIGHT JOIN means the same.
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyId,
Comp.CompanyName
FROM Candidate Cand
RIGHT JOIN Company Comp
ON Cand.CompanyId = Comp.CompanyId
What is Full Outer Join in SQL Server? Explain with an example?
Let us understand Full Outer join in SQL Server with an example. If we want to select all
the rows from the LEFT Table (In our example Candidate Table) plus all the rows from
the RIGHT table (In our example Company Table), then we use FULL OUTER JOIN. A
query involving a FULL OUTER JOIN for the Candidate and Company Table is shown
below.
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyId,
Comp.CompanyName
FROM Candidate Cand
FULL OUTER JOIN Company Comp
ON Cand.CompanyId = Comp.CompanyId
If we run the above query the output will be as shown below. If you look at the output, we
now got 8 rows. All the rows from the Candidate Table and all the rows from
the Company Table.