QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › Database structure

What are normalisation and denormalisation?

Normalisation splits data across tables so each fact is stored once, removing update anomalies. Denormalisation is deliberate duplication for read speed, for example a stored balance. Every copy can drift from its source, so QA finds out whether it is an intentional snapshot required by the specification (a price in an order) or a cache of a derived value, which needs a consistency check.

SQL · Constraints and indexes

A user changed their email and old payments still show the old one. A bug or by design?

In the payment history the same customer appears with two different emails: the new one in newer rows, the old one in older rows. One developer says "it is stored that way, a snapshot at payment time". Another says it was simply forgotten. The answer depends on how the schema is designed and whether the copy is deliberate. This lesson is about normalisation, denormalisation and what follows for tests.

How it works and why. Normalisation splits data across tables so that each fact is stored once. The aim is to remove update anomalies. If the email sits in every transaction, changing it means dozens of rows and some can be missed. The normal forms in plain words: 1NF, one value per cell (not a list "+380…, +380…" separated by commas); 2NF, every non-key column depends on the whole key, not part of it; 3NF, non-key columns do not depend on one another (the email belongs to the user, not to the transaction). The result: a transaction stores user_id, the email lives in users, and a change happens in one place.

Denormalisation is deliberate duplication for read speed: a separately stored balance instead of summing all transactions, a comment counter, a copy of a product name in an order. The price: copies can drift from the source, so they must be maintained (code, a trigger, a background job) and checked.

Example 1: an update anomaly. A non-normalised table and detecting the drift with a query:

-- before: the email repeats in every row
-- transactions_flat: (id, user_id, user_email, amount)
--   (1, 10, 'ana@old.com', 100)
--   (2, 10, 'ana@old.com',  50)
--   (3, 11, 'bo@test.com',  70)

-- the user changed email, only one row was updated
UPDATE transactions_flat
SET user_email = 'ana@new.com' WHERE id = 1;

SELECT user_id, COUNT(DISTINCT user_email) AS emails
FROM transactions_flat
GROUP BY user_id
HAVING COUNT(DISTINCT user_email) > 1;
-- result: user_id 10, emails 2

The query groups by user and counts distinct emails. For user 10 there are two: a drift. Such a check (the number of distinct values of a "dependent" field per owner) finds under-normalised data. Note: if by requirement the email in a payment is a snapshot, the drift is not a bug. First find out whether the copy is deliberate.

Example 2: a denormalised balance. The balance is stored in the account and the source of truth is the sum of transactions. A consistency check:

-- accounts: (1, balance 150), (2, balance 80), (3, balance 0)
-- transactions: account 1: 100 and 50; account 2: 100 and -30; account 3: none
SELECT a.id, a.balance, COALESCE(SUM(t.amount), 0) AS calc
FROM accounts a
LEFT JOIN transactions t ON t.account_id = a.id
GROUP BY a.id, a.balance
HAVING a.balance <> COALESCE(SUM(t.amount), 0);
-- result: id 2, balance 80, calc 70

Verification against the data above: account 1 has 100 + 50 = 150 and matches; account 3 with no transactions has calc 0 thanks to COALESCE and matches too; account 2 has 100 + (-30) = 70 while the field holds 80: a mismatch, a synchronisation bug. Without LEFT JOIN an account with no transactions would drop out of the check, and without COALESCE its sum would be NULL.

A copy in the database: a snapshot or a forgotten duplicate

A deliberate snapshot

order_items.unit_price

The price at purchase time must not follow the catalogue. You check that it does not change.

A cache of a derived value

accounts.balance

Must always equal the sum of transactions. You check with a consistency query after every change.

Figure 1. Both copies are legitimate, but they are checked in opposite ways.

Limits and alternatives. Normalisation is not a goal in itself: more tables mean more JOINs and slower reads, and for reports and analytics wide denormalised tables or materialised views are convenient. Read-heavy systems often denormalise on purpose. Higher normal forms (BCNF, 4NF) are rare in practice. For a tester another thing matters more: knowing where the schema holds copies and having a consistency check for each.

Common mistakes.

  • Treating any copy as a bug without asking whether it is a snapshot by requirement.
  • Checking only one place after a change: users was updated, but was the copy?
  • Forgetting a path without UI (an import, a background job) that changes the source data and misses the copy.
  • Checking consistency only on new data: old data may hold the drift.
  • Writing a check without LEFT JOIN and COALESCE: empty cases (an account with no transactions) drop out.

Check yourself

The order_items table has a unit_price column copying the price from products. A developer changed the price in products and old orders did not change. Is it a defect?

Answer

No, it is a deliberate snapshot: the price at purchase time must stay as it was, otherwise order history would drift. A defect would be old orders changing. The test checks precisely that the snapshot is unchanged.

The phones column stores "+380501112233, +380671112233". Which normal form is violated and what is the practical problem?

Answer

1NF is violated: the cell holds several values. Problems: searching by one number needs LIKE, uniqueness or a foreign key cannot be set reliably, changing one number means rewriting the string. A separate phones table with one number per row is better.

In an interview. Junior: Normalisation is designing tables so each fact is stored in one place, without duplication. Denormalisation is deliberate duplication for speed, for example a separately stored balance. For QA every such copy is a desync risk. Middle/Senior: First I find out whether the copy is a snapshot by requirement (a price in an order) or a cache of a derived value (a balance). For a cache I write a consistency query with LEFT JOIN and COALESCE and run it after every change and on old data. For a snapshot I check that it does not change with the source.

More articles in this chapter