Write graph queries that use the MATCH operator (DP-800 Exam Prep)

This post is a part of the DP-800: Developing AI-Enabled Database Solutions Exam Prep Hub.
This topic falls under these sections:
Design and develop database solutions (35–40%)
   --> Write advanced T-SQL code
      --> Write graph queries that use the MATCH operator


Note that there are 10 practice questions (with answers) at the end of each section to help you solidify your knowledge of the material. Also, there are 4 practice tests with 30 questions each available from the hub's main page below the exam topics section.

Introduction

Many business problems involve relationships between entities rather than simple rows and columns. Examples include social networks, organizational hierarchies, fraud detection, recommendation engines, transportation networks, supply chains, and knowledge graphs. While relational databases excel at storing structured data, querying complex relationships often requires multiple self-joins that become increasingly difficult to write and maintain.

To address these scenarios, SQL Server and Azure SQL Database support graph databases through node tables, edge tables, and the MATCH operator. These capabilities allow developers to model and query relationships using graph patterns while continuing to leverage the relational database engine.

For the DP-800: Developing AI-Enabled Database Solutions certification exam, you should understand how to create graph objects and write graph queries using the MATCH operator.


What Is a Graph Database?

A graph database represents information as:

  • Nodes – entities or objects
  • Edges – relationships between entities

Instead of focusing solely on tables and foreign keys, graph databases emphasize how data is connected.

Example:

Alice ---- WorksWith ---- Bob
|
LivesIn
|
Orlando

In this example:

  • Alice, Bob, and Orlando are nodes
  • WorksWith and LivesIn are edges

Graph Database Components

SQL Server graph databases consist of two primary object types:

ObjectPurpose
Node TableStores entities
Edge TableStores relationships

Node Tables

Node tables represent entities.

Examples include:

  • Employees
  • Customers
  • Products
  • Cities
  • Departments
  • Suppliers

Example:

CREATE TABLE Person
(
PersonID INT PRIMARY KEY,
FullName NVARCHAR(100)
)
AS NODE;

The AS NODE clause creates a graph node table.


Edge Tables

Edge tables represent relationships between nodes.

Example:

CREATE TABLE WorksWith
(
SinceDate DATE
)
AS EDGE;

This table stores the relationship between two Person nodes.

SQL Server automatically maintains hidden graph metadata for node and edge tables.


Node and Edge Relationships

Suppose the following data exists:

John ---- WorksWith ---- Mary
Mary ---- WorksWith ---- Susan
John ---- Manages ---- David

Each person exists once in the node table.

Relationships exist separately in edge tables.


Why Use Graph Queries?

Traditional relational queries require joins.

Example:

Employee
Manager
Department

This often becomes:

Employee
JOIN Manager
JOIN Department
JOIN Office
JOIN Region

Graph queries simplify relationship traversal.


The MATCH Operator

The MATCH operator is the primary mechanism for querying graph relationships.

Instead of writing multiple joins, developers specify graph patterns.

General syntax:

SELECT ...
FROM ...
WHERE MATCH(pattern);

The pattern describes how nodes are connected.


Basic MATCH Query

Suppose the database contains:

Persons

  • John
  • Mary
  • Susan

Relationship

John → WorksWith → Mary

Query:

SELECT
p1.FullName,
p2.FullName
FROM Person p1,
WorksWith w,
Person p2
WHERE MATCH
(
p1-(w)->p2
);

Result:

FullNameFullName
JohnMary

Understanding Graph Pattern Syntax

Example:

p1-(w)->p2

Meaning:

  • Start with node p1
  • Traverse edge w
  • Reach node p2

Arrow direction matters.


Reverse Direction

Example:

p1<-(w)-p2

Meaning:

p2 → p1

The relationship is traversed in the opposite direction.


Multiple Relationships

Suppose:

John → Mary
Mary → Susan

Query:

