Manual Testing · Specialised Testing

Database Testing with SQL

What the screen shows is not always what was saved. SQL lets testers verify the data itself.

Advanced 5 min Lesson 21 of 24
Before you start:SQL Server: SELECT
0/24 in Manual Testing
On this page · 5 min · Advanced

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#

SQLQA checks after placing an order Illustrative
-- 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;
Expected output
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#

  1. Confirm the new order row and its values.
  2. Confirm side effects such as stock changes.
  3. Check uniqueness rules.
  4. 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#

Running UPDATE/DELETE on shared databases

Use read-only access unless test data setup requires changes.

Trusting the UI alone

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
SQL Illustrative
SELECT Id, Name, Stock FROM Products WHERE Stock < 0;

Mini quiz#

Database Testing with SQL quizMultiple choice Question 1 of 1

Which query pattern finds orphan child rows?

Score so far: 0/0

Want more? Practise all Manual Testing MCQs · Manual Testing interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.