Lesson 7

Common SQL Mistakes

Avoid accidental cross joins, null traps, wrong time windows, and unsafe assumptions.

SQL mistakes often look small in text but large in production behavior. A missing join condition, a wrong time boundary, or an unexpected NULL can change a report, an API response, or a migration.

Missing or weak join conditions

If a join condition is missing, the database can combine far more rows than intended. Even when an ON clause exists, make sure it uses the right key and includes tenant or account boundaries when your schema requires them.

NULL comparisons

NULL is not equal to ordinary values. Use IS NULL or IS NOT NULL:

WHERE deleted_at IS NULL

Do not expect deleted_at = NULL to behave like a normal comparison.

Ambiguous time windows

Time filters should be explicit about inclusive and exclusive edges:

WHERE created_at >= '2026-01-01'
  AND created_at < '2026-02-01'

This is usually safer than trying to include the last second of a day or month.

SELECT star in application paths

SELECT * is useful while exploring, but risky in application code. It can fetch unnecessary columns, hide dependency on schema shape, and make reviews harder.

Key takeaway

Format the query, read it clause by clause, and look for the small assumptions: join keys, null behavior, time ranges, and selected columns.

The SQL Formatter can help expose those assumptions before you execute or review the query.

When you want to practice, use the related DevCove tool — optional, not part of this lesson.

Open related tool

Back to course overview