Return the first 50% of employee rows ordered by id, rounding up for odd counts

Quick Overview

Write a SQL query that returns the first half of an employees table in id order, rounding up so the middle row is kept when the row count is odd. It tests window functions for row position and total count, integer versus decimal division, and handling of id gaps and NULL columns.

Return the first 50% of employee rows ordered by id, rounding up for odd counts

Company: IBM

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Return the first 50% of rows of `employees` when sorted by `id`. If the row count is odd, round up so the middle row is included. Return `id`, `name`, `department` and `salary`, ordered by `id` ascending. ```hint Two numbers per row The cutoff depends on the size of the whole table. Look for a way to know each row's position in `id` order and the total row count in the same query. ``` ### Input Table(s) Table `employees`, one row per employee. Primary key: `id`. | Column | Type | Meaning | |---|---|---| | id | INT | Employee identifier (primary key) | | name | VARCHAR(100) | Employee name (NOT NULL) | | department | VARCHAR(100) | Department name (nullable) | | salary | DECIMAL(10,2) | Salary, non-negative when present (nullable) | ### Output Contract - Columns, in this order: `id`, `name`, `department`, `salary`, with values exactly as stored. - If the table has $n$ rows, return the $\lceil n/2 \rceil$ rows with the smallest `id` values. - Order by `id` ascending. ### Example Input `employees`: | id | name | department | salary | |---|---|---|---| | 1 | Alice | Engineering | 90000 | | 2 | Bob | Marketing | 75000 | | 3 | Charlie | Engineering | 85000 | | 4 | Diana | HR | 70000 | | 5 | Eve | Marketing | 72000 | Expected output: | id | name | department | salary | |---|---|---|---| | 1 | Alice | Engineering | 90000 | | 2 | Bob | Marketing | 75000 | | 3 | Charlie | Engineering | 85000 | There are 5 rows, and half of 5 rounded up is 3, so the three rows with the smallest `id` are returned. The middle row (Charlie, id 3) is included because the count is odd. ### Constraints and Clarifications - `employees` has 1 to 10,000 rows, so the result always has at least one row. - `id` values are unique but are not guaranteed to be consecutive. Gaps are possible. - With an even row count, return exactly half the rows. - `department` and `salary` can be NULL. Those rows still count toward the total and are returned with their NULLs as they are.

Overview: Write a SQL query that returns the first half of an employees table in id order, rounding up so the middle row is kept when the row count is odd. It tests window functions for row position and total count, integer versus decimal division, and handling of id gaps and NULL columns.

IBM logo
IBM
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Return the first 50% of rows of employees when sorted by id. If the row count is odd, round up so the middle row is included. Return id, name, department and salary, ordered by id ascending.

Input Table(s)

Table employees, one row per employee. Primary key: id.

ColumnTypeMeaning
idINTEmployee identifier (primary key)
nameVARCHAR(100)Employee name (NOT NULL)
departmentVARCHAR(100)Department name (nullable)
salaryDECIMAL(10,2)Salary, non-negative when present (nullable)

Output Contract

  • Columns, in this order: id , name , department , salary , with values exactly as stored.
  • If the table has nn rows, return the ⌈n/2⌉\lceil n/2 \rceil rows with the smallest id values.
  • Order by id ascending.

Example

Input employees:

idnamedepartmentsalary
1AliceEngineering90000
2BobMarketing75000
3CharlieEngineering85000
4DianaHR70000
5EveMarketing72000

Expected output:

idnamedepartmentsalary
1AliceEngineering90000
2BobMarketing75000
3CharlieEngineering85000

There are 5 rows, and half of 5 rounded up is 3, so the three rows with the smallest id are returned. The middle row (Charlie, id 3) is included because the count is odd.

Constraints and Clarifications

  • employees has 1 to 10,000 rows, so the result always has at least one row.
  • id values are unique but are not guaranteed to be consecutive. Gaps are possible.
  • With an even row count, return exactly half the rows.
  • department and salary can be NULL. Those rows still count toward the total and are returned with their NULLs as they are.