Skip to main content

Cumulative Employee Salary

easy
Software engineer

Write a SQL query to calculate the cumulative salary for each employee.

The cumulative salary is defined as the sum of the current employee's salary plus the salaries of all employees with a lower id.

Schema

Table: p_cumul_employees

Column Type Description
id INTEGER Unique identifier for the employee.
name TEXT Name of the employee.
salary INTEGER The employee's current salary.

Task

Return a result table containing the id, name, salary, and the cumulative_salary. The results should be ordered by id in ascending order.

Example 1

Input

Table p_cumul_employees:
| id | name | salary |
|----|------|--------|
| 1  | John | 3000   |
| 2  | Jane | 2000   |

Output

| id | name | salary | cumulative_salary |
|----|------|--------|-------------------|
| 1  | John | 3000   | 3000              |
| 2  | Jane | 2000   | 5000              |

Constraints

Results must be ordered by id ASC.

Screening
SQLite
Schema (setup SQL)
CREATE TABLE p_cumul_employees (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  salary INTEGER NOT NULL
);

INSERT INTO p_cumul_employees (id, name, salary) VALUES
(1, 'John', 3000),
(2, 'Jane', 2000),
(3, 'Alex', 1500),
(4, 'Emily', 3500);
Editor loads in the browser.

Output

Run your query to see results here.