You are on page 1of 11

http://nirajtiwari.

com/c-interview-question-part-2/
http://learnsqltips.blogspot.in/2012/01/introduction-to-sql-server.html
http://www.smarttestguru.com/online-test-interview-questions.aspx?id=12

http://arindamcctvaccesscontrol.blogspot.in/2014/03/difference-between-sql-server20002005.html
http://touchstone-systems.co.uk/blog/?p=74

http://www.sqlserverquest.com/p/tsql-interview-questions.html
http://www.gcreddy.com/2014/06/sql-queries.html

What is a query execution plan?


SQL Server has an optimizer that usually does a great job of optimizing code for the most
effective execution. A query execution plan is the breakdown of how the optimizer will run
(or ran) a query. There are several ways to view a query execution plan. This includes using
the Show Execution Plan option within Query Analyzer; Display Estimated Execution Plan
on the query dropdown menu; or use the SET SHOWPLAN_TEXT ON command before
running a query and capturing the execution plan event in a SQL Server Profiler trace.
what is execution plan? can u give broad explanation on this?
WHAT --> Execution Plan is a step by step method to execute the query in an efficient way.
HOW --> With the given resources, Relational Engine can generate a plan by applying various Permutations &
Combinations. But, if it try generate all it's combinations, Generation of Plan itself may consume all the time. So, it
has to cut down at some point of time. There "Heuristics" will help in avoiding to generate all the plans. So, it will
generate few plans considering that it shouldn't take too much time than the time it takes to execute the query.
Relational Engine will choose one of those generate plans whichever it feels efficient.

Database
What
What
What
What
What

is DBMS?
is RDBMS?
are the ACID properties?
is Normalization?
is D-normalization and when do we need it?

What are the different Normal Forms?

Table Design
What is Table? *
What is Data type? *
What are the different types of data types in SQL Server? *
What is the difference between Float, BIG Int and Integer? *
Explain the Decimal Data types and its uses. *
What is the use of different types of Integer data types? *
What is the difference between bit, Char and Varchar? *
What is Unicode? **
What is the difference between Varchar and Nvarchar? **
What are different date time data types? *
What are unique identifiers in SQL Server Table? **
What is check constraint in SQL Server? **
What is default constraint? *
What is Null ability? *
Explain table relationship? *
What is Primary Key? *
What is Foreign Key? *
What is composite key? *
What are Primary Key and Foreign Key Constraint? *
How to get the list of primary key and foreign key of the table? ****
Can we insert null value in primary column? *
No we cant insert null value in a single column primary key while in composite
primary key only one column can be null
What is difference between Primary Key and Unique Key? **
We can insert one null value in unique key but not in primary key.
What is Identity Column in Table? *
Syntax to check current Identity of the table? *
What is the difference between Scope of Identity on @@identity? ***
Can we change identity key values for a table or reset the identity key value. ***
What is function Ident_INCR? ***
What is times stamp data type is SQL Server? **
What is the alternative of timestamp? **

JOINS AND SELECT


What is select statement in TSQL? *
What is the Join in SQL Server? *
What are the different types of join? *
Difference between Left join and Outer Join? *
What is left outer join and right outer join? *
What is full outer join? *
What is inner join? *
What is cross join? *

Write one example for self-join? **


What is where clause? *
What are the sub queries? *
What are the nested queries in SQL Server? **
What is inline query? **
What are the aggregate functions in SQL Server? *
What is Group By? *
What is Having clause? **
What is difference between where and having clause? **
What is the table variable? **
What is the temporary table? **
What is the global temporary table? **
Difference between temporary table and table variable? **
What is the union all clause? **
What is union Clause? **
Difference between union all and union clause? **
What is the Ranking function in SQL Server? ****
Difference between rank and dense rank? ****
What is top operator in SQL Server? **
What is table sample in SQL Server? ***
What is the delete command? *
What is truncate command? **
Difference between delete and truncate? **
What are dynamic queries? ***
What is distinct clause? How it works? **
How to use Order by in Sub Queries? **
What is the Limit of small date time function? **
What is the @@error? **
What is the @@raise error? **
What is the @@row count? **
What is Date Diff function? **
What is Date Add Function? **
What is date part function? ***
What is coalesce function in SQL Server? ***
What is difference between stuff and Replace function? ***
What are the sparse column and when we use it? ****

