{"success":true,"name":"decide-sql-order-by-explicitly-including-ties","description":"Returning SQL results where the order shown matters.","category":"ai-brimind","path":"ai-brimind/decide-sql-order-by-explicitly-including-ties/SKILL.md","file":"SKILL.md","content":"---\nname: decide-sql-order-by-explicitly-including-ties\ndescription: \"Returning SQL results where the order shown matters.\"\n---\n\n# Decide SQL ORDER BY Explicitly, Including Ties\n\n## When to use\nProject uses SQL, and you are writing a query whose results feed a list, export, or pagination, and its ordering is missing or has ties.\n\n## Example request\nNo ORDER BY but 'it always came back sorted' → an index change reshuffled everything; without ORDER BY, order is a coincidence, not a promise. Sorting by created_at with many equal values → tied rows swap between runs and pagination shows duplicates; a unique tiebreaker (id) makes order deterministic. Sorting text 'strangely' → 'Z' before 'a', or accents misplaced; collation rules were deciding, not you. ORDER BY on an unindexed column of a huge table → the whole result sorts in memory; ordering has a cost the index can absorb.\n\n## Instructions\nAct like a developer who knows the database promises nothing about order unless asked. For any query feeding a display, export, or pagination: write an explicit ORDER BY reflecting the intended order, and always append a unique column (id) as the final tiebreaker so equal values can't shuffle between executions — unstable order under pagination shows users duplicates and gaps. For text ordering, know that collation decides case and accent behavior; check the column's collation instead of being surprised by it. On large tables, check with EXPLAIN whether an index can serve the sort or whether you're paying for a full in-memory sort. Only finalize once the order is fully deterministic — you can predict the exact sequence of any two rows — and stated in the query itself, not assumed from observation.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1078\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}