How to Determine the Execution Order of SQL Scripts (Which File Runs First?)

You have a folder of SQL scripts and no README. Run them in the wrong order and half of them fail because a table does not exist yet. The right order is not mysterious: each script must run after the scripts that create the tables it reads. This guide shows how to work it out, by hand and automatically. (If you were looking for the order in which a single query clauses are evaluated, that is a different topic; this one is about running many scripts.)

Open the SQL Execution Order Tool

The rule

A script can run once everything it reads already exists. So the correct order is a dependency order: if script B reads a table that script A creates, A comes first. Scripts that do not depend on each other can run in any order.

In graph terms this is a topological order. You do not need to remember the term, only the rule.

A manual method

1. List what each file creates and reads

For every file, write down the tables it creates (CREATE TABLE, CREATE VIEW, SELECT INTO, the target of INSERT) and the tables it reads (FROM, JOIN, subqueries).

2. Find the files with no dependencies

Any file that only reads tables nobody in the folder creates (raw tables) can go first.

3. Place the rest in waves

After the first wave, the next files are those whose inputs are all created by earlier files. Repeat until every file is placed.

4. Watch for loops

If you reach a point where every remaining file waits on another remaining file, you have a circular dependency. No order works until it is broken.

An example

Three files:

-- load.sql
create table raw_orders as select * from landing.orders;

-- staging.sql
create view stg_orders as select id as order_id, amount from raw_orders;

-- marts.sql
create table revenue as select sum(amount) as total from stg_orders;

load.sql creates raw_orders; staging.sql reads it and creates stg_orders; marts.sql reads that. The order is load, staging, marts. Alphabetical order would put load, marts, staging, which fails at marts.

The automatic method

The SQL execution order tool does all four steps from the files themselves. Drop the folder, a ZIP or the files onto the page and open the Run order tab. Each file is listed with a position number and a note saying which files it needs and which tables link them. Files with nothing in common keep alphabetical order, so the result is stable.

The same information is behind the graph: click a table in the Graph tab and choose Upstream to see exactly what has to exist before it can be built.

Dependency order is not the only constraint

Be careful about what the order does not cover:

  • Data timing: a script may need data loaded by a job outside your files.
  • Side effects: scripts that insert, update or delete may need a specific order even without a table dependency.
  • Idempotence: scripts that cannot be safely re-run need guarding in any order.
  • Dynamic SQL: tables referenced through strings built at run time are invisible to a static reader.

Treat the computed order as a safe starting point, then check scripts with side effects by hand.

What to do about circular dependencies

If two files depend on each other, one of them is probably doing two jobs. Split the file, or move the shared logic into a third script that both can read. The Issues tab names the tables involved in the loop.

For dbt projects

dbt works out the order itself, from ref() and source(). If you only want to see it, a dbt DAG viewer draws the graph and the Run order tab lists the files, without running dbt.

A checklist before you run scripts in order

Once you have the computed order, spend two minutes on these checks before executing a long list of scripts:

  • Confirm the first scripts only read tables that already exist, such as landing or raw tables.
  • Look for scripts that drop or truncate tables, and make sure nothing earlier in the order still needs them.
  • Run the scripts in a test schema first if they create or replace objects.
  • Keep a log of which scripts ran, so a failure halfway through tells you where to resume.

If you have to run the sequence regularly, convert it into a numbered file naming scheme or a small runner script, so the order is part of the repository and not just in someone's head.

For the wider dependency view, see how to find SQL dependencies across multiple files.

Open the SQL Execution Order Tool

Related tool pages

  • SQL Lineage Analyzer for a Folder of SQL Files: Upload a folder of SQL files or a dbt project and get table and column lineage across every file, a run order and impact analysis. Free, runs in your browser.
  • dbt Lineage Viewer for Your SQL Models: Drop your dbt models folder and see the lineage graph from sources through staging to marts, with column links. No dbt run, no warehouse. Free, private.
  • SQL Column Lineage Tool: See which source columns build every column of a view or table across many SQL files. Trace to the raw table or forward to dependants. Free, in your browser.
  • SQL Impact Analysis: What Breaks If a Table Changes: Pick a table and see everything downstream that depends on it, and every upstream source it is built from. Works across a whole folder of SQL files, free.
  • SQL File Lineage Tool: Free SQL file lineage tool: drop a folder of .sql files and see which file feeds which table, column by column. Nothing is uploaded.
  • SQL Query Visualizer: Free SQL query visualizer: paste a query or drop SQL files and see tables, joins and columns as a clear diagram. Runs in your browser, no upload.

Also try

How to use the SQL Execution Order Tool: step by step

  1. Drop the SQL files into the execution order tool.
  2. Open Run order and read the numbered list.
  3. Check Issues for circular dependencies and unreadable statements.

When to use the SQL Execution Order Tool

  • Find which SQL file to run first
  • Rebuild a database from scripts
  • Fix "table does not exist" failures
  • Detect scripts that depend on each other

Example

Input: a folder of .sql files with CREATE TABLE and INSERT statements

Output: Run order: 1. schema.sql, 2. stg_orders.sql, 3. fct_sales.sql …

Frequently asked questions

How do I determine the execution order of SQL scripts?

Run each script after the scripts that create the tables it reads. A tool that reads the SQL can compute this order automatically.

What if two scripts depend on each other?

That is a circular dependency. No order works until you break it, usually by splitting a script. The Issues tab shows the tables involved.

Is the computed order always safe?

It is a safe dependency order for tables. Scripts with side effects, external data loads or dynamic SQL still need a manual check.

Does it work with dbt?

Yes. dbt models are ordered by ref() and source(), and the tool shows the same order from the SQL files.

Tip: Name scripts with a numeric prefix once you know the order, for example 01_load.sql, so the order is obvious to the next person.