Oracle 23.26SQLLab-verified · 2026-11-24

ORA-01789: query block has incorrect number of result columns

The SELECT statements joined by UNION / UNION ALL return different numbers of columns.

Oracle AI Database 26ai Free 23.26.3.0.0
ORA-01789: query block has incorrect number of result columns

Jump to a cause

  1. 01One SELECT in the UNION has fewer columns
  2. 02SELECT * in a UNION after a column was added to one table
  3. 03The recursive member of a WITH query has fewer columns

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

ORA-01789 appears when queries combined with UNION, UNION ALL, INTERSECT or MINUS do not return the same number of columns. Oracle matches the columns by position, so every branch must have exactly the same count.

The error points to the start of the query (or to the branch that does not match), not to the missing column itself. Count the columns of each SELECT, and watch out for SELECT *, which changes when a table changes.

01One SELECT in the UNION has fewer columns

ORDERS has a STATUS column but ORDERS_HIST does not. The first SELECT returned 4 columns and the second only 3, so the query failed.

Add a value in the same position in the shorter SELECT. A constant such as 'CLOSED' worked, and so did CAST(NULL AS VARCHAR2(10)) when there is no value: all 3 rows were returned, with STATUS empty for the history rows.

Setup
Setuprun as lab_a
CREATE TABLE orders (order_id NUMBER, cust_id NUMBER, amount NUMBER, status VARCHAR2(10));
CREATE TABLE orders_hist (order_id NUMBER, cust_id NUMBER, amount NUMBER);
INSERT INTO orders VALUES (1003, 2, 700, 'NEW');
INSERT INTO orders_hist VALUES (1001, 1, 500);
INSERT INTO orders_hist VALUES (1002, 1, 300);
COMMIT;
OUTPUT
Table created.


Table created.


1 row created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
SELECT order_id, cust_id, amount, status FROM orders
UNION ALL
SELECT order_id, cust_id, amount FROM orders_hist
ORDER BY order_id;
✕ ERROR
SELECT order_id, cust_id, amount, status FROM orders
*
ERROR at line 1:
ORA-01789: query block has incorrect number of result columns
Help: https://docs.oracle.com/error-help/db/ora-01789/
Checkrun as lab_a
SELECT order_id, cust_id, amount, status FROM orders
UNION ALL
SELECT order_id, cust_id, amount, 'CLOSED' FROM orders_hist
ORDER BY order_id;
OUTPUT
  ORDER_ID    CUST_ID     AMOUNT STATUS
---------- ---------- ---------- ------------------------------
      1001          1        500 CLOSED
      1002          1        300 CLOSED
      1003          2        700 NEW

3 rows selected.

Use a NULL of the same type (CAST(NULL AS ...)) or a fixed value to fill the missing column.

After the fixrun as lab_a
SELECT order_id, cust_id, amount, status FROM orders
UNION ALL
SELECT order_id, cust_id, amount, CAST(NULL AS VARCHAR2(10)) FROM orders_hist
ORDER BY order_id;
✓ SUCCESS
  ORDER_ID    CUST_ID     AMOUNT STATUS
---------- ---------- ---------- ------------------------------
      1001          1        500
      1002          1        300
      1003          2        700 NEW

3 rows selected.

02SELECT * in a UNION after a column was added to one table

SELECT * FROM orders UNION ALL SELECT * FROM orders_hist worked while both tables had 3 columns. After ALTER TABLE orders ADD (memo VARCHAR2(100)), ORDERS had 4 columns and the same query failed with ORA-01789.

The view ALL_ORDERS, created with the same SELECT *, kept working. USER_VIEWS shows why: Oracle saved the view as SELECT "ORDER_ID","CUST_ID","AMOUNT", so the new column is not in it. The error hits SQL in scripts and programs that still use SELECT *.

Listing the columns explicitly in both SELECTs fixed the query.

Setup
Setuprun as lab_a
CREATE TABLE orders (order_id NUMBER, cust_id NUMBER, amount NUMBER);
CREATE TABLE orders_hist (order_id NUMBER, cust_id NUMBER, amount NUMBER);
INSERT INTO orders VALUES (1003, 2, 700);
INSERT INTO orders_hist VALUES (1001, 1, 500);
COMMIT;
CREATE VIEW all_orders AS
SELECT * FROM orders
UNION ALL
SELECT * FROM orders_hist;
SELECT * FROM orders
UNION ALL
SELECT * FROM orders_hist;
ALTER TABLE orders ADD (memo VARCHAR2(100));
OUTPUT
Table created.


Table created.


1 row created.


1 row created.


Commit complete.


View created.


  ORDER_ID    CUST_ID     AMOUNT
