Showing posts with label JOINS. Show all posts
Showing posts with label JOINS. Show all posts

Oracle Anti Join FAQs with Examples

1. What is an Anti Join in Oracle?

An Anti Join returns rows from one table that do not have matching rows in another table.

Unlike an inner join, which returns matching records, an Anti Join returns non-matching records.

Oracle does not have an ANTI JOIN keyword. It is implemented using:

  • NOT EXISTS (recommended)
  • NOT IN (with care around NULL values)
  • LEFT OUTER JOIN ... IS NULL
  • The optimizer may internally transform these into an anti join execution plan.

2. Why do we use an Anti Join?

An Anti Join is used to:

  • Find employees without departments.
  • Find customers without orders.
  • Find products never sold.
  • Find students not enrolled in courses.
  • Detect missing relationships between tables.

3. What is the syntax of an Anti Join?

Using NOT EXISTS (recommended):

SELECT column_list
FROM table1 t1
WHERE NOT EXISTS
(
  SELECT 1
  FROM table2 t2
  WHERE t1.column_name = t2.column_name
);

4. How does an Anti Join work?

Suppose we have:

EMPLOYEES

EMPLOYEE_ID   EMPLOYEE_NAME   DEPARTMENT_ID
101           John            10
102           David           20
103           Scott           40

DEPARTMENTS

DEPARTMENT_ID   DEPARTMENT_NAME
10              Sales
20              HR
30              Finance

Query

SELECT employee_name
FROM employees e
WHERE NOT EXISTS
(
  SELECT 1
  FROM departments d
  WHERE d.department_id = e.department_id
);

Output

Employee
Scott

Scott belongs to department 40, which does not exist.

5. Why is NOT EXISTS preferred for Anti Joins?

NOT EXISTS:

  • Handles NULL values correctly.
  • Is usually easier for the optimizer to transform into an efficient anti join.
  • Is the preferred method in most Oracle applications.

Example:

SELECT employee_name
FROM employees e
WHERE NOT EXISTS
(
  SELECT 1
  FROM departments d
  WHERE d.department_id = e.department_id
);

6. Can an Anti Join be written using LEFT JOIN?

Yes.

SELECT e.employee_name
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id
WHERE d.department_id IS NULL;

This returns employees without matching departments.

7. Can an Anti Join be written using NOT IN?

Yes.

SELECT employee_name
FROM employees
WHERE department_id NOT IN
(
  SELECT department_id
  FROM departments
);

Important: If the subquery returns a NULL, NOT IN may return no rows because comparisons with NULL evaluate to unknown.

Safer version:

SELECT employee_name
FROM employees
WHERE department_id NOT IN
(
  SELECT department_id
  FROM departments
  WHERE department_id IS NOT NULL
);

8. What is the difference between NOT EXISTS and NOT IN?

NOT EXISTS NOT IN
Safely handles NULL values Can produce unexpected results if the subquery returns NULL
Usually preferred Requires extra care with NULL values
Often optimized efficiently May behave differently depending on data

9. Can an Anti Join use aliases?

Yes.

SELECT e.employee_name
FROM employees e
WHERE NOT EXISTS
(
  SELECT 1
  FROM departments d
  WHERE d.department_id = e.department_id
);

10. Can an Anti Join use a WHERE clause?

Yes.

SELECT e.employee_name
FROM employees e
WHERE e.salary > 5000
AND NOT EXISTS
(
  SELECT 1
  FROM departments d
  WHERE d.department_id = e.department_id
);

11. Can an Anti Join use multiple conditions?

Yes.

SELECT *
FROM orders o
WHERE NOT EXISTS
(
  SELECT 1
  FROM shipments s
  WHERE s.order_id = o.order_id
  AND s.customer_id = o.customer_id
);

12. Can an Anti Join use multiple tables?

Yes.

SELECT c.customer_name
FROM customers c
WHERE NOT EXISTS
(
  SELECT 1
  FROM orders o
  JOIN payments p
  ON o.order_id = p.order_id
  WHERE o.customer_id = c.customer_id
);

13. Can an Anti Join return duplicate rows?

Yes. If the driving table contains duplicate rows, those duplicates are returned because the anti join filters rows—it does not automatically remove duplicates.

Example:

SELECT DISTINCT employee_name
FROM employees e
WHERE NOT EXISTS
(
  SELECT 1
  FROM departments d
  WHERE d.department_id = e.department_id
);

Use DISTINCT only if duplicate elimination is required.

14. What are common uses of an Anti Join?

  • Customers without orders.
  • Employees without departments.
  • Departments without employees.
  • Products without sales.
  • Students without enrollments.
  • Suppliers without purchase orders.
  • Orders not yet shipped.

15. Real-Time Example – Customers Without Orders

CUSTOMERS

CUSTOMER_ID   CUSTOMER_NAME
1             Alice
2             Bob
3             Charlie

ORDERS

ORDER_ID   CUSTOMER_ID
100        1
101        2

Query

SELECT customer_name
FROM customers c
WHERE NOT EXISTS
(
  SELECT 1
  FROM orders o
  WHERE o.customer_id = c.customer_id
);

Output

Customer
Charlie

16. What are the advantages of an Anti Join?

  • Finds missing records efficiently.
  • NOT EXISTS handles NULL values correctly.
  • Commonly optimized by Oracle.
  • Useful for data validation and reporting.
  • Easy to understand and maintain.

17. What are the disadvantages of an Anti Join?

  • NOT IN can produce incorrect results when NULL values are present.
  • Performance depends on indexing and data volume.
  • Complex correlated subqueries can be harder to read.

18. Common Errors with Anti Joins

Error Cause
ORA-00904 Invalid column name
ORA-00942 Table or view does not exist
ORA-00933 SQL command not properly ended
Logical errors Using NOT IN with a subquery that returns NULL values

19. Anti Join vs Semi Join

Anti Join Semi Join
Returns rows without a match Returns rows with a match
Usually uses NOT EXISTS Usually uses EXISTS
Finds missing relationships Finds existing relationships

Semi Join Example

SELECT employee_name
FROM employees e
WHERE EXISTS
(
  SELECT 1
  FROM departments d
  WHERE d.department_id = e.department_id
);

20. Anti Join vs LEFT JOIN

Anti Join LEFT JOIN
Returns only unmatched rows Returns matched and unmatched rows
Often implemented with NOT EXISTS Uses LEFT OUTER JOIN

Equivalent Anti Join using LEFT JOIN:

SELECT e.employee_name
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id
WHERE d.department_id IS NULL;

