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.

Table `employees` stores one row per employee: - `id` INTEGER, the employee identifier (primary key) - `name` VARCHAR(100), the employee name (NOT NULL) - `department` VARCHAR(100), the department name (nullable) - `salary` DECIMAL(10,2), the salary, non-negative when present (nullable) 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: if the table has n rows, return the ceil(n/2) rows with the smallest `id` values. With an even row count, return exactly half the rows. Return the columns `id`, `name`, `department` and `salary`, in this order, with values exactly as stored, ordered by `id` ascending. `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, and gaps are possible. `department` and `salary` can be NULL. Those rows still count toward the total and are returned with their NULLs as they are. For example, if `employees` holds 5 rows with ids 1 to 5, half of 5 rounded up is 3, so the rows with ids 1, 2 and 3 are returned.

Tables

employees(id INTEGER, name VARCHAR(100), department VARCHAR(100), salary DECIMAL(10,2))

Hints

  1. 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.
  2. Rows with a NULL `department` or `salary` still count toward the total row count.
IBMData ScientisteasyData Manipulation (SQL/Python)

First Half of Employees by ID, Rounded Up

Table employees stores one row per employee:

  • id INTEGER, the employee identifier (primary key)
  • name VARCHAR(100), the employee name (NOT NULL)
  • department VARCHAR(100), the department name (nullable)
  • salary DECIMAL(10,2), the salary, non-negative when present (nullable)

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: if the table has n rows, return the ceil(n/2) rows with the smallest id values. With an even row count, return exactly half the rows.

Return the columns id, name, department and salary, in this order, with values exactly as stored, ordered by id ascending.

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, and gaps are possible. department and salary can be NULL. Those rows still count toward the total and are returned with their NULLs as they are.

For example, if employees holds 5 rows with ids 1 to 5, half of 5 rounded up is 3, so the rows with ids 1, 2 and 3 are returned.

Expected Output

Tables

employees
employees
ColumnType
idINTEGER
nameVARCHAR(100)
departmentVARCHAR(100)
salaryDECIMAL(10,2)

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions