grok14ENGINEERING FIELD SCHOOL
Build / Week 3

SQL & data contracts

Make business calculations reliable by defining grain, validating joins, and treating data assumptions as contracts.

5 lesson sections16-hour study & practice planModule 02 or equivalent experience

See the system

Make business calculations reliable by defining grain, validating joins, and treating data assumptions as contracts.

Source records pass through validation and curated tables. Transformations establish usable entities before search indexing. Reconciliation checks completeness, freshness, updates, and deletions.
From source records to reliable evidence. Source records pass through validation and curated tables. Transformations establish usable entities before search indexing. Reconciliation checks completeness, freshness, updates, and deletions.
Module 03 / Lesson 01

Define the grain before writing a query

The grain is what one row represents: a ticket, an event, a document revision, or a daily snapshot. If you join ticket rows to event rows, a ticket may appear several times. Counting the result can exaggerate workload and cost.

State the primary key, business key, and timestamp semantics. A creation time is different from an ingestion time. Explicit grain and time definitions prevent dashboards and model features from telling incompatible stories.

Apply the ideaDefine the grain of tickets, ticket_events, policy_versions, and daily_team_metrics.
Module 03 / Lesson 02

Understand nulls and join cardinality

NULL represents an unknown or missing value, not zero or an empty string. Comparisons with NULL require appropriate SQL semantics. Use COALESCE only when the replacement has a defensible business meaning.

Before a join, establish whether each side has one or many rows per key. Check unmatched keys and unexpected multiplication. A left join preserves the left side’s unmatched rows, but a later filter on the right-hand table can accidentally remove them.

Apply the ideaWrite a query that reveals duplicate customer IDs and another that finds orphan tickets.
Module 03 / Lesson 03

Use windows to preserve row detail

Window functions calculate across related rows without collapsing all rows into an aggregate. ROW_NUMBER can select the latest revision when ordering includes a deterministic tie-breaker. SUM over an ordered window can produce a running total.

Be precise about the time range and frame. “Latest” needs an ordering rule for identical timestamps, and a cumulative calculation needs a defined partition. Otherwise reruns may select different records.

Apply the ideaSelect one current policy version per document, with a deterministic tie-breaker.
Module 03 / Lesson 04

Model changes explicitly

Operational records change. A current-state table answers what is true now; a history table can answer what was true when a decision happened. Slowly changing dimensions preserve selected attribute changes, but add complexity that needs a real analytical purpose.

For AI auditability, retain a stable document revision identifier with the answer evidence. If the source is later edited, an answer should remain explainable against the version actually retrieved.

Apply the ideaDesign fields that link an answer to the exact policy revision used.
Module 03 / Lesson 05

Turn assumptions into quality rules

A data contract names fields, types, keys, allowed values, freshness expectations, and ownership. Quality checks need an action: reject, quarantine, alert, or tolerate within a defined threshold. A dashboard warning without an owner is not an operational response.

Test transformations against small fixtures with known results. Reconcile source and target counts carefully: deduplication or filtering can legitimately change counts, so explain the difference instead of requiring equality everywhere.

Apply the ideaCreate five contract checks and assign an action and owner to each.

Worked scenario

A dashboard reports 240 tickets after joining 80 tickets to their three average status events. COUNT(*) measures joined rows. Aggregate events to one row per ticket before joining, or count distinct ticket IDs when that matches the intended metric. Verify the answer against a fixture with known ticket counts.

A small example

This example isolates one concept. Read its boundary conditions before adapting it to an application.

sql
WITH ranked AS (
  SELECT document_id, revision_id, body,
         ROW_NUMBER() OVER (
           PARTITION BY document_id
           ORDER BY effective_at DESC, revision_id DESC
         ) AS rn
  FROM policy_versions
)
SELECT document_id, revision_id, body
FROM ranked
WHERE rn = 1;

Practical assignment

This is a practical design or implementation assignment. Use synthetic data. Where managed services are required, verify account access, costs, supported features, and cleanup before provisioning.
  1. Create a small synthetic ticket/event dataset.
  2. Document each table’s grain and keys.
  3. Write join, orphan, duplicate, and null checks.
  4. Calculate workload without event multiplication.
  5. Version the schema and define quality failure actions.
  6. Submit SQL plus fixture-based expected results.

What to submit

Submit the artifacts named above, a short explanation of your decisions, and evidence of the checks you performed. Distinguish measured results from estimates and designs from executed integrations.

Review dimensionSubmission evidence
CorrectnessShow the expected behavior and a meaningful counterexample.
ReproducibilityState setup, inputs, versions, and what was actually executed.
Delivery judgmentExplain the client impact, alternative, and unresolved assumption.
Operational boundaryIdentify permissions, failure behavior, and any resource cleanup.

Knowledge check

1. What does a table’s grain describe?
2. Why add a tie-breaker when selecting the latest row?
3. A quality check fails. What is required?

Answer guide
  1. What a single row represents. Grain determines which joins and aggregations are meaningful.
  2. To guarantee a deterministic choice. Equal timestamps alone do not determine a unique winner.
  3. A documented response and owner. The action should reflect the business impact and data contract.

References & next step

Platform examples are environment-dependent. Start with the official documentation in the reference library and verify the exact cloud, region, privileges, and versions you use.

Open the official reference library

Editorial edition: 5 October 2026. The local reference lab is executed locally; this course does not claim a live Databricks deployment.