CTE and joins begin Day 16 of this SQL Server interview series. The original questions also cover CLR, synonyms, LINQ, XML, and concurrency. Read the introductory post and use the complete series index to follow the sequence.

What are the Advantages of Using CTE?
A common table expression names a query result within one statement. It can make a complex query easier to read and maintain. A CTE also supports recursive queries when the problem needs recursion.
A CTE isn’t a stored table or a guarantee that results are materialized. It can participate in supported SELECT and data-modification statements. Updateability still depends on the underlying query.
How can we Rewrite Sub-Queries into Simple Select Statements or with Joins?
Some subqueries can become joins or CTEs, but equivalent results come first. A join can multiply rows when several matches exist. Replacing an EXISTS test with a join therefore needs care.
This version keeps the original employee-department example and uses current AdventureWorks column names. It returns recorded department assignments, including historical ones. It doesn’t filter to current assignments.
USE AdventureWorks2025;
GO
;WITH EmployeeDepartment_CTE AS
(
SELECT BusinessEntityID, DepartmentID, ShiftID
FROM HumanResources.EmployeeDepartmentHistory
)
SELECT e.BusinessEntityID, d.DepartmentID,
d.Name AS DepartmentName, e.ShiftID
FROM EmployeeDepartment_CTE AS e
INNER JOIN HumanResources.Department AS d
ON d.DepartmentID = e.DepartmentID
ORDER BY e.BusinessEntityID, d.DepartmentID, e.ShiftID;Use your installed database name if it differs. The current sample-database guide explains the setup. Naming this query as a CTE improves organization, not necessarily speed.
What is CLR?
CLR means Common Language Runtime. SQL Server CLR integration supports managed functions, procedures, triggers, types, and aggregates. These modules require deliberate deployment and security configuration, not just a different query syntax.
The original answer mentioned Visual Studio 2008. That is historical tooling context. See the original CLR explanation for the earlier introduction.
What are Synonyms?
A synonym provides an alternative name for another database object. It can simplify a long object reference without copying the underlying data. It doesn’t remove the need for permissions on the referenced operation.
See the synonym introduction for the original discussion. A synonym is a database object, unlike a table alias that exists inside one query.
What is LINQ?
LINQ means Language Integrated Query. It brings query expressions into .NET languages. LINQ to SQL is a specific .NET Framework mapping technology that translates supported queries into SQL.
In that model, entity classes represent relational data. DataContext tracks changes and coordinates database operations. LINQ itself is broader than LINQ to SQL and isn’t a SQL Server engine feature.
What are Isolation Levels?
Isolation levels define how a transaction observes changes made by other transactions. Depending on the settings, reads use locks or committed row versions. Read Committed, Repeatable Read, Serializable, Snapshot, and Read Uncommitted provide different guarantees.
Read Committed can use locking or statement-level row versions when READ_COMMITTED_SNAPSHOT is enabled. Snapshot provides a transaction-level consistent view. See the transaction-lock discussion for related background.
What is Use of EXCEPT Clause?
EXCEPT returns distinct rows present in the first result but absent from the second. Both queries need the same column count and compatible data types. For this comparison, two NULL values are treated as equal.
SELECT Number
FROM (VALUES (1), (1), (2), (NULL)) AS FirstSet(Number)
EXCEPT
SELECT Number
FROM (VALUES (1), (NULL)) AS SecondSet(Number);The result is 2. Duplicate 1 values don’t produce extra output, and NULL appears in both inputs. The original EXCEPT discussion compares this idea with Oracle MINUS.
What is XPath?
XPath expressions navigate XML nodes. A path such as /Employee/FirstName identifies a child element beneath Employee. Predicates narrow a selection, and namespaces affect which names match.
What is NOLOCK?
NOLOCK uses Read Uncommitted behavior for data reads. It can return uncommitted values, duplicate rows, or miss rows. It is not a general recommendation for improving concurrency.
It also doesn’t mean no locks: schema-stability locks still apply. Investigate blocking and the required consistency before changing isolation. See locking hints with correct scope and safe examples.
What is the Difference between Update Lock and Exclusive Lock?
For the same lock resource, an update lock is compatible with shared locks but not another update lock. Before modifying the protected data, it can convert to an exclusive lock. An exclusive lock is incompatible with shared, update, and exclusive locks on that resource.
That compatibility explanation doesn’t mean every reader must wait: row-versioned reads can behave differently. Lock duration also depends on isolation and engine features. See the update-lock and exclusive-lock discussion.
Continue through the Day 1 through Day 31 series index or browse SQL Server interview questions and answers.
A useful interview answer is not a memorized slogan, it is an explanation with the right limits.
Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.
Discover more from SQL Authority with Pinal Dave
Subscribe to get the latest posts sent to your email.





5 Comments. Leave new
Hi Pinal,
I have a doubt. I have an SP in which I have specified this statement at the top.
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
Even inside the SP I have given Nolock for all the tables which am joining.
This is the structure of the select in my sp :-
select a,b,c
,case tempb.d when null then tempa.d else tempb.d end
,case tempb.e when null then tempa.e else tempb.e end
from tablea (nolock)
join tableb (nolock) on criteria
join tablex (nolock) on criteria
join tablec (nolock) on criteria
left join (select c,d from tabled join tablee on criteria
tablee join tablef on criteria)tempa on criteria
left join (select d,e from tabled join tablee on criteria
tablee join tablef on criteria)tempb on criteria
where condition
I have all indexes in place. My query returns 115000 rows. It took 14 minutes yesterday. But now without altering anything my query is taking 1 hr 10 minutes.
SQL Server Execution Times:
CPU time = 3775797 ms, elapsed time = 4247687 ms.
selecting the output
I tried removing the Transaction Isolation level statement also. It didnt work. Can you please let me know what else I need to check for making this execute faster?? What else I need to look in? Please help.
Regards
Sathya
I’m really enjoying this post series Pinal. Great work and thanks for sharing it with us.
Hello sir … your Blog is very good for the Beginners.. can u please explain about below questions…. Thanks in advance
can you please tell the limitations of triggers over stored procedures?
what is the difference between temporary table and temp variable , when both are used in stored procedure?
wat type of lock in issued when the insert statement is used
Nice psting sir