Module 1 · Tables, types and the question you are actually asking

Grain: the sentence to say before you write the query

In this lesson

  • 8 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 10 of 12 in this chapter

This chapter’s goalSet up a working database on your own machine, read an unfamiliar schema well enough to say what one row of each table means and which columns join to which, and state the grain of any result you are about to compute before you compute it.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmNot availableNot available
Full audiobookNot availableNot available
The grain changes at the join, and every aggregate reads the new onecustomersone row per customerordersone row per ordercustomers JOIN ordersone row per order; the customer’s columns repeatsum(credit_limit) over that resulteach limit counted once per order: wrongAggregate orders first, then join one to oneone row per customer, and the sum is rightRead the grain at each step. A customer-level column summed after a one-to-many join is inflated onceper order, and nothing in the output shows it. Aggregating the many side first keeps the grain, andonly then is sum(credit_limit) the number it looks like.
Read the grain at each step. A customer-level column summed after a one-to-many join is inflated once per order, and nothing in the output shows it. Aggregating the many side first keeps the grain, and only then is sum(credit_limit) the number it looks like.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 7Free · no account needed
Lesson steps

One sentence, and most wrong answers do not happen

Before writing any query, finish this sentence out loud:

One row of my result is one ____.

One customer. One order. One customer-month. One payment attempt. If you cannot finish it, you are not ready to write the query, and the number you produce will be wrong in a way that does not look wrong.

That sentence names the grain of your result. Grain is the most useful idea in this course and almost nobody is taught it explicitly.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Building Apps with AI exam.

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘Grain: the sentence to say before you write the query’, in Data, SQL and Getting to the Answer. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/data-and-sql/the-grain-of-a-result (Accessed: 29 November 2026).