0% found this document useful (0 votes)
71 views26 pages

SQL Interview Questions With Theory Answers

The document discusses SQL interview questions and answers. It provides details about tables used for questions, frequently asked SQL concepts and queries including joins, aggregation and normalization. Example questions on JOINS, stored procedures and user defined functions are also included.

Uploaded by

saikiranm031
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views26 pages

SQL Interview Questions With Theory Answers

The document discusses SQL interview questions and answers. It provides details about tables used for questions, frequently asked SQL concepts and queries including joins, aggregation and normalization. Example questions on JOINS, stored procedures and user defined functions are also included.

Uploaded by

saikiranm031
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

SQL Interview Questions &

Answers
Palle Technologies
Updated on 22-oct-2022
all queries are based on below tables
table 1 - department table
unique key
dept_id (PK) dept_name (UK) dept_location
primary key
1 HR BTM

2 Development HSR

3 Training HSR

4 Sales Bommanahalli

5 Marketing Bommanahalli

6 MD HSR

7 Recruitment HSR

2
All queries are based on below tables
foreign key
Primary key table 2 - employee table
emp_id (PK) emp_name city emp_salary manage d_id
r_id (FK)

1 Ramesh mumbai 22000 10 1


2 Suresh bangalore 25000 11 2
3 Mahesh chennai 21000 8 3
4 Lokesh chennai 35000 5 4
5 Veena delhi 32000 9 4
6 Sravanthi bangalore 18000 11 2
7 Divya mysore 15000 9 5
8 Jona chennai 30000 9 3
9 Steve chennai 40000 Null 6
10 Nithesh delhi 35000 9 1
11 Mohan kolkata 36000 9 2
--
3
coding or query based questions
• dear students mock interviewers can ask any
questions based on the concepts being
covered as part of the course while taking
mock
• we request all students to prepare the all

concepts related queries or code properly


• most frequently asked queries or concepts are
listed in the next slide. please refer and prepare
accordingly.
• sql query for creating department table with
appropriate data types and primary key and
unique constraints
• sql query for creating employee table with
appropriate data types and primary key and
foreign key constraints

5
• question based on update query on the
employee table
• question based on delete query on the
employee table
• question based on insert query on the
employee table
• questions based on select statement with
where clause on employee table
• questions based on select statement with like
clause on employee table
• questions based on group by clause on
employee table
• questions based on having clause on
employee table
• questions based on inner joins on employee
and department table
• questions based on left outer joins on
department and employee table
• questions based on self join on employee
table
• questions based on sub queries
• questions based on co-related sub queries
and nth highest nth lowest values
1. what is the diff between char and varchar?
char is a fixed size data type & varchar is varying
size data type. wastage of memory will be more
in char type.

2. what are the diff between Primary Key &


Unique constraint?
Primary Key Unique Constraint
Only one primary key is allowed per A table can have more than one
table. unique key.

Primary key will produce clustered index Unique key will produce non-clustered
index.
Primary key will not allow null values Unique constraint will allow only one
null value.

8
3. what is normalization? and what are the
benefits?
 normalization is used for eliminating the data
redundancy. normalization will eliminate the
insert, update anomalies.

4. is it possible to store null in a foreign key


column?
Yes.
5. How to stop null value coming in foreign key
column?
By using not null constraint.
6. How many foreign keys allowed per table?
we can create any number of Foreign keys per table.
7. What are the diff between cast & convert functions?
Both CAST and CONVERT are functions used to convert
one data type to another data type.
Convert function will allow us to format date time values
based on the style. cast will not allow us to apply style.

8. what are the diff between drop and truncate statements?


• TRUNCATE TABLE will remove all data from a table.
truncate is a non logged operation. truncate is faster than
drop.
• DROP TABLE removes table definitions, data , indexes,
triggers, constraints, permissions etc..

10
8. what are the diff between drop and truncate
statements?
drop truncate

Drop will delete all data in Truncate statement will delete


table and also it will delete all data present in a table.
table along with constraints
indexes etc..
drop is not a logged operation Truncate is a non logged
operation

11
9. what are the diff between delete without
where and truncate statements?
Delete without where truncate
Delete deletes the records Truncate delete all data
from table 1 after the other present in a table at once.

delete is a logged operation Truncate is a non logged


operation
Delete is slow operation Truncate is faster

