SQL Interview Question-Answer
SQL Interview Question-Answer
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. (i.e. affects on schema or structure)
These commands are CREATE, ALTER, DROP, RENAME, and TRUNCATE.
Data Manipulation Language (DML) –
These SQL commands are used for storing, retrieving, modifying, and deleting data.
(i.e. affects on data)
These 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.
Data Control Language (DCL) –
These SQL commands are used for providing security to database objects.
These commands are GRANT and REVOKE.
Truncate
- TRUCATE is a DDL command and it is faster.
- TRUNCATE removes all rows from a table.
- The operation cannot be rolled back and no triggers will be fired.
- Truncate command also reset identity.
Drop
- Drop is a DDL command.
- The DROP command removes a table from the database.
- All the tables' rows, indexes and privileges will also be removed.
- No DML triggers will be fired.
- The Drop operation cannot be rolled back.
[Reason why Truncate is faster ? :When you type DELETE, all the data get copied into the Rollback
Tablespace first then delete operation get performed. That’s why 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. That’s why TRUNCATE is faster. Once you Truncate you can't get back the
data.]
7. 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('tblName') –
Returns the last identity value that is created for a specific table across any session and any
scope.
8. 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.
9. What is Normalization in SQL? Why do we need of Normalization? What are different forms of
Normalization? Explain 1st , 2nd and 3rd Normal form.
- Database normalization is the step by step process to design a better database.
- Remove duplication (redundancy)
1NF
1. The data in each column should be atomic. (There should not be comma separated values).
2. The table does not contain any repeating column groups.
3. Every record should identify uniquely (i.e. every record should have primary key).
2NF
1. The table satisfy all the conditions of 1NF.
2. Identify groups and split into multiple tables.
3. Create relationship between these tables using foreign keys.
3NF
1. The table satisfy all the conditions of 1NF and 2NF.
2. Remove all columns (attributes) that are not fully dependent upon the primary key.
11. 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 –
Inner joins returns only the matching rows from both the tables (i.e. Common records from both
tables).
Cross Join –
Cross join returns Cartesian product of the tables involved in the join.
12. 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.
- Output should be :
- Or
SELECT E.Name as Employee, CASE WHEN M.Name IS NULL THEN 'No Manager'
ELSE M.Name END as Manager
FROM tblEmployee E
LEFT JOIN tblEmployee M
ON E.ManagerID = M.EmployeeID
- Or
16. 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 :
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.
2. Reduces network traffic - You only need to send, EXECUTE SP_Name statement, over the
network, instead of the entire batch of adhoc SQL code.
3. Code reusability and better maintainability - A stored procedure can be reused with
multiple applications. If the logic has to change, we only have one place to change, where as
if it is inline sql, and if you have to use it in multiple applications, we end up with multiple
copies of this inline sql. If the logic has to change, we have to change at all the places, which
makes it harder maintaining inline sql.
4. Better Security - A database user can be granted access to an SP and prevent them from
executing direct "select" statements against a table. This is fine grain access control which
will help control what data a user has access to.
18. Write SQL statement to call a Stored Procedures with Return value.
- Let’s say there is stored procedure usp_PersonIdByName .
- We can call this procedure as :
20. What is difference between Output Parameter and Return values in Stored Procedures?
- Using return values, we can only return one value of type integer.
- Whereas output parameters, can return multiple values of any type.
- 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.
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 RAND()
23. What id RAND() function? What if you pass it a parameter e.g. RAND(1) ?
- Rand() function is a Non-deterministic function i.e. every time called gives new value between
0 and 1.
- 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.
25. Write down syntax for functions ? (Note : Interviewer can ask you to write syntax for any of
these UDF’s)
- Scalar Function :
26. What is difference between Inline Table Valued Function and Multi Statement Table Valued
Function?
- 1. In an Inline Table Valued function, the RETURNS clause cannot contain the structure of the
table, the function returns. Whereas, with the multi-statement table valued function, we specify
the structure of the table that gets returned
2. Inline Table Valued function cannot have BEGIN and END block, whereas the multi-statement
function can have.
3. Inline Table valued functions are better for performance, than multi-statement table valued
functions. If the given task, can be achieved using an inline table valued function, always prefer
to use them, over multi-statement table valued functions.
4. It's possible to update the underlying table, using an inline table valued function, but not
possible using multi-statement table valued function.
27. What are differences between Stored Procedures and Functions?
- Store procedures are used for business logic (Insert, Update, Delete on Records) whereas
Functions are used for computational logic(Calculations).
- Store procedures can not call in select statement whereas Functions can call in select statement.
- We can do error handling in Store procedures whereas we can’t do error handling in Functions.
- We can implement transaction in Store procedures whereas we can’t implement transaction in
Functions.
- We can create temporary tables inside Store procedure whereas we can’t create temporary
tables inside Functions.
- We need not to write return statement in Store Procedure whereas it is compulsory to write
return statement inside Function.
- Store procedure can call Store procedure, Store procedure can call Function whereas Function
can call Function but Function can not call Store procedure.
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?
- Temporary tables can be stored in TempDB whereas Table variables can be stored in memory
but if there is a memory pressure table variables can be stored in TempDB.
- Temporary tables participates in transaction whereas Table variables does not participate in
transaction this makes table variable faster than a temporary table.
- You can not pass Temporary table as parameter whereas you can pass Table variable as
parameter to store procedure and function.
- 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.
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 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 non clustered 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 non clustered 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 :
1. Views can be used to reduce the complexity of the database schema, for non IT users. For
example the view can hides the complexity of joins. Non-IT users, finds it easy to query the
view, rather than writing complex joins.
2. Views can provide row and column level security.
3. Views can be used to present only aggregated data and hide detailed data.
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.
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.
- 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
39. What are different magical/special tables available in Triggers?
- There are 2 magical tables available while working with triggers:
1. INSERTED Table
2. DELETED Table
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 RAISERROR 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?
- 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
- We use Commit command to commit the changes permanently to database and Rollback
command to rollback the changes on any error while working with transactions.
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.]
45. What are Row_Number(), Rank(), Dense_Rank() functions?
Row_Number function
Rank function
Dense_Rank functions
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.
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 EId Ename Did
2 HR 1 a 1
3 ADMIN 2 b 1
4 NETWORK 3 c 2
4 d NULL
5 e 3
6 f 3
O/P :
Eid Ename
4 D
O/P :
- Write a sql query to transpose rows to columns. The output should be as shown
below.
Select Country, City1, City2, City3
From
(
Select Country, City,
'City'+
cast(row_number() over(partition by Country order by Country)
as varchar(10)) ColumnSequence
from Countries
) Temp
pivot
(
max(City)
for ColumnSequence in (City1, City2, City3)
) Piv
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.
[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 :