The idea: removing the names is not enough

A hospital wants to share patient data for research. It deletes the names and keeps zip code, age, sex and diagnosis. That looks anonymous, but it often is not. Most people are the only one with their exact zip, age and sex. Another list that has names next to the same three columns, such as a voter roll, can be joined with the release on those columns, and the name comes back. Columns that are harmless alone but identify a person together are called quasi-identifiers. The diagnosis is the sensitive attribute.

Left: the released table, with row 2: zip 47602, age 27, sex F, diagnosis Asthma, and no name. Right: a toy public list with Ana: zip 47602, age 27, sex F. A join on zip, age and sex links them, so the result says Ana has Asthma.
The release has no names, but zip, age and sex are unique to row 2, so one join with a list that has names gives Ana's diagnosis.

Everything on this page is made up: 12 rows, three people on a toy public list (Ana, Ben, Cara). The join is shown only to explain the defence. The defence is k-anonymity: change the quasi-identifiers so that every row looks the same as at least k − 1 other rows (Demo: names removed, every row unique; Demo: a toy join puts names back: 3 of 3 people re-identified).

Equivalence classes and the k reached

Rows with the same values in all quasi-identifiers form an equivalence class. Anyone who knows a person's zip, age and sex can find the person's class, but not which row inside it is theirs. A table is k-anonymous when every class has at least k rows. The k a table reaches is the size of its smallest class. In the raw table every class has 1 row, so k = 1.

Finding the classes is a plain GROUP BY. In MariaDB, with the generalized values as columns, this lists the classes that are still too small for k = 3, and how many different diagnoses each class holds:

SELECT zip_g, age_g, sex_g, COUNT(*) AS size, COUNT(DISTINCT diagnosis) AS l
FROM released
GROUP BY zip_g, age_g, sex_g
HAVING COUNT(*) < 3;

The classes panel on the canvas is exactly this query without the HAVING, one line per group. How GROUP BY splits rows into groups is shown step by step on Pandas GroupBy: Split, Apply, Combine.

Generalization: move a value up its hierarchy

Generalization replaces a value with a less exact one. Each quasi-identifier has a hierarchy of levels. On this page the whole column moves to one level together (full-domain generalization), so every row is treated the same way.

Three hierarchies. zip: 47602 at level 0, 4760* at level 1, 47*** at level 2, ***** at level 3. age: 27, then 20–29, then 20–39, then *. sex: F, then *. Higher levels give bigger classes and lose more information.
Each step up a hierarchy merges more rows into one value: classes grow, and the data says less.

Generalizing zip to 4760* and age to 10-year ranges joins the rows in pairs. Two rows are still alone: row 10 (a woman in her 40s) and row 12 (the only person in her 50s). The table is still 1-anonymous (Demo: generalize ZIP and age, k is still 1: 7 classes of sizes 2, 2, 2, 2, 2, 1, 1).

Suppression: leave out the outliers

A few unusual rows can force a whole column far up its hierarchy. Suppression leaves those rows out of the release instead. With k = 2, rows 10 and 12 are suppressed and the other 10 rows form 5 classes of 2 (Demo: suppress 2 outliers, k = 2). Hiding sex as well makes classes of 4, 4, 3 and 1; suppressing row 12 then gives k = 3 (Demo: hide sex too, k = 3).

What it costs: information loss

Every step up a hierarchy and every suppressed row makes the data less useful. The page counts the loss with Sweeney's precision measure: for each of the 12 × 3 quasi-identifier cells, take its level divided by the top level of its hierarchy (zip and age have 3 levels above exact, sex has 1), and average. A suppressed row counts 1 for all three cells.

Levels (zip, age, sex)SuppressedClassesklLoss
exact, exact, kept012 × 1110.0%
4760*, 10-year, kept02, 2, 2, 2, 2, 1, 11122.2%
4760*, 10-year, keptrows 10, 125 × 22135.2%
4760*, 10-year, hiddenrow 124, 4, 33159.3%
4760*, 20-year, hidden08, 44466.7%
all hidden012125100.0%

The goal is the smallest loss that still reaches the k you need. Tools such as ARX search all level combinations (the generalization lattice) for it; with only 4 × 4 × 2 = 32 combinations here, you can try them by hand with the selects.

When k-anonymity still leaks: homogeneity and l-diversity

k-anonymity hides which row is yours. It does not hide what your class says. In the k = 3 table, Ben's class is (4760*, 30–39, *): 4 rows, and all 4 say Heart disease. The join cannot pick Ben's row, but it does not need to: Ben has heart disease (Demo: k = 3 but one class leaks). This is the homogeneity attack. An attacker with background knowledge ("Ana's class has Flu, Asthma and Diabetes, and Ana does not have diabetes") can narrow a class down the same way.