21. Anti Join vs MINUS

Both can be used to find differences between datasets.

Using MINUS:

SELECT department_id
FROM employees

MINUS

SELECT department_id
FROM departments;
Anti Join MINUS
Compares related rows Compares result sets
Uses joins or subqueries Uses set operations
More flexible Simpler for set differences

22. How does Oracle execute an Anti Join?

Although SQL does not have an ANTI JOIN keyword, Oracle's optimizer may internally use execution plans such as:

  • HASH JOIN ANTI
  • MERGE JOIN ANTI
  • NESTED LOOPS ANTI

The chosen plan depends on factors such as table size, indexes, statistics, and optimizer decisions.

23. Interview Scenario

Question: Display customers who have never placed an order.

Answer: Use NOT EXISTS.

SELECT customer_name
FROM customers c
WHERE NOT EXISTS
(
  SELECT 1
  FROM orders o
  WHERE o.customer_id = c.customer_id
);

24. Which method is best for implementing an Anti Join?

General recommendation:

  1. NOT EXISTS – Best choice in most cases.
  2. LEFT JOIN ... IS NULL – Also widely used and easy to read.
  3. NOT IN – Use only when you are certain the subquery cannot return NULL values.

Anti Join is a practical way to find missing relationships, and NOT EXISTS is usually the safest and clearest choice in Oracle.

Oracle Semi Join FAQs with Examples

1. What is a Semi Join in Oracle?

A Semi Join returns rows from the first (outer) table only when at least one matching row exists in the second table.

Unlike an inner join, a Semi Join does not return columns from the second table, and each qualifying row from the first table is returned only once.

Oracle does not have a SEMI JOIN SQL keyword. The optimizer may internally transform certain queries into a semi join.

The most common way to write a Semi Join is with:

  • EXISTS (recommended)
  • IN

2. Why do we use a Semi Join?

A Semi Join is used to:

  • Check whether related records exist.
  • Filter rows based on matching records.
  • Avoid duplicate rows caused by regular joins.
  • Improve readability for existence checks.
  • Retrieve only data from the first table.

3. What is the syntax of a Semi Join?

Using EXISTS (recommended):

SELECT column_list
FROM table1 t1
WHERE EXISTS
(
  SELECT 1
  FROM table2 t2
  WHERE t1.column_name = t2.column_name
);

4. How does a Semi Join work?

Suppose we have:

CUSTOMERS

CUSTOMER_ID   CUSTOMER_NAME
1             Alice
2             Bob
3             Charlie

ORDERS

ORDER_ID   CUSTOMER_ID
100        1
101        1
102        2

Query

SELECT customer_name
FROM customers c
WHERE EXISTS
(
  SELECT 1
  FROM orders o
  WHERE o.customer_id = c.customer_id
);

Output

Customer
Alice
Bob

Although Alice has two orders, she appears only once.

5. Why is EXISTS preferred for a Semi Join?

EXISTS:

  • Stops searching once the first matching row is found.
  • Clearly expresses an existence check.
  • Is commonly optimized by Oracle into a semi join.
  • Avoids duplicate rows from the second table.

Example:

SELECT employee_name
FROM employees e
WHERE EXISTS
(
  SELECT 1
  FROM departments d
  WHERE d.department_id = e.department_id
);

6. Can a Semi Join be written using IN?

Yes.

SELECT employee_name
FROM employees
WHERE department_id IN
(
  SELECT department_id
  FROM departments
);

This is logically similar to:

SELECT employee_name
FROM employees e
WHERE EXISTS
(
  SELECT 1
  FROM departments d
  WHERE d.department_id = e.department_id
);

7. What is the difference between EXISTS and IN?

EXISTS IN
Correlated subquery Usually non-correlated subquery
Stops after first match Compares against the complete result set
Often preferred for correlated existence checks Often convenient for simple membership tests

Both can produce the same results in many cases, but the optimizer may choose different execution plans.

8. Can a Semi Join use aliases?

Yes.

SELECT e.employee_name
FROM employees e
WHERE EXISTS
(
  SELECT 1
  FROM departments d
  WHERE d.department_id = e.department_id
);

9. Can a Semi Join use a WHERE clause?

Yes.

SELECT e.employee_name
FROM employees e
WHERE e.salary > 5000
AND EXISTS
(
  SELECT 1
  FROM departments d
  WHERE d.department_id = e.department_id
);

10. Can a Semi Join use multiple conditions?

Yes.

SELECT *
FROM orders o
WHERE EXISTS
(
  SELECT 1
  FROM shipments s
  WHERE s.order_id = o.order_id
  AND s.customer_id = o.customer_id
);

11. Can a Semi Join use multiple tables?

Yes.

SELECT c.customer_name
FROM customers c
WHERE EXISTS
(
  SELECT 1
  FROM orders o
  JOIN payments p
  ON o.order_id = p.order_id
  WHERE o.customer_id = c.customer_id
);

12. Does a Semi Join return columns from the second table?

No. A Semi Join returns only columns from the outer query.

SELECT customer_name
FROM customers c
WHERE EXISTS
(
  SELECT 1
  FROM orders o
  WHERE o.customer_id = c.customer_id
);

The query returns only CUSTOMER_NAME.

13. Can a Semi Join return duplicate rows?

Normally, no, provided the driving (outer) table has unique rows.

Even if multiple matching rows exist in the second table, the outer row is returned only once.

Example:

Alice has two orders.

Result:

Customer
Alice

Not:

Customer
Alice
Alice

14. What are common uses of a Semi Join?

  • Customers with orders.
  • Employees assigned to departments.
  • Students enrolled in courses.
  • Products that have sales.
  • Suppliers with purchase orders.
  • Orders that have shipments.

15. Real-Time Example – Customers Who Placed Orders

CUSTOMERS

CUSTOMER_ID   CUSTOMER_NAME
1             Alice
2             Bob
3             Charlie

ORDERS

ORDER_ID   CUSTOMER_ID
100        1
101        2

Query

SELECT customer_name
FROM customers c
WHERE EXISTS
(
  SELECT 1
  FROM orders o
  WHERE o.customer_id = c.customer_id
);

Output

Customer
Alice
Bob

Charlie is excluded because no order exists.

16. What are the advantages of a Semi Join?

  • Efficient for checking existence.
  • Avoids duplicate rows from joins.
  • Returns only required data.
  • Easy to understand.
  • Commonly optimized by Oracle.

17. What are the disadvantages of a Semi Join?

  • Returns data only from the outer table.
  • Cannot directly display columns from the related table.
  • Complex correlated subqueries can be harder to maintain.
  • Performance depends on indexes and optimizer choices.

