Oracle 23.26SQLLab-verified · 2026-11-30

ORA-01791: not a SELECTed expression

With SELECT DISTINCT, ORDER BY may only use expressions that are in the select list.

Oracle AI Database 26ai Free 23.26.3.0.0
ORA-01791: not a SELECTed expression

Jump to a cause

  1. 01SELECT DISTINCT customer list sorted by order date
  2. 02DISTINCT on TO_CHAR(date) but ORDER BY the raw date column

Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-30 · container image gvenzl/oracle-free:23-slim

After DISTINCT merges duplicate rows, one output row can stand for several source rows with different values. Oracle cannot sort by such a column, so ORDER BY must use only what is in the SELECT list. Otherwise you get ORA-01791 and the * points at the ORDER BY column.

The usual fixes: sort by the selected expression or its alias, or replace DISTINCT with GROUP BY and sort by an aggregate such as MAX().

01SELECT DISTINCT customer list sorted by order date

The query wants customers ordered by latest order: SELECT DISTINCT cust_name ... ORDER BY order_date DESC. order_date is not selected, and one customer can have several dates.

Setup
Setuprun as lab_a
CREATE TABLE orders (order_id NUMBER PRIMARY KEY, cust_name VARCHAR2(20), order_date DATE);
INSERT INTO orders VALUES (1, 'Kim', DATE '2026-11-03');
INSERT INTO orders VALUES (2, 'Lee', DATE '2026-11-10');
INSERT INTO orders VALUES (3, 'Kim', DATE '2026-11-20');
INSERT INTO orders VALUES (4, 'Park', DATE '2026-11-15');
COMMIT;
OUTPUT
Table created.


1 row created.


1 row created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
SELECT DISTINCT cust_name FROM orders ORDER BY order_date DESC;
✕ ERROR
SELECT DISTINCT cust_name FROM orders ORDER BY order_date DESC
                                               *
ERROR at line 1:
ORA-01791: not a SELECTed expression
Help: https://docs.oracle.com/error-help/db/ora-01791/
Check the causerun as lab_a
SELECT cust_name, TO_CHAR(order_date, 'YYYY-MM-DD') AS order_date FROM orders ORDER BY cust_name, order_date;
OUTPUT
CUST_NAME            ORDER_DATE
-------------------- ----------
Kim                  2026-11-03
Kim                  2026-11-20
Lee                  2026-11-10
Park                 2026-11-15

4 rows selected.

Kim has two orders (2026-11-03 and 2026-11-20). After DISTINCT there is one Kim row, so it is not clear which date to sort by.

Checkrun as lab_a
SELECT cust_name, COUNT(*) AS orders, COUNT(DISTINCT order_date) AS dates FROM orders GROUP BY cust_name ORDER BY cust_name;
OUTPUT
CUST_NAME                ORDERS      DATES
-------------------- ---------- ----------
Kim                           2          2
Lee                           1          1
Park                          1          1

3 rows selected.

Counting per customer shows Kim has 2 different dates. Using GROUP BY cust_name and ORDER BY MAX(order_date) DESC makes the sort key explicit: Kim (2026-11-20), Park, Lee.

After the fixrun as lab_a
SELECT cust_name, TO_CHAR(MAX(order_date), 'YYYY-MM-DD') AS last_order
  FROM orders
 GROUP BY cust_name
 ORDER BY MAX(order_date) DESC;
✓ SUCCESS
CUST_NAME            LAST_ORDER
-------------------- ----------
Kim                  2026-11-20
Park                 2026-11-15
Lee                  2026-11-10

3 rows selected.

02DISTINCT on TO_CHAR(date) but ORDER BY the raw date column

The month list selects TO_CHAR(sale_date, 'YYYY-MM') but sorts by sale_date. Only the formatted value is in the select list.

Setup
Setuprun as lab_a
CREATE TABLE sales (sale_id NUMBER PRIMARY KEY, sale_date DATE, amount NUMBER);
INSERT INTO sales VALUES (1, DATE '2026-09-12', 100);
INSERT INTO sales VALUES (2, DATE '2026-11-02', 200);
INSERT INTO sales VALUES (3, DATE '2026-10-21', 150);
INSERT INTO sales VALUES (4, DATE '2026-11-28', 300);
COMMIT;
OUTPUT
Table created.


1 row created.


1 row created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
SELECT DISTINCT TO_CHAR(sale_date, 'YYYY-MM') AS ym FROM sales ORDER BY sale_date;
✕ ERROR
SELECT DISTINCT TO_CHAR(sale_date, 'YYYY-MM') AS ym FROM sales ORDER BY sale_date
                                                                        *
ERROR at line 1:
ORA-01791: not a SELECTed expression
Help: https://docs.oracle.com/error-help/db/ora-01791/
Checkrun as lab_a
SELECT TO_CHAR(sale_date, 'YYYY-MM-DD') AS sale_date, TO_CHAR(sale_date, 'YYYY-MM') AS ym FROM sales ORDER BY sale_date;
OUTPUT
SALE_DATE  YM
---------- -------
2026-09-12 2026-09
2026-10-21 2026-10
2026-11-02 2026-11
2026-11-28 2026-11

4 rows selected.

Two November sales (11-02, 11-28) become one 2026-11 row, so sale_date cannot be the sort key. Sorting by the alias ym works, and YYYY-MM text sorts in date order: 2026-09, 2026-10, 2026-11.

After the fixrun as lab_a
SELECT DISTINCT TO_CHAR(sale_date, 'YYYY-MM') AS ym FROM sales ORDER BY ym;
✓ SUCCESS
YM
-------
2026-09
2026-10
2026-11

3 rows selected.

FAQ

How do I fix ORA-01791 with SELECT DISTINCT and ORDER BY?

Sort only by columns or expressions in the select list (an alias works). If you need another column for sorting, use GROUP BY and an aggregate like MAX(col) in ORDER BY.

Can I just add the ORDER BY column to the SELECT list?

You can, but then DISTINCT works on the extra column too and duplicates come back (one row per customer and date). Check that this is the result you want.

Why does ORDER BY alias work with DISTINCT?

The alias refers to the selected expression itself, so every output row has exactly one value for it.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary users created for the test; system is the DBA account. Oracle AI Database 26ai Free Release 23.26.3.0.0 - Develop, Learn, and Run for Free 23.26.3.0.0