WHERE MATCH
(
John-(WorksWith)->Mary-(WorksWith)->Susan
);

The MATCH operator follows multiple hops.


Multi-Hop Queries

Graph databases excel at traversing multiple relationships.

Example:

Find employees connected through two working relationships.

Employee
WorksWith
Employee
WorksWith
Employee

Without graphs this may require several joins.

With MATCH the relationship path is much easier to express.


Multiple Edge Types

Suppose the graph contains:

John
WorksWith
Mary
LivesIn
Seattle

Query:

John-(WorksWith)->Mary-(LivesIn)->Seattle

The MATCH operator supports multiple relationship types within a single query.


Using MATCH with SELECT

Example:

SELECT
p.FullName,
c.CityName
FROM Person p,
LivesIn l,
City c
WHERE MATCH
(
p-(l)->c
);

Result

PersonCity
JohnSeattle
MaryOrlando

Combining MATCH with WHERE

Additional filtering can be applied.

Example:

SELECT
p.FullName
FROM Person p,
WorksWith w,
Person p2
WHERE MATCH
(
p-(w)->p2
)
AND p2.Department='Sales';

Graph traversal occurs first.

The remaining rows are filtered normally.


MATCH and JOINs

Graph queries can still use relational joins.

Example:

SELECT
p.FullName,
d.DepartmentName
FROM Person p,
WorksWith w,
Person p2
JOIN Department d
ON p2.DepartmentID=d.DepartmentID
WHERE MATCH
(
p-(w)->p2
);

Graph features integrate with standard SQL.


Graph Queries for AI Applications

Graph databases are becoming increasingly valuable for AI applications because they naturally represent relationships between people, documents, products, concepts, and events.

Examples include:

  • Knowledge graphs
  • Recommendation systems
  • Fraud detection
  • Supply chain analysis
  • Social networks
  • Customer relationship analysis
  • Semantic search
  • Retrieval-Augmented Generation (RAG)
  • Entity linking
  • Relationship discovery

Large Language Models (LLMs) often benefit from graph data because relationships provide richer context than isolated rows.


Knowledge Graph Example

Suppose an AI application stores:

Customer
Purchased
Product
ManufacturedBy
Company

The MATCH operator can quickly discover:

  • Which products customers purchased
  • Which companies manufacture them
  • Similar purchasing relationships
  • Connected entities

Fraud Detection

Graph databases are excellent for identifying suspicious relationships.

Example:

Customer
Owns
Account
TransfersMoneyTo
Account
OwnedBy
Customer

MATCH queries can identify complex money-transfer networks that would require many joins in a traditional relational model.


Recommendation Engines

Streaming services often recommend content based on relationships.

Example:

User
Likes
Movie
DirectedBy
Director

Graph queries efficiently discover similar users and related content.


Relationship Discovery

Graph databases make it easy to answer questions such as:

  • Who works with whom?
  • Which customers purchased similar products?
  • Which suppliers serve the same regions?
  • Which employees report to the same manager?
  • Which products share common components?

These scenarios are ideal for MATCH queries.


Performance Considerations

Graph queries can outperform complex self-joins when relationship traversal is the primary objective.

Best practices include:

  • Keep node and edge tables appropriately indexed.
  • Filter data before traversing large graphs when possible.
  • Avoid unnecessary relationship hops.
  • Use graph queries only when relationships are central to the problem.
  • Continue using relational tables for highly tabular data.

Best Practices

  • Model entities as node tables.
  • Model relationships as edge tables.
  • Use descriptive edge names.
  • Keep graph models simple.
  • Combine MATCH with relational filtering when appropriate.
  • Choose graph queries only when relationship traversal is required.
  • Avoid replacing relational designs unnecessarily.
  • Document graph relationships clearly.
  • Test graph queries with realistic datasets.
  • Consider graph databases for AI-powered relationship analysis.

Common Exam Tips

