SQL Performance & Data Modeling|
Filter Before Joining

Filtering Early

SQL

Filter Before Joining

Filter Early for Better Performance

The Principle

Filter rows before expensive operations like JOINs when possible.

Example

Less efficient:

SELECT * FROM orders o
JOIN customers c ON o.customer_id = c.id
WHERE o.status = 'completed'  -- filters after join

More efficient (conceptually):

SELECT * FROM (
    SELECT * FROM orders WHERE status = 'completed'
) o
JOIN customers c ON o.customer_id = c.id

In practice, modern databases often optimize this. But writing clear, filtered queries helps.

Your Task

Find users who had sessions in January 2026:

  • Join users and sessions
  • Filter for sessions where started_at is in January 2026
  • Show: username, session_id, started_at
  • Order by started_at
Query Editor
Loading SQL engine...
Loading...
Loading SQL engine...

Run your query to see results