SQL for Data Quality Work
Why SQL belongs in a governance practice, and three checks that catch problems at the source.
SQL is the primary language used to implement data governance directly within the database, at the source, rather than relying on downstream processes to translate data policies into controls after the data has already been extracted, transformed, or worse, consumed.
It enables organizations to enforce governance policies through:
- Access control: Define who can access specific data using roles, permissions, and privileges.
- Data security: Protect sensitive information through masking, views, and row-level security.
- Data quality: Apply constraints, validation rules, and integrity checks.
- Data privacy: Restrict access to personally identifiable and confidential information.
- Auditability: Track data access and changes through database auditing mechanisms.
- Policy enforcement: Embed governance rules directly into database objects, procedures, and transactions.
The fundamental principle: Data governance should be enforced as close to the data as possible.
Rather than treating governance as a downstream reporting responsibility, SQL allows organizations to make governance an integral part of how data is stored, accessed, modified, and shared. This ensures data confidence while reducing costs by minimizing the time and resources required for manual data cleaning during subsequent data processing stages.
In this approach, the database becomes not just a data repository, but an active enforcement layer for your organization's data governance program.
To illustrate this, we'll examine three examples that should form part of nearly every data quality audit.
Duplicate values in a column that should be unique
SELECT customer_id, COUNT(*) AS copies
FROM customers
GROUP BY customer_id
HAVING COUNT(*) > 1;
This query groups records by customer_id and counts how many times each value appears. Any result indicates a customer ID that appears more than once, potentially compromising the reliability of customer records.
Orphaned records with no matching parent record
SELECT o.order_id
FROM orders AS o
LEFT JOIN customers AS c ON c.customer_id = o.customer_id
WHERE c.customer_id IS NULL;
This query identifies orders that reference a customer ID that does not exist in the customers table. These orphaned records indicate a disconnect between related tables and can lead to incomplete reporting.
Missing values in a required field
SELECT COUNT(*) AS missing_email
FROM customers
WHERE email IS NULL OR email = '';
This query counts customer records where the email field is either NULL or an empty string. Missing information can affect downstream processes, reporting, and the reliability of customer datasets.
Each check identifies issues when they are found, making the results easy to interpret. A query returning no records, or a count of zero, indicates a pass. This makes the checks easy to schedule, repeat, and explain to people who do not write SQL.
Making it the first step
The goal is not to write clever queries. It is to make data quality checks the first step of every project, rather than an afterthought that gets skipped.
