📘Introduction

Once you've created models in dbt, you can reference them using the ref() function. This is fundamental to how dbt builds dependencies and orchestrates model execution. In this hands-on tutorial, we’ll show you how to use the ref()function within a sample project.

📌 This is a must-know topic for the dbt Analytics Engineering Certification Exam, so mastering it now puts you one step closer to passing the exam and leveling up your data engineering skills! 👨‍🎓

✅ Prerequisites

Before you start, make sure you have:

☑️ A dbt project set up
☑️ At least one model defined in your models/ directory
☑️ Source configurations defined in sources.yml

💡What is the ref() function?

The ref() function in dbt is used to reference other models in your project. It returns the fully qualified table name of the compiled model and lets dbt:

  • Track dependencies between models
  • Build your project in the correct order
  • Show your models visually in the DAG (Directed Acyclic Graph)

The syntax looks as follows:

{{ ref("model_name") }}
💡
Jinja allows you to embed dynamic Python-like logic inside SQL files — making functions like ref() possible.

🔍1️⃣ Check your model files

In this example, we have already created the dbt model cleaned_student:

🛠️2️⃣ Use ref() function

Now, we want to create a new model transformed_student in silver that should reference the model cleaned_student in bronze.

To simply select all columns from cleaned_student, you can use:

You can view this post with the tier: Academy Membership

Join academy now to read the post and get access to the full library of premium posts for academy members only.

Join Academy Already have an account? Sign In