18. Common Errors with Semi Joins

Error Cause
ORA-00904 Invalid column name
ORA-00942 Table or view does not exist
ORA-00933 SQL command not properly ended
ORA-00936 Missing expression

19. Semi Join vs Inner Join

Semi Join Inner Join
Returns only columns from the first table Returns columns from both tables
Each qualifying outer row appears once May return multiple rows if multiple matches exist
Used for existence checks Used to retrieve related data

Example:

Semi Join

SELECT customer_name
FROM customers c
WHERE EXISTS
(
  SELECT 1
  FROM orders o
  WHERE o.customer_id = c.customer_id
);

Inner Join

SELECT c.customer_name,
       o.order_id
FROM customers c
JOIN orders o
ON c.customer_id = o.customer_id;

20. Semi Join vs Anti Join

Semi Join Anti Join
Returns rows that have a match Returns rows that do not have a match
Uses EXISTS Uses NOT EXISTS

Semi Join

WHERE EXISTS (...)

Anti Join

WHERE NOT EXISTS (...)

21. Semi Join vs DISTINCT

Sometimes an inner join with DISTINCT produces the same result as a semi join.

SELECT DISTINCT c.customer_name
FROM customers c
JOIN orders o
ON c.customer_id = o.customer_id;

However, using EXISTS is often clearer because it directly expresses an existence check.

22. How does Oracle execute a Semi Join?

Although SQL has no SEMI JOIN keyword, Oracle's optimizer may choose execution plans such as:

  • HASH JOIN SEMI
  • MERGE JOIN SEMI
  • NESTED LOOPS SEMI

The execution method depends on factors such as indexes, statistics, and table sizes.

23. Interview Scenario

Question: Display customers who have placed at least one order.

Answer: Use EXISTS.

SELECT customer_name
FROM customers c
WHERE EXISTS
(
  SELECT 1
  FROM orders o
  WHERE o.customer_id = c.customer_id
);

24. Which method is best for implementing a Semi Join?

General recommendation:

  1. EXISTS – Best choice for correlated existence checks.
  2. IN – Good for simple membership tests.
  3. Inner Join + DISTINCT – Use only when you also need data from the joined table or for specific query requirements.

Semi Join is a clean and efficient way to test for existence, and in Oracle it is most commonly expressed with EXISTS.

Oracle LATERAL JOIN FAQs with Examples

1. What is a LATERAL JOIN in Oracle?

A LATERAL JOIN allows a subquery in the FROM clause to reference columns from tables that appear before it in the same FROM clause.

Without LATERAL, a subquery in the FROM clause cannot reference columns from preceding tables.

Introduced in Oracle Database 12c, LATERAL is useful for correlated subqueries that return multiple rows.

Syntax

SELECT column_list
FROM table1 t1,
LATERAL (
  SELECT ...
  FROM table2 t2
  WHERE t2.column_name = t1.column_name
);

2. Why do we use a LATERAL JOIN?

A LATERAL JOIN is used to:

  • Reference columns from preceding tables inside a subquery.
  • Return multiple related rows for each row in the outer table.
  • Simplify correlated subqueries.
  • Retrieve Top-N rows per group.
  • Improve readability compared to complex nested queries.

3. How does a LATERAL JOIN work?

Suppose we have:

DEPARTMENTS

DEPARTMENT_ID   DEPARTMENT_NAME
10              Sales
20              HR

EMPLOYEES

FIRST_NAME   DEPARTMENT_ID   SALARY
John         10              5000
David        10              4500
Scott        20              7000

Query

SELECT d.department_name,
       e.first_name,
       e.salary
FROM departments d,
LATERAL (
  SELECT first_name,
         salary
  FROM employees e
  WHERE e.department_id = d.department_id
) e;

Output

Department   Employee   Salary
Sales        John       5000
Sales        David      4500
HR           Scott      7000

4. What is the syntax of a LATERAL JOIN?

SELECT *
FROM table1 t1,
LATERAL (
  SELECT *
  FROM table2 t2
  WHERE t2.id = t1.id
);

5. What problem does LATERAL solve?

Without LATERAL, the following query is invalid because the inline view cannot reference d.department_id:

SELECT *
FROM departments d,
(
  SELECT *
  FROM employees e
  WHERE e.department_id = d.department_id
);

With LATERAL, it becomes valid:

SELECT *
FROM departments d,
LATERAL (
  SELECT *
  FROM employees e
  WHERE e.department_id = d.department_id
);

6. Can LATERAL return multiple rows?

Yes. Unlike a scalar subquery, a LATERAL subquery can return zero, one, or many rows for each row in the outer table.

SELECT d.department_name,
       e.first_name
FROM departments d,
LATERAL (
  SELECT first_name
  FROM employees e
  WHERE e.department_id = d.department_id
) e;

7. Can LATERAL use ORDER BY?

Yes.

SELECT d.department_name,
       e.first_name,
       e.salary
FROM departments d,
LATERAL (
  SELECT first_name,
         salary
  FROM employees e
  WHERE e.department_id = d.department_id
  ORDER BY salary DESC
) e;

8. Can LATERAL be used to fetch the Top-N rows per group?

Yes.

Example: Highest-paid employee in each department.

SELECT d.department_name,
       e.first_name,
       e.salary
FROM departments d,
LATERAL (
  SELECT first_name,
         salary
  FROM employees e
  WHERE e.department_id = d.department_id
  ORDER BY salary DESC
  FETCH FIRST 1 ROW ONLY
) e;

Output

Department   Employee   Salary
Sales        John       5000
HR           Scott      7000

9. Can LATERAL be used with LEFT OUTER JOIN?

Yes. Oracle supports LEFT OUTER JOIN LATERAL.

SELECT d.department_name,
       e.first_name
FROM departments d
LEFT JOIN LATERAL (
  SELECT first_name
  FROM employees e
  WHERE e.department_id = d.department_id
) e
ON 1 = 1;

This returns all departments, even those without employees.

10. Can LATERAL be used with CROSS JOIN?

Yes.

SELECT d.department_name,
       e.first_name
FROM departments d
CROSS JOIN LATERAL (
  SELECT first_name
  FROM employees e
  WHERE e.department_id = d.department_id
) e;

CROSS JOIN LATERAL returns only departments for which the lateral subquery returns rows.

11. Can LATERAL use aggregate functions?

Yes.

SELECT d.department_name,
       x.avg_salary
