Warehouse Layers and Staging

What is staging in a database?

Staging is a temporary area where loaded data is checked and reshaped before it reaches final tables. Here is how it works, with a simple SQL example.

On this page
  1. How a staging step works
  2. A simple example
  3. Transient and persistent staging
  4. Staging and the landing area
  5. Staging is not a staging environment
  6. Common problems

In a database, staging is a holding area for data that is being prepared for its final home. Data is loaded into staging tables, checked and cleaned there, and then moved into the permanent tables. Staging keeps messy input out of the tables people query and report from.

How a staging step works

A typical load has four steps:

  1. Extract the data from a source, such as an application database, a file export or an API.
  2. Load it into staging tables with little or no change.
  3. Check and transform it in staging. That can mean removing duplicates, fixing formats, looking up codes, and dropping rows that fail basic rules.
  4. Insert or merge the cleaned rows into the final tables.

Staging can take a few forms. It may be a separate schema or database on the same server. In many cloud warehouses, it is a set of raw tables inside the same platform. The idea is the same in each case.

Much of this checking and reshaping is written in plain SQL. For the wider question of how SQL fits into loading, see is SQL used for ETL.

A simple example

The SQL below reloads a staging table from a source, then moves only the valid rows into the final table:

-- Staging: emptied and reloaded on every run
TRUNCATE TABLE staging.customers;

INSERT INTO staging.customers (customer_id, email, country_code)
SELECT id, LOWER(TRIM(email)), UPPER(country)
FROM source.customers;

-- Final table: only rows that pass a basic check
INSERT INTO dw.customers (customer_id, email, country_code)
SELECT customer_id, email, country_code
FROM staging.customers
WHERE customer_id IS NOT NULL
  AND email LIKE '%@%';

TRIM removes stray spaces from the email before it is saved. For more on how it behaves, see what does trim() do in SQL. The check on the email is deliberately crude, and a real pipeline would use stricter rules.

The sketch has a gap. If it runs twice, the final table gets the same rows again. Real pipelines avoid that with a MERGE statement, where the database supports one, or with delete-and-reload logic for the affected rows.

Transient and persistent staging

Many staging areas are transient. Their tables are cleared before or after each run, so they hold only the latest load.

Some are persistent. A persistent staging area keeps the source’s full change history, which helps with audits and with questions like what a record said last month.

Staging and the landing area

Many warehouse designs keep a raw copy first, in a landing area in data warehouse setups, before any staging happens. Sources don’t agree on the line between the two. In the usual split, the landing area holds raw data as it was received, and staging is where the work happens. Some teams use one area for both jobs.

Staging is not a staging environment

In software teams, “staging” also means a copy of an application environment used for testing before release. That is a different thing. A staging table holds data during a load. A staging environment holds a test version of an app. Check which one a colleague means before acting on it.

Common problems

  • Old rows left behind. If a run fails halfway and the staging table isn’t cleared first, old rows mix with new ones. Clearing the table at the start of each run prevents this.
  • No checks before loading. Bad rows pass straight into the final tables, and reports show the errors.
  • Skipping staging for speed. Loading straight into final tables is quicker to build, but it makes reruns and testing harder.

The complete guide to warehouse layers and staging

What Is the Landing Area in a Data Warehouse?
Read the guide

Keep exploring

Guides from other topics

All topics →