Anda di halaman 1dari 15

SQL Queries Interview Questions for Experts (5-10 Years of

experience)
Suppose, you are build a application like FACEBOOK and as a business you need to
suggest Friend by displaying Friends of Friends. So you need to build a query that
should return resultset which should have Friend Friend name. Just for reference,
this is how friend table looks like,
create table Friend
(
ProfileID uniqueidentifier,
Name varchar(50),
Friend_Name varchar(50)
)
insert Friend values (NEWid(),'RAM', 'Shyam')
insert Friend values ('FFCB96AD-0F16-4A77-B634-3CE1F697A3D1','RAM', 'Tony')
insert Friend values ('FFCB96AD-0F16-4A77-B634-3CE1F697A3D1','RAM', 'Vibha')

insert Friend values (NEWid(),'SHYAM', 'RAM')


insert Friend values ('42A6A0EC-8EE5-4611-92C7-A23B0970B463','SHYAM', 'SAM')
insert Friend values ('42A6A0EC-8EE5-4611-92C7-A23B0970B463','SHYAM', 'Vibha')
insert Friend values ('42A6A0EC-8EE5-4611-92C7-A23B0970B463','SHYAM', 'John')

insert Friend values (NEWid(),'VIBHA', 'RAM')


insert Friend values ('AC40284F-4E54-495A-BF62-9701474C44C0','VIBHA', 'SHYAM')
insert Friend values ('AC40284F-4E54-495A-BF62-9701474C44C0','VIBHA', 'George')
insert Friend values ('AC40284F-4E54-495A-BF62-9701474C44C0','VIBHA', 'TOM')

insert Friend values (NEWid(),'TOM', 'RAM')