FROM departments d,
LATERAL (
  SELECT AVG(salary) AS avg_salary
  FROM employees e
  WHERE e.department_id = d.department_id
) x;

12. Can LATERAL use GROUP BY?

Yes.

SELECT d.department_name,
       x.employee_count
FROM departments d,
LATERAL (
  SELECT COUNT(*) AS employee_count
  FROM employees e
  WHERE e.department_id = d.department_id
  GROUP BY department_id
) x;

13. Can LATERAL use analytic functions?

Yes.

SELECT d.department_name,
       x.first_name,
       x.salary_rank
FROM departments d,
LATERAL (
  SELECT first_name,
         RANK() OVER (ORDER BY salary DESC) AS salary_rank
  FROM employees e
  WHERE e.department_id = d.department_id
) x;

14. What is the difference between LATERAL and a normal inline view?

Normal Inline View LATERAL
Cannot reference previous tables Can reference previous tables
Independent subquery Correlated subquery
Less flexible More flexible

15. What is the difference between LATERAL and a correlated subquery?

Correlated Subquery LATERAL
Usually in the SELECT or WHERE clause Appears in the FROM clause
Often returns one value Can return multiple rows
Limited flexibility More flexible for complex row sets

16. What are common uses of LATERAL?

  • Top-N rows per group.
  • Latest order per customer.
  • Highest-paid employee per department.
  • JSON processing.
  • XML processing.
  • Table functions that depend on each input row.

17. Real-Time Example – Latest Order Per Customer

CUSTOMERS

CUSTOMER_ID   CUSTOMER_NAME
1             Alice
2             Bob

ORDERS

ORDER_ID   CUSTOMER_ID   ORDER_DATE
101        1             2025-01-01
102        1             2025-03-10
103        2             2025-02-15

Query

SELECT c.customer_name,
       o.order_id,
       o.order_date
FROM customers c,
LATERAL (
  SELECT order_id,
         order_date
  FROM orders o
  WHERE o.customer_id = c.customer_id
  ORDER BY order_date DESC
  FETCH FIRST 1 ROW ONLY
) o;

Output

Customer   Latest Order
Alice      102
Bob        103

18. What are the advantages of LATERAL?

  • Supports correlated inline views.
  • Can return multiple rows.
  • Simplifies complex SQL.
  • Excellent for Top-N queries.
  • Works well with table functions.
  • Improves readability.

19. What are the disadvantages of LATERAL?

  • Available only in Oracle Database 12c and later.
  • Can be harder to understand for developers unfamiliar with it.
  • Poorly written correlated subqueries may affect performance on large datasets.

20. Common Errors with LATERAL

Error Cause
ORA-00904 Invalid column name
ORA-00942 Table or view does not exist
ORA-00933 SQL command not properly ended
ORA-00907 Missing right parenthesis

21. LATERAL vs CROSS JOIN

LATERAL CROSS JOIN
Can reference preceding tables Cannot reference preceding tables
Correlated subquery Cartesian product
Returns related rows Returns every possible combination

22. LATERAL vs CROSS APPLY

Oracle supports CROSS APPLY and OUTER APPLY, which are closely related to LATERAL.

LATERAL CROSS APPLY
SQL standard syntax Oracle/Microsoft-style syntax
Correlated inline view Correlated table expression
Returns matching rows Similar behavior to CROSS JOIN LATERAL

Example using CROSS APPLY:

SELECT d.department_name,
       e.first_name
FROM departments d
CROSS APPLY (
  SELECT first_name
  FROM employees e
  WHERE e.department_id = d.department_id
) e;

23. LATERAL vs APPLY

Feature LATERAL CROSS APPLY OUTER APPLY
Returns only matching rows Yes Yes No
Returns unmatched outer rows No No Yes
Correlated subquery Yes Yes Yes

OUTER APPLY behaves similarly to a LEFT OUTER JOIN LATERAL.

24. Interview Scenario

Question: Display the highest-paid employee in every department.

Answer: Use a LATERAL JOIN with FETCH FIRST 1 ROW ONLY.

SELECT d.department_name,
       e.first_name,
       e.salary
FROM departments d,
LATERAL (
  SELECT first_name,
         salary
  FROM employees e
  WHERE e.department_id = d.department_id
  ORDER BY salary DESC
  FETCH FIRST 1 ROW ONLY
) e;

LATERAL is useful when a subquery must depend on rows from tables that appear earlier in the FROM clause.

Oracle USING Clause Join FAQs with Examples

1. What is the USING Clause in Oracle JOIN?

The USING clause is used in a JOIN when both tables have a column with the same name and compatible data type.

It eliminates the need to write the join condition with the ON clause.

Syntax

SELECT column_list
FROM table1
JOIN table2
USING (common_column);

2. Why do we use the USING clause?

A USING clause is used to:

  • Simplify join syntax.
  • Avoid repeating identical column names.
  • Improve query readability.
  • Join tables using common columns.

3. What is the syntax of the USING clause?

SELECT columns
FROM table1
JOIN table2
USING (column_name);

Example:

SELECT employee_name,
       department_name
FROM employees
JOIN departments
USING (department_id);

4. How does the USING clause work?

Suppose we have:

EMPLOYEES

EMPLOYEE_ID   EMPLOYEE_NAME   DEPARTMENT_ID
101           John            10
102           David           20
103           Scott           30

DEPARTMENTS

DEPARTMENT_ID   DEPARTMENT_NAME
10              Sales
20              HR
30              Finance

Query

SELECT employee_name,
       department_name
FROM employees
JOIN departments
USING (department_id);

Output

Employee   Department
John       Sales
David      HR
Scott      Finance

5. What is the difference between USING and ON?

USING ON
Used when column names are identical Can use different column names
Simpler syntax More flexible
Column name written only once Both table columns must be specified
Cannot use expressions in the join condition Can use expressions and multiple conditions

Example using ON:

SELECT employee_name,
       department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id;

Example using USING:

SELECT employee_name,
       department_name
FROM employees
JOIN departments
USING (department_id);

6. Can USING be used with multiple columns?

Yes.

SELECT *
FROM employees
JOIN departments
USING
(
  department_id,
  location_id
);

Both columns must exist in both tables with the same names and compatible data types.

7. Can USING be used with different column names?

No.

Example:

Table A: dept_id

Table B: department_id

You must use ON.

SELECT *
FROM employees e
JOIN departments d
ON e.dept_id = d.department_id;

8. Can USING be used with INNER JOIN?

Yes.

SELECT employee_name,
       department_name
FROM employees
INNER JOIN departments
USING (department_id);

9. Can USING be used with LEFT JOIN?

