SQL SERVER – Interview Questions and Answers – Frequently Asked Questions – Day 16 of 31 – CTE- Joins

Please read the Introductory Post before continuing reading interview questions and answers. In this blog post we will learn about few popular topics of SQL Server like CTE and joins.

Click here to get free chapters (PDF) in the mailbox

List of all the Interview Questions and Answers Series blogs

What are the Advantages of Using CTE?

    • Using CTE improves the readability and enables easy maintenance of complex queries.
    • The query can be divided into separate, simple, and logical building blocks, which can be then used to build more complex CTEs until the final result set is generated.
    • CTE can be defined in functions, stored procedures, triggers or even views.
    • After a CTE is defined, it can be used as a Table or a View and can SELECT, INSERT, UPDATE or DELETE Data.

How can we Rewrite Sub-Queries into Simple Select Statements or with Joins?

Yes. We can rewrite sub-queries using the Common Table Expression (CTE). A Common Table Expression (CTE) is an expression that can be thought of as a temporary result set which is defined within the execution of a single SQL statement. A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query.

USE AdventureWorks
GO
WITH EmployeeDepartment_CTE AS (
SELECT EmployeeID,DepartmentID,ShiftID
FROM HumanResources.EmployeeDepartmentHistory
)
SELECT ecte.EmployeeId,ed.DepartmentID, ed.Name,ecte.ShiftID
FROM HumanResources.Department ed
INNER JOIN EmployeeDepartment_CTE ecte ON ecte.DepartmentID = ed.DepartmentID
GOO

What is CLR?

In SQL Server 2008, SQL Server objects such as user-defined functions can be created using such CLR languages. This CLR language support extends not only to user-defined functions, but also to stored procedures and triggers. You can develop such CLR add-ons to SQL Server using Visual Studio 2008. (Read more here)

What are Synonyms?

Synonyms give you the ability to provide alternate names for database objects. You can alias object names; for example, using the Employee table as Emp. You can also shorten names. This is especially useful when dealing with three and four part names; for example, shortening server.database.owner.object to object. (Read more here)

What is LINQ?

Language Integrated Query (LINQ) adds the ability to query objects using .NET languages. The LINQ to SQL object/relational mapping (O/RM) framework provides the following basic features:

  • Tools to create classes (usually called entities) mapped to database tables
  • Compatibility with LINQ’s standard query operations
  • The DataContext class with features such as entity record monitoring, automatic SQL statement generation, record concurrency detection, and much more

What are Isolation Levels?

SQL SERVER - Interview Questions and Answers - Frequently Asked Questions - Day 16 of 31 - CTE- Joins presentation Transactions specify an isolation level that defines the degree to which one transaction must be isolated from resource or data modifications made by other transactions. Isolation levels are described in terms of which concurrency side-effects, such as dirty reads or phantom reads, are allowed.

Transaction isolation levels control the following:

  • Whether locks are taken when data is read, and what type of locks are requested.
  • How long the read locks are held.
  • Whether a read operation referencing rows modified by another transaction
    • blocks until the exclusive lock on the row is freed,
    • retrieves the committed version of the row that existed at the time the statement or transaction started, and
    • reads the uncommitted data modification.(Read more here)

What is Use of EXCEPT Clause?

EXCEPT clause is similar to MINUS operation in Oracle. The EXCEPT query and MINUS query return all rows in the first query that are not returned in the second query. Each SQL statement within the EXCEPT query and MINUS query must have the same number of fields in the result sets with similar data types. (Read more here)

What is XPath?

XPath uses a set of expressions to select nodes to be processed. The most common expression that you’ll use is the location path expression, which returns back a set of nodes called a node set. XPath can use both an unabbreviated and abbreviated syntax. The following is the unabbreviated syntax for a location path:

/axisName::nodeTest[predicate]/axisName::nodeTest[predicate]

What is NOLOCK?

Using the NOLOCK query optimizer hint is generally considered a good practice in order to improve concurrency on a busy system. When the NOLOCK hint is included in a SELECT statement, no locks are taken on data when data is read. The result is a Dirty Read, which means that another process could be updating the data at the exact time you are reading it. There are no guarantees that your query will retrieve the most recent data. The advantage to performance is that your reading of data will not block updates from taking place, and updates will not block your reading of data. SELECT statements take Shared (Read) locks. This means that multiple SELECT statements are allowed simultaneous access, but other processes are blocked from modifying the data. The updates will queue until all the reads have completed, and reads requested after the update will wait for the updates to complete. The result to your system is delay (blocking). (Read more here)

What is the Difference between Update Lock and Exclusive Lock?

When Exclusive Lock is on any process, no other lock can be placed on that row or table. Every other process have to wait till Exclusive Lock completes its tasks.

Update Lock is a type of Exclusive Lock, except that it can be placed on the row which already has Shared Lock on it. Update Lock reads the data of the row which has the Shared Lock as soon as the Update Lock is ready to change the data it converts itself to the Exclusive Lock. (Read more here)

List of all the Interview Questions and Answers Series blogs

Reference: Pinal Dave (https://blog.sqlauthority.com)

SQL Scripts, SQL Server
Previous Post
SQL SERVER – Interview Questions and Answers – Frequently Asked Questions – Day 15 of 31
Next Post
SQL SERVER – Interview Questions and Answers – Frequently Asked Questions – Day 17 of 31

Related Posts

5 Comments. Leave new

  • Sathya Narayanan
    July 16, 2011 9:06 am

    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

    Reply
  • I’m really enjoying this post series Pinal. Great work and thanks for sharing it with us.

    Reply
  • Sreenivas Remuri
    July 18, 2011 8:07 pm

    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?

    Reply
  • wat type of lock in issued when the insert statement is used

    Reply
  • Nice psting sir

    Reply

Leave a Reply