insert Friend values ('DE86E5EC-9748-47A0-936E-0BB6BCBCA1A0','TOM', 'DNATAG')
insert Friend values ('DE86E5EC-9748-47A0-936E-0BB6BCBCA1A0','TOM', 'Reddy')

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier
New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre
{ margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color:
#0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt
{ background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color:
#606060; }

Solution

-- Query Solution
declare @user varchar(50)
set @user = 'RAM'
SELECT

f1.name,
f2.friend_name as friend_of_friend

FROM

friend f1,
friend f2

WHERE

f1.name = @user

AND

f1.friend_name = f2.name

AND

NOT EXISTS
(SELECT 1 FROM friend f3
WHERE f3.name = f1.name
AND

f3.friend_name = f2.friend_name) and f2.friend_name

<>@user

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier
New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre
{ margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color:
#0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt
{ background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color:
#606060; }

What is blocking and how would you troubleshoot it?


Blocking happens when one connection from an application holds a lock and a second
connection requires a conflicting lock type. This forces the second connection to wait,
blocked on the first.
What are the steps you will take to improve 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, table scans, missing or out of date statistics, blocking, excess recompilations of
stored procedures, procedures and triggers without SET NOCOUNT ON, poorly written query
with unnecessarily complicated joins, too much normalization, excess usage of cursors and
temporary tables.
Some of the tools/ways that help you troubleshooting performance problems are:

SET SHOWPLAN_ALL ON,

SET SHOWPLAN_TEXT ON,

SET STATISTICS IO ON,

SQL Server Profiler,

Windows NT /2000 Performance monitor,

Graphical execution plan in Query Analyzer.

You are being you being assigned a task to move 5 million rows from one server
to another using T-SQL with a linked-server. What will you consider to avoid
transaction log fill up at destination server?

Will prefer to use SET ROWCOUNT and a while loop to commit data in batches.
What is the optimal Disk configuration for a database server and what RAID
configurations would you use if budget is not a constraint?
1. RAID 1 for the OS / Applications
2. RAID 1 for the page file
3. RAID 10 for the Data file (or RAID 5 for few writes)
4. RAID 1 (or 10) for the transaction log
What is a deadlock and what is a live lock? How will you go about resolving
deadlocks?
Deadlock is a situation when two processes, each having a lock on one piece of data,
attempt to acquire a lock on the others piece. Each process would wait indefinitely for the
other to release the lock, unless one of the user processes is terminated. SQL Server detects
deadlocks and terminates one users process.
A livelock is one, where a request for an exclusive lock is repeatedly denied because a
series of overlapping shared locks keeps interfering. SQL Server detects the situation after
four denials and refuses further shared locks. A livelock also occurs when read transactions
monopolize a table or page, forcing a write transaction to wait indefinitely.
Check out SET DEADLOCK_PRIORITY and "Minimizing Deadlocks" in SQL Server books
online. Also check out the article Q169960 from Microsoft knowledge base.
What is blocking and how would you troubleshoot it?
Blocking happens when one connection from an application holds a lock and a second
connection requires a conflicting lock type. This forces the second connection to wait,
blocked on the first.
Read up the following topics in SQL Server books online: Understanding and avoiding
blocking, Coding efficient transactions.
What are statistics, under what circumstances they go out of date, how do you
update them?
Statistics determine the selectivity of the indexes. If an indexed column has unique values
then the selectivity of that index is more, as opposed to an index with non-unique values.
Query optimizer uses these indexes in determining whether to choose an index or not while
executing a query.
Some situations under which you should update statistics:

1. If there is significant change in the key values in the index


2. If a large amount of data in an indexed column has been added, changed, or
removed (that is, if the distribution of key values has changed), or the table
has been truncated using the TRUNCATE TABLE statement and then
repopulated
3. Database is upgraded from a previous version
Look up SQL Server books online for the following commands:
UPDATE STATISTICS,
STATS_DATE,
DBCC SHOW_STATISTICS,
CREATE STATISTICS,
DROP STATISTICS,
sp_autostats,
sp_createstats,
sp_updatestats
Write SQL query to find the products which have continuous increase in sales
every year considering the following Schema and tell which optimized query?
Table Structure
CREATE TABLE PRODUCTS
(
PRODUCT_ID

INTEGER,

PRODUCT_NAME

VARCHAR(30)

);
CREATE TABLE SALES
(
SALE_ID

INTEGER,

PRODUCT_ID

INTEGER,

YEAR

INTEGER,

Quantity

INTEGER,

PRICE

INTEGER

);

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier
New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre
{ margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color:
#0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt
{ background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color:
#606060; }
This table, contains the following rows,

Solution
SELECT PRODUCT_NAME
FROM
(
SELECT P.PRODUCT_NAME,
S.QUANTITY LEAD(S.QUANTITY,1,0) OVER (
PARTITION BY P.PRODUCT_ID
ORDER BY S.YEAR DESC
) QUAN_DIFF

FROM

PRODUCTS P,
SALES S

WHERE

P.PRODUCT_ID = S.PRODUCT_ID

)A
GROUP BY PRODUCT_NAME
HAVING MIN(QUAN_DIFF) >= 0;

Note This Solution is using a LEAD fn, which is available in SQL Server 2012.
Based on above mentioned tables, Write two alternative SQL query to find the
products which does not have sales at all and identify, which is more optimized.
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family:
consolas, "Courier New", courier, monospace; background-color: #ffffff; /*whitespace: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color:
#008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color:
#006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color:
#cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html
{ color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt
{ background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum
{ color: #606060; }
-- OPTION 1
SELECT P.PRODUCT_NAME
FROM

PRODUCTS P
LEFT OUTER JOIN
SALES S

ON

(P.PRODUCT_ID = S.PRODUCT_ID)

WHERE

S.QUANTITY IS NULL

-- OPTION 2

SELECT P.PRODUCT_NAME
FROM

PRODUCTS P

WHERE

P.PRODUCT_ID NOT IN
(SELECT DISTINCT PRODUCT_ID FROM SALES);

-- Option 3
SELECT P.PRODUCT_NAME
FROM

PRODUCTS P

WHERE

NOT EXISTS
(SELECT 1 FROM SALES S WHERE S.PRODUCT_ID = P.PRODUCT_ID);

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier
New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre
{ margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color:
#0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt
{ background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color:
#606060; }
Based on above mentioned tables,Write two alternatives query to select the top
product sold in each year?
-- OPTION 1
SELECT P.PRODUCT_NAME,
S.YEAR,
S.QUANTITY
FROM
(

SELECT YEAR,
MAX(QUANTITY) QUAN
FROM SALES
GROUP BY YEAR
)A, SALES S,
PRODUCTS P
WHERE A.YEAR = S.YEAR
AND A.QUAN = S.QUANTITY
AND S.PRODUCT_ID = P.PRODUCT_ID;

-- Option 2
SELECT PRODUCT_NAME,
YEAR
FROM
(
SELECT P.PRODUCT_NAME,
S.YEAR,
RANK() OVER (
PARTITION BY S.YEAR
ORDER BY S.QUANTITY DESC
) RNK
FROM

PRODUCTS P,
SALES S

WHERE

P.PRODUCT_ID = S.PRODUCT_ID

) A
WHERE RNK = 1;

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier
New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre
{ margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color:
#0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt
{ background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color:
#606060; }
OUTPUT

Based on above mentioned tables structure, Write a query to find the products
whose quantity sold in a year should be greater than the average quantity sold
across all the years??

Based on above mentioned tables structure, Write a query to compare the


products sales of "IPhone" and "Samsung" in each year?

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier
New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre
{ margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color:
#0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt
{ background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color:
#606060; }

Could you please some items which you may see in an execution plan indicating
the query
is not optimized.
1. Index Scan or Table Scan
2. Hash Joins
3. Thick arrows (indicating large work tables)
4. Parallel streams (Parallelism)
5. Bookmark lookup (or key lookup)
What structure can you implement for the database to speed up table reads?
Follow the rules of DB tuning we have to:

1] properly use indexes ( different types of indexes)


2] properly locate different DB objects across different tablespaces, files and so on.
3] create a special space (tablespace) to locate some of the data with special datatype ( for
example CLOB, LOB and )

Anda mungkin juga menyukai