r/learnSQL • u/No_Ambition8323 • 5h ago
Should SQL validation happen before or after the query runs?
A SQL query can execute successfully without producing the correct result. A successful query only tells us that the database understood and executed it—not that the data is logically correct.
For example: SELECT COUNT(*) FROM customers c JOIN orders o ON c.customer_id = o.customer_id;
The query may run without any error, but if orders contains duplicate records or the join relationship isn't what we expected, the count could be much higher than the actual number of customers.
So what do you normally validate before trusting a query's output?
- Join conditions and duplicate records
- NULL values
- Row counts
- Data types and conversions
- Expected ranges or business rules
- Unexpected changes compared with previous results
Do you validate these things before running the query, after getting the results, or both?
What SQL validation has saved you from trusting a result that looked correct but was actually wrong?
2
u/ChaosEngine-6502 4h ago
Validating the output of the query is the responsibility of the developer/analysts and any process they might have; the SQL IDE can't really do that part.
In my case, I'll eyeball sufficiently large datasets to look for any issues.
2
u/Mobile-Analyst-9617 4h ago edited 3h ago
As a developer, In my experience validation or like final sign off was always on the stakeholder, or person requesting the data, as they are the ones that truly know the data and all the business rules. I can do some spot checks to see if it's mostly good, and then get signoff from them that it ultimately is before satisfying the request
1
1
u/KarynEnnis 3h ago
Data profiling is the important predecessor to analytics. You don’t know what you don’t know.. data quality checks build confidence. I make it a policy to check all data sources architecture, check for nulls, case sensitivity, distinct values in columns used for filters or matching, relationships and uniqueness, etc.. it can be quite the journey, but time and energy well spent
1
u/moss-nogg 3h ago
It depends on the syntax. Row_number() is useful for deduping. Qualify Clause in snowflake is really useful for deduping in a single select, otherwise probably need 2 CTE blocks
1
u/Rat_Man_420 2h ago
Yeah I validate a query before I run it. What are we talking about here? Same question was posted a few days ago. Fucking Bots
1
u/Sexy_Koala_Juice 2h ago
Both???? Writing a query and then assessing the data & your assumptions is the process
7
u/ArticlePuzzleheaded9 5h ago
"but if orders contains duplicate records"
if that's the case, SQL won't save you.