Yes.

SELECT employee_name,
       department_name
FROM employees
LEFT JOIN departments
USING (department_id);

Returns all employees, including those without matching departments.

10. Can USING be used with RIGHT JOIN?

Yes.

SELECT employee_name,
       department_name
FROM employees
RIGHT JOIN departments
USING (department_id);

Returns all departments, even if no employee belongs to them.

11. Can USING be used with FULL OUTER JOIN?

Yes.

SELECT employee_name,
       department_name
FROM employees
FULL OUTER JOIN departments
USING (department_id);

12. Can USING be used with CROSS JOIN?

No.

A CROSS JOIN does not have a join condition.

Incorrect:

SELECT *
FROM employees
CROSS JOIN departments
USING (department_id);

13. Can table aliases be used with USING?

Yes.

SELECT employee_name,
       department_name
FROM employees e
JOIN departments d
USING (department_id);

After a USING clause, the join column is treated as a single column in the result. Do not qualify the join column with a table alias in the SELECT list.

Correct:

SELECT department_id,
       employee_name
FROM employees e
JOIN departments d
USING (department_id);

Incorrect:

SELECT e.department_id
FROM employees e
JOIN departments d
USING (department_id);

This raises:

ORA-25154: column part of USING clause cannot have qualifier

14. What columns appear in the result?

The join column appears only once.

Example:

Tables:

EMPLOYEES

DEPARTMENT_ID

DEPARTMENTS

DEPARTMENT_ID

Output:

DEPARTMENT_ID only once.

15. Can USING be used with WHERE?

Yes.

SELECT employee_name,
       department_name
FROM employees
JOIN departments
USING (department_id)
WHERE department_name = 'Sales';

16. Can USING be combined with GROUP BY?

Yes.

SELECT department_name,
       COUNT(*)
FROM employees
JOIN departments
USING (department_id)
GROUP BY department_name;

17. Can USING be combined with ORDER BY?

Yes.

SELECT employee_name,
       department_name
FROM employees
JOIN departments
USING (department_id)
ORDER BY department_name;

18. What are the advantages of USING?

  • Simple syntax.
  • Less typing.
  • Improved readability.
  • Avoids repeating identical column names.
  • Returns the join column only once.

19. What are the disadvantages of USING?

  • Only works when column names are identical.
  • Cannot join on expressions.
  • Cannot compare differently named columns.
  • Less flexible than ON.

20. Common Errors with USING

Error Cause
ORA-25154 Qualified a column that is part of the USING clause (for example, e.department_id)
ORA-00904 Invalid column name
ORA-00942 Table or view does not exist
ORA-00933 SQL command not properly ended

21. Real-Time Example

Suppose an HR database has:

EMPLOYEES

EMPLOYEE_NAME   DEPARTMENT_ID
John           10
David          20

DEPARTMENTS

DEPARTMENT_ID   DEPARTMENT_NAME
10              Sales
20              HR

Query

SELECT employee_name,
       department_name
FROM employees
JOIN departments
USING (department_id);

Output

Employee   Department
John       Sales
David      HR

22. USING vs ON vs NATURAL JOIN

Feature USING ON NATURAL JOIN
Same column names required Yes No Yes
Specify join condition Partially (column list only) Yes No
Multiple columns supported Yes Yes Yes (automatic)
Join column appears once Yes No (unless explicitly selected) Yes
Recommended for production Yes Yes Rarely

23. When should you use USING instead of ON?

Use USING when:

  • The join columns have the same name in both tables.
  • The join condition is a simple equality comparison.
  • You want shorter, cleaner SQL.

Use ON when:

  • Column names differ.
  • Multiple or complex join conditions are required.
  • You need expressions or non-equality conditions in the join.

USING clause is helpful for cleaner joins when column names match, but ON remains the more flexible and widely used option.

Oracle FULL OUTER JOIN FAQs with Examples

1. What is a FULL OUTER JOIN in Oracle?

A FULL OUTER JOIN returns:

  • All matching rows from both tables.
  • All unmatched rows from the left table.
  • All unmatched rows from the right table.
  • NULL values where no matching row exists.

It combines the results of a LEFT OUTER JOIN and a RIGHT OUTER JOIN.

Syntax

SELECT column_list
FROM table1 t1
FULL OUTER JOIN table2 t2
ON t1.column_name = t2.column_name;

You can also write:

SELECT column_list
FROM table1 t1
FULL JOIN table2 t2
ON t1.column_name = t2.column_name;

The keyword OUTER is optional.

2. Why do we use a FULL OUTER JOIN?

A FULL OUTER JOIN is used to:

  • Retrieve all rows from both tables.
  • Find matching and non-matching records.
  • Compare two datasets.
  • Identify missing records.
  • Create reconciliation reports.

3. How does a FULL OUTER JOIN work?

Suppose we have:

EMPLOYEES

EMPLOYEE_ID   EMPLOYEE_NAME   DEPARTMENT_ID
101           John            10
102           David           20
103           Scott           40

DEPARTMENTS

DEPARTMENT_ID   DEPARTMENT_NAME
10              Sales
20              HR
30              Finance

Query

SELECT e.employee_name,
       d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id;

Output

Employee   Department
John       Sales
David      HR
Scott      NULL
NULL       Finance

Explanation:

  • John and David have matching departments.
  • Scott has no matching department.
  • Finance has no matching employee.

4. What is the syntax of a FULL OUTER JOIN?

SELECT *
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id;

Or:

SELECT *
FROM employees e
FULL JOIN departments d
ON e.department_id = d.department_id;

5. What is the difference between FULL JOIN and FULL OUTER JOIN?

There is no difference.

FULL JOIN and FULL OUTER JOIN produce the same results.

6. What happens when there is no matching row?

Oracle returns:

  • Values from the existing table.
  • NULL values for the missing table.

Example:

Employee   Department
Scott      NULL
NULL       Finance

7. Can a FULL OUTER JOIN use aliases?

Yes.

SELECT e.employee_name,
       d.department_name
FROM employees e
FULL JOIN departments d
ON e.department_id = d.department_id;

Aliases improve readability.

8. Can a FULL OUTER JOIN use a WHERE clause?

Yes.

Example:

SELECT e.employee_name,
       d.department_name
FROM employees e
FULL JOIN departments d
ON e.department_id = d.department_id
WHERE d.department_name = 'Sales';

To return only unmatched rows from either table:

SELECT e.employee_name,
       d.department_name
FROM employees e
FULL JOIN departments d
ON e.department_id = d.department_id
WHERE e.department_id IS NULL
OR d.department_id IS NULL;

