Database Transactions: Keep Related Changes Together
Some changes only make sense together. If a reservation is created but its related capacity record is not updated, the system may…
A query that returns five rows has not necessarily returned the five rows you intended. LIMIT controls how many rows are returned, but it does not define their order. This matters when building reports and paginated screens.
SELECT ticket_id, created_at
FROM tickets
ORDER BY created_at DESC, ticket_id DESC
LIMIT 5;
This teaching query asks for the newest timestamps first and uses ticket_id to break ties. Assume ticket_id is unique. PostgreSQL recommends a predictable unique ordering when using LIMIT. Without it, you cannot rely on a particular subset simply because it looked stable in one test.
Create six invented tickets, including two with the same timestamp. Predict which five should appear. Then remove the ID tie-breaker and explain what is no longer fully specified. The timestamp alone does not choose between rows with equal values.
Even with a clear order, rows can be inserted or removed between separate page requests. An offset-based page may therefore shift. Decide what consistency the product needs rather than assuming an ORDER BY clause solves every pagination issue.
For a Malta data or backend interview, explain the requested subset in words before writing syntax. That makes the intended behaviour easier to test.
Browse current technology vacancies with Huwell Consultancy and ask us about the responsibilities that matter to you.
Explore opportunitiesSome changes only make sense together. If a reservation is created but its related capacity record is not updated, the system may…
Two reports can disagree about “yesterday” even when they use the same events. One may use UTC dates while the other uses…
WHERE and HAVING both filter a SQL result, but they answer different questions. WHERE chooses input rows. HAVING filters groups after aggregation.…