VIEWS
What is the view in SQL Server? **
What are the updated views? ***
What are the materialized views or Indexed Views in SQL Server? ****
What is view with check option? ****
The with check option causes the where clause of the view to check the data being
inserted or updated through the view in addition to the data being retrieved. In a
sense, it makes the where clause a two-way restriction.
This option is useful when the view should limit inserts and updates with the same
restrictions applied to the where clause.
Write the Create syntax for Views. **

What is common table expression (CTE)? ***


What is difference between CTE and View? ****

Stored procedure
What is stored procedure in SQL Server? **
How to pass record set in stored procedure? ***
Is nesting possible in stored procedure? If yes How many number of times? ***
Write the create syntax for the procedure. **
How to return output parameter from stored procedure? ***
How to perform error handling in Stored procedure? ****

Function
What is the User defined functions? **
What are the difference types of UDF? **
What are difference between UDF and stored procedure? ***
Can we create a table in function? ****

Triggers
What is a trigger in SQL Server? **
What is the nesting level of triggers? **
What are the different types of triggers in SQL Server? ***

Cursors
What is the cursor in SQL server? **
What are the different types of cursor in SQL Server? ***
When to use Cursor in SQL Server? ***

Index
What is Index in SQL Server? ***
Explain different types of index in SQL Server? ***
What is filter index in SQL Server? ****
What is Covering Index? ****
What is index scan and index seek? ****
What is B+ tree index? ****
Explain the architecture of the index? ****
Explain the performance impact of index? ****
Can we create clustered index on null value column? ****
What are indexes in SQL Server? ****
How many clustered index can be created on the table? ****
How to enforce index on a query? ****

Performance tuning
What is SQL Profiler?

SQL Profiler is a tool that captures SQL Server events from the server and saves those events in
what's
known as a trace file. You can then analyze or use the trace file to troubleshoot logic or performance
problems. You can use this utility to monitor several areas of server activity, such as:

Analyzing and debugging SQL statements and stored procedures.

Monitoring slow performance.

Stress analysis.

General debugging and troubleshooting.

Fine-tuning indexes.

Auditing and reviewing security activity

Explain the use of SQL Profiler.


What is Database Tuning Advisor?
What is Execution Plan?
How to Read Execution Plan?
What are the statistics and the impact on SQL Server?
What are the table hint and how we can provide it on table?
How to read execution plan?
What are the algorithms does SQL Server used to fetch data?
What is bookmark lookup?
How do you tell SQL Server to preserve the join order in the FROM clause during
optimization?
You can use OPTION (FORCE ORDER)

Transaction
What are the different types of transaction errors?
What is No lock?
What happens if a transaction is failed in nested transaction, then other outer
transactions will execute or not?
What is isolation level in SQL Server?
What are different types of isolation level in SQL Server?
What is default isolation Level for SQL Server?

RANDOM
Write a query to find the nth minimum and maximum.
Write a query to get the last record of the table.
Write a query to get the no rows of a table without using count or any other
clause.
How many columns can be used with roll up or with cube command?
10 columns
How many columns can be selected in a select statement?
4096 columns can be selected.
How many columns can be selected in an insert statement?
4096 columns can be selected.
What level of nesting is possible for sub Queries?