l-diversity adds a rule: every class must hold at least l different sensitive values. The simplest version, distinct l-diversity, is the COUNT(DISTINCT diagnosis) in the query above. Widening age to 20-year ranges mixes the heart patients with the younger rows: classes of 8 and 4, each with 4 diagnoses, so the table is 4-anonymous and 4-diverse, at 66.7% loss (Demo: l-diversity with 20-year ages).

Left: the class 4760*, 30–39, * with 4 rows, all Heart disease: k = 4 for this class but l = 1, so Ben's diagnosis leaks. Right: the class 4760*, 20–39, * with 8 rows and diagnoses Flu, Asthma, Diabetes and Heart disease: l = 4, the join cannot tell Ben's diagnosis.
Four rows that all say Heart disease protect nobody; l-diversity asks each class for several different diagnoses.

l-diversity is not the end either. In the 20-year class, 4 of the 8 rows say Heart disease: someone in that class has a 50% chance, far above the table's 33%. t-closeness asks that the spread of diagnoses in every class stays close to the spread in the whole table. The page only names it.

Where it fits

k-anonymity and l-diversity change a table once, before it is published, and give a guarantee about that table. They are one layer of defence among several. Access control decides who may read the raw rows at all (OWASP Broken Access Control). Differential privacy takes a different road: it adds calibrated noise to query answers and gives a guarantee that does not depend on what the attacker already knows (Differential Privacy: Laplace Noise, ε and the Budget). The join itself is ordinary SQL matching, like the row-by-row tests on SQL Subqueries, Row by Row.

See also Differential Privacy for protecting query answers with noise instead, Pandas GroupBy for grouping rows into classes, OWASP Broken Access Control for keeping the raw table out of reach, and Encryption vs Hashing for why hashing a zip code does not anonymise it either (a hash of the same zip is the same everywhere, so it joins just as well).

See also Differential Privacy, which adds Laplace noise to answers instead of changing the published rows, and bounds what any answer reveals about one person by e^ε.

What the page leaves out

Local recoding (different levels for different rows), the Mondrian and Incognito search algorithms, and the full generalization lattice search are only named. Entropy and recursive l-diversity, t-closeness's Earth Mover's Distance, and other loss measures (discernibility, average class size) are not computed. Attacks that combine several releases of the same table, or a release with a later update, are not shown. Noise-based protection is on the Differential Privacy page. MariaDB has no built-in k-anonymity feature: the GROUP BY query above only checks a table that has already been generalized.

On the mind map

Open the Web & Security mind mapWeb & SecurityCrypto building blocks: keys, hashes, signaturesCrypto building blocksStoring passwords: slow salted hash, never plaintextStoring passwordsBrowser rules: which request carries whatBrowser rulesRequest attacks: trick a browser or a serverRequest attacksLogin & identity: prove who you areLogin & identityOWASP Top 10 2025: common flaws, then the fixOWASP Top 10 2025OWASP: supply chain & ops: dependencies, logs and error pathsOWASP: supply chain & opsBroken access controlBroken access controlcheck ownership and role on the serverK-anonymityEvery zip/age/sex combo in at least k rowsDifferential privacyDifferential privacynoise b = Δ/ε, ratio ≤ e^ε, budget adds upSecurity misconfigurationSecurity misconfigurationdefaults, debug errors, missing headersCryptographic failuresCryptographic failuresno TLS, MD5, ECB mode, hard-coded keysInsecure designInsecure designtrusting the client, races, no try limitsSTRIDE threat modelSTRIDE threat modelSix threats per DFD element, each mitigatedAuthentication failuresAuthentication failuresstuffing, brute force: rate limit, MFAIntegrity failuresIntegrity failuresverify data and code: HMAC, SRI, fixed algOpen the Web & Security mind mapWeb & Securitycommon flaws, then the fixOWASP Top 10 2025Broken access controlBroken access controlcheck ownership and role on the serverK-anonymityEvery zip/age/sex combo in at least k rowsDifferential privacyDifferential privacynoise b = Δ/ε, ratio ≤ e^ε, budget adds upSecurity misconfigurationSecurity misconfigurationdefaults, debug errors, missing headersCryptographic failuresCryptographic failuresno TLS, MD5, ECB mode, hard-coded keysInsecure designInsecure designtrusting the client, races, no try limitsSTRIDE threat modelSTRIDE threat modelSix threats per DFD element, each mitigatedAuthentication failuresAuthentication failuresstuffing, brute force: rate limit, MFAIntegrity failuresIntegrity failuresverify data and code: HMAC, SRI, fixed alg
This page on the Web & Security mind map · 42 pages in 7 branches · open it to see the whole map and test yourself in recall mode.