T-SQL Interview Questions and Answers 2026

Table of Contents

Spread the love

A simple guide for freshers and working developers who want to crack SQL Server interviews

If you are getting ready for a SQL Server job, you will face T-SQL questions. It does not matter if the role is for a developer, a data analyst, or a database admin. Almost every company that uses Microsoft SQL Server checks your T-SQL skills first.

I have put the most common questions below, with short and clear answers. I also added small code examples, because interviewers love to say, “Okay, now write it.” Read the answers, then type the code yourself. That is the best way to remember it.

What Is T-SQL?

T-SQL stands for Transact-SQL. It is Microsoft’s own version of SQL. It works with SQL Server and Azure SQL Database. On top of normal SQL, it gives you extra things like variables, loops, IF conditions, error handling, and stored procedures.

Think of normal SQL as the basic language. T-SQL is that language plus tools to write real programs inside the database.

Basic T-SQL Interview Questions

Q1. What is the difference between SQL and T-SQL?

SQL is a standard language used by many databases like MySQL, Oracle and PostgreSQL. T-SQL is the extra-powerful version made only for SQL Server. With T-SQL you can use variables, WHILE loops, TRY…CATCH, and built-in functions that plain SQL does not have.

Q2. What are the main types of SQL commands?

TypeFull NameCommon CommandsUsed For
DDLData Definition LanguageCREATE, ALTER, DROP, TRUNCATEBuilding and changing tables
DMLData Manipulation LanguageINSERT, UPDATE, DELETE, MERGEChanging the data
DQLData Query LanguageSELECTReading the data
DCLData Control LanguageGRANT, REVOKE, DENYGiving or removing access
TCLTransaction Control LanguageBEGIN TRAN, COMMIT, ROLLBACKManaging transactions

Q3. What is the difference between DELETE, TRUNCATE and DROP?

This one comes up in almost every interview. Here is a simple way to remember it.

PointDELETETRUNCATEDROP
What it removesSelected rows or all rowsAll rowsThe whole table
WHERE clauseYesNoNo
Can you roll it back?YesYes, inside a transactionYes, inside a transaction
SpeedSlower (logs each row)FastFast
Identity valueStays as it isResets to the startTable is gone
Triggers fire?YesNoNo

Many people think TRUNCATE cannot be rolled back. In SQL Server it can, as long as you run it inside a transaction. Mentioning this in an interview shows you really know the topic.

Q4. What is the difference between a primary key and a unique key?

  • A primary key does not allow NULL values. A table can have only one.
  • A unique key allows one NULL value. A table can have many unique keys.
  • By default, a primary key creates a clustered index. A unique key creates a non-clustered index.

Q5. What is the difference between WHERE and HAVING?

WHERE filters rows before grouping. HAVING filters groups after GROUP BY. So if you want to filter on COUNT() or SUM(), you must use HAVING.

SELECT Department, COUNT(*) AS Total

FROM Employees

WHERE IsActive = 1

GROUP BY Department

HAVING COUNT(*) > 5;

Q6. What is the difference between CHAR and VARCHAR?

CHAR(10) always takes 10 bytes, even if you store only 3 letters. VARCHAR(10) takes only the space it needs. Use CHAR for fixed-size data like country codes. Use VARCHAR for names, emails and addresses.

Joins and Query Order

Q7. What are the different types of joins?

  • INNER JOIN gives only the rows that match in both tables.
  • LEFT JOIN gives all rows from the left table, and matching rows from the right.
  • RIGHT JOIN is the opposite of LEFT JOIN.
  • FULL JOIN gives all rows from both tables.
  • CROSS JOIN pairs every row of one table with every row of the other.

The blue part shows which rows come back in each join.

A good interviewer may ask, “How do you find rows in table A that have no match in table B?” Use a LEFT JOIN and check for NULL:

SELECT a.CustomerId

FROM Customers a

LEFT JOIN Orders b ON a.CustomerId = b.CustomerId

WHERE b.CustomerId IS NULL;

Q8. In what order does SQL Server run a query?

Most people believe SQL runs from top to bottom. It does not. You write SELECT first, but SQL Server reads it fifth. Look at the picture below.

This is why you cannot use a column alias from SELECT inside a WHERE clause.

Intermediate T-SQL Interview Questions

Q9. What is the difference between a stored procedure and a function?

PointStored ProcedureFunction
Return valueOptionalMust return a value
Used inside SELECTNoYes (scalar and table functions)
Can change data (INSERT, UPDATE)YesNo (mostly)
TRY…CATCH allowedYesNo
Called withEXECSELECT or FROM

Q10. What is a CTE?

CTE means Common Table Expression. It is a temporary result that you name and use right after in the same query. It makes long queries easier to read. It can also call itself, which is called a recursive CTE, and this is useful for tree data like employee and manager.

WITH HighSalary AS (

    SELECT Name, Salary

    FROM Employees

    WHERE Salary > 80000

)

SELECT * FROM HighSalary;

