skipnothing/SQL

Tables, Rows, Columns

A CSV says Ada,ada@x.example,28, and a database asks back: what columns, what shape do these values fit? Once that's declared, the engine knows what to allow and what to reject, every row follows the same plan.

  • ▸Every column commits to a kind of value before any row arrives.
  • ▸Run the same query twice without ordering, the engine is free to return rows in different sequences.
  • ▸Two identical inserts get stored as two identical rows unless a key constraint says no.
01

The Shape: Columns with Names and Types

Start with a stream of values: 1, Ada, ada@x.example, then 2, Brent, brent@y.example, then 3, Cleo, cleo@z.example. Out there in the world, those are just commas and characters, a CSV row, a JSON line, an array.

Declare three columns up front: id for whole numbers, name for text, email for the same. The declaration is the shape, and from now on every row must fit it. A 7 drops into id. The string dee@x.example drops into email. Try to put seven (the word, not the number) into id, the database rejects it at the boundary; the column knew what kind of value it was supposed to hold.

Three columns declared once. Every row that arrives looks the same way: three pieces of data, in the same positions, of the same kinds. The shape is the contract, and the database enforces it on the way in, not as a cleanup step later.

AIM EACH VALUE AT A TYPED COLUMN
02

One Value per Cell

A row holds one value per column. So what happens when a user has three labels, vip, early, beta, and the temptation is to cram them into a single tags cell as the string 'vip,early,beta'?

The data fits, but the cell now carries three things instead of one. Every query that needs to find users with the vip tag has to parse the string: split on commas, strip whitespace, match each piece. The cell looks like one value but means three; the database treats it as text, not as a set of labels. An index is the engine's shortcut, a structure it reads instead of every row. A plain index on tags can't help; only one built over the re-split string can. Spelling matters. A typo in one of the comma-joined pieces hides forever.

The fix isn't bigger cells, it's a second table: user_tags(user_id, tag), one row per (user, tag) pair. Now vip lives in its own cell, sitting in the tag column with a known type. Filtering for VIPs is a direct match instead of a string parse. One value per cell is what every later SQL operation depends on: each column has a known type, each cell holds one value of that type, and every operation, filter or count, can rely on it.

FIND THE VIPS IN A CRAMMED CELL
03

Rows Have No Inherent Order

Run a query against users and the database returns three rows: Ada, Brent, Cleo. Rewrite Ada's row without changing a single value, run the same query, and the rows come back as Brent, Cleo, Ada. No value changed; the table never had an order in the first place.

A relational table is a set, not a list. The engine stores and reads rows however it wants. The rows are the same rows. The order is the engine's business, not the data's.

If you need a specific order, you ask for it. The next unit covers the syntax in full; the move here is the prediction. Without an explicit order request, the same query can return the same rows in any sequence the engine prefers, and queries that depended on 'whatever order the rows happened to be in' break silently the moment the engine picks a different way to read the table.

REORDER THE ROWS WITHOUT CHANGING THE DATA

Keep going, sign up to unlock the rest

1 more part in this topic, plus 30+ more topics in SQL.

Sign up, it's freeSee the full SQL
The Relational Model0/4
#2 Types & NULL