← Back to SQL, page 1

sql-0020 · Easy fresher

Can ORDER BY refer to a column by its position, as in ORDER BY 2? Is that a good idea?

Yes. ORDER BY 2 sorts by the second column in the select list, and most engines support it. It is fine for quick ad-hoc queries, but risky in saved code: if someone adds or reorders a column in the SELECT list, the sort silently changes to a different column. Naming the column or alias is safer.

Full answer

A number in ORDER BY refers to a position in the select list, counting from 1. ORDER BY 2 DESC means "sort by the second output column, descending".

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
SELECT name, salary FROM employees WHERE dept_id = 1 ORDER BY 2 DESC, 1;
namesalary
Asha120000
Mei95000
Ravi95000

This is handy at a SQL prompt. In code that will be edited later, it is fragile. Suppose a teammate adds a column at the front:

sql
SELECT dept_id, name, salary FROM employees WHERE dept_id = 1 ORDER BY 2 DESC, 1;
dept_idnamesalary
1Ravi95000
1Mei95000
1Asha120000

The query still runs, but now it sorts by name, not by salary, and nobody gets an error. Sorting by the column name or alias keeps working whatever happens to the column order:

sql
SELECT dept_id, name, salary FROM employees WHERE dept_id = 1 ORDER BY salary DESC, name;
dept_idnamesalary
1Asha120000
1Mei95000
1Ravi95000

Positions work in GROUP BY too (GROUP BY 1) on PostgreSQL, MySQL and SQLite, with the same trade-off. A position outside the list, such as ORDER BY 5 with three columns, is an error.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

SELECT name, salary FROM employees WHERE dept_id = 1 ORDER BY 2 DESC, 1;

SELECT dept_id, name, salary FROM employees WHERE dept_id = 1 ORDER BY 2 DESC, 1;

SELECT dept_id, name, salary FROM employees WHERE dept_id = 1 ORDER BY salary DESC, name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • ORDER BY n sorts by the n-th select list column
  • Editing the select list silently changes the sort
  • Prefer names or aliases in saved queries

Common mistakes

  • Adding a column to the SELECT and breaking a positional sort without noticing

Follow-ups

  • Does GROUP BY 1 work the same way?
  • What happens with ORDER BY 0?

Tips

  • Say it works, then say why you would not commit it