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

SQL › Database basics

Why does QA need to know SQL?

QA needs SQL to check the system state deeper than the interface: the UI may show success while the database holds a wrong amount or status. SQL helps verify the result of an action, prepare test data, locate a bug and find integrity violations. Checks on a shared environment use a read-only account, data is changed only in a transaction with a condition, and real personal data is never used.

SQL · Basics

The UI says "Transfer successful". How do you make sure the money really changed the accounts?

A test transfers 100 from account 10 to account 11. The UI shows "Transfer successful", the green tick is in place. But the message only says that the client received a response without an error. Whether the balances changed, whether a history record appeared, whether the fee went off is visible only in the data. So a QA engineer who can write SQL checks the system deeper than the interface. This lesson is about where exactly SQL is useful and where its limits are.

How it works and why. A system consists of layers: the interface shows, the API responds, the database stores. Each layer can tell something that another does not know: the UI may be optimistic and show success before the server answers, the API may return 200 on a partial failure, the database is the source of truth about the state. A bug that lives only in the data is never visible from "above".

Where to look for the truth

UI: "Transfer successful"what the user sawAPI: status and response bodywhat the server answeredDatabase: balances, history, statuseswhat was really storedSide effects: logs, queuesemails, events, audit
Figure 1. The deeper the layer, the closer to the actual state. SQL gives access to the database layer.

What SQL gives a tester.

  • Checking the result: after an action in the UI or the API the amount, status, fee and history record are right.
  • Test data: find a user in the needed state or create one without going through ten UI steps.
  • Diagnostics: is the bug in the frontend, in the API or in the data? A database query often answers in a minute.
  • Integrity: duplicates, orphan records, impossible states.
  • Automated tests: preparing and cleaning data, checks after actions.

Example: from a "green" UI to a found defect. First the test checks only the message:

// before: only what the UI showed is checked
await page.getByRole('button', { name: 'Transfer' }).click();
await expect(page.getByRole('status')).toHaveText('Transfer successful');

Such a test is green even when no money moved. We add a database check after the action:

SELECT id, balance FROM accounts WHERE id IN (10, 11);
-- illustration: 10 → 500.00 (was 500), 11 → 0.00

SELECT id, from_account, to_account, amount, status
FROM transactions
WHERE from_account = 10
ORDER BY created_at DESC
LIMIT 1;
-- illustration: one row with status PENDING

Here (the data is illustrative) the balance of account 10 did not change and the transaction stayed pending although the UI showed success. This is a defect: the customer was told about a success that did not happen. Without the query this bug would go unnoticed. The UI showed what it was told, so the cause must be sought deeper, in the API or in the data.

What to check after a transfer

Not enough

Only the UI message

It shows that the client got no error. It does not prove the balances and history are right.

Enough

UI + API response + database state

Balances of both accounts, one history record with a status, the expected fee, nothing extra.

Figure 2. The same scenario is checked on several layers, and a mismatch between layers points to the place of the defect.

Limits and alternatives. SQL does not replace UI and API tests: it checks state, not what the user sees. Database access is often restricted: on a shared environment a separate read-only account is made, there is no access to production, and real user data (personal data) is not copied into a test environment. If the database is not visible, state is checked through the API, and the application's ORM logic is tested through the application rather than around it. Database replicas can lag by fractions of a second, so a check right after an action may see the old state: in an automated test it is retried with a timeout instead of a pause. And the main rule: changes on a shared database only with a transaction and a condition, while reading is free.

Common mistakes.

  • Treating the green tick in the UI as proof that data was saved correctly.
  • Writing a data change on a shared environment without WHERE and without a transaction.
  • Checking only one layer: the bug stays where nobody looked.
  • Copying real user data into a test database.
  • Checking the database right after an action without retrying: the replica or queue has not caught up.

Check yourself

The UI shows "Saved" but the database has no record. Where to look for the cause and what kind of defect is it?

Answer

First check the API response (status and body), then the write path. If the API returned success without a write, the defect is in the backend; if there was an error and the UI ignored it, it is in the frontend. The API response separates the one from the other, and the database query confirms the record is really missing.

Why should real production user data not be copied into a test database for checks?

Answer

It is personal data: its protection is regulated by law and company policy. For checks, synthetic data or anonymised copies are created, and queries are not run on production.

In an interview. Junior: SQL lets me verify the truth outside the interface. The UI may show "success" while the database holds the wrong amount or status. I also use SQL to quickly find or prepare test data and to localise a bug: frontend, backend or data. Middle/Senior: I check several layers: the UI, the API response and the database state. A mismatch between layers shows where the defect is. I look for integrity violations (duplicates, orphan records). I work carefully: a read-only account, changes only in a transaction with a condition, no real personal data, and I retry the post-action state check with a timeout.

More articles in this chapter