How to assemble a car?

Hierarchical and Recursive Queries in SQL Server

Jasmin Ludolf

Content Developer

Disassemble a car

An image about a disassembled car.

Hierarchical and Recursive Queries in SQL Server

List of parts of a car

Different levels of components:

  • Level 1: SUV, Cabrio
  • Level 2: Body, Engine, Interior Decoration, Wheel
  • Level 3: Door, Hood, Engine Body, Cylinder, Seats

Hierarchy of car components

Hierarchical and Recursive Queries in SQL Server

Create the data model

Elements to create hierarchy:

  • PartID & SubPartID

Elements to describe characteristics:

  • Component: Engine
  • Title: V6BiTurbo
  • Vendor: BMW
  • ProductKey: EV3891ASF
  • Cost: 3000
  • Quantity: 1

Data model description of BillOfMaterial

Hierarchical and Recursive Queries in SQL Server

Use the hierarchical data model

  • What are the levels of components that build up the car?
+-----------------+---------------------------+
| Component       | Level                     |
|-----------------|---------------------------|
| SUV             | 1                         |
|-----------------|---------------------------|
| Body            | 2                         |
|-----------------|---------------------------|
| Hood            | 3                         |
+-----------------+---------------------------+
Hierarchical and Recursive Queries in SQL Server

Use the hierarchical data model

  • What is the total quantity of each component required to build the car for each component level?
+-----------------+---------------------------+
| Component       | Quantity                  |
|-----------------|---------------------------|
| SUV             | 1                         |
|-----------------|---------------------------|
| Body            | 1                         |
|-----------------|---------------------------|
| Wheels          | 4                         |
+-----------------+---------------------------+
Hierarchical and Recursive Queries in SQL Server

Let's assemble a car!

Hierarchical and Recursive Queries in SQL Server

Preparing Video For Download...