9. Can a FULL OUTER JOIN use GROUP BY?

Yes.

SELECT d.department_name,
       COUNT(e.employee_id) AS employee_count
FROM employees e
FULL JOIN departments d
ON e.department_id = d.department_id
GROUP BY d.department_name;

10. Can a FULL OUTER JOIN use ORDER BY?

Yes.

SELECT e.employee_name,
       d.department_name
FROM employees e
FULL JOIN departments d
ON e.department_id = d.department_id
ORDER BY department_name;

11. Can a FULL OUTER JOIN use multiple conditions?

Yes.

SELECT *
FROM orders o
FULL JOIN shipments s
ON o.order_id = s.order_id
AND o.customer_id = s.customer_id;

12. Can a FULL OUTER JOIN use multiple tables?

Yes.

SELECT e.employee_name,
       d.department_name,
       l.city
FROM employees e
FULL JOIN departments d
ON e.department_id = d.department_id
LEFT JOIN locations l
ON d.location_id = l.location_id;

13. Can a FULL OUTER JOIN return duplicate rows?

Yes. If multiple matching rows exist in either table, duplicate combinations may appear.

14. How do you find unmatched rows?

Use IS NULL.

SELECT e.employee_name,
       d.department_name
FROM employees e
FULL JOIN departments d
ON e.department_id = d.department_id
WHERE e.department_id IS NULL
OR d.department_id IS NULL;

This returns:

  • Employees without departments.
  • Departments without employees.

15. Can FULL OUTER JOIN be written using UNION?

Yes. A FULL OUTER JOIN can be simulated using a LEFT OUTER JOIN, a RIGHT OUTER JOIN, and UNION (or UNION ALL with appropriate filtering).

SELECT e.employee_name,
       d.department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id

UNION

SELECT e.employee_name,
       d.department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id = d.department_id;

Using the native FULL OUTER JOIN is generally simpler and easier to maintain.

16. What are common uses of a FULL OUTER JOIN?

  • Data reconciliation.
  • Data migration validation.
  • Comparing two systems.
  • Employee vs department reports.
  • Inventory comparisons.
  • Customer vs order analysis.

17. Real-Time Example – Comparing Customers and Orders

CUSTOMERS

CUSTOMER_ID   CUSTOMER_NAME
1             Alice
2             Bob
3             Charlie

ORDERS

ORDER_ID   CUSTOMER_ID
100        1
101        2
102        4

Query

SELECT c.customer_name,
       o.order_id
FROM customers c
FULL OUTER JOIN orders o
ON c.customer_id = o.customer_id;

Output

Customer   Order
Alice      100
Bob        101
Charlie    NULL
NULL       102

Explanation:

  • Charlie has no orders.
  • Order 102 belongs to a customer not found in the CUSTOMERS table.

18. What are the advantages of a FULL OUTER JOIN?

  • Returns every row from both tables.
  • Finds missing records on either side.
  • Excellent for reconciliation.
  • Reduces the need for multiple queries.
  • Easy to identify unmatched data.

19. What are the disadvantages of a FULL OUTER JOIN?

  • Can return large result sets.
  • May be slower than inner or left joins.
  • More memory-intensive for large datasets.
  • Can produce duplicate rows when multiple matches exist.

20. Common Errors with FULL OUTER JOIN

Error Cause
ORA-00904 Invalid column name
ORA-00942 Table or view does not exist
ORA-00918 Column ambiguously defined
ORA-00933 SQL command not properly ended

21. FULL OUTER JOIN vs LEFT OUTER JOIN

FULL OUTER JOIN LEFT OUTER JOIN
Returns all rows from both tables Returns all rows from the left table
Includes unmatched rows from both tables Includes only unmatched rows from the left table

22. FULL OUTER JOIN vs RIGHT OUTER JOIN

FULL OUTER JOIN RIGHT OUTER JOIN
Returns all rows from both tables Returns all rows from the right table
Includes unmatched rows from both tables Includes only unmatched rows from the right table

23. FULL OUTER JOIN vs INNER JOIN

FULL OUTER JOIN INNER JOIN
Returns matching and non-matching rows Returns only matching rows
Includes NULL values for missing matches Excludes unmatched rows

24. Does the old Oracle (+) syntax support FULL OUTER JOIN?

No.

The legacy (+) operator supports left and right outer joins but does not support full outer joins.

Use ANSI syntax:

SELECT *
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id;

25. Interview Scenario

Question: Compare two tables and display:

  • Matching rows.
  • Rows existing only in the first table.
  • Rows existing only in the second table.

Answer: Use a FULL OUTER JOIN.

SELECT e.employee_name,
       d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id;

26. How can you return only unmatched rows from a FULL OUTER JOIN?

Filter for NULL values in the join columns.

SELECT e.employee_name,
       d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id
WHERE e.department_id IS NULL
OR d.department_id IS NULL;

This excludes matched rows and returns only records that exist in one table but not the other.


FULL OUTER JOIN is useful for reconciliation, comparisons, and finding unmatched data from both sides.

Oracle RIGHT OUTER JOIN FAQs with Examples

1. What is a RIGHT OUTER JOIN in Oracle?

A RIGHT OUTER JOIN returns:

  • All rows from the right table.
  • Matching rows from the left table.
  • NULL values for the left table columns when no matching row exists.

It is useful when you want to keep every row from the right table, regardless of whether a matching row exists in the left table.

Syntax

SELECT column_list
FROM table1 t1
RIGHT OUTER JOIN table2 t2
ON t1.column_name = t2.column_name;

The OUTER keyword is optional:

SELECT column_list
FROM table1 t1
RIGHT JOIN table2 t2
ON t1.column_name = t2.column_name;

2. Why do we use a RIGHT OUTER JOIN?

A RIGHT OUTER JOIN is used to:

  • Retrieve all records from the right table.
  • Find rows in the right table without matching rows in the left table.
  • Generate reports including unmatched records.
  • Identify missing relationships.

3. How does a RIGHT OUTER JOIN work?

Suppose we have:

EMPLOYEES

EMPLOYEE_ID   EMPLOYEE_NAME   DEPARTMENT_ID
101           John            10
102           David           20

DEPARTMENTS

DEPARTMENT_ID   DEPARTMENT_NAME
10              Sales
20              HR
30              Finance

Query

SELECT e.employee_name,
       d.department_name
FROM employees e
RIGHT OUTER JOIN departments d
ON e.department_id = d.department_id;

Output

Employee   Department
John       Sales
David      HR
NULL       Finance

