Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
backend ruleP2universalStack: sql
databasequery-designperformancejoins
Avoid DISTINCT selects; they usually signal a join flaw
A DISTINCT in a query is usually a sign that a join is fanning out incorrectly; fix the join/grouping rather than deduplicating after the fact, since DISTINCT is also comparatively expensive.
PR: hegnar-journalist-boost · org-mining-2026-06Created: Jun 17, 2026
Bad example
Old codesql
| 1 | SELECT DISTINCT u.id, u.name |
| 2 | FROM users u |
| 3 | JOIN shares s ON s.user_id = u.id; |
Explanation (EN)
Objašnjenje (HR)
Good example
New codesql
| 1 | -- collapse the fan-out at the source instead of papering over it with DISTINCT |
| 2 | SELECT u.id, u.name |
| 3 | FROM users u |
| 4 | WHERE EXISTS (SELECT 1 FROM shares s WHERE s.user_id = u.id); |
Explanation (EN)
Objašnjenje (HR)