easyJOINsLEFT JOINNULL handling
Employee Bonus
Report every employee's name and bonus, including those with none.
Start practicePrompt
Employee Bonus
Prompt
Given the employee and bonus tables below, write a query that returns every employee whose bonus is less than 1000, or who has no bonus at all.
- Output columns:
nameandbonus. - An employee with no bonus row (or a
NULLbonus) must appear withbonus = NULL. - Only the
nameandbonuscolumns should be returned.
Example
| emp_id | name | salary |
|---|---|---|
| 1 | Alice | 100 |
| 2 | Bob | 200 |
| 3 | Carol | 300 |
| emp_id | bonus |
|---|---|
| 1 | 500 |
| 2 | 2000 |
Expected result:
| name | bonus |
|---|---|
| Alice | 500 |
| Bob | 2000 |
| Carol | NULL |
Constraints
employee.emp_idandbonus.emp_idare primary keys.bonus.bonusis nullable.
Tips
- Start from
employeewith aLEFT JOINso employees without a bonus still appear. bonus < 1000alone excludesNULLbonuses — addOR bonus.bonus IS NULL.
Expected concepts
- LEFT JOIN
- COALESCE
- NULL comparison
Schema
CREATE TABLE employee (
emp_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
salary INTEGER NOT NULL
);
CREATE TABLE bonus (
emp_id INTEGER PRIMARY KEY REFERENCES employee(emp_id),
bonus INTEGER -- NULL allowed
);
Sample data
INSERT INTO employee (emp_id, name, salary) VALUES
(1, 'Alice', 100),
(2, 'Bob', 200),
(3, 'Carol', 300),
(4, 'Dave', 400),
(5, 'Eve', 500);
INSERT INTO bonus (emp_id, bonus) VALUES
(1, 500),
(2, 2000),
(3, 100),
(5, 1500);
Additional hidden fixtures are applied during validation to test edge cases.