Find the hour of the day with the highest total sales

Quick Overview

Write a SQL query over a sales table that finds the hour of the day with the largest total sales amount across all dates, returning the smallest hour on a tie. It tests extracting a time part from a timestamp, grouping and summing, and writing a deterministic sort with a limit.

Find the hour of the day with the highest total sales

Company: Adobe

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Each row of the `sales` table is one sale with its timestamp and amount. Find the hour of the day (0 to 23) whose sales add up to the largest total amount, counting all dates in the table together. Return that hour as `sale_hour` and its total as `total_sales`. If several hours tie for the largest total, return the smallest hour. ```hint Where ties are decided A query that returns one row can still be nondeterministic. Check that your sort order alone decides which of several tied hours comes first. ``` ### Input Table(s) Table `sales` | Column | Type | Meaning | |---|---|---| | id | INT | Sale identifier | | sale_time | TIMESTAMP | Date and time of the sale (DATETIME in the original schema), NOT NULL | | amount | DECIMAL(10,2) | Sale amount, NOT NULL | Primary key: `id`. ### Output Contract - Exactly one row with two columns, in this order: `sale_hour` (integer, 0 to 23) and `total_sales` (the sum of `amount` over every sale in that hour, a decimal with two places, no extra rounding). - The row is the hour with the largest `total_sales`. On a tie, it is the smallest `sale_hour`, so the answer is unique. ### Example Input `sales`: | id | sale_time | amount | |---|---|---| | 1 | 2024-06-10 08:00:00 | 50.00 | | 2 | 2024-06-10 12:30:00 | 200.00 | | 3 | 2024-06-11 12:15:00 | 100.00 | | 4 | 2024-06-11 18:00:00 | 300.00 | | 5 | 2024-06-12 08:45:00 | 250.00 | Expected result: | sale_hour | total_sales | |---|---| | 8 | 300.00 | Hour 8 totals 50 + 250 = 300, hour 12 totals 200 + 100 = 300, and hour 18 totals 300. All three tie at 300.00, so the smallest hour, 8, is returned. The two hour-8 sales fall on different dates and still count together. ### Constraints and Clarifications - `sales` has 1 to 10,000 rows, so at least one hour has sales and the result always has exactly one row. - `sale_time` and `amount` are never NULL, and every `amount` is positive. - Only the hour part of `sale_time` matters. Minutes and seconds are ignored (12:30 counts toward hour 12), and sales on different dates in the same hour are added together. - Read the hour from `sale_time` as stored. No time zone conversion is needed. - Hours with no sales never need to appear. Because every amount is positive, they can never have the largest total.

Overview: Write a SQL query over a sales table that finds the hour of the day with the largest total sales amount across all dates, returning the smallest hour on a tie. It tests extracting a time part from a timestamp, grouping and summing, and writing a deterministic sort with a limit.

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

Each row of the sales table is one sale with its timestamp and amount. Find the hour of the day (0 to 23) whose sales add up to the largest total amount, counting all dates in the table together. Return that hour as sale_hour and its total as total_sales. If several hours tie for the largest total, return the smallest hour.

Input Table(s)

Table sales

ColumnTypeMeaning
idINTSale identifier
sale_timeTIMESTAMPDate and time of the sale (DATETIME in the original schema), NOT NULL
amountDECIMAL(10,2)Sale amount, NOT NULL

Primary key: id.

Output Contract

  • Exactly one row with two columns, in this order: sale_hour (integer, 0 to 23) and total_sales (the sum of amount over every sale in that hour, a decimal with two places, no extra rounding).
  • The row is the hour with the largest total_sales . On a tie, it is the smallest sale_hour , so the answer is unique.

Example

Input sales:

idsale_timeamount
12024-06-10 08:00:0050.00
22024-06-10 12:30:00200.00
32024-06-11 12:15:00100.00
42024-06-11 18:00:00300.00
52024-06-12 08:45:00250.00

Expected result:

sale_hourtotal_sales
8300.00

Hour 8 totals 50 + 250 = 300, hour 12 totals 200 + 100 = 300, and hour 18 totals 300. All three tie at 300.00, so the smallest hour, 8, is returned. The two hour-8 sales fall on different dates and still count together.

Constraints and Clarifications

  • sales has 1 to 10,000 rows, so at least one hour has sales and the result always has exactly one row.
  • sale_time and amount are never NULL, and every amount is positive.
  • Only the hour part of sale_time matters. Minutes and seconds are ignored (12:30 counts toward hour 12), and sales on different dates in the same hour are added together.
  • Read the hour from sale_time as stored. No time zone conversion is needed.
  • Hours with no sales never need to appear. Because every amount is positive, they can never have the largest total.