For the DP-800 exam, remember the following:

  • Graph databases store entities as nodes and relationships as edges.
  • Node tables are created using AS NODE.
  • Edge tables are created using AS EDGE.
  • The MATCH operator traverses graph relationships.
  • Arrow direction (-> and <-) determines relationship direction.
  • MATCH can traverse multiple relationships in a single query.
  • Graph queries integrate with standard SQL statements.
  • Graph databases are well suited for knowledge graphs, recommendation engines, fraud detection, supply chains, and AI-enabled applications that rely on relationship analysis.

Practice Exam Questions

Question 1

Which SQL Server object stores relationships between entities in a graph database?

A. View

B. Node table

C. Edge table

D. Stored procedure

Answer: C

Explanation: Edge tables store the relationships between nodes and are created using the AS EDGE clause.


Question 2

Which clause is used when creating a graph node table?

A.

AS GRAPH

B.

AS NODE

C.

AS ENTITY

D.

AS OBJECT

Answer: B

Explanation: A graph node table is created by appending the AS NODE clause to a CREATE TABLE statement.


Question 3

What is the primary purpose of the MATCH operator?

A. Perform full-text searches

B. Compare two strings

C. Traverse graph relationships between nodes

D. Create graph indexes

Answer: C

Explanation: MATCH specifies graph traversal patterns, allowing SQL Server to navigate relationships represented by edge tables.


Question 4

In the graph pattern:

p1-(w)->p2

what does the arrow (->) indicate?

A. The relationship flows from p1 through edge w to p2.

B. The relationship flows from p2 to p1.

C. The query performs an inner join.

D. The graph contains duplicate nodes.

Answer: A

Explanation: The arrow indicates the direction of traversal from the starting node (p1) through the edge (w) to the destination node (p2).


Question 5

Which scenario is best suited for SQL Server graph queries?

A. Calculating monthly payroll totals

B. Traversing employee reporting relationships across multiple organizational levels

C. Sorting sales by date

D. Updating a single customer record

Answer: B

Explanation: Graph queries excel at traversing complex relationships, such as organizational hierarchies and reporting structures.


Question 6

Which statement about graph queries in SQL Server is true?

A. They cannot be combined with traditional SQL queries.

B. They require a separate graph database engine.

C. They can be combined with relational filtering and joins.

D. They replace foreign keys.

Answer: C

Explanation: SQL Server graph queries integrate with standard T-SQL and can be combined with joins, filters, and other relational features.


Question 7

Which of the following is represented by a node table?

A. A relationship between two customers

B. A connection between two products

C. A customer entity

D. A graph traversal path

Answer: C

Explanation: Node tables represent entities such as customers, employees, products, or cities, while edge tables represent the relationships between them.


Question 8

Why are graph databases valuable for Retrieval-Augmented Generation (RAG) and other AI solutions?

A. They automatically train language models.

B. They store only vector embeddings.

C. They eliminate the need for SQL queries.

D. They model and query rich relationships that provide additional context for AI systems.

Answer: D

Explanation: Graph databases capture connections among entities, allowing AI applications to retrieve contextual information that improves reasoning and search results.


Question 9

What is the advantage of using the MATCH operator instead of multiple self-joins?

A. It encrypts graph data automatically.

B. It simplifies expressing relationship traversal patterns.

C. It automatically creates indexes.

D. It eliminates the need for edge tables.

Answer: B

Explanation: MATCH provides a concise, intuitive syntax for traversing relationships that would otherwise require numerous joins.


Question 10

A database models employees, departments, and managers as graph nodes connected by edge tables. Which query feature should be used to find employees connected to a specific manager through defined relationships?

A. LIKE

B. GROUP BY

C. MERGE

D. MATCH

Answer: D

Explanation: The MATCH operator is specifically designed for traversing relationships in SQL Server graph databases and is the appropriate choice for this type of query.


Go to the DP-800 Exam Prep Hub main page

Leave a comment