32 level of nesting.
What level of nesting is possible for Triggers?
32 level of nesting.
How many non-clustered indexes can be created on a table?
Only one clustered index can be created
How many parameters can be provided to a stored procedure?
2100 parameters can be provided to a SP.
How many parameters can be provided to a UDF?
2100 parameters can be provided to a UDF.
How many columns can be taken in update statement?
4096 columns can be updated in an update statement
What is NO COUNT used for?
What is SET ANSI NULL ON used for?
What is arithmetic abort used for?
What is SET ROW COUNT used for?
What is SET ANSI padding?
What is SET ANSI WARNING on?
What is SET NO EXEC?
Which command using Query Analyzer will give you the version of SQL server and
operating system?
How to delete duplicate records from table using query?

TSQL Interview Questions: Table


A table is a set of data elements (values) that is organized using a model of vertical columns (which
are identified by their name) and horizontal rows, the cell being the unit where a row and column
intersect. A table has a specified number of columns, but can have any number of rows [citation
needed]. Each row is identified by the values appearing in a particular column subset which has been
identified as a unique key index.

TSQL Iterview Questions: Data Types


Data type: A data type is an attribute that specifies the type of data that the object can store:
numeric data, character data, monetary data, date and time data, binary data, and so many more.
SQL Server supplies a set of system data types that define all the types of data that can be used with
SQL Server.
Data type Range Storage
bigint -2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807) 8 Bytes
int -2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647) 4 Bytes
smallint -2^15 (-32,768) to 2^15-1 (32,767) 2 Bytes
tinyint 0 to 255 1 Byte

Difference between Varchar and Nvarchar

As we all know the basic difference between varchar and Nvarchar is that one holds
non Unicode and other holds Unicode data. But we are just aware theoretically and
dont know how it works actually.
Here I am showing an example of how varchar and nvarchar data types works in
query analyzer
Declare @sampleVar varchar(10)
Declare @sampleNVar nvarchar(10)
set @sampleVar = 'VarSamp '
set @sampleNVar = 'NVarSamp '
Select @sampleNVar, @sampleVar
set @sampleNVar = N'NVarSamp '
set @sampleVar = N'VarSamp '
Select @sampleNVar, @sampleVar

If you will see we have added the suffix N while setting the Unicode data type and
its displays the results otherwise it shows the same result as varchar data type. By
seeing this example we can say in Unicode data types can be handled through
nvarchar so while developing bilingual application or if there is any possibility to use
current application in different languages we should use Nvarchar data types.

Keywords: How to read nvarchar data in SQL Query Analyzer, SQL Server Data
types, SQL Server Questions, Varchar ,Nvarchar.

TSQL Interview Questions: DateTime


There are two types of datetime data types

Datetime
Stores dates from January 1, 1753 through December 31, 9999, to an accuracy of one threehundredth of a second (equivalent to 3.33 milliseconds or 0.00333 seconds). Values are rounded to
increments of .000, .003, or .007 seconds, as shown in the table.

Small Datetime
Stores dates from January 1, 1900, through June 6, 2079, with accuracy to the minute. smalldatetime
values with 29.998 seconds or lower are rounded down to the nearest minute; values with 29.999
seconds or higher are rounded up to the nearest minute.
Internally SQL server stores Datetime data in 2 4 bytes integers and smalldatetime stores as 2 2 byte
integers. One part stores days before or after base date and next part stores the number of
milliseconds after midnight.

How to Get the Primary Key and foreign Key between two
tables
These are the few tables which can be used to check the foreign key and
primary key column in a table.
It will give both columns from both tables.
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
INFORMATION_SCHEMA.TABLE_CONSTRAINTS
INFORMATION_SCHEMA.KEY_COLUMN_USAGE

Can we insert null value in primary column? *


No we cant insert null value in a single column primary key while in composite
primary key only one column can be null
What is difference between Primary Key and Unique Key? **
We can insert one null value in unique key but not in primary key.

TSQL Interview Question: Identity column


Identity is the property of column which inserts incremental value in the column at the insert of new row in
parent table of that column.
For example in Employee table when we set ID as identity then for every inserted employee record there
will be an auto incremental value get inserted in the ID column automatically.
For Identity you can set the increment value i.e. the value by which next inserted value increment. So if
you set increment by 10 then inserted value will be like 10, 20, 30, 40..

