SQL Interview Queries Question / Questions Answers
This is a never ending topic and there is no limit to questions that all depends on profile and interviewer.
I am trying my level best to divide questionnaire based on Experience in Query Writing skills
- 5+ Years of Experience (Expert)
- 2-5 Years of Experience (Intermediate)
- 1-2 Years of Experience (Beginner)
- Fresher to 1 years of Experience (Fresher)
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 Friend’s. 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')
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
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?
- RAID 1 for the OS / Applications
- RAID 1 for the page file
- RAID 10 for the Data file (or RAID 5 for few writes)
- 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 other’s 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 user’s 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:
- If there is significant change in the key values in the index
- 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
- 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 );
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;