12
10. what is the purpose of group by clause?
using group by clause we can group data based on one
or more column.
11. what is the purpose of having clause?
using having clause we can filter the records which are
produced by group by clause.
12. is it possible to use aggregate function in where
clause?
we can't use an aggregate function directly in a
WHERE clause
13. write a query for displaying second highest salary
from employee table ( assuming table is having salary
and emp_id columns)
note:- query must be written without using top or
distinct clauses.

13
14. what is the purpose of joins in sql?
joins are useful for getting data from 2 or more tables into a result
set.
syntax : select col1,col2,…. from left_table alias1 join right_table
alias2 on condition ;
note : students must practice more queries based on joins for clearing
interviews.
15. what are the different types of joins supported in sql server?
1.inner join
2.outer join
1)Left Outer Join
2)Right Outer Join
3)Full Outer Join
3.Self Join
4.Cross Join

14
16. what is inner join?
Inner join is used for getting matched data from
2 or more tables based on the condition.

17. what is Left Outer Join?


In left outer join data from left table is
completely included into the result-set but only
matched data from right side table is included.
In place of right table data null’s are included
where there is no match.

9
18. What is Right Outer Join?
In right outer join data from right table is completely
included into the result-set but only matched data
from left side table is included. In place of left table
data null’s are included where there is no match.

19. What is Full Outer Join?


In case of Full Outer Join data from both the tables
are included into the result-set irrespective of the
condition. Null’s are included where ever there is no
match.

20. What is self join?


1.Joining a table to itself is called as self join.
2.we must use inner join keyword for self join.

16
21. what is cross join?
cross join will join each row present in a table with all rows
present in other table. in simple words it will produce Cartesian
product of all rows present in both the tables.

22. what is a stored procedure?


a stored procedure is a compiled query. it is almost like function in
other programming languages.
syntax : create proc proc_name
(
@param1 dt, …..
)
as
begin
-- body of sp
end

17
23. why sp execution is faster compared to a
direct t-sql statement?
when we pass a query to data base engine
first the query execution plan will be
prepared and query will be executed based
on that.
in case of sp the query execution plan will
be cached hence query will be directly
executed based on the cached plan.
24. what are the diff between a sp and udf?
• sp can return multiple result sets but an udf
can return max 1 result set.
• udf can’t have out put parameters.
• sp can call udf but udf can’t call sp.
• Udf will not support DML statements, sp will
support DML statements.

19
24. what are the diff between a sp and udf?
SP UDF

Sp can have input and output Udf can have only input parameters
parameters
Sp can return any number of result Udf can return only 1 result set
sets
Sp can call udf Udf can’t call sp

Inside sp we can write any type of t-sql Inside the udf we can’t write any
statements statements which modifies current
state of database
( ex delete statement )

20
25. what is an user defined function?
user defined function is a compiled query just
like stored procedure. but it can’t take output
parameters and can’t return multiple result sets.
26. is it possible to call one stored procedure from
other?
yes we can call one sp from other .
27. what is an index?
Indexes are used to retrieve data from the
database more quickly. we have 2 types of
index.
1. clustered index 2. non clustered index

21
28. what are the diff between clustered and
non clustered index?
CLUSTERED NON CLUSTERED

In clustered index leaf will have actual In non clustered index leaf pages will
data have pointers to actual data
Only 1 clustered index allowed per We can create multiple non clustered
table indexes per table

22
29. what is a view? explain diff types of views ?
Views are a virtual tables or a stored query
that hold data from one or more tables and it
can only contain select statements.
Types of Views :
 updatable views (using this view we can
update underlying table data)

 non updatable views ( using this view we


cannot update underlying table data )

23
30. what is a transaction?
• Transaction is a logical unit of work. Using
transaction we can group logically related
queries.
• All queries present in a transaction must be
100% successfully executed or must be 100%
rolled back.
• A transaction must support ACID properties

24
31. what are the acid properties in transactions?
every transaction must exhibit 4 properties called ACID
properties. ACID refers to Atomicity consistency isolation
and durability.
32. what is a trigger?
A trigger is a procedure in database which will be
automatically executed whenever a special event in the
database occurs like insert or update or delete operation.
33. what is the difference between union and union all?
union will not allow duplicate rows and union all will allow
duplicate rows into the result set.
34. Is it possible to debug Stored procedure?
Yes, if it is in local system we can debug by using F11 key

25
Link for tables
• Student can use the following referenced
tables ( usually mock interviews we ask
questions based on the given tables )
• https://palle-sql-tables.s3.ap-south-
1.amazonaws.com/index.html

26

You might also like