SQL Server Interview Questions
-
Posted on June 23, 2009 by Derek Dieter
-
2
The following interview questions have been used on multiple interviewees and seem to work out well in finding the different areas of expertise for an individual. The rating part at the beginning gives you an understanding of where the interviewer thinks they are at in the listed categories. You can then judge yourself after you ask the questions.
- On a scale of 1-10,
rate yourself in the following categories: - T-SQL
- Optimization
(SQL Server Internals) - Data Warehouse
- Database Administration
- Database Architecture
- Database slowdown
troubleshooting
T-SQL
- Name all the different kinds of Joins.
- OUTER
JOIN – LEFT, RIGHT, CROSS, FULL - INNER
JOIN - On a scale of 1 to 10, how important would you consider cursors or while loops for a transactional database?
- As close to zero as possible, mainly only used for maintenance or warehouse operations.
- What is a correlated sub query?
- When a sub query is tied to the outer query. Mostly used in self joins.
- What is faster, a correlated sub query or an inner join?
- Correlated
sub query. - What is faster, a correlated sub query or an exists?
- Exists
- What is the having clause and when is it used.
- Used to further filter a group by.
- What are the pros and cons of putting a scalar function in a queries select list or in the where clause?
- Trick question, mostly just cons. Scalar functions in these places make the query slow down dramatically.
Internals
- Describe lock escalation
- A query first takes the lowest level lock possible with the smallest footprint
(row-level). When too many rows are locked (requiring too much RAM) the lock is escalated to a range or page lock. If too many pages are locked, it may escalate to a table lock. - What are the internal differences between #temp tables and @table variables
- SQL Server can create column statistics on #temp tables
- Indexes can be created on #temp tables
- @table variables are stored in memory up to a certain threshold.
- How does a clustered index differ from a non-clustered?
- Clustered index physically sorts the table data in that order.
- Non clustered index is a duplication of the data in the table with a pointer to the clustered index key.
- What are some of the join algorithms used when SQL Server joins tables.
- Loop Join (indexed keys unordered)
- Merge Join (indexed keys ordered)
- Hash Join (non-indexed keys)
- Name some possible warning signals you may see in an execution plan indicating the query
is not optimized. - Index Scan or Table Scan
- Hash Joins
- Thick arrows (indicating large work tables)
- Parallel streams (Parallelism)
- Bookmark lookup (or key lookup)
- What is a bookmark lookup? (Or key lookup)?
- When a non clustered index is used for the seek and the data needed was not
at the leaf level. - Which is faster a Table Scan, or a Clustered Index Scan?
- Trick question. Same speed.
- Describe recompilation. What causes it to happen and what are the differences between 2000 and 2005?
- When the cached execution plan for a query cannot be used so the procedure
recompiles. It may happen because (1) underlying statistics change. (2) DDL changes within the procedure. (3) The parameters the procedure was compiled with vary from the recently passed in parameters. (4) The query plan was flushed from cache.
Data Warehouse
- If moving a larger number of rows from one server to another using T-SQL with a linked-server,
how would you limit the size of the batches so the transaction log does not fill up? - Create a looping mechanism using SET ROWCOUNT and a while loop
- Use the new SQL 2005 GO statement to set batch sizes.
- In DTS or SSIS, name the three types of precedence constraints used in moving from one
task to another. - Success
- Failure
- Completion
Slowdown Troubleshooting
- When there is a whole system slowdown that is verified to be the database, describe
the steps you take to investigate it. - Run sp_who2 and see if any blocking is taking place, or any queries are
eating too much CPU or disk IO. Also check the # of connections. - Locking.
- Log into the box, see if the box is slow. Check CPU, memory usage, page
file usage. - Open perfmon and check the page life expectancy and disk latency.
- If there are no physical indicators for the system slowdown, what is the first thing you should do?
- Update Statistics
- When one particular query is slow, describe the steps you take to investigate it.
- Run SQL profiler and determine if abnormal amounts of IO or CPU is used.
- Run profiler to determine if recompilation is a factor.
- Update the statistics.
- Check the execution plan.
- Name all the potential points of contention that can cause a database slowdown:
- CPU
bottleneck - Memory
bottleneck - Network
IO bottleneck - Disk
IO bottleneck - Paging
File (process trimming) - Lock
contention - Corrupt
index - Recompilation
Database Administration
- What is the difference between a truncate and a delete and what is the minimum fixed-server role needed to truncate?
- Truncates are not logged and cannot be undone. Truncate requires dbo.
- What is the default number of worker threads in SQL 2000?
- 255
- What is parallelism?
What is the default query threshold for parallelism? - The optimizer decides to utilize multiple SPIDS running on different processors
to query / transfer data. Default threshold is 5 seconds. - Describe Log Shipping vs. Mirroring vs. Transactional Replication. What are the pros and cons?
- Log Shipping is asynchronous and sends transaction log file updates at a
minimal interval of 2 minutes. - Mirroring replicates the entire database and is synchronous. By default, it commits the data on both sides prior to releasing the transaction.
- Transactional replication is asynchronous and can isolate specific tables.
- How many transaction logs can you create?
- As many as you want.
- What is the optimal Disk configuration for a database server and what RAID configurations would you use?
- RAID
1 for the OS / Applications - RAID
1 for the page file - RAID
10 for the Data file (possibly RAID 5 for few writes) - RAID
1 (or 10) for the transaction log - What do the following commands do?
- DBCC SHOWCONTIG
- Shows fragmentation within tables / indexes
- DBCC FREEPROCCACHE
- Clears the procedure cache removing all execution plans, all procedures are
recompiled. - DBCC DBREINDEX
- Performs a complete reorganization of the index. (Intrusive process)
- DBCC DROPCLEANBUFFERS
- Drops
all the data that was cached in memory. - What is AWE and
what are the requirements? - Address Windowing Extensions – Allows SQL to utilize greater than 4 GB of RAM. When used with 32 bit Windows PAE (Physical Address Extension) needs to be turned on.
Queries
1) Write a query to return the firstname, lastname and the most recent OrderID for all customers. The orders table contains all the orders for each customerID and the Customers table contains the customer personal information. Each order in the Orders table has an OrderDate.
Answer
[cc lang=”sql”]
SELECT
o.OrderID
,c.FirstName
,c.LastName
FROM Orders o
JOIN Customers c
ON o.CustomerID = c.CustomerID
WHERE OrderDate = (SELECT MAX(OrderDate)
FROM Orders
WHERE CustomerID = o.CustomerID
[/cc]
OR
[cc lang=”sql”]
SELECT
c.FirstName,
c.LastName,
o.OrderNumber
FROM Orders o
JOIN
(
SELECT MAX(OrderDate) AS MaxOrderDate,
custid
FROM orders
GROUP BY custid
) o_2
ON o.custid = o_2.custid
AND o.orderdate = o_2.MaxOrderDate
JOIN customers c
ON c.CustID = o.CustID
[/cc]
2. If this was the only query on used for these two tables, how would you create the clustered indexes?
Customers – CustomerID
Orders – CustomerID, OrderDate
- Comments (RSS)
- Trackback
- Permalink