SQL - Questions - Answers V3
SQL - Questions - Answers V3
1. What are different types of SQL Commands?Or Explain DDL, DML, TCL Commands.
- There are different commands which we use to communicate with the database
to perform specific tasks.
Data Definition Language (DDL) - These SQL commands are used for creating,
modifying, and dropping the structure of database objects. The commands are
CREATE, ALTER, DROP, RENAME, and TRUNCATE.
Data Manipulation Language (DML) - These SQL commands are used for storing,
retrieving, modifying, and deleting data.
These Data Manipulation Language commands are: SELECT, INSERT, UPDATE,
and DELETE.
Transaction Control Language (TCL) - These SQL commands are used for
managing changes affecting the data. These commands are COMMIT,
ROLLBACK, and SAVEPOINT.
Data Control Language (DCL) - These SQL commands are used for providing
security to database objects. These commands are GRANT and REVOKE.
[Reason why Truncate is faster ? :When you type DELETE.all the data get copied into the Rollback Tablespace
first.then delete operation get performed.Thatswhy when you type ROLLBACK after deleting a table ,you can get
back the data(The system get it for you from the Rollback Tablespace).All this process take time.But when you type
TRUNCATE,it removes data directly without copying it into the Rollback Tablespace.Thatswhy TRUNCATE is
faster.Once you Truncate you cann't get back the data.]
1|Page
3. What is difference between Primary key and Unique key?
- Both unique key and Primary key are used to enforce uniqueness in the column
records.
- We can find 2 differences in both the keys :
I. We can have a single Primary across the table whereas we can have multiple
Unique Key across the table.
II. Primary Key does not allow NULL value whereas Unique key allows a single NULL
value.
Or
5. If I have a primary key foreign key relationship in two tables. Can I delete foreign
key table values if I am deleting primary key table values? If yes then how?
Employee Table
EId EName Did
1 a 2
2 b 1
3 c 1
4 d NULL
5 e 3
Department Table
Did Dname
1 HR
2 IT
3 Admin
2|Page
Delete from Department where Did = 1
What will happen on execution of this query?
- This query will give us the error of primary key foreign key relationship violation.
- Though we have option to use Cascading referential integrity where we can change
this behavior.
[If interviewer asks you to explain then we can explain the answer from Question. 4]
8. What are different way to get last generated identity column value?
- There are three ways to fetch last generated identity column value
SCOPE_IDENTITY() - returns the last identity value that is created in the same session
and in the same scope.
@@IDENTITY - returns the last identity value that is created in the same session and
across any scope.
IDENT_CURRENT('TableName') - returns the last identity value that is created for a
specific table across any session and any scope.
9. What will be the value of identity column value if all rows deleted from the table?
Will it reset to default values automatically? If No then, How to reset identity
column seed and increment value to default values?
- If all rows get deleted from table but still we able to see last generated identity value.
- We can reset this identity column value to default value by using DBCC CHECKIDENT
command.
DBCC CHECKIDENT(tblName, RESEED, 0)
3|Page
by comma.
2. The table does not contain any repeating column groups
3. Identify each record uniquely using primary key.
- A table is said to be in 2NF, if
1. The table meets all the conditions of 1NF
2. Move redundant data to a separate table
3. Create relationship between these tables using foreign keys.
- A table is said to be in 3NF, if the table
1. Meets all the conditions of 1NF and 2NF
2. Does not contain columns (attributes) that are not fully dependent upon
the primary key.
12. What all different types of joins available in SQL? Explain them.
- Basically there are 3 types of joins available in SQL.
1. Inner Join
2. Outer Join which again classified to 3 subtypes
A. Left Outer Join
B. Right Outer Join
C. Full Outer Join
3. Cross Join
Inner Join–This joins returns only the matching rows from both the tables.
Left Outer Join - This joins returns matching rows from both the tables and non
matching rows from the left table.
Right Outer Join - This joins returns matching rows from both the tables and non
matching rows from the right table.
Full Outer Join - This joins returns all the rows from both the tables as well as all non
matching rows.
Cross Join – This join returns Cartesian product of the tables involved in the join.
13. What is self join? Can you write a query using self join with one scenario?
- Joining a table with itself is called as SELF JOIN. SELF JOIN is not a different type of
JOIN. It can be classified under any type of JOIN - INNER, OUTER or CROSS Joins.
4|Page
- Output should be :
5|Page
LEFT JOIN tblEmployee M
ON E.ManagerID = M.EmployeeID
17. What is a sub query? What are its various types? Explain Correlated and Non
Correlated Sub query?
- A subquery is simply a select statement, that returns a single value and can be nested
inside a SELECT, UPDATE, INSERT, or DELETE statement.
- Subqueries are always enclosed in parenthesis and are also called as inner queries,
and the query containing the subquery is called as outer query.
- There are two types of subqueries :
1. Non Correlated SubQuery :
- Sub query is executed first and only once. The sub query results are then used by the
outer query.
- A non-correlated subquery can be executed independently of the outer query.
2. Correlated SubQuery :
- If the subquery depends on the outer query for its values, then that sub query is
called as a correlated subquery.
- Correlated subqueries get executed, once for every row that is selected by the outer
query.
- Corelated subquery, cannot be executed independently of the outer query.
6|Page
- A stored procedure is group of T-SQL (Transact SQL) statements. If you have a
situation, where you write the same query over and over again, you can save that
specific query as a stored procedure and call it just by it's name.
Advantages :
1. Execution plan retention and reusability - Stored Procedures are compiled
and their execution plan is cached and used again, when the same SP is executed
again. Although adhoc queries also create and reuse plan, the plan is reused only
when the query is textual match and the datatypes are matching with the
previous call. Any change in the datatype or you have an extra space in the query
then, a new plan is created.
19. Write SQL statement to call a Stored Procedures with Output parameter.
- Let’s say there is stored procedure spGetTotalCountOfEmployees with
@TotalCount output parameter. We can call this procedure as :
Declare @TotalEmployees int
Execute spGetTotalCountOfEmployees @TotalEmployees Output
Select @TotalEmployees
20. What is difference between Output Parameter and Return values in Stored
Procedures?
- Using return values, we can only return integers, and that too, only one integer.
- It is not possible, to return more than one value using return values, whereas output
parameters, can return any datatype and an SP can have more than one output
parameters.
- We always prefer, using output parameters, over RETURN values.
- In general, RETURN values are used to indicate success or failure of stored procedure,
especially when we are dealing with nested stored procedures. Return a value of 0,
indicates success, and any nonzero value indicates failure.
7|Page
- Convert provides more flexibility than Cast. For example, it's possible to control how
you want DateTime datatypes to be converted using styles with convert function.
- Cast is based on ANSI standard and Convert is specific to SQL Server. So, if
portability is a concern and if you want to use the script with other database
applications, use Cast().
-
22. What are deterministic and non-deterministic functions in SQL? Please list down
some non deterministic functions?
- Deterministic functions always return the same result any time they are called with a
specific set of input values and given the same state of the database.
Examples: Sum(), AVG(), Square(), Power() and Count()
- All aggregate functions are deterministic functions.
- Nondeterministic functions may return different results each time they are called
with a specific set of input values even if the database state that they access remains
the same.
Examples: GetDate() and CURRENT_TIMESTAMP, RAND()
23. What id RAND() function? What if you pass it a parameter e.g. RAND(1) ?
- Rand() function is a Non-deterministic function, but if you provide the seed value, the
function becomes deterministic, as the same value gets returned for the same seed
value.
24. What are functions in SQL? What are different types of functions? Explain.
- Functions are block of sql statement which are used to perform some computational
logic.
- There are 3 different types of functions are available in SQL :
1. Scalar Function :
Scalar functions may or may not have parameters, but always return a single
(scalar) value. The returned value can be of any data type, except text, ntext,
image, cursor, and timestamp.
8|Page
Inline Table Valued function cannot have BEGIN and END block, where as the
multi-statement function can have.
25. Write down syntax for functions ? (Note : Interviewer can ask you to write syntax
for any of these UDF’s)
- Scalar Function :
CREATE FUNCTION Function_Name(@Parameter1 DataType, @Parameter2
DataType,..@Parametern Datatype)
RETURNS Return_Datatype
AS
BEGIN
Function Body
Return Return_Datatype
END
9|Page
a stored procedure in a select statement.
3. UDF's cannot return Image, Text where as a StoredProcedure can return any
datatype.
4. In general, User Defined Functions are used for computations whereas Stored
Procedures are used for performing business logic.
5. UDF should return a value whereas Stored Procedure need not.
6. User Defined Functions accept lesser number of input parameters than Stored
Procedures. UDF can have upto 1023 input parameters whereas aStored
Procedure can have upto 21000 input parameters.
7. Temporary Tables cannot be used in a UDF where as a StoredProcedure can use
Temporary Tables.
8. UDF cannot Execute Dynamic SQL where as a Stored Procedure can execute
Dynamic SQL
9. User Defined Function does not support error handling whereas Stored
Procedure supports error handling. RAISEERROR or @@ERROR are not allowed
in UDFs.
INSERTINTO @userData
SELECT name, city FROM Employees
10 | P a g e
30. What is CTE in SQL? Can you write a syntax to create a CTE?
- A CTE is a temporary result set, that can be referenced within a SELECT,
INSERT, UPDATE, or DELETE statement, that immediately follows the CTE.
Syntax :
WITH cte_name (Column1, Column2, ..)
AS
( CTE_query )
[Note : There can be some queries asked by interviewer where he can confuse with creation of
CTE then some other query and then a query using CTE table. Please remember cte scope is
only till next immediate statement.]
31. What are differences between Temporary tables, Table Variables and CTE?
- 1. Table variable is created in the memory where as a temporary table is created in
the TempDB. But, if there is a 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
table variable faster than a temporary table.
3. You can pass table variable as parameter to functions and stored procedures,
where as you cannot do the same with temporary table.
4. A temporary table can have indexes, whereas a table variable can only have a
primary index. If speed is an issue Table variables 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 you 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.
32. What is difference between INSERT INTO and SELECT INTO statements?
- Both the statements are use to copy data into the table.
- For insert into statement it is mandatory to create the table and then fire insert into
query whereas for SELECT INTO statement table creation is not needed this query
automatic generates the table and copy the data.
Syntax for INSERT INTO :
INSERT INTO @targetTblName
SELECT col1, col2 FROM sourceTblName
- [We need to create @targetTblName with required columns before firing this query otherwise it
will going to through error.]
33. What are Indexes? Types of Indexes? Advantages and Disadvantages of Indexes?
- Indexes are used by queries to find data from tables quickly. Indexes are created on
tables and views.
- The existence of the right indexes, can drastically improve the performance of the
query. If there is no index to help the query, then the query engine, checks every row
11 | P a g e
in the table from the beginning to the end. This is called as Table Scan. Table scan is
bad for performance.
- There are 2 types indexes in SQL :
1. Clustered Index:
- A clustered index determines the physical order of data in a table. For this reason, a
table can have only one clustered index.
2.Non Clustered Index:
- The data is stored in one place, the index in another place. The index will have
pointers to the storage location of the data. Since, the nonclustered index is stored
separately from the actual data, a table can have more than one non clustered index.
Disadvantages of Indexes:
Additional Disk Space: Clustered Index does not, require any additional storage.
Every Non-Clustered index requires additional space as it is stored separately from
the table. The amount of space required will depend on the size of the table, and the
number and types of columns used in the index.
Insert Update and Delete statements can become slow: When DML (Data
Manipulation Language) statements (INSERT, UPDATE, DELETE) modifies data in a
table, the data in all the indexes also needs to be updated. Indexes can help, to
search and locate the rows, that we want to delete, but too many indexes to update
can actually hurt the performance of data modifications.
[Note : On the top of this Interviewer may ask you to write a syntax for creating an Index.]
A clustered index, always covers a query, since it contains all of the data in a table. A
composite index is an index on two or more columns. Both clustered and
nonclustered indexes can be composite indexes. To a certain extent, a composite
index, can cover a query.
35. Scenario : Interviewer may give you a table and a query and ask you for on which
column should I create a Clustered Index and Why?
Let’s say there is a Employee table with Id Column as a Primary Key :
12 | P a g e
We have below query in SP which is very slow :
SELECT Id, Name, Salary, Gender from Employee Where Salary BETWEEN 2500 AND
50000
- This query has more focus on Salary column so we should have something over Salary
column which will help this query to execute faster.
- Firstly we can go ahead and create a non clustered index on Salary Column and we
will check the results. If this works then we are good but if this fails then we have to
go with creating Clustered Index on Salary by removing Clustered Index from Primary
Key Id Column. Which will definitely helps this query to run faster.
2. Views can be used as a mechanism to implement row and column level security.
Row Level Security:
For example, I want an end user, to have access only to IT Department employees. If I
grant him access to the underlying tblEmployees and tblDepartments tables, he will
be able to see, every department employees. To achieve this, I can create a view,
which returns only IT Department employees, and grant the user access to the view
and not to the underlying table.
3. Views can be used to present only aggregated data and hide detailed data.
13 | P a g e
37. Can we update underlying base tables through View? [Tricky question]Depending
on your answer he can ask you Single/Multiple base tables?
- Yes. We can update the base tables through a view if there is single underlying base
table.
- For a view based on multiple base tables we can use instead of trigger to correctly
update the base table values.
-
38. What are Triggers? Different types of Triggers?
- A trigger is a special kind of stored procedure that automatically executes when an
event occurs in the database server.
- There are different types of triggers :
1. DML Triggers which are again divided into Instead Of Triggers and After Triggers
2. DDL Triggers
3. Logon Triggers.
DML Triggers :
- DML stands for Data Manipulation Language. INSERT, UPDATE, and DELETE
statements are DML statements. DML triggers are fired, whenever data is modified
using INSERT, UPDATE, and DELETE events.
- DML triggers can be again classified into 2 types.
1. After triggers (Sometimes called as FOR triggers)
2. Instead of triggers
After triggers, as the name says, fires after the triggering action. The INSERT, UPDATE,
and DELETE statements, causes an after trigger to fire after the respective statements
complete execution.
On other hand, as the name says, INSTEAD of triggers, fires instead of the triggering
action. The INSERT, UPDATE, and DELETE statements, can cause an INSTEAD OF
trigger to fire INSTEAD OF the respective statement execution.
DDl Triggers :
- DDL triggers fire in response to DDL events - CREATE, ALTER, and DROP (Table,
Function, Index, Stored Procedure etc...).
- DDL Triggers again classified into 2 categories :
- Database Triggers : DDL Triggers specific to database.
- Server Scoped Trigger :When you create a server scoped DDL trigger, it will fire in
response to the DDL events happening in all of the databases on that server.
Logon Triggers :
- As the name implies Logon triggers fire in response to a LOGON event. Logon triggers
fire after the authentication phase of logging in finishes, but before the user session is
actually established.
14 | P a g e
- Logon triggers can be used for
1. Tracking login activity
2. Restricting logins to SQL Server
3.Limiting the number of session for a specific user
41. Can we limit connections for a particular user? If yes then how?
- Yes. We can limit the connections for a particular user.
- We can use Logon Triggers to achieve this.
- [Note : Interviewer may extend his question by asking how to create that trigger. Remember the syntax
of creating LOGON Trigger.]
42. How Error handling done in SQL? Have you done error handling in your project?
- We use Try Catch block just like C# language to catch and handle the exceptions in
SQL. We cannot use try catch blocks in functions. If we have to through error to
calling application then we use RAISEERROR function in catch block.
- Also we specify ROLLBACK command in catch block when we are dealing with
transactions.
- RAISEERROR Function is use to throw exception directly to the calling application.
Syntax of RAISEERROR Function is
- 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.
- ErrorState is also an integer between 1 and 255. RAISERROR only generates errors
with state from 1 through 127.
43. What are transactions in SQL? What all commands used in Transaction?
15 | P a g e
- A transaction is a group of commands that change the data stored in a database. A
transaction is treated as a single unit. A transaction ensures that, either all of the
commands succeed, or none of them. If one of the commands in the transaction fails,
all of the commands fail, and any data that was modified in the database is rolled
back. In this way, transactions maintain the integrity of data in a database.
Transaction processing follows these steps:
1. Begin a transaction.
2. Process database commands.
3. Check for errors.
If errors occurred,
rollback the transaction,
else,
commit the transaction
44. What are Cursors in SQL? Can you tell me what all steps are followed while using
Cursors?
- If there is ever a need to process the rows, on a row-by-row basis, then cursors are
your choice. Cursors are very bad for performance, and should be avoided always.
Most of the time, cursors can be very easily replaced using joins.
There are different types of cursors in sql server as listed below.
1. Forward-Only
2. Static
3. Keyset
4. Dynamic
[Note : If interviewer ask where did u use cursors in your project? Ans : I have never came across
situation where I can implement cursors in my project. Again they are bad over performance.]
16 | P a g e
Rank and Dense_Rank functions
- Returns a rank starting at 1 based on the ordering of rows imposed by the ORDER BY
clause
- ORDER BY clause is required
- PARTITION BY clause is optional
- When the data is partitioned, rank is reset to 1 when the partition changes
- Difference between Rank and Dense_Rank functions
- Rank function skips ranking(s) if there is a tie where as Dense_Rank will not.
Basic guidelines followed while designing database. (Google it. Or I will provide you
the list of basic guidelines.)
Queries – Please note, below are the questions will be asked based on a given sample table.
They will not always ask you to use direct concept but you have to think of the concept how
to achieve the given output.
17 | P a g e
1st way using SubQuery :
WITH RESULT AS
(
SELECT SALARY,
DENSE_RANK() OVER (ORDER BY SALARY DESC) AS DENSERANK
FROM EMPLOYEES
)
SELECT TOP 1 SALARY
FROM RESULT
WHERE DENSERANK = N
[Please note we can use Row_Number() function over Dense_Rank() but if there are duplicates then
Dense_Rank() function gives correct output.]
49. Self Join query. Employee table will be given having EmployeeId and ManagerId
columns and ask you to fetch Employee Name and Manager Name.
18 | P a g e
- SELECT E.Name as Employee, ISNULL(M.Name,'No Manager') as Manager
FROM tblEmployee E
LEFT JOIN tblEmployee M
ON E.ManagerID = M.EmployeeID
50. Advanced join queries? i.e. Finding just left table data or just right table data or
uncommon data from both the tables.
Did Dname
1 IT
2 HR
3 ADMIN
4 NETWORK
O/P :
19 | P a g e
51. Convert rows into to the columns. (Pivot query)
52. Query using Group by and aggregate functions. For example, 2 tables Employee and
Department and they can ask you to fetch Department Name With Employee
Count, Or City column with Employee Count.
-
Write a query to fetch department name and employee count if employee are not there in
that department it should give 0 count.
selectd.DepartmentName,COUNT(e.ID)asEmployeeCount
20 | P a g e
from[dbo].[tblEmployee]erightjoin[dbo].[tblDepartment]d
one.DepartmentId=d.Id
groupbyd.DepartmentName
selectd.DepartmentName,COUNT(e.ID)asEmployeeCount
from[dbo].[tblEmployee]erightjoin[dbo].[tblDepartment]d
one.DepartmentId=d.Id
groupbyd.DepartmentName
havingd.DepartmentName='IT'
[Understand the above queries same queries can be asked with different table
like Product and ProductSales or Employee and Job Tables]
53. Find domain name from Email Id column, also find the Domain Name with
Employee Count. (Use Substring, CharIndex, Group By )
Write a query to find out total number of emails, by domain. The result of the
query should be as shown below.
Query
Select SUBSTRING(Email, CHARINDEX('@', Email) + 1,
LEN(Email) - CHARINDEX('@', Email)) as EmailDomain,
COUNT(Email) as Total
from tblEmployee1
Group By SUBSTRING(Email, CHARINDEX('@', Email) + 1,
LEN(Email) - CHARINDEX('@', Email))
Advanced Questions :
21 | P a g e
54. Table Hints in SQL
55. Isolation Level in SQL and its Types
56. Deadlocks
57. How to Handle Deadlocks
58. Profiler in SQL
59. Query Execution Plan
60. Few functions introduced in SQL 2012
22 | P a g e