The Finance department appears even though no employee belongs to it.

4. What is the syntax of a RIGHT OUTER JOIN?

SELECT *
FROM employees e
RIGHT OUTER JOIN departments d
ON e.department_id = d.department_id;

Or:

SELECT *
FROM employees e
RIGHT JOIN departments d
ON e.department_id = d.department_id;

5. What is the difference between RIGHT JOIN and RIGHT OUTER JOIN?

There is no difference.

These are equivalent:

  • RIGHT JOIN
  • RIGHT OUTER JOIN

The keyword OUTER is optional.

6. What happens when there is no matching row?

Oracle returns:

  • All columns from the right table.
  • NULL values for the left table columns.

Example:

Employee   Department
NULL       Finance

7. Can a RIGHT OUTER JOIN use aliases?

Yes.

SELECT e.employee_name,
       d.department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id = d.department_id;

8. Can a RIGHT OUTER JOIN use a WHERE clause?

Yes.

Example:

SELECT e.employee_name,
       d.department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id = d.department_id
WHERE d.department_name = 'Sales';

A WHERE condition on the left table can filter out unmatched rows. If you need to preserve all rows from the right table while applying a condition to the left table, place that condition in the ON clause.

Example:

SELECT e.employee_name,
       d.department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id = d.department_id
AND e.employee_name LIKE 'J%';

9. Can a RIGHT OUTER JOIN use GROUP BY?

Yes.

SELECT d.department_name,
       COUNT(e.employee_id) AS employee_count
FROM employees e
RIGHT JOIN departments d
ON e.department_id = d.department_id
GROUP BY d.department_name;

10. Can a RIGHT OUTER JOIN use ORDER BY?

Yes.

SELECT e.employee_name,
       d.department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id = d.department_id
ORDER BY d.department_name;

11. Can a RIGHT OUTER JOIN use multiple conditions?

Yes.

SELECT *
FROM orders o
RIGHT JOIN shipments s
ON o.order_id = s.order_id
AND o.customer_id = s.customer_id;

12. Can a RIGHT OUTER JOIN use multiple tables?

Yes.

SELECT e.employee_name,
       d.department_name,
       l.city
FROM employees e
RIGHT JOIN departments d
ON e.department_id = d.department_id
LEFT JOIN locations l
ON d.location_id = l.location_id;

13. Can a RIGHT OUTER JOIN return duplicate rows?

Yes. If multiple rows in the left table match a row in the right table, multiple result rows are returned.

14. How do you find unmatched rows using a RIGHT OUTER JOIN?

Use IS NULL on a column from the left table.

SELECT d.department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id = d.department_id
WHERE e.department_id IS NULL;

This returns departments with no employees.

15. What is the old Oracle (+) syntax for a RIGHT OUTER JOIN?

SELECT e.employee_name,
       d.department_name
FROM employees e,
     departments d
WHERE e.department_id(+) = d.department_id;

Equivalent ANSI syntax:

SELECT e.employee_name,
       d.department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id = d.department_id;

ANSI join syntax is recommended for modern Oracle applications.

16. What are common uses of a RIGHT OUTER JOIN?

  • Departments without employees.
  • Products without sales.
  • Courses without students.
  • Categories without products.
  • Projects without assigned employees.
  • Suppliers without purchase orders.

17. Real-Time Example – Courses Without Students

COURSES

COURSE_ID   COURSE_NAME
1          SQL
2          PL/SQL
3          Oracle DBA

ENROLLMENTS

STUDENT_ID   COURSE_ID
101          1
102          2

Query

SELECT e.student_id,
       c.course_name
FROM enrollments e
RIGHT JOIN courses c
ON e.course_id = c.course_id;

Output

Student   Course
101       SQL
102       PL/SQL
NULL      Oracle DBA

18. What are the advantages of a RIGHT OUTER JOIN?

  • Returns all rows from the right table.
  • Finds unmatched rows.
  • Useful for reporting.
  • Supports optional relationships.
  • Easy to understand.

19. What are the disadvantages of a RIGHT OUTER JOIN?

  • Can return large result sets.
  • Duplicate matches can increase row counts.
  • Incorrect WHERE conditions may unintentionally eliminate unmatched rows.
  • Some developers find LEFT JOIN easier to read because it keeps the primary table first.

20. Common Errors with RIGHT OUTER JOIN

Error Cause
ORA-00904 Invalid column name
ORA-00942 Table or view does not exist
ORA-00918 Column ambiguously defined
ORA-00933 SQL command not properly ended

21. RIGHT OUTER JOIN vs LEFT OUTER JOIN

RIGHT OUTER JOIN LEFT OUTER JOIN
Returns all rows from the right table Returns all rows from the left table
Missing left-side matches appear as NULL Missing right-side matches appear as NULL

Example:

-- RIGHT JOIN
SELECT *
FROM employees e
RIGHT JOIN departments d
ON e.department_id = d.department_id;

Equivalent LEFT JOIN:

SELECT *
FROM departments d
LEFT JOIN employees e
ON d.department_id = e.department_id;

22. RIGHT OUTER JOIN vs INNER JOIN

RIGHT OUTER JOIN INNER JOIN
Returns all rows from the right table Returns only matching rows
Includes unmatched rows with NULL values Excludes unmatched rows

23. RIGHT OUTER JOIN vs FULL OUTER JOIN

RIGHT OUTER JOIN FULL OUTER JOIN
Returns all rows from the right table Returns all rows from both tables
Includes matching rows and unmatched right rows Includes matching rows plus unmatched rows from both tables

24. Interview Scenario

Question: Display all departments, including departments that have no employees.

Answer: Use a RIGHT OUTER JOIN (or equivalently, a LEFT JOIN with the table order reversed).

SELECT e.employee_name,
       d.department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id = d.department_id;

25. Is RIGHT OUTER JOIN commonly used?

Yes, but many developers prefer writing the query as a LEFT OUTER JOIN by reversing the table order because it often makes the query easier to read.

For example, these two queries are equivalent:

SELECT e.employee_name,
       d.department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id = d.department_id;
SELECT e.employee_name,
       d.department_name
FROM departments d
LEFT JOIN employees e
ON d.department_id = e.department_id;

RIGHT OUTER JOIN is useful for unmatched right-side data, but many developers prefer LEFT JOIN for readability and consistency.

Oracle LEFT OUTER JOIN FAQs with Examples

1. What is a LEFT OUTER JOIN in Oracle?

A LEFT OUTER JOIN returns:

  • All rows from the left table.
  • Matching rows from the right table.
  • NULL values for the right table columns when no matching row exists.

