Sql script to find invalid email addresses
Stefan Bogdanescu
Founder & Senior Architect
The Developer's Guide: Finding Invalid Email Addresses in SQL
Data migration often presents a messy reality: data imported from legacy systems frequently lacks crucial validation. When dealing with an Access database dump, finding invalid email addresses requires more than just a simple SELECT. As a senior developer, I can tell you that while pure SQL can handle basic string pattern matching, true, robust email validation is a complex task that should ideally be handled in the application layer.
This post will guide you through how to approach this problem using SQL for initial filtering, and more importantly, why relying solely on it is insufficient, pushing us toward better architectural practices—especially when building modern applications like those leveraging frameworks such as Laravel.
The SQL Approach: Initial Filtering with Pattern Matching
Since email validation involves complex structural rules (presence of @, presence of a domain part, valid characters), we must rely on string functions and regular expressions within the SQL environment. We cannot perfectly validate an email address purely in SQL without external services, but we can flag obvious structural errors.
Let's assume your data resides in a table named users with an email column.
Example 1: Finding Emails Missing the '@' Symbol
The most basic check is to find rows where the string does not contain the required separator.
SELECT email
FROM users
WHERE email NOT LIKE '%@%'
AND email IS NOT NULL;
Example 2: Identifying Emails with Invalid Structure (Using REGEXP)
For a more thorough check, we can use regular expressions to look for patterns that strongly suggest an invalid format. While the exact syntax depends on your SQL dialect (e.g., MySQL uses REGEXP, PostgreSQL uses ~), the principle remains the same. Here is an example focusing on addresses missing common structural elements:
-- Example using a conceptual REGEXP operator common in many dialects
SELECT email
FROM users
WHERE email IS NOT NULL
AND (email NOT LIKE '%@%') -- Missing @ symbol
OR (email LIKE '@%' AND email NOT LIKE '%@%.%') -- Missing domain part after @
OR (LENGTH(email) < 5); -- Basic length check as a quick filter
Developer Insight: Notice that these SQL queries are excellent for flagging potential issues based on syntax. They act as a useful pre-filter during data cleanup. However, they will miss subtle errors (like invalid TLDs or disallowed characters) that require external context.
Beyond SQL: The Application Layer is King
As developers building robust systems—whether you are using PHP, Python, or Laravel—the responsibility for data integrity shifts to the application layer. Relying solely on database queries for complex validation leads to brittle code and poor user experience.
Best Practice: Always perform comprehensive email validation in your backend logic before saving any data.
When integrating with a framework like Laravel, you would use built-in validation rules combined with external libraries or services. For example, ensuring an email is structurally sound and potentially verifying its deliverability requires more than what SQL can offer. You should focus on building resilient APIs where input sanitization happens immediately upon receipt. This approach ensures that data quality is maintained regardless of the source database format.
Conclusion
In summary, use SQL scripts as a powerful data cleanup tool to quickly identify rows with obvious structural errors (like missing the @ symbol). However, do not treat SQL as the sole source of truth for email validity. For truly comprehensive and reliable validation—checking for valid character sets, TLDs, and deliverability—delegate this complex task to your application's backend logic. By combining targeted SQL filtering with robust application-level validation, you ensure that your data is not only clean but also trustworthy, aligning perfectly with the principles of building scalable systems found in modern frameworks like Laravel.
Note: Blog content is currently available in English.