SQL Server Questions and Answers – Hierarchies

This set of SQL Server Multiple Choice Questions & Answers (MCQs) focuses on “Hierarchies”.

1. Which of the following code snippet insert the top level manager ‘Jeff Brown’ as hierarchy root?
a)

INSERT INTO Employees
VALUES (1, 'Jeff Brown', NULL, hierarchyid::Root());

b)

advertisement
advertisement
INSERT INTO Employees
VALUES (1, 'Jeff Brown', NULL, hierarchyid::GET());

c)

INSERT INTO Employees
VALUES (1, 'Jeff Brown', NULL, hierarchyid::GetRoot());

d)

advertisement
INSERT INTO Employees
VALUES (1, 'Jeff Brown', NULL, hierarchy::GetRoot());
View Answer
Answer: c
Explanation: HIERARCHYID data type provides compact storage and convenient methods to manipulate hierarchies.
 
 

2. Point out the correct statement.
a) HierarchyID data type maps the data as a hashmap, so when traversing the binary tree structure
b) In real scenarios, you always need to create indexes using Hierarchy ID data type
c) In HierarchyID, we create indexes in order to make the traversal efficient
d) None of the mentioned
View Answer

Answer: c
Explanation: In order to make the query execution efficient, we create indexes.
advertisement

3. Which of the following is invalid code associated with hierarchical data type?
a)

CREATE TABLE H (
   Node HierarchyID PRIMARY KEY CLUSTERED,
   NodeLevel AS Node.GetLevel(),
   ID INT UNIQUE NOT NULL,
   Name VARCHAR(50) NOT NULL
 )

b)

CREATE TABLE H (
   Node HierarchyID PRIMARY KEY NON CLUSTERED,
   NodeLevel AS Node.GetLevel(),
   ID INT UNIQUE NOT NULL,
   Name VARCHAR(50) NOT NULL
 )

c)

CREATE TABLE H (
   Node HierarchyID FOREIGN KEY CLUSTERED,
   NodeLevel AS Node.GetLevel(),
   ID INT UNIQUE NOT NULL,
   Name VARCHAR(50) NOT NULL
 )

d) All of the mentioned
View Answer

Answer: c
Explanation: Node is the column which has the HierarchyID type, NodeLevel is a calculated column which has the level of a particular node. ID and Name are custom columns for additional information.

4. Which of the following code is used to have a Breadth First index?
a)

 CREATE UNIQUE  bfs_index
   ON H (NodeLevel,Node)

b)

 CREATE UNIQUE INDEX bfs_index
   ON H (NodeLevel,Node)

c)

 CREATE  INDEX bfs_index
   ON H (NodeLevel,Node)

d)

 CREATE UNIQUE INDEX bfs_index
   ON H (NodeLevel)
View Answer
Answer: b
Explanation: HierarchyID data type provides 2 types of methods – Depth First and Breadth First.
 
 

5. Point out the wrong statement.
a) The Depth column comes in handy for performing the indent by using the Space() function
b) Stored procedures are the easiest way to extract hierarchical data
c) Inside SQL Server 2008, Microsoft introduces a new data type Index on HierarchyID to handle such type of data and reduce the complexity
d) None of the mentioned
View Answer

Answer: b
Explanation: Common Table Expressions can be a great way to extract hierarchical data.

6. What is purpose of GetDescendant method in the following code?

DECLARE @parent HierarchyId = HierarchyId::GetRoot()
INSERT INTO H (Node,ID,Name) VALUES (@parent.GetDescendant(NULL,NULL),2,'Johnny')

a) Takes 2 arguments
b) Takes 3 arguments
c) Takes 4 arguments
d) All of the mentioned
View Answer

Answer: a
Explanation: GetDescendant method takes 2 arguments indicating the left and right nodes on the child level respectively.

7. Which of the following function returns true in the following code?

DECLARE @parent HierarchyId = (SELECT Node FROM H WHERE Name = 'Thuru')
DECLARE @parentNodeLevel INT = (SELECT NodeLevel FROM h WHERE name = 'Thuru')
SELECT Node.ToString() AS NodeText, *  FROM H WHERE Node.IsDescendantOf(@parent) = 'TRUE' 
AND Node != @parent AND 
NodeLevel = @parentNodeLevel + 1

a) IsDescendantOf
b) DescendantOf
c) IsDescendant
d) None of the mentioned
View Answer

Answer: a
Explanation: In the above query,immediate children is returned using the NodeLevel column (@parentNodeLevel + 1). And also notice that I have opted out the parent node because IsDescendantOf function includes the parent node as well in the result.

8. Which of the code deletes node H using hierarchical data type?
a) DELETE FROM H WHERE Name = ‘Steve’
b) DROP FROM H WHERE Name = ‘Steve’
c) DELETE H WHERE Name = ‘Steve’
d) All of the mentioned
View Answer

Answer: a
Explanation: Deleting a node does not automatically delete the child nodes, this would result in orphaned children.

9. Which of the following function will be used in the following code for moving nodes?

DECLARE @newParent HierarchyId = (SELECT Node FROM H WHERE name = 'Johnny')
UPDATE H SET Node = Node.__________(Node.GetAncestor(1),@newParent)
WHERE Name = 'S1'

a) GetReparentedVal
b) GetReparentedValue
c) GetValue
d) None of the mentioned
View Answer

Answer: b
Explanation: The GetReparentedValue function is used to move the nodes to different locations.

10. Which of the following code will not throw an error?
a)

DECLARE @child HierarchyId = (SELECT Node FROM H WHERE Name = 'S1')
SELECT * FROM H WHERE Node = @child.GetAnces(2)

b)

DECLARE @child HierarchyId = (SELECT Node FROM H WHERE Name = 'S1')
SELECT * FROM H WHERE Node = @child.GetAncestor(2)

c)

DECLARE @child HierarchyId = (SELECT Node FROM H WHERE Name = 'S1')
SELECT * FROM H WHERE Node = @child.Ancestor(2)

d) None of the mentioned
View Answer

Answer: b
Explanation: GetAncestors function returns the ancestors of a specified node in the specified level.

Sanfoundry Global Education & Learning Series – SQL Server.

To practice all areas of SQL Server, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.