Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Database Internals the lesson

The sentence that says what a show in 8 chapters

Goal: answer 4 questions about 100,000 users, writing no step of how

1 of 8 Your guess

Before we start

Apps that save things, a sign-up form, a bank balance, a list of past orders, keep them in a database. You want one answer out of one: the names of everyone in the toy department.

Which picture is closest to how you get that answer?

Pick the closest. You find out which fits at the end.

the EMP tableNAMEDEPTTOYTOYTOYa row:one employeea column:one field ofevery rowthe whole grid: a table, called EMPExample rows: the lesson doesn't print them.

2 of 8 The problem

May 1974: a language for accountants

In 1970 Edgar Codd, at IBM, proposed keeping data as plain grids. The picture is one: employees, with each one's name and department.

The grid is a table, and this one is called EMP. Each line across is a row, one employee. Each line down is a column, one field of every row: NAME, or DEPT, the department.

Step 1 of 5

  1. FROM userswhich table
idnamecityemailsigned_up_at1bob_1Pune2carol_2Kolkata3dev_3Pune4esha_4Hyderabad5farid_5Hyderabad… 99,995 more rows, not drawn5 rows drawn + 99,995 not drawn = 100,000 rowsGrey bars: values the lessondoesn't print.

3 of 8 The idea

Say what: three clauses

Question 1: who are users 1 to 5, and where do they live?

Start by naming the table. In SQL each part of a question is a clause, and the first one the database reads is FROM users.

users has five columns: id, name, email, city, signed_up_at. Each user's id is their number, 1 to 100,000. The first five rows are drawn.

Step 1 of 4

  1. FROM usersthe first table
  2. JOIN accountsand a second one
  3. WHERE users.id <= 6users 1 to 6 only
usersaccountsidnameuser_idkindbalance1bob_12carol_23dev_34esha_45farid_56gita_61savings27,3042savings6,5293credit17,9033credit34,3844savings7,6766checking2336checking27,549pairs: none yetDrawn: 6 of 100,000 users,7 of 106,667 accounts.

4 of 8 The idea

Two tables, one answer

Question 2: which accounts belong to each of users 1 to 6?

Accounts live in a second table, accounts, 106,667 rows. Each account row carries a user_id: the id of the user who owns it, like a borrowing slip carrying a reader's card number.

Step 1 of 4

  1. FROM userswhich table
  2. WHERE id <= 5users 1 to 5
  3. GROUP BY cityone pile per city
  4. SELECT city, count(*)each pile's city and size
users 1 to 5one pile per citybob_1Punecarol_2Kolkatadev_3Puneesha_4Hyderabadfarid_5HyderabadPunecount 2bob_1dev_3Kolkatacount 1carol_2Hyderabadcount 2esha_4farid_55 rows in, 3 rows out: 2 + 1 + 2 = 5 users

5 of 8 The problem

Piles, and the blank that is not a value

Question 3: how many users live in each city? That is a question about piles, not rows.

GROUP BY city sorts the rows into one pile per city. count(*) counts the rows in each pile. One row comes out per pile. Here, the five users from before.

Step 1 of 6

your query: says what… WHERE users.id = 42the plannerdecides howusers, 100,000 rowsaccounts, 106,667 rowsuser 42, jumped to through the lookupthe table keeps on its idsall 106,667 checked for user_id 42users: 1 row touchedaccounts: all 106,667 rows read

6 of 8 The idea

Who picks the how

Every query so far said what it wanted and left the how alone. Something inside the database decides it, fresh, for every query. That part is the planner, the librarian of this story.

Two queries of the same shape: users joined to their accounts. First for one user, id 42. The planner jumped straight to user 42 through a lookup the table keeps on its ids, then read every account to find user 42's, since this practice database keeps no such lookup on user_id.

Step 1 of 3

7 of 8 Your guess

Your picture now

Goal met: four questions answered, users 1 to 5, 7 pairs, ten piles, and 90,063 not 100,000, with no step of how written by you. The planner wrote those. Which picture is closest now?

Pick one.

The sentence that says whatName the answer. The planner picks the steps.123SELECT the columnsFROM the tableWHERE the rows that count0steps of how, written by youJOIN pairs rows on ashared value: user_id = id7 → 8rows for users 1 to 6, thenLEFT JOIN keeps user 5NULL means unknown.Use IS NULL, never = NULL.90,063Pune or not Pune, of 100,000:9,937 unknown cities fall outchalkbrain.com
The sentence that says whatName the answer. The planner picksthe steps.1SELECT the columnsFROM the tableWHERE the rows that count0steps of how, written by you2JOIN pairs rows on a sharedvalue: user_id = id7 → 8rows for users 1 to 6, LEFT JOIN keeps 53NULL means unknown. UseIS NULL, never = NULL.90,063Pune or not Pune, of 100,000chalkbrain.com

8 of 8 Keep this

Keep this

A query names the answer: SELECT the columns, FROM a table, WHERE the rows that count. A join pairs rows on a shared value, GROUP BY folds them into piles, and the planner picks the steps.

The trap: NULL means unknown, and WHERE drops unknown. Ask about everyone and 9,937 users can vanish. Test for it with IS NULL.

Read the full lesson