Database Testing with SQL
What the screen shows is not always what was saved. SQL lets testers verify the data itself.
On this page · 5 min · Advanced
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Advanced
- Updated
- Oct 29, 2026
Related tutorials
Learning objectives#
- Verify UI actions in the database
- Check constraints and data integrity
- Find duplicates and orphan records
- Validate report numbers with SQL
Explanation#
After a UI or API action, query the tables to confirm the right rows were inserted or updated — with correct values, timestamps and statuses.
Integrity checks: required fields not null, unique values unique, foreign keys pointing to real rows.
Report validation: recompute a dashboard number with your own SQL and compare.
Use read-only accounts on shared environments and never modify production data.
Code example: QA checks after placing an order#
-- 1. Was the order saved correctly?
SELECT TOP (1) Id, CustomerId, Total, Status, CreatedAt
FROM Orders WHERE CustomerId = 42 ORDER BY CreatedAt DESC;
-- 2. Was stock reduced?
SELECT Name, Stock FROM Products WHERE Id = 1;
-- 3. Any duplicate customer emails?
SELECT Email, COUNT(*) AS Copies FROM Customers GROUP BY Email HAVING COUNT(*) > 1;
-- 4. Any order lines pointing to missing orders (orphans)?
SELECT ol.Id FROM OrderLines ol LEFT JOIN Orders o ON o.Id = ol.OrderId WHERE o.Id IS NULL;
Id CustomerId Total Status CreatedAt 1002 42 56400.00 Paid 2026-10-29 10:15 Laptop | 6 (was 8, ordered 2 ✔) (0 duplicate emails) (0 orphan order lines)
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- Confirm the new order row and its values.
- Confirm side effects such as stock changes.
- Check uniqueness rules.
- Check referential integrity.
Real-world use case#
A tester finds that cancelled orders never restore stock — invisible in the UI, obvious in the database.
Common mistakes#
Use read-only access unless test data setup requires changes.
Caches and formatting can hide wrong stored data.
Best practices#
- Read-only accounts for QA
- Save useful queries with your test cases
- Check side effects and audit fields
- Validate reports independently
Remember this#
UI shows; database proves.
Check rows, side effects, integrity.
Read-only by default.
Practice exercise#
Your task: Write a query to find products with negative stock.
Show solution
SELECT Id, Name, Stock FROM Products WHERE Stock < 0;
Mini quiz#
Which query pattern finds orphan child rows?
Want more? Practise all Manual Testing MCQs · Manual Testing interview questions
My notes#
Saved in this browser.