Untitled Lesson
Skill Matrix
UNLOCK NODES BY LEARNING NEW TAGS.
Why can a sort field name (unlike a filter VALUE) never be safely parameterized using a database driver's standard parameterized query mechanism?
💻 Code Challenge | +75 XP
Implement multi-field sorting for a GET /orders endpoint supporting comma-separated sort fields with a -prefix for descending, validated against an explicit allowlist, with a unique id tiebreaker always appended.
Paginated results sorted by "status" occasionally show the same order appearing on two different pages, even though the underlying data isn't being modified between requests. Reorder the steps to diagnose and fix this ordering bug.
Task: Reorder the blocks in logical sequence to solve the problem.
A.D.A. Interface
Adaptive Didactic Assistant

Pascual Vila
Frontend Instructor // Code Syllabus
The Error //
Directly interpolating a client-supplied sort field name into a raw SQL ORDER BY clause without validating it against an allowlist
// Wrong: injection risk, field name used directly
const query = `SELECT * FROM orders ORDER BY ${req.query.sort}`;
// Correct: validated against an allowlist first
if (!ALLOWED_SORT_FIELDS.includes(field)) throw new Error("Invalid sort field");The Solution //
A sort field name is a structural part of the query, not a value — it cannot be safely parameterized the way a filter value can, making it a genuine SQL injection risk if a client-supplied field name is used directly without validation. Validate against an explicit allowlist of genuinely sortable, known-safe field names before ever including it in a query.
The Error //
Sorting only by a non-unique field with no unique tiebreaker, when the result is also paginated
// Undefined relative order among tied rows
ORDER BY status ASC
// Correct: a unique tiebreaker guarantees stable, deterministic ordering
ORDER BY status ASC, id ASCThe Solution //
Rows tied on a non-unique sort field (like status) have an undefined relative order as far as the database is concerned — combined with pagination, this can cause the same row to appear on two different pages or a row to be skipped, since the database is free to order tied rows differently between separate query executions.