Helix Systems' HR team is preparing a briefing for a new executive who wants to understand the most recent additions to the team.
Write a query to return the ID, name, and hire date of the 4 most recently hired employees.
Assumptions:
- The
employeestable contains every active and former employee at Helix Systems. - When two employees share a
hire_date, the employee with the loweridshould appear first.
Output:
- One row per employee, with columns
id,name, andhire_date, sorted byhire_datedescending (andidascending for ties), capped at 4 rows.
Schema · hr 4 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
id,
name,
hire_date
FROM
employees
ORDER BY
hire_date DESC
LIMIT
4 The shape
Sorting hire_date descending puts the most recent hires at the top of the result, and LIMIT 4 cuts the output at the four rows the new executive's briefing needs.
Clause by clause
SELECT id, name, hire_datepicks the three columns the briefing displays. Other employee fields stay out of the result.FROM employeesreads every active and former employee on file at Helix Systems. The recency cut happens after the sort, not as aWHEREfilter.ORDER BY hire_date DESCsorts so the latesthire_datelands first. Casey Davis on 2022-09-01 takes the top row; the four most recent hires fall into the first four positions.LIMIT 4caps the output at four rows. PostgreSQL completes the sort, hands back the first four, and stops.
Why descending matters here
Without DESC, ORDER BY hire_date sorts ascending — earliest hires first. That would surface the four employees who joined longest ago, the opposite of what the briefing asks for. The direction of the sort is what turns "four employees" into "the four most recently hired employees," and the LIMIT is what cuts the result to briefing size.
You practiced sorting by date descending and capping the result with LIMIT. "The N most recent ..." is the everyday shape of a recency feed; the deterministic tiebreaker is what guarantees the same N rows come back run after run.