ETL Testing Using SQL: Tips and Query Examples
✅ ETL Testing Using SQL: Tips and Query Examples
ETL Testing (Extract, Transform, Load Testing) is the process of validating data as it moves from source systems to the data warehouse. SQL is a vital tool in ETL testing because it helps you validate data accuracy, completeness, and integrity at every step of the ETL process.
π§© What is ETL Testing?
ETL Testing ensures:
Data is correctly extracted from source systems.
Data is properly transformed as per business rules.
Data is accurately loaded into the target system or warehouse.
π Key ETL Testing Activities Using SQL
Test Type Goal SQL Example
Row Count Validation Check source vs target row counts SELECT COUNT(*) FROM table_name;
Data Comparison Check if values match between systems SELECT * FROM source MINUS SELECT * FROM target;
Null Checks Ensure mandatory fields are not NULL SELECT * FROM table WHERE column IS NULL;
Data Type Validation Confirm correct data types in columns SELECT COLUMN_NAME, DATA_TYPE FROM USER_TAB_COLUMNS WHERE TABLE_NAME = 'TABLE';
Duplicate Check Detect duplicate rows SELECT column, COUNT(*) FROM table GROUP BY column HAVING COUNT(*) > 1;
Transformation Rules Verify logic like concatenation or math `SELECT first_name
Referential Integrity Validate foreign key constraints SELECT * FROM child WHERE parent_id NOT IN (SELECT id FROM parent);
π§ͺ Common SQL Queries for ETL Testing
1. Row Count Match
sql
Copy
Edit
-- Source
SELECT COUNT(*) FROM source_table;
-- Target
SELECT COUNT(*) FROM target_table;
2. Data Validation Between Source and Target
sql
Copy
Edit
-- Find mismatched rows
SELECT * FROM source_table
MINUS
SELECT * FROM target_table;
3. Duplicate Records
sql
Copy
Edit
SELECT id, COUNT(*)
FROM target_table
GROUP BY id
HAVING COUNT(*) > 1;
4. Null Value Check
sql
Copy
Edit
SELECT * FROM target_table
WHERE important_column IS NULL;
5. Check Transformed Columns
For example, a salary increase by 10%:
sql
Copy
Edit
SELECT employee_id, salary, new_salary
FROM transformed_table
WHERE new_salary != salary * 1.10;
π‘ ETL Testing Best Practices
✅ Understand business rules before writing SQL.
π§Ύ Validate metadata: column names, types, lengths, and formats.
π Use control tables to compare expected vs actual data loads.
π΅️♂️ Log errors and mismatches for debugging.
π Test incremental loads with timestamp or ID-based queries.
πΎ Back up data before re-running ETL jobs during testing.
⏱️ Monitor performance of long-running SQL queries.
π§ Summary
SQL is the backbone of effective ETL testing. By writing smart, targeted queries, you can:
Detect data issues early.
Verify transformations.
Ensure the reliability of your data pipeline.
Learn ETL Testing Training in Hyderabad
Read More
Common ETL Bugs and How to Find Them
How to Perform Data Validation in ETL Testing
Step-by-Step Guide to Writing ETL Test Cases
Why ETL Testing is Crucial in Data Warehousing
Visit Our IHUB Talent Training Institute in Hyderabad
Comments
Post a Comment