Outer Apply SQL

Galaxy Glossary

What is an OUTER APPLY in SQL and how does it differ from an INNER JOIN?

OUTER APPLY in SQL is a powerful operator that allows you to perform a table-valued function against each row of an input table. It's particularly useful when you need to join a table with the result of a function or subquery that might not have a corresponding row in the other table.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Table of Contents

OUTER APPLY is a crucial part of SQL's arsenal for complex data manipulation. Unlike INNER JOIN, which only returns rows where a match exists in both tables, OUTER APPLY returns all rows from the left table, even if there's no matching row in the right table. This is where it differs significantly from INNER JOIN and LEFT JOIN. It's particularly useful when you need to apply a function or subquery to each row of a table and incorporate the results into the final output, even if the function or subquery doesn't produce a result for every row. Imagine you have a table of customer orders and a function that calculates the total discount for each order. Using OUTER APPLY, you can easily incorporate this discount calculation into your query, even for orders that didn't receive any discount. This is a significant advantage over INNER JOIN, which would exclude orders without a discount. OUTER APPLY is also beneficial when dealing with scenarios where the result set of the function or subquery might vary in size or structure for each row of the input table. This flexibility makes it a valuable tool for complex data analysis and manipulation.

Why Outer Apply SQL is important

OUTER APPLY is crucial for scenarios where you need to incorporate results from a function or subquery into your main query, even if the function or subquery doesn't produce a result for every row. It's a powerful tool for complex data analysis and manipulation, enabling more comprehensive and accurate results.

Outer Apply SQL Example Usage


-- Sample tables
CREATE TABLE Customers (
    CustomerID INT PRIMARY KEY,
    CustomerName VARCHAR(50)
);

CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    CustomerID INT,
    OrderDate DATE,
    FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

CREATE TABLE Discounts (
    DiscountID INT PRIMARY KEY,
    OrderID INT,
    DiscountAmount DECIMAL(10, 2),
    FOREIGN KEY (OrderID) REFERENCES Orders(OrderID)
);

-- Sample data (insert into Customers, Orders, Discounts)
INSERT INTO Customers (CustomerID, CustomerName) VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Charlie');
INSERT INTO Orders (OrderID, CustomerID, OrderDate) VALUES (101, 1, '2023-10-26'), (102, 2, '2023-10-27'), (103, 3, '2023-10-28');
INSERT INTO Discounts (DiscountID, OrderID, DiscountAmount) VALUES (1, 101, 5.00);

-- Using OUTER APPLY
SELECT
    c.CustomerName,
    o.OrderID,
    o.OrderDate,
    d.DiscountAmount
FROM
    Customers c
OUTER APPLY
    (SELECT OrderID, OrderDate FROM Orders WHERE c.CustomerID = Orders.CustomerID) o
OUTER APPLY
    (SELECT DiscountID, DiscountAmount FROM Discounts WHERE o.OrderID = Discounts.OrderID) d;

Outer Apply SQL Syntax



Common Mistakes

Frequently Asked Questions (FAQs)

When should I use OUTER APPLY instead of a LEFT JOIN?

OUTER APPLY shines when you need to run a correlated sub-query or table-valued function for every row of the left-hand table and include its output even when that function returns nothing. LEFT JOIN can only join existing rows from a static table expression, whereas OUTER APPLY can invoke logic that produces a different row set for each input row—such as a discount-calculation function for every order.

What happens if the function or subquery returns no rows for a given input row?

With OUTER APPLY the input row is preserved and the columns coming from the right side are simply returned as NULL. This behaviour is similar to a LEFT JOIN but keeps the flexibility of executing bespoke logic per row, ensuring that orders without discounts still appear in the result set.

How can Galaxy help me write and optimize OUTER APPLY queries faster?

Galaxy’s AI copilot understands correlated subqueries and table-valued functions. As you type, it autocompletes function names, suggests the correct OUTER APPLY syntax, and even refactors queries when the underlying schema changes. Combined with Galaxy Collections, you can share endorsed OUTER APPLY patterns across your team without pasting SQL into Slack or Notion.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.