Module 3. Practice: from requirements to automated tests · Lesson 24 of 41 · 16 min
SQL for testers: verifying data after actions in the UI
SELECT, WHERE, JOIN, GROUP BY and subqueries on an online store example; how to find mismatches between the UI, the API and the database.
Why QA needs the database
The UI can say "Order placed" while the database holds two identical rows, the wrong total or an empty discount field. SQL lets a tester verify the fact instead of trusting the screen.
Work only with a test database and a read-only account. Never run UPDATE or DELETE in shared environments without agreement.
Example tables
- users (id, email, created_at)
- orders (id, user_id, total, discount, promo_code, status, created_at)
- promo_codes (code, percent, valid_until, is_active)
The basics
-- the test user's latest orders
SELECT id, total, discount, promo_code, status
FROM orders
WHERE user_id = 42
ORDER BY created_at DESC
LIMIT 5;
JOIN
-- orders with the user's email, promo-code orders only
SELECT u.email, o.id, o.total, o.discount
FROM orders o
JOIN users u ON u.id = o.user_id
WHERE o.promo_code = 'SALE10';
You need a LEFT JOIN to find "orphans" — for example, users without a single order, or orders with a promo code that doesn't exist:
SELECT o.id, o.promo_code
FROM orders o
LEFT JOIN promo_codes p ON p.code = o.promo_code
WHERE o.promo_code IS NOT NULL AND p.code IS NULL; -- a code missing from the lookup table
GROUP BY and finding anomalies
-- duplicate orders: same user, total and minute
SELECT user_id, total, date_trunc('minute', created_at) AS m, count(*)
FROM orders
GROUP BY user_id, total, m
HAVING count(*) > 1;
-- the discount doesn't match the promo code's percentage
SELECT o.id, o.total, o.discount, p.percent
FROM orders o
JOIN promo_codes p ON p.code = o.promo_code
WHERE o.discount <> round((o.total + o.discount) * p.percent / 100.0, 2);
A typical check after a UI action
| Step | Check in the database |
|---|---|
| Applied SALE10 to a $125.00 cart | discount = 12.50, total = 112.50 |
| Placed the order | Exactly one row, status = 'new' |
| The promo code is single-use | Applying it again doesn't create a second redemption |
| Canceled the order | status = 'cancelled', the redemption isn't refunded twice |
Common mistakes
- Comparing decimal numbers without rounding.
- Forgetting about time zones in created_at.
- Searching by email without ignoring case (lower(email) = lower('...')).
- Concluding there's a defect without checking you're looking at the same environment's database.
Data with valid checksums is in the test data generator. SQL interview questions are in the junior QA section.
Key takeaways
- SQL lets you verify the fact in the database instead of trusting a UI message.
- A LEFT JOIN with IS NULL finds orphans and broken relationships.
- GROUP BY with HAVING count(*) > 1 catches duplicates quickly.
Check yourself
1. Which JOIN shows users without a single order?
2. How do you find duplicate orders?
3. Can QA run DELETE on a shared staging database without agreement?