Find the last transaction of each type on each day

Quick Overview

Write a SQL query that returns the full row of the last deposit and the last withdrawal on each day, where the last one has the largest transaction ID. It tests greatest-per-group selection, joining an aggregate back to its source rows, and deterministic ordering.

Find the last transaction of each type on each day

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Each row of `Transactions` is a deposit or a withdrawal made on some day. For every (`day`, `type`) pair that occurs in the table, return the full row of the last transaction, where the last one is the transaction with the largest `transaction_id`. ```hint Two steps Deciding which row wins in each group and fetching that row's other columns can be done as separate steps. ``` ### Input Table(s) Table `Transactions`: | Column | Type | Meaning | |---|---|---| | transaction_id | INT | Unique ID of the transaction | | day | DATE | Calendar day of the transaction | | type | ENUM('Deposit', 'Withdraw') | Either 'Deposit' or 'Withdraw' | | amount | INT | Transaction amount, a positive integer | Primary key: `transaction_id`. ### Output Contract - Columns in this order: `transaction_id`, `day`, `type`, `amount`, copied unchanged from the input row. - One row per (`day`, `type`) pair that appears in the table. - Rows are ordered by `transaction_id` ascending. The ID is unique, so the order is fully determined. - No rounding is involved. ### Example Input `Transactions`: | transaction_id | day | type | amount | |---|---|---|---| | 1 | 2024-03-01 | Deposit | 100 | | 2 | 2024-03-01 | Withdraw | 40 | | 3 | 2024-03-01 | Deposit | 25 | | 4 | 2024-03-02 | Withdraw | 10 | | 5 | 2024-03-02 | Deposit | 50 | | 6 | 2024-03-02 | Withdraw | 70 | | 7 | 2024-03-03 | Deposit | 30 | Expected output: | transaction_id | day | type | amount | |---|---|---|---| | 2 | 2024-03-01 | Withdraw | 40 | | 3 | 2024-03-01 | Deposit | 25 | | 5 | 2024-03-02 | Deposit | 50 | | 6 | 2024-03-02 | Withdraw | 70 | | 7 | 2024-03-03 | Deposit | 30 | On 2024-03-01, deposit 3 comes after deposit 1, and withdrawal 2 is the only withdrawal. On 2024-03-02, withdrawal 6 comes after withdrawal 4, and deposit 5 is the only deposit. 2024-03-03 has a single deposit, so it contributes one row. The result is listed by `transaction_id`. ### Constraints and Clarifications - The table has at most 10,000 rows. - `amount` is a positive integer. - The table has no time-of-day column. "Last" is decided by `transaction_id` alone. - `transaction_id` is unique, so each (`day`, `type`) group has exactly one last row and there are no ties. - A day with only one type of transaction returns one row. Pairs that never occur return nothing. - Assume `day` and `type` are never NULL.

Overview: Write a SQL query that returns the full row of the last deposit and the last withdrawal on each day, where the last one has the largest transaction ID. It tests greatest-per-group selection, joining an aggregate back to its source rows, and deterministic ordering.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Each row of Transactions is a deposit or a withdrawal made on some day. For every (day, type) pair that occurs in the table, return the full row of the last transaction, where the last one is the transaction with the largest transaction_id.

Input Table(s)

Table Transactions:

ColumnTypeMeaning
transaction_idINTUnique ID of the transaction
dayDATECalendar day of the transaction
typeENUM('Deposit', 'Withdraw')Either 'Deposit' or 'Withdraw'
amountINTTransaction amount, a positive integer

Primary key: transaction_id.

Output Contract

  • Columns in this order: transaction_id , day , type , amount , copied unchanged from the input row.
  • One row per ( day , type ) pair that appears in the table.
  • Rows are ordered by transaction_id ascending. The ID is unique, so the order is fully determined.
  • No rounding is involved.

Example

Input Transactions:

transaction_iddaytypeamount
12024-03-01Deposit100
22024-03-01Withdraw40
32024-03-01Deposit25
42024-03-02Withdraw10
52024-03-02Deposit50
62024-03-02Withdraw70
72024-03-03Deposit30

Expected output:

transaction_iddaytypeamount
22024-03-01Withdraw40
32024-03-01Deposit25
52024-03-02Deposit50
62024-03-02Withdraw70
72024-03-03Deposit30

On 2024-03-01, deposit 3 comes after deposit 1, and withdrawal 2 is the only withdrawal. On 2024-03-02, withdrawal 6 comes after withdrawal 4, and deposit 5 is the only deposit. 2024-03-03 has a single deposit, so it contributes one row. The result is listed by transaction_id.

Constraints and Clarifications

  • The table has at most 10,000 rows.
  • amount is a positive integer.
  • The table has no time-of-day column. "Last" is decided by transaction_id alone.
  • transaction_id is unique, so each ( day , type ) group has exactly one last row and there are no ties.
  • A day with only one type of transaction returns one row. Pairs that never occur return nothing.
  • Assume day and type are never NULL.