Oracle 26ai New Feature: JOIN TO ONE – Preventing Unexpected Row Multiplication

When should we use JOIN TO ONE?

During development and testing: use it when a JOIN is not expected to return multiple matching rows. It can help detect missing join conditions or unexpected row multiplication.

In production: consider keeping it in queries where row multiplication is never acceptable, especially financial reports and other sensitive aggregations. If Oracle detects a non-unique join at runtime, the application or reporting job should handle the error, log it, alert the responsible team if appropriate, and avoid publishing incomplete or incorrect results.

JOIN TO ONE was introduced in Oracle AI Database 26ai (Release Update 23.26.2). Let’s test how it works with a simple banking example.

1. Test environment

I am running the following tests on Oracle AI Database 26ai Enterprise Edition, version 23.26.3.0.0.

SQL> SELECT version_full FROM v$instance;
VERSION_FULL
-----------------
23.26.3.0.0

2. Test setup

Imagine we need a report showing the total account balance by customer risk category, using the December 1, 2026 risk assessment.
Account balances are stored in ACCOUNTS, while monthly assessments are stored in CUSTOMER_RISK_HISTORY.
Each customer can have multiple historical assessments, but only one per snapshot date – the composite primary key enforces this rule.

CREATE TABLE accounts (
account_id NUMBER PRIMARY KEY,
customer_id NUMBER,
balance NUMBER
);
CREATE TABLE customer_risk_history (
customer_id NUMBER,
assessment_date DATE,
risk_category VARCHAR2(10),
PRIMARY KEY (customer_id, assessment_date)
);
INSERT INTO accounts VALUES (101, 1, 1000);
INSERT INTO accounts VALUES (102, 2, 2000);
INSERT INTO customer_risk_history
VALUES (1, DATE '2026-11-01', 'LOW');
INSERT INTO customer_risk_history
VALUES (1, DATE '2026-12-01', 'HIGH');
INSERT INTO customer_risk_history
VALUES (2, DATE '2026-12-01', 'LOW');
COMMIT;

3. Traditional JOIN: incorrect results

Suppose we forget to include the assessment date in the JOIN condition:

SELECT c.risk_category, SUM(a.balance) AS total_balance
FROM accounts a
LEFT OUTER JOIN customer_risk_history c
ON a.customer_id = c.customer_id
GROUP BY c.risk_category
ORDER BY c.risk_category;

Expected result:

RISK_CATEG TOTAL_BALANCE
---------- -------------
HIGH 1000
LOW 3000

The report shows a total balance of 4000 instead of 3000. Account 101 is counted twice because it matches both historical assessments.
The problem is simply a missing filter in the JOIN condition. Such a mistake can easily go unnoticed, especially in a complex SQL query.

4. JOIN TO ONE: detecting the mistake

Let’s use JOIN TO ONE with the same incomplete join condition:

SELECT c.risk_category, SUM(a.balance) AS total_balance
FROM accounts a
JOIN TO ONE (
customer_risk_history c
ON a.customer_id = c.customer_id
)
GROUP BY c.risk_category
ORDER BY c.risk_category;
...
ERROR at line 4:
ORA-18640: JOIN TO ONE reached multiple rows joining to "C", resulting in a
non-unique join
Help: https://docs.oracle.com/error-help/db/ora-18640/

We have encountered ORA-18640 when the non-unique join is encountered.

Instead of silently returning incorrect results, JOIN TO ONE is designed to raise an error when it encounters multiple matching rows. Its default join type is LEFT OUTER JOIN, which is why I used LEFT OUTER JOIN in the previous example.

5. Correcting the JOIN condition

The actual fix is to include the missing assessment date:

SELECT c.risk_category, SUM(a.balance) AS total_balance
FROM accounts a
JOIN TO ONE (
customer_risk_history c
ON a.customer_id = c.customer_id
AND c.assessment_date = DATE '2026-12-01'
)
GROUP BY c.risk_category
ORDER BY c.risk_category;

Expected result:

RISK_CATEG TOTAL_BALANCE
------------- -------------
HIGH 1000
LOW 2000

A traditional LEFT OUTER JOIN with the same date predicate would also return the correct result. JOIN TO ONE is not a substitute for a correctly written join condition. It is an additional safeguard against unexpected row multiplication caused by mistakes such as a missing join predicate.

References

Oracle AI Database 26ai – Building Queries with Correct JOINS More Easily
Oracle Database Error Help – ORA-18640

Retrieve first 100 prime numbers using SQL Query

On an interview , one of our colleague asked participant the following question:

“How can you generate prime numbers using just query?”

Answering to this question using PL/SQL is simple using loop. But using SELECT πŸ™‚ not so simpleΒ πŸ™‚

I found the answer and want to share with you.

select l prime_number
from (select level l from dual connect by level <= 100)
, (select level m from dual connect by level <= 100)
where m<=l
group by l
having count(case l/m when trunc(l/m) then ‘Y’ end) = 2
order by l