CMD Guide
HomeDatabasesSQL Practice Problems

Trips and Users

Daily cancellation rate among unbanned users

Trips(id, client_id, driver_id, status, request_at), Users(users_id, banned, role). For each day in 2013-10-01 .. 2013-10-03, compute the cancellation rate of requests where neither the client nor the driver is banned, rounded to 2 decimals. The robust way to exclude banned users is to join to Users twice (client and driver) filtering banned = 'No' — not NOT IN, which misbehaves with NULLs.

SELECT t.request_at AS Day,
       ROUND(SUM(t.status <> 'completed') / COUNT(*), 2) AS "Cancellation Rate"
FROM Trips t
JOIN Users c ON t.client_id = c.users_id AND c.banned = 'No'
JOIN Users d ON t.driver_id = d.users_id AND d.banned = 'No'
WHERE t.request_at BETWEEN '2013-10-01' AND '2013-10-03'
GROUP BY t.request_at;

The two ideas that make it correct

Pitfalls

Takeaways


Re-authored for correctness for this guide (the prior version's NOT IN filter is NULL-fragile). Pattern: LeetCode 262. See also: INNER JOIN, Handle NULLs, Aggregate Functions.

🎯 STRICT STANDOUT: Why / mental model / when-not / worked / failure / hostile panel — Trips and Users

Why this concept exists (judgment layer)

Cancellation rate with dual ban filters teaches semi-join correctness: join Users twice on banned='No' beats NOT IN (NULL trap). Boolean sum is the rate numerator pattern.

Mental model (install this intuition)

Keep trip only if client AND driver pass banned='No' (two INNER JOINs). Per day: cancelled_count / all_kept_trips. Cancelled = status <> 'completed' (both cancel types).

Worked example with numbers or traced steps

JOIN Users c ON client_id AND c.banned='No'
JOIN Users d ON driver_id AND d.banned='No'
Trip with banned driver disappears (fails second join)
SUM(status<>'completed')/COUNT(*) → rate; ROUND 2 decimals
NOT IN (SELECT id WHERE banned='Yes') dies if subquery has NULL

When NOT to use / named alternative

Do not use NOT IN for ban lists. Do not filter only clients. On Postgres use FILTER or ::int for boolean aggregates — not MySQL 1/0 silent cast alone.

Failure mode & ops fingerprint

Fingerprint: rate includes banned parties; NOT IN empties whole day; division integer truncation without ROUND/float; BETWEEN date off-by-one on timestamps.

Hostile-panel drills (defend the decision)

Q1. Why two joins not one?
Model answer: Client and driver are different Users rows; both must be unbanned independently.

Q2. Why avoid NOT IN for banned ids?
Model answer: If subquery returns NULL, NOT IN becomes UNKNOWN for all candidates → empty result.

Q3. Numerator for cancellation rate?
Model answer: Count of non-completed statuses among trips that survived both unbanned joins — not only 'cancelled by client'.

🤖 Don't fully get this? Learn it with Claude

Stuck on Trips and Users? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Trips and Users** (Databases) and want to truly understand it. Explain Trips and Users from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Trips and Users** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Trips and Users** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Trips and Users** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes