Above six are the most important concepts related to CTE and SQL Server. There are many more things one has to learn, but without beginners fundamentals one can’t learn the advanced concepts. Let us have small quiz and check how many of you get the fundamentals right. Let us explore that how to build Hierarchical Query using a Recursive CTE
1) You have an employee table with the following data.
You need to write a recursive CTE that shows the EmpID, FirstName, LastName, MgrID, and employee level. The CEO should be listed at Level 1. All people who work for the CEO will be listed at Level 2. All of the people who work for those people will be listed at Level 3. Which CTE code will achieve this result?
2) You have a table named Employee. The EmployeeID of each employee’s manager is in the ManagerID column. You need to write a recursive query that produces a list of employees and their manager. The query must also include the employee’s level in the hierarchy. You write the following code segment:
WITH EmployeeList (EmployeeID, FullName, ManagerName, Level)
–PICK ANSWER CODE HERE
Now make sure that you write down all the answers on the piece of paper.
Watch following video and read the earlier article over here. If you want to change the answer you still have a chance.
Now compare let us check the answers and compare your answers to following answers. I am very confident you will get them correct.
Please leave your feedback in the comment area for the quiz and video. Did you know all the answers of the quiz?
Reference: Pinal Dave (http://blog.sqlauthority.com)