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:
- 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 2is much faster than typing out a long column name likepublication_year. - 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; - Grouping by multiple columns: In some database systems (like PostgreSQL), using numbers in
GROUP BY 1, 2, 3is a common shorthand to keep the query readable.
When it is BAD to use column numbers:
- Production Code & Scripts: Never use them in application code (Java, Python, etc.) or saved scripts. If a colleague changes the
SELECTstatement later to add a column at the beginning, your sort order will instantly break or sort by the wrong data. - Readability:
ORDER BY pricetells the next person exactly what the logic is.ORDER BY 3forces the reader to count columns to understand the intent. - Large SELECT lists: If you are selecting 20 columns, trying to figure out if
priceis 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