These properties can be set in for table while designing it or opening existing table in design mode.

Identity key doesn't guarantee

Uniqueness of the value

Consecutive values within a transaction If there are multiple transaction then the identity value
for every row can be differ in a single transaction.

Reuse of values if there is error while inserting row that the identity generated will lost and next
time SQL server will insert new identity value. It doesn't reuse it.
The seed value is the value inserted into an identity column for the very first row loaded into the table.

How to Check Identity Value


DBCC CHECKIDENT
(
table_name
[, { NORESEED | { RESEED [, new_reseed_value ] } } ]
)

DBCC CHECKIDENT (table)


DBCC CHECKIDENT (table, NORESEED)
DBCC CHECKIDENT (table, RESEED, 10)

SCOPE_IDENTITY, IDENT_CURRENT, and @@IDENTITY


SCOPE_IDENTITY, IDENT_CURRENT, and @@IDENTITY are function used for same value type but
different results.
IDENT_CURRENT returns the current identity value irrespective of session and scope.
SCOPE_IDENTITY returns last identity value generated the current scope Irrespective of tables. I.e. it
could be value from last table which is updated in current scope.

@@IDENTITY returns the last identity values that are generated in any table in the current session. Its
not limited to any specific scope. I.e. while updating in current scope the trigger on this table insert data in
some other table
For example, there are two tables, Table1 and Table2, and an INSERT trigger is defined on Table1.
When a row is inserted to Table1, the trigger inserts a row in Table2. This scenario illustrates two scopes:
the insert on Table1 and the insert on Table2 by the trigger.
Table1 and Table2 have identity columns, @@IDENTITY and SCOPE_IDENTITY will return different
values at the end of an INSERT statement on Table1.
@@IDENTITY will return the last identity column value inserted across any scope in the current session.
This is the value inserted in Table2.
SCOPE_IDENTITY () will return the IDENTITY value inserted in Table1.

JOINS AND SELECT

Sql Server
10 Complex Sql Query on basis of Sql
Interview Question between 1 to 5 year
work experience.
1. Sql Query to find second highest salary of Employee.
select MAX(Salary) from Employee WHERE Salary NOT IN (select MAX(Salary)
from Employee );

2. SQL Query to find Max Salary from each department.


SELECT DeptID, MAX(Salary) FROM Employee

GROUP BY DeptID.

3.Write a SQL Query to print the name of distinct employee whose DOB
is between 01/01/1960 to 31/12/1975.
SELECT DISTINCT EmpName FROM
31/12/1975;

[adsenseyu1]

Employees WHERE DOB

BETWEEN 01/01/1960 AND

4. Write an SQL Query find number of employees according


to gender whose DOB is between 01/01/1960 to 31/12/1975.
SELECT COUNT(*), sex from Employees
31/12/1975 GROUP BY sex

WHERE

DOB BETWEEN 01/01/1960 ' AND

5.Write an SQL Query to find employee whose Salary is equal or


greater than 10000.
SELECT EmpName FROM

Employees WHERE

Salary>=10000;

6. Write an SQL Query to find name of employee whose name Start with
M
SELECT * FROM Employees WHERE EmpName like 'M%';

7. Select all record from emp table where deptno =10 or 40.
select * from emp where deptno=30 or deptno=10;

8. How to create a clone of another table


Create table emp1 as select * from emp where 1=2;

9.How can I retrive all records of emp1 those should not present in
emp2?
Select * from emp) Minus (Select * from emp1

10.How to fetch only common records from two tables emp and emp1?
(Select * from emp) Intersect (Select * from emp1)

Why we need to create an index?


You can say, we use index "TO AVOID FULL TABLE SCAN"...Index holds addresses of all the records of the table. So
it goes there and picks up that record. Like this Performance improves.

You might also like