---------- ---------- ----------
      1003          2        700
      1001          1        500

2 rows selected.


Table altered.
Reproducerun as lab_a
SELECT * FROM orders
UNION ALL
SELECT * FROM orders_hist;
✕ ERROR
SELECT * FROM orders
*
ERROR at line 1:
ORA-01789: query block has incorrect number of result columns
Help: https://docs.oracle.com/error-help/db/ora-01789/
Checkrun as lab_a
SET LONG 200
COLUMN text FORMAT A60 WORD_WRAPPED
SELECT text FROM user_views WHERE view_name = 'ALL_ORDERS';
SELECT * FROM all_orders ORDER BY order_id;
OUTPUT
TEXT
------------------------------------------------------------
SELECT "ORDER_ID","CUST_ID","AMOUNT" FROM orders
UNION ALL
SELECT "ORDER_ID","CUST_ID","AMOUNT" FROM orders_hist


1 row selected.


  ORDER_ID    CUST_ID     AMOUNT
---------- ---------- ----------
      1001          1        500
      1003          2        700

2 rows selected.

Do not use SELECT * on both sides of a UNION. A column added later to one table breaks it.

After the fixrun as lab_a
SELECT order_id, cust_id, amount FROM orders
UNION ALL
SELECT order_id, cust_id, amount FROM orders_hist
ORDER BY order_id;
✓ SUCCESS
  ORDER_ID    CUST_ID     AMOUNT
---------- ---------- ----------
      1001          1        500
      1003          2        700

2 rows selected.

03The recursive member of a WITH query has fewer columns

A recursive WITH org (emp_id, name, lvl) has two parts joined by UNION ALL. The anchor SELECT returned 3 columns (emp_id, name, 1), but the recursive SELECT returned only e.emp_id, e.name. Oracle reported the error at line 4, the recursive part.

Adding o.lvl + 1 as the third column fixed it: KIM, LEE and PARK came back with levels 1, 2 and 3.

Setup
Setuprun as lab_a
CREATE TABLE emp (emp_id NUMBER, name VARCHAR2(20), mgr_id NUMBER);
INSERT INTO emp VALUES (1, 'KIM', NULL);
INSERT INTO emp VALUES (2, 'LEE', 1);
INSERT INTO emp VALUES (3, 'PARK', 2);
COMMIT;
OUTPUT
Table created.


1 row created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
WITH org (emp_id, name, lvl) AS (
  SELECT emp_id, name, 1 FROM emp WHERE mgr_id IS NULL
  UNION ALL
  SELECT e.emp_id, e.name FROM emp e JOIN org o ON e.mgr_id = o.emp_id
)
SELECT * FROM org;
✕ ERROR
  SELECT e.emp_id, e.name FROM emp e JOIN org o ON e.mgr_id = o.emp_id
  *
ERROR at line 4:
ORA-01789: query block has incorrect number of result columns
Help: https://docs.oracle.com/error-help/db/ora-01789/
Checkrun as lab_a
SELECT emp_id, name, 1 AS lvl FROM emp WHERE mgr_id IS NULL;
OUTPUT
    EMP_ID NAME                                  LVL
---------- ------------------------------ ----------
         1 KIM                                     1

1 row selected.

In recursive WITH, the anchor, the recursive member and the column list in parentheses must all have the same number of columns.

After the fixrun as lab_a
WITH org (emp_id, name, lvl) AS (
  SELECT emp_id, name, 1 FROM emp WHERE mgr_id IS NULL
  UNION ALL
  SELECT e.emp_id, e.name, o.lvl + 1 FROM emp e JOIN org o ON e.mgr_id = o.emp_id
)
SELECT * FROM org ORDER BY lvl;
✓ SUCCESS
    EMP_ID NAME                                  LVL
---------- ------------------------------ ----------
         1 KIM                                     1
         2 LEE                                     2
         3 PARK                                    3

3 rows selected.

FAQ

Which part of the query causes ORA-01789?

A branch of the UNION / UNION ALL / INTERSECT / MINUS returns a different number of columns than the others. Count the select list of each branch.

How do I UNION two tables that have different columns?

Fill the missing column with a value or with CAST(NULL AS type) in the SELECT that does not have it. In the test, both ways returned all 3 rows.

My UNION with SELECT * suddenly fails with ORA-01789. Why?

A column was probably added to one of the tables. In the test, the query worked with 3 columns each and failed right after ALTER TABLE ... ADD. List the columns explicitly.

Will a view created with SELECT * also break after adding a column?

Not in the test. Oracle saves the view with the column names at creation time, so the view kept returning the old 3 columns.

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