Time to put everything together. Open the Banking Portal and work through these exercises. Each one targets a specific validation technique from this chapter. Do not just read — write the SQL yourself, run it, and see the results.
Exercise 1: Field-Level Validation
Query information_schema to list all columns in the accounts table with their data types and NULL rules.
Find any accounts where balance is NULL or negative.
Find any users where phone number length is not exactly 10 digits.
Check if the email column has a UNIQUE constraint by trying to insert a duplicate.
Verify that the balance column uses DECIMAL, not FLOAT.
Exercise 2: Record-Level Validation
Write a query to find any transactions where the amount is zero, negative, or NULL.
For any order, verify that the order total equals the sum of its line items.
Find insurance policies where end_date is before or equal to start_date.
Check that no discount exceeds the item price on any order.
Exercise 3: Referential Integrity
Write a LEFT JOIN query to find orphan transactions (transactions pointing to non-existent accounts).
Test cascade behavior: delete a test user and verify whether child records (orders, addresses) were handled.
Query information_schema to list all foreign keys in the database and their cascade rules.
Try inserting an order with a non-existent user_id — does the FK constraint block it?
Exercise 4: Duplicate Detection
Find exact duplicate emails in the users table using GROUP BY HAVING.
Find fuzzy duplicate names using LOWER() and TRIM().
Find transactions that look like double-submits (same user, same amount, within 60 seconds).
Write a query to show all duplicate rows side by side with their IDs and timestamps.
Exercise 5: Business Rule Validation
Perform a money transfer in the Banking Portal. Verify that the sender and receiver balances changed correctly.
Run a conservation check: verify the total money across all accounts has not changed.
Find any accounts where the balance does not match the calculated sum of all transactions.
Try transferring more than the available balance. Check what happens in the database — is the constraint enforced?
Exercise 6: Write a JDBC Validation Test
Write a Java class with TestNG tests that validates the following programmatically:
No NULL emails exist in the users table.
No orphan orders exist.
Every order total matches the sum of its line items.
Total system balance is positive and matches an expected value.
Pick any two exercises and complete them fully. Then move on. You can always come back for the rest. Progress is better than perfection.