When to use column numbers for ORDER BY?

QuestionsQuestions8 SkillsProDec, 25 2025
085

In professional development, using column numbers in ORDER BY is generally discouraged, but there are specific scenarios where it is helpful.

Here is a guide on when to use them and when to avoid them:

When it is GOOD to use column numbers:

  1. Quick Ad-hoc Queries: If you are manually typing queries in the terminal (like you are doing now in this lab) to quickly inspect data, using ORDER BY 2 is much faster than typing out a long column name like publication_year.
  2. Complex Expressions or Aliases: If you are calculating a value or using a complex function, sometimes it's easier to reference the position:
    SELECT (price * tax_rate / discount_code) + shipping_fee AS final_cost 
    FROM orders 
    ORDER BY 1 DESC;
  3. Grouping by multiple columns: In some database systems (like PostgreSQL), using numbers in GROUP BY 1, 2, 3 is a common shorthand to keep the query readable.

When it is BAD to use column numbers:

  1. Production Code & Scripts: Never use them in application code (Java, Python, etc.) or saved scripts. If a colleague changes the SELECT statement later to add a column at the beginning, your sort order will instantly break or sort by the wrong data.
  2. Readability: ORDER BY price tells the next person exactly what the logic is. ORDER BY 3 forces the reader to count columns to understand the intent.
  3. Large SELECT lists: If you are selecting 20 columns, trying to figure out if price is column 14 or 15 is prone to human error.

Lab Tip: For this experiment, feel free to try ORDER BY 2 DESC just to see it work, but try to get into the habit of using column names for better clarity

0 Comments

no data
Be the first to share your comment!