INTERMEDIATE SQL Track · 5 Lessons

Intermediate SQL
Track

Connect all four tables, aggregate soil data, and write your first real-world calculation: Available Water Storage from horizon data.

Lessons
01

The Full JOIN Chain

legend → mapunit → component → chorizon — all four tables

Intermediate

Now we go all the way down to horizon-level data. The key choice: INNER JOIN vs LEFT JOIN for chorizon. Use LEFT JOIN — not all components have horizon records, and an INNER JOIN would silently drop them.

INNER vs LEFT: INNER JOIN only returns rows that match on both sides. LEFT JOIN returns all rows from the left table even if there's no match on the right. For horizons, always LEFT JOIN unless you specifically need only components with horizon data.
Full Profile Query — All Four Tables
02

Aggregation — GROUP BY, AVG, SUM

Summarizing soil properties across map units

Intermediate

Aggregation lets you summarize data — average organic matter by drainage class, total acres by HSG, min/max texture by soil order. The pattern is always: SELECT group columns + aggregate functions, FROM + JOINs, GROUP BY the same group columns.

Average Surface OM by Drainage Class — Iowa
03

CASE WHEN — Classification Logic

Categorizing soils by property — the SSURGO way

Intermediate

CASE WHEN is SQL's if/then — essential for translating coded values into readable labels, classifying soils into risk tiers, or building custom rating scales.

HSG Classification with Description
04

CTEs — WITH Clause

Breaking complex queries into readable, named steps

Intermediate

A CTE (Common Table Expression) names an intermediate result set. This turns a complex nested query into a series of readable steps — and in SSURGO, where queries often span 4+ tables with multiple conditions, CTEs are essential for maintainability.

CTE syntax: WITH name AS (SELECT …) SELECT … FROM name. You can chain multiple CTEs: WITH a AS (…), b AS (SELECT … FROM a) SELECT … FROM b.
CTE — Dominant Component per Map Unit, Then Aggregate
05

Available Water Storage

A real-world multi-step soil calculation

Intermediate
What is AWS? Available Water Storage measures how much plant-available water a soil holds — the hydraulic buffer between drought and a successful harvest. It's calculated as the sum of AWC × horizon thickness across depth zones.

Formula: AWS = Σ (min(hzdepb_r, 150) − max(hzdept_r, 0)) × awc_r for each horizon within 0–150 cm.

Available Water Storage 0–150cm — WI025
Real science: A fine loam in the Corn Belt typically holds 8–10 inches of plant-available water to 150 cm depth. Sandy soils may hold only 2–3 inches. This difference determined which fields survived the 2012 drought.
Up Next

Ready for Expert?

Soil organic carbon formula, NCCPI productivity index, interpretations, and spatial AOI queries.

Expert Track → Open Query Lab