Time to get your hands dirty. These exercises simulate real migration scenarios you will face in projects. Try writing the SQL yourself before looking at the hints. The goal is to build the muscle memory so that when a real migration lands on your desk, you know exactly what queries to write.
The Banking Portal is migrating from a legacy schema to a normalized schema. The old schema has one table: users(id, name, email, phone, account_number, balance, address). The new schema splits this into three tables: users(id, first_name, last_name, email, phone), accounts(id, user_id, account_number, balance, type), addresses(id, user_id, street, city, state, pin).
The transactions table stores dates as VARCHAR in format "DD-MM-YYYY" (Indian format). The new system needs proper DATE columns. Write SQL to:
-- Hint: Find invalid date strings
SELECT txn_id, txn_date_varchar
FROM transactions
WHERE txn_date_varchar NOT REGEXP
'^[0-9]{2}-[0-9]{2}-[0-9]{4}$';
-- Hint: Convert and compare
SELECT txn_id,
txn_date_varchar,
STR_TO_DATE(txn_date_varchar, '%d-%m-%Y') AS converted_date
FROM transactions
WHERE STR_TO_DATE(txn_date_varchar, '%d-%m-%Y') IS NULL
AND txn_date_varchar IS NOT NULL;Your company is migrating from Oracle to MySQL. Write queries to verify:
The insurance portal is migrating its claims data to a new cloud database. Write a test plan that covers:
This migration script has four bugs. Find them all.
-- Migration: users table to new schema
INSERT INTO new_db.users (id, first_name, last_name, email, phone)
SELECT
id,
SUBSTRING_INDEX(name, ' ', 1) AS first_name,
SUBSTRING_INDEX(name, ' ', -1) AS last_name,
email,
phone
FROM old_db.users;
-- Bug 1: Single-word names — first_name and last_name will be the same
-- Bug 2: Names with 3+ words — "Aamir Khan Shah" → first="Aamir", last="Shah", middle lost
-- Bug 3: No WHERE clause to exclude soft-deleted users (is_deleted = 1)
-- Bug 4: No ON DUPLICATE KEY handling — re-running this creates duplicatesAn ETL job extracted customer data from three source systems (CRM, Billing, Support), transformed and merged them into a single target table. Write queries to check:
For each exercise, start by listing what you expect BEFORE writing the query. "I expect 5000 records in the target" is more useful than "let me run COUNT(*) and see." If you do not know what the right answer is, the query result is meaningless.
Key Point: Practice with real scenarios: schema restructuring, date migrations, platform changes, ETL validation, and debugging buggy scripts. Always know the expected result before running the query.