What you'll learn
Develop a precise mental model for relations, rows, attributes, domains, and the data types that protect meaning. The lab uses PostgreSQL while identifying the semantics that transfer to other relational systems.
By the end of this lesson, you'll be able to:
- Apply Relations and tuples to a realistic data question
- Apply Column domains to a realistic data question
- Apply Text and numbers to a realistic data question
- Apply Dates and timestamps to a realistic data question
Core mental model
SQL is declarative: describe the result or invariant you need, then let the database choose a physical execution strategy. Use this table to connect syntax to design decisions.
| Concept | What it means | Decision rule |
|---|---|---|
| Row grain | The real-world fact represented by one row | Write the grain in plain language before choosing columns |
| Domain | The allowed kind and range of a column value | Choose the narrowest type that preserves meaning |
| Timestamp with time zone | An instant normalized by PostgreSQL | Use timestamptz for real instants and convert for display |
Professional workflow
Work from a defined question and result grain, then verify correctness before performance.
- State the relational type model question and the exact grain of the expected result.
- Inspect table definitions, keys, constraints, representative values, and row counts.
- Write the smallest correct query with explicit columns, aliases, and predicates.
- Test missing, duplicate, boundary, and NULL cases before trusting the result.
- Inspect the execution plan or affected rows when cost or data change matters.
- Save the query with its assumptions, parameters, verification, and recovery notes.
Make results explainable
Guided SQL lab
Inspect types and values together
Catalog metadata explains declared types while sample rows reveal actual domain shape.
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_schema = 'course' AND table_name = 'books'
ORDER BY ordinal_position;
SELECT id, title, price, published_at
FROM course.books
LIMIT 5;Production practice
Contract
Define the expected row grain, inputs, output columns, invariants, and failure or empty-result behavior before writing SQL.
Verification
Use representative fixtures and independent row-count, uniqueness, NULL, and boundary checks; compare plans when cost matters.
Operations
Save reviewed SQL with explicit schema names where appropriate, bounded scope, least privilege, observability, and a recovery path for changes.
Common failure mode
Independent workshop
Build a review-ready relational type model lab against the course commerce dataset.
Your finished workshop must include:
- Relations and tuples
- Column domains
- Text and numbers
- Dates and timestamps
- Boolean values
- Verification notes and edge-case evidence
Definition of done
Recap & quick check
Key takeaways
- Row grain: Write the grain in plain language before choosing columns
- Domain: Choose the narrowest type that preserves meaning
- Timestamp with time zone: Use timestamptz for real instants and convert for display
Quick check
1. Which rule best applies to Row grain?
2. Which rule best applies to Domain?
3. Which rule best applies to Timestamp with time zone?
Next: SELECT Lists, Aliases & Expressions