Recent Hires By Department
Return one row per department that hired at least one person in the first
half of 2024 — hired_on on or after 2024-01-01 and
before 2024-07-01 — with:
department— the department namehires— how many people it hired in that rangelatest_hire— the most recenthired_onamong them
Order by hires descending, then by department
ascending.
Expected output
department | hires | latest_hire
------------+-------+------------
Engineering | 3 | 2024-06-30
Sales | 2 | 2024-05-20
Support | 1 | 2024-04-04
Editorial
SELECT d.name AS department,
count(*) AS hires,
max(e.hired_on) AS latest_hire
FROM employee e
JOIN department d ON d.id = e.department_id
WHERE e.hired_on >= DATE '2024-01-01'
AND e.hired_on < DATE '2024-07-01'
GROUP BY d.name
ORDER BY hires DESC, department ASC;
Research and Judy's department drop out because their only hires fall
outside the range, and an INNER JOIN is right here — the statement
asks only for departments that hired somebody.
The half-open range, and why not BETWEEN
>= start AND < end is the shape to default to.
BETWEEN '2024-01-01' AND '2024-07-01' is inclusive on both sides,
so it picks up Heidi on 1 July and reports 3 hires for Sales. The half-open
form has a second advantage that matters more as soon as the column becomes a
timestamp: BETWEEN ... AND '2024-06-30' silently
excludes everything that happened during 30 June, because
2024-06-30 09:15 is greater than 2024-06-30 00:00.
Half-open ranges tile without gaps or overlaps, and that is the whole
argument.
The predicate the index cannot use
These three WHERE clauses select the same rows:
e.hired_on >= DATE '2024-01-01' AND e.hired_on < DATE '2024-07-01'
extract(year FROM e.hired_on) = 2024 AND extract(month FROM e.hired_on) <= 6
date_trunc('month', e.hired_on) < DATE '2024-07-01' AND e.hired_on >= DATE '2024-01-01'
Only the first can use employee_dept_hired. The reason is
mechanical: a B-tree stores hired_on values in order, and a range
scan works by finding one boundary and walking. Wrap the column in a function
and the index holds hired_on, not
extract(year FROM hired_on) — there is nothing ordered by the thing
being compared, so the only option is to read every row and compute the
function. A predicate an index can serve is called sargable,
and the rule for producing one is: keep the bare column on the left and
do the arithmetic on the literal.
WHERE hired_on + INTERVAL '90 days' > DATE '2024-07-01' -- not sargable
WHERE hired_on > DATE '2024-07-01' - INTERVAL '90 days' -- sargable, same rows
If you genuinely need the function form, PostgreSQL will index the expression itself:
CREATE INDEX employee_hired_month ON employee (date_trunc('month', hired_on));
That is a real tool and it has a real cost — a second index to maintain on every write, serving one predicate shape.
Why EXPLAIN shows a sequential scan anyway
Run it in the playground:
EXPLAIN ANALYZE
SELECT d.name, count(*) FROM employee e
JOIN department d ON d.id = e.department_id
WHERE e.hired_on >= DATE '2024-01-01' AND e.hired_on < DATE '2024-07-01'
GROUP BY d.name;
You will see Seq Scan on employee, and that is correct
behaviour, not a missing index. The table has ten rows on one page.
An index scan would read the index page and then the heap page — two reads to
avoid one. The planner costs both and picks the cheaper, which is why a
sequential scan on a small table is a sign the planner is working.
Seeing it switch needs volume. The crossover is usually somewhere around a few thousand rows, and it depends on the fraction of the table the predicate selects: an index is worth it for 1% of a million rows and not for 60% of them, because at 60% you are doing random heap reads for most of the table. This is the single most useful thing to know about indexes and the hardest to see on toy data.
What an interviewer is checking
Two things, and the second is the real one. First, that you write a sargable range rather than a function on the column. Second, that when they say "the index isn't being used" you ask how big is the table and how selective is the predicate before concluding anything — because the answer is often that the planner is right.
department
4 rows
| idinteger | nametext |
|---|---|
| 1 | Engineering |
| 2 | Sales |
| 3 | Support |
| 4 | Research |
employee
10 rows
| idinteger | nametext | department_idinteger | hired_ondate |
|---|---|---|---|
| 1 | Alice | 1 | 1705276800000000 |
| 2 | Bob | 1 | 1709337600000000 |
| 3 | Carol | 1 | 1719705600000000 |
| 4 | Dan | 1 | 1703980800000000 |
| 5 | Erin | 2 | 1707868800000000 |
Showing 5 of 10 rows.