Exercício prático · SQL / PostgreSQL

Practice lab: Keep the zero-count rows

do caderno de estudos · atualizado em 2 de outubro de 2026
Para quem vai encarar entrevista de SQL / PostgreSQL2 min

Tente resolver a tarefa antes de consultar os critérios. Compare o resultado e explique o que mudou no seu raciocínio.

Tarefa e critérios de verificação

Task, 15 minutes, on a disposable local database: create users Ada, Ben, and Cy. Ada has one paid order and one pending order. Ben has one pending order. Cy has no orders. Write one query returning every user and their paid-order count, including zero.

Check your work:

  • The result is Ada 1, Ben 0, Cy 0, with each user appearing once.
  • A LEFT JOIN with the paid condition in its ON clause can preserve users without paid matches. Count a non-null order id, not COUNT(*) on the joined rows.
  • Move the paid condition into WHERE and explain which users disappear. Restore the intended result.
  • With representative local data, inspect the plan and propose an index for the actual join and filter. Explain its write/storage cost instead of assuming every index helps.

Follow-up: two concurrent requests decrement the last unit of stock. Show an atomic conditional update that cannot take stock below zero, and explain how the caller detects that no row was updated.

No app, a tarefa fica visível e você pode revelar os critérios depois da tentativa. Use as notas para registrar sua solução.

Praticar este exercício no app