Q11. What is the difference between a temp table and a table variable?

  • A temp table (#Temp) is stored in tempdb. It can have indexes and statistics. It is better for large data.
  • A table variable (@Table) is best for small data. It does not have statistics, so SQL Server may guess the row count wrong on big data.
  • Both are cleaned up on their own when the session or batch ends.

Q12. What are window functions?

A window function does a calculation across a group of rows, but it does not squash them into one row like GROUP BY does. The most asked ones are:

FunctionWhat It Does
ROW_NUMBER()Gives a unique number to each row: 1, 2, 3, 4
RANK()Same rank for ties, then skips numbers: 1, 2, 2, 4
DENSE_RANK()Same rank for ties, no skipping: 1, 2, 2, 3
LAG() / LEAD()Reads the value from the previous or next row
SUM() OVER()Running total or total per group

Q13. What is an index? What is the difference between clustered and non-clustered?

An index helps SQL Server find data fast, like the index page at the back of a book.

  • A clustered index sorts the actual table data. You get only one per table.
  • A non-clustered index is a separate list that points to the data. You can have many.
  • Too many indexes slow down INSERT, UPDATE and DELETE, so add them with care.

Advanced T-SQL Interview Questions

Q14. What is ACID in a transaction?

  • Atomicity: all steps work, or none of them do.
  • Consistency: data stays valid before and after.
  • Isolation: two transactions do not disturb each other.
  • Durability: once saved, data stays saved, even if the server crashes.

Q15. How do you handle errors in T-SQL?

Use TRY…CATCH. Put the risky code in the TRY block. If something fails, the code jumps to CATCH.

BEGIN TRY

    BEGIN TRAN;

    UPDATE Accounts SET Balance = Balance – 500 WHERE Id = 1;

    UPDATE Accounts SET Balance = Balance + 500 WHERE Id = 2;

    COMMIT;

END TRY

BEGIN CATCH

    IF @@TRANCOUNT > 0 ROLLBACK;

    THROW;

END CATCH;

Q16. What is a deadlock and how can you avoid it?

A deadlock happens when two transactions wait for each other, and neither can move. SQL Server picks one as the “victim” and stops it. To avoid deadlocks:

  • Access tables in the same order every time.
  • Keep transactions short.
  • Use proper indexes so fewer rows get locked.
  • Do not keep a transaction open while waiting for user input.

Q17. What are the transaction isolation levels?

LevelDirty ReadNon-repeatable ReadPhantom Read
READ UNCOMMITTEDPossiblePossiblePossible
READ COMMITTED (default)NoPossiblePossible
REPEATABLE READNoNoPossible
SERIALIZABLENoNoNo
SNAPSHOTNoNoNo

Q18. How do you make a slow query faster?

There is no single trick. I usually go step by step:

  1. Look at the execution plan to see where the time goes.
  2. Check for missing indexes, and for table scans that should be seeks.
  3. Avoid SELECT *. Ask only for the columns you need.
  4. Avoid functions on columns in the WHERE clause. WHERE YEAR(OrderDate) = 2026 cannot use an index well. Use a date range instead.
  5. Update statistics if they are old.
  6. Replace loops and cursors with set-based code when you can.

Popular T-SQL Coding Questions

Q19. Find the second highest salary.

SELECT MAX(Salary) AS SecondHighest

FROM Employees

WHERE Salary < (SELECT MAX(Salary) FROM Employees);

For the Nth highest, use DENSE_RANK(). It handles ties in a clean way:

SELECT Salary FROM (

    SELECT Salary, DENSE_RANK() OVER (ORDER BY Salary DESC) AS rnk

    FROM Employees

) t WHERE rnk = 3;

Q20. Find duplicate records.

SELECT Email, COUNT(*) AS Times

FROM Users

GROUP BY Email

HAVING COUNT(*) > 1;

Q21. Delete duplicate rows but keep one.

WITH Dups AS (

    SELECT *, ROW_NUMBER() OVER (PARTITION BY Email ORDER BY Id) AS rn

    FROM Users

)

DELETE FROM Dups WHERE rn > 1;

Q22. Get a running total of sales.

SELECT OrderDate, Amount,

       SUM(Amount) OVER (ORDER BY OrderDate

           ROWS UNBOUNDED PRECEDING) AS RunningTotal

FROM Sales;

Tips to Crack a T-SQL Interview in 2026

  • Practice on a real server. Install SQL Server Developer Edition (it is free) and use the AdventureWorks sample database.
  • Explain your thinking. Say your steps out loud. Interviewers like clear thinking more than a perfect answer.
  • Learn window functions well. They come up in most mid-level and senior interviews now.
  • Read execution plans. Even a basic idea of scans and seeks sets you apart.
  • Know a little about the cloud. Many teams now use Azure SQL, so knowing the basics helps.
  • Do not bluff. If you do not know something, say so, and then explain how you would find the answer.

Frequently Asked Questions (FAQ)

Is T-SQL hard to learn?

No. If you already know basic SQL, you can pick up T-SQL in a few weeks. The extra parts, like variables and stored procedures, feel natural once you write a few of them.

How long does it take to prepare for a T-SQL interview?

If you are a fresher, plan for 3 to 4 weeks of daily practice. If you already work with SQL Server, one week to refresh joins, indexes and window functions is often enough.

Is T-SQL still in demand in 2026?

Yes. SQL Server and Azure SQL are still used in banks, hospitals, retail and many other fields. Jobs for data analysts, backend developers and BI developers list T-SQL as a needed skill.

What is the difference between T-SQL and PL/SQL?

T-SQL is for Microsoft SQL Server. PL/SQL is for Oracle. Both add programming features to SQL, but the syntax is different.

Which topics are most asked in T-SQL interviews?

Joins, indexes, stored procedures, CTEs, window functions, transactions and query tuning. If you are strong in these seven, you can answer most questions.

Final Words

Interviews are not about remembering every word. They test whether you can think in sets, write clean queries and fix slow ones. Go through the questions above, run every example on your own machine, and change the code a little to see what happens.

Similar Article

Leave a Comment