SQL Interview Questions 2.Docx
SQL Interview Questions 2.Docx
Unique Key
1. Enforces the uniqueness of the column in a table.
2.Default non-clustered index.
3.Allows one null value
6) Define the following keys:
Candidate key, Alternate key, Composite key.
1.Candidate key –Key which can uniquely identify a row in the table.
2. Alternate key –If the table has more than one candidate key and
when one becomes a primary key the rest becomes alternate keys.
3. Composite key –More than one key uniquely identifies a row in a
table.
7) What are defaults? Is there a column to which a default can’t be
bound?
It is a value that will be used by a column if no value is supplied to
that column while inserting data.
I can’t be assigned for identity and timestamp values.
8) What are user-defined data types and when you should go for
them?
Lets you extend the base SQL Server data types by providing a
descriptive name and format to the database
E.g. Flight_num appears in many tables and all these tables have
varchar(8)
Create a user-defined data-type
10) What part does database design have to play in the performance
of a SQL Server-based application?
It plays a very major part. When building a new system, or adding to
an existing system, the design must be correct. Ensuring that the
correct data is captured and is placed in the appropriate tables, that
the right relationships exist between the tables, and that data
redundancy is eliminated is an ultimate goal when considering
performance. Planning a design should be an iterative process, and
constantly reviewed as an application is developed.
Types of cursors:
Disadvantages of cursors:
Each time you fetch a row from the cursor, it results in a network
roundtrip, whereas a normal SELECT query makes only one roundtrip,
however large the result set is. Cursors are also costly because they
require more resources and temporary storage (which results in
more IO operations). Further, there are restrictions on the SELECT
statements that can be used with some types of cursors.
Most of the time set-based operations can be used instead of
cursors.
Here is an example:
If you have to give a flat hike to your employees using the following
criteria:
14) Write down the general syntax for a SELECT statement covering
all the options.
Here’s the basic syntax: (Also checkout SELECT in books online for
advanced syntax).
SELECT select_list
[HDEV:INTO new_table_]
FROM table_source
[HDEV:WHERE search_condition]
[HDEV:GROUP BY group_by_expression]
[HDEV:HAVING search_condition]
[ORDER BY order_expression [ASC | HDEV:DESC] ]
15) What is a Join? Explain Different Types of Joins
Joins are used in queries to explain how different tables are related.
Joins also let you select data from a table depending upon data from
another table.
Types of joins: INNER JOINs, OUTER JOINs, CROSS JOINs. OUTER
JOINs are further classified as LEFT OUTER JOINS, RIGHT OUTER
JOINS, and FULL OUTER JOINS.
18) What is the system function to get the current user’s user id?
USER_ID(). Also check out other system functions like USER_NAME(),
SYSTEM_USER, SESSION_USER, CURRENT_USER, USER, SUSER_SID(),
HOST_NAME().
19) What are triggers? How many triggers you can have on a table?
How to invoke a trigger on demand?
Triggers are special kinds of stored procedures that get executed
automatically when an INSERT, UPDATE or DELETE operation takes
place on a table. In SQL Server 6.5 you could define only 3 triggers
per table, one for INSERT, one for UPDATE, and one for DELETE. From
SQL Server 7.0 onwards, this restriction is gone, and you could create
multiple triggers per each action. But in 7.0 there’s no way to control
the order in which the triggers fire. In SQL Server 2000 you could
specify which trigger fires first or fires last using sp_settriggerorder.
29) What do you consider are the best reasons to use stored
procedures in your application instead of passing Transact-SQL code
directly to SQL Server?
First and foremost, a stored procedure is a compiled set of code,
where passing T-SQL through languages such as VB, Visual FoxPro,
etc., means that the set of code needs to be compiled first. Although
T-SQL within VB, etc., can be prepared before running, this is still
slower than using a stored procedure. Then, of course, there is the
security aspect, where, by building a stored procedure, you can place
a great deal of security around it.
When dealing with sensitive data, you can use an encrypted stored
procedure to hide sensitive columns, calculations, and so on. Finally,
by using a stored procedure, I feel that transactional processing
becomes a great deal easier and, in fact, using nested transactions
becomes more insular and secure. Having to deal with transactions
within code that may have front-end code, will slow up a transaction,
and therefore a lock will be held for longer than necessary.
But what you may find tends to help, is to break down the code and
try to determine which join it is that is causing the performance
problem. Then analyze this specific join and see why it is a problem.
Always check out a stored procedure’s performance as you build it up
by using the SHOWPLAN commands.
The main area of your focus should be, is there an alternative way of
doing things? Even if I have to break this down into several chunks of
work, can I do this work without using cursors, and so result in faster
performance. Another area that you can look at is the use of CASE
statements within your query.
By using a CASE statement, you can check the value within a column
and make decisions and operations based on what you have found.
Although you will still be working on a whole set of data, rather than
a subset found in a cursor, you can use CASE to leave values, or
records as they are, if they do not meet the right criteria. Care should
be taken here though, to make sure that by looking at all the data,
you will not be creating a large performance impact.
33) If you have no choice but to use a SQL Server-based cursor, what
tips do you have to optimize them?
Perhaps the best performance gain is when you can create a cursor
asynchronously rather than needing the whole population operation
to be completed before further processing can continue. Then, by
checking specific global variables settings, you can tell when there is
no further processing to take place. However, even here, care has to
be taken. The asynchronous population should only occur on large
record sets rather than those that only deal with a small number of
rows.
Use the smallest set of data possible. Break out of the cursor loop as
soon as you can. If you find that a problem has occurred, or
processing has ended before the full cursor has been processed, then
exit. If you are using the same cursor more than once in a batch of
work, and this could mean within more than one stored procedure,
then define the cursor as a global cursor by using the GLOBAL
keyword, and not closing or deallocating the cursor until the whole
process is finished. A fair amount of time will be saved, as the cursor
and the data contained will already be defined, ready for you to use.
34) What are the steps you will take to improve the performance of a
poor-performing query?
This is a very open-ended question and there could be a lot of
reasons behind the poor performance of a query. But some general
issues that you could talk about would be:
No indexes
No Table scans
Missing or out of date statistics
Blocking
Excess recompilations of stored procedures
35) What is an ER Diagram?
An ER diagram or Entity-Relationship diagram is a special picture
used to represent the requirements and assumptions in a system
from a top-down perspective. It shows the relations between entities
(tables) in a database.
Unique key – can have more than one per table. It can have null
values. It cannot have repeating values. Maximum of 999 clustered
indexes per table.
61) What do you mean by CTEs? How will you use it?
CTEs also is known as common table expressions are used to create a
temporary table that will only exist for the duration of a query. They
are used to create a temporary table whose content you can
reference to simplify a query structure.
63) What would the command: DENY CREATE TABLE TO Peter do?
It wouldn’t allow the user Peter to operate CREATE TABLE regardless
of his role.
72) What are before images, after images, undo activities, and redo
activities in relation to transactions?
Before images refer to the changes that are rolled back on if a
transaction is rolled back. After images are used to roll forward and
enforce a transaction. Using the before images are called the undo
activity. Using after images are called the redo activity.
Explore SQL Server Sample Resumes! Download & Edit, Get Noticed
by Top Employers!
73) What are shared, exclusive, and updated locks?
A shared lock locks a row so that it can only be read. An exclusive lock
locks a row so that only one operation can be performed on it at a
time. An update lock basically has the ability to convert a shared lock
into an exclusive lock.
Department:
Department_No Department_Name
10 ECE
20 ECE
30 CSE
40 IT
Employee Details:
Dept_No Depat_Name
10 EEE
20 EEE
30 CSE
Null Null
50 IT
96) Write a Query to display only left records?
SELECT e.*
FROM Employee E
LEFT OUTER JOIN Department D
ON E.D_no
WHERE D.D_No IS NULL
97) Write a Query to display employee details where employee no is
101?
SELECT *
FROM Employee E
WHERE E_No=101
98) Write a Query to display employee details where the employee
number is null?
SELECT *
FROM Employee E
WHERE E_No IS NULL
99) Write a Query to display only the right records?
SELECT D.*
FROM Employee E
RIGHT OUTER JOIN Department D
ON E.D.No=D.D_No
WHERE E.D_No IS NULL
100) Write a Query to display all the records from the table except
matching records?
SELECT E.*, D.*
FROM Employee E
FULL JOIN Department D
ON E.D_No=D.D_No
WHERE E.D_No IS NULL or D.D_No IS NULL
Department Details Table
Dept_No Dept_Name
1 ECE
2 CSE
3 EEE
Course Details Table
Course_ID Course_Name Cr
1 EDC 4
2 PDC 4
3 SS 4
4 DAA 4
5 OS 4
Student Details Table
Student_No Student_Name
101 Anil
102 Sunil
103 Ajay
104 Vijay
105 John
Enroll Details Table
Emp_No Address
E1 Hyderabad
E2 Vizag
E3 Hyderabad
E4 Bangalore
E5 Hyderabad
Employee Details Table
Emp_No Emp_Name
E1 Arun
E2 Kiran
E3 Kumar
E4 Anus
E5 James
Semester Details Table
Semester Sn
S1 1
S2 2-1
S3 2-2
S4 3-1
S5 3-2
S6 4-1
S7 4-2
Course Department Details
Dept_No Course_ID
10 1
10 2
10 3
20 4
20 5
Syllabus Table
Emp_No Dept_No
E1 10
E2 10
E3 10
E4 20
E5 30
Course Instructor Table
111) Write a Query to display employee details whose salary > 20000
and whose age >23?
SELECT * FROM Employee
WHERE Salary>20000 AND Age>23;
112) Write a Query to display employee details whose salary >20000
and who is working in the ECE department?
SELECT * FROM Employee
WHERE Salary>20000 AND Dept_Name=’ECE’
113) Write a Query to display employee details whose age is
BETWEEN 18 and 22?
SELECT * FROM Employee Details
WHERE Age BETWEEN 18 AND 22;
114) Write a Query to display employee details whose salary range
BETWEEN 20000 and 23000?
SELECT * FROM Employee
WHERE Salary BETWEEN 20000 AND 23000;
115) Write a Query to display employee details whose age is NOT
BETWEEN 18 & 22?
SELECT * FROM Employee
WHERE Age NOT BETWEEN 18 AND 22;
Using String Operators:: LIKE, NOT LIKE