Understanding DAG in SQL

Using DAG to represent a sequence of data transformation tasks from SQL CTEs.

Using DAG to represent a sequence of data transformation tasks from SQL CTEs.

Poom Wettayakorn

Jan 20, 2024

Jan 20, 2024


👋 Hi there, welcome to Datascale: SQL+Data modeling. We help create database diagrams and map out data models from existing schema and SQL queries 🌱. Find us on LinkedIn 🎉

Understanding DAG in SQL with CTEs


In the world of SQL, there are a variety of tools and concepts that can help make data analysis and management more efficient. One such tool is a Directed Acyclic Graph (DAG). In this guide, we'll explain what a DAG is, how it works, and how it can be used in SQL with CTEs.

What is CTEs?

A common table expression, or CTE is often used to simplify complex subqueries or break down a large query into more manageable pieces. They are defined using the WITH keyword followed by the CTE name and its definition enclosed in parentheses.

WITH my_cte AS (
  SELECT a,b
  FROM my_table

What is a DAG?

A ---> C --> D

A DAG is a directed acyclic graph, which is essentially a set of nodes (i.e., tables or CTEs in SQL) that are connected by directed edges. In other words, a DAG is a graphical representation of a set of tasks or operations, where each task depends on one or more other tasks to be completed before it can be executed.

CTEs as building blocks for DAG

In SQL, a DAG can be used to represent a sequence of data transformation tasks. For example, imagine you have a dataset that needs to be cleaned, filtered, and then aggregated before it can be analyzed. You might represent this sequence of tasks as a DAG, with each task as a node and the dependencies between them as edges.

Using a DAG in SQL can have several benefits, including:

  1. Breakdown of Complex Logic: CTEs enable you to decompose intricate data transformations into smaller, reusable steps. Each CTE acts as a node in the DAG, representing a specific stage of the processing pipeline.

  2. Enhanced Readability and Maintainability: By isolating each step within a named CTE, your code becomes clearer and easier to follow. This simplifies debugging and makes modifying specific stages less intrusive to the overall flow.

  3. Increased Transparency: Data lineage becomes readily apparent through visualized dependencies, promoting understanding of data pipelines.

Pro tips for DAG mastery

  1. Visualize your workflow: Leverage graphical tools to create a visual representation of your DAG, making the data flow and dependencies readily apparent. Check out: https://datascale.cloud/demo

  2. Modular code is king: Treat each CTE as a self-contained unit, promoting reusability and making integration into larger pipelines seamless.

Try this CTEs on Datascale

  cte1 AS (
    SELECT a, b
    FROM table1
  cte2 AS (
    SELECT c
    FROM table2
  cte3 AS (
    SELECT a, c
    FROM cte1
    JOIN cte2 ON cte1.b = cte2.c
  final_result AS (
    SELECT cte1.a, cte3.c
    FROM cte1
    JOIN cte3 ON cte1.a = cte3.a
FROM final_result;


In summary, a DAG is a powerful tool for representing complex sequences of tasks in SQL. By using a DAG, you can improve the reliability, and maintainability of your data analysis and management tasks.

Next, try visualizing SQL to ERD: https://getdatascale.com/blog/sql-to-erd

Related blogs



Model your database diagram

Datascale helps map out data models from existing schema and SQL queries

Try for free

Data Modeling for SQL

Data Modeling for SQL