It is commonly used when you want to keep every row from the left table, regardless of whether a match exists in the right table.

Syntax

SELECT column_list
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t1.column_name = t2.column_name;

You can also omit the OUTER keyword:

SELECT column_list
FROM table1 t1
LEFT JOIN table2 t2
ON t1.column_name = t2.column_name;

2. Why do we use a LEFT OUTER JOIN?

A LEFT OUTER JOIN is used to:

  • Retrieve all records from the primary (left) table.
  • Find rows that have no matching records.
  • Generate reports that include unmatched data.
  • Identify missing relationships between tables.

3. How does a LEFT OUTER JOIN work?

Suppose we have:

EMPLOYEES

EMPLOYEE_ID   EMPLOYEE_NAME   DEPARTMENT_ID
101           John            10
102           David           20
103           Scott           40

DEPARTMENTS

DEPARTMENT_ID   DEPARTMENT_NAME
10              Sales
20              HR
30              Finance

Query

SELECT e.employee_name,
       d.department_name
FROM employees e
LEFT OUTER JOIN departments d
ON e.department_id = d.department_id;

Output

Employee   Department
John       Sales
David      HR
Scott      NULL

Scott is returned even though department 40 does not exist.

4. What is the syntax of a LEFT OUTER JOIN?

SELECT *
FROM employees e
LEFT OUTER JOIN departments d
ON e.department_id = d.department_id;

Or simply:

SELECT *
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id;

5. What is the difference between LEFT JOIN and LEFT OUTER JOIN?

There is no difference.

These statements are equivalent:

  • LEFT JOIN
  • LEFT OUTER JOIN

The keyword OUTER is optional.

6. What happens when there is no matching row?

Oracle returns:

  • All columns from the left table.
  • NULL values for columns from the right table.

Example output:

Employee   Department
Scott      NULL

7. Can a LEFT OUTER JOIN use aliases?

Yes.

SELECT e.employee_name,
       d.department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id;

Aliases improve readability.

8. Can a LEFT OUTER JOIN use a WHERE clause?

Yes.

Example:

SELECT e.employee_name,
       d.department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id
WHERE d.department_name = 'Sales';

This returns only matching rows where the department is Sales. Rows with NULL department values are filtered out.

If you want to keep unmatched rows while filtering the joined table, place the condition in the ON clause:

SELECT e.employee_name,
       d.department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id
AND d.department_name = 'Sales';

9. Can a LEFT OUTER JOIN use GROUP BY?

Yes.

SELECT d.department_name,
       COUNT(e.employee_id) AS employee_count
FROM departments d
LEFT JOIN employees e
ON d.department_id = e.department_id
GROUP BY d.department_name;

This shows departments even if they have no employees.

10. Can a LEFT OUTER JOIN use ORDER BY?

Yes.

SELECT e.employee_name,
       d.department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id
ORDER BY e.employee_name;

11. Can a LEFT OUTER JOIN use multiple conditions?

Yes.

SELECT *
FROM orders o
LEFT JOIN shipments s
ON o.order_id = s.order_id
AND o.customer_id = s.customer_id;

12. Can a LEFT OUTER JOIN use multiple tables?

Yes.

SELECT e.employee_name,
       d.department_name,
       l.city
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id
LEFT JOIN locations l
ON d.location_id = l.location_id;

13. Can a LEFT OUTER JOIN return duplicate rows?

Yes. If multiple matching rows exist in the right table, each matching combination is returned.

For example, if one department has multiple matching records in the joined table, an employee may appear multiple times.

14. How do you find unmatched rows using a LEFT OUTER JOIN?

Use IS NULL on a column from the right table.

SELECT e.employee_name
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id
WHERE d.department_id IS NULL;

This returns employees who are not assigned to a valid department.

15. What is the old Oracle (+) syntax for LEFT OUTER JOIN?

SELECT e.employee_name,
       d.department_name
FROM employees e,
     departments d
WHERE e.department_id = d.department_id(+);

Equivalent ANSI syntax:

SELECT e.employee_name,
       d.department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id;

ANSI joins are recommended for new development.

16. What are common uses of a LEFT OUTER JOIN?

  • Employees without departments.
  • Customers without orders.
  • Products without sales.
  • Departments without employees.
  • Students without course registrations.
  • Orders without shipments.

17. Real-Time Example – Customers Without Orders

CUSTOMERS

CUSTOMER_ID   CUSTOMER_NAME
1             Alice
2             Bob
3             Charlie

ORDERS

ORDER_ID   CUSTOMER_ID
100        1
101        2

Query

SELECT c.customer_name,
       o.order_id
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id;

Output

Customer   Order
Alice      100
Bob        101
Charlie    NULL

Charlie has not placed any orders.

18. What are the advantages of a LEFT OUTER JOIN?

  • Returns all rows from the left table.
  • Identifies missing relationships.
  • Useful for reporting.
  • Supports optional relationships.
  • Easy to understand.

19. What are the disadvantages of a LEFT OUTER JOIN?

  • Can return large result sets.
  • Duplicate matches may increase row counts.
  • Incorrect WHERE conditions can unintentionally turn it into an inner join.
  • May require indexing for good performance on large tables.

20. Common Errors with LEFT OUTER JOIN

Error Cause
ORA-00904 Invalid column name
ORA-00942 Table or view does not exist
ORA-00918 Column ambiguously defined
ORA-00933 SQL command not properly ended

21. LEFT OUTER JOIN vs INNER JOIN

LEFT OUTER JOIN INNER JOIN
Returns all rows from the left table Returns only matching rows
Includes unmatched rows with NULL values Excludes unmatched rows
Used for optional relationships Used for mandatory relationships

22. LEFT OUTER JOIN vs RIGHT OUTER JOIN

LEFT OUTER JOIN RIGHT OUTER JOIN
Returns all rows from the left table Returns all rows from the right table
Missing right-side matches appear as NULL Missing left-side matches appear as NULL

23. LEFT OUTER JOIN vs FULL OUTER JOIN

LEFT OUTER JOIN FULL OUTER JOIN
Returns all rows from the left table Returns all rows from both tables
Includes matching rows and unmatched left rows Includes matching rows plus unmatched rows from both tables

24. Interview Scenario

Question: List all customers, including those who have never placed an order.

Answer: Use a LEFT OUTER JOIN.

SELECT c.customer_name,
       o.order_id
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id;

LEFT OUTER JOIN is one of the most useful joins for reporting and identifying missing relationships in Oracle SQL.