DataSunrise Achieves AWS DevOps Competency Status in AWS DevSecOps and Monitoring, Logging, Performance

Snowflake Cross Apply

Snowflake Cross Apply

Snowflake Cross Apply

Introduction

When using SQL for complex queries, you may need to combine data from different tables in more advanced ways. This goes beyond just using inner and outer joins. The CROSS APPLY operator in SQL Server lets you join a table with a table-valued function, creating versatile query options. However, if you’re using the Snowflake cloud data platform, you may have noticed that there is no Snowflake CROSS APPLY analog.

This article will explain how CROSS APPLY works and how to use it in Snowflake with LATERAL JOIN. We will also compare the differences between these two methods. We will also compare the differences between these two approaches.

By the end, you’ll have a solid understanding of how to perform complex joins in Snowflake that are analogous to SQL Server’s CROSS APPLY. Let’s dive in!

What is CROSS APPLY?

In SQL Server, CROSS APPLY is an operator that allows you to join a table with a table-valued function. It applies the function to each row of the left table and produces a result set by combining the rows from the left table with the corresponding rows returned by the function.

Here’s a simple example to illustrate how it works:

-- Using CROSS APPLY
SELECT *
FROM Person p
CROSS APPLY (
SELECT *
FROM Company c
WHERE p.companyid = c.companyId
) Czip;

In this example, the subquery is executed for each row in the Person table using the operator. It returns the matching rows from the Company table where companyId matches. The result is a join between Person and Company based on the ‘companyId’ relationship.

The equivalent query using a standard INNER JOIN syntax would be:

-- Equivalent query using INNER JOIN
SELECT *
FROM Person p
INNER JOIN Company c ON p.companyid = c.companyId;

Both queries will return the same result set, but the CROSS APPLY version allows for more complex join conditions and can be particularly useful when working with table-valued functions.

Snowflake Cross Apply

Snowflake does not have CROSS APPLY, but it has similar functionality with the LATERAL keyword when used with a join. In ANSI SQL standard, a lateral join allows you to use columns from previous tables in the join condition. This results in the same outcome as using CROSS APPLY.

Here’s an example of how you can use a lateral join in Snowflake to achieve the same result as the example above:

-- Using LATERAL JOIN in Snowflake
SELECT *
FROM Person p
LEFT JOIN LATERAL (
SELECT *
FROM Company c
WHERE p.companyid = c.companyId
) Czip ON TRUE;

In this example, the LATERAL keyword is used to indicate that the subquery following it can reference columns from the preceding Person table. The ON TRUE condition unconditionally joins the lateral subquery with the Person table.

The lateral join behaves similarly to CROSS APPLY, executing the subquery for each row of the left table and combining the results. The main difference is that CROSS APPLY performs an inner join, while the lateral join example above uses a left join. You can achieve an inner join behavior by simply changing LEFT JOIN LATERAL to INNER JOIN LATERAL.

Execution Plans

Let’s take a closer look at the execution plans for the CROSS APPLY and lateral join examples to understand how they differ.

For the CROSS APPLY example in SQL Server:

  1. The system scans the Person table to retrieve all rows.
  2. For each row in Person, SQL Server runs subquery following the CROSS APPLY. It filters the Company table based on the companyId condition.
  3. The query joins the resulting rows from the subquery with the corresponding row from Person.
  4. The function returns the final result set.

For the LATERAL JOIN example in Snowflake:

  1. The system scans the Person table to retrieve all rows.
  2. The lateral subquery executes for each row in Person, filtering the Company table based on the companyId condition.
  3. The resulting rows from the lateral subquery are left joined with the corresponding row from Person using the ON TRUE condition.
  4. The function returns the final result set.

The execution plans for both approaches are similar, with the main difference being the type of join used (inner join for CROSS APPLY and left join for the lateral join example).

Example with Preliminary Setup

Let’s look at a more comprehensive example that includes some preliminary setup steps. Suppose we have two tables: Orders and OrderItems. Each order can have multiple order items, and we want to retrieve the total amount for each order along with the order details.

First, let’s create the necessary tables:

-- Create Orders table
CREATE TABLE Orders (
OrderID INT,
CustomerID INT,
OrderDate DATE
);
-- Create OrderItems table
CREATE TABLE OrderItems (
OrderID INT,
ItemID INT,
Quantity INT,
Price DECIMAL(10, 2)
);
-- Insert sample data into Orders table
INSERT INTO Orders (OrderID, CustomerID, OrderDate)
VALUES
(1, 101, '2023-05-01'),
(2, 102, '2023-05-02'),
(3, 101, '2023-05-03');
-- Insert sample data into OrderItems table
INSERT INTO OrderItems (OrderID, ItemID, Quantity, Price)
VALUES
(1, 1, 2, 10.00),
(1, 2, 1, 15.00),
(2, 1, 3, 10.00),
(2, 3, 2, 20.00),
(3, 2, 1, 15.00);

Now, let’s use a lateral join to retrieve the order details along with the total amount for each order:

SELECT
o.OrderID,
o.CustomerID,
o.OrderDate,
oi.TotalAmount
FROM Orders o
LEFT JOIN LATERAL (
SELECT
OrderID,
SUM(Quantity * Price) AS TotalAmount
FROM OrderItems
WHERE OrderID = o.OrderID
GROUP BY OrderID
) oi ON TRUE;

In this example, for each row in the Orders table, the lateral subquery is executed to calculate the total amount of the order by summing the product of Quantity and Price for the corresponding order items. The result then joins with the Orders table using the ON TRUE condition.

The output of this query will be:

OrderID | CustomerID | OrderDate  | TotalAmount
--------+------------+------------+------------
1       | 101        | 2023-05-01 | 35.00
2       | 102        | 2023-05-02 | 70.00
3       | 101        | 2023-05-03 | 15.00

Performance Considerations

LATERAL JOIN performance varies based on data volume. Small datasets typically process quickly in Snowflake. Large tables may require query optimization techniques. Proper indexing improves LATERAL JOIN efficiency. Consider materialized views for frequently used subqueries. Snowflake’s query profiler helps identify bottlenecks. Warehouses should be sized appropriately for complex LATERAL operations. Data clustering enhances performance with large lateral subqueries. Avoid unnecessary columns in lateral subqueries. Apply filters early to reduce processing overhead. Monitor query history to track performance patterns. Testing with representative data volumes is essential. Cache results when possible for repeated operations.

Summary and Conclusion

In this article, we looked at how CROSS APPLY works in SQL Server and its equivalent in Snowflake using lateral joins. We found out that Snowflake doesn’t have CROSS APPLY, but lateral joins work similarly. They let subqueries use columns from previous tables in the join condition.

We looked at examples of how to use CROSS APPLY in SQL Server and how to achieve the same result using a lateral join in Snowflake. We talked about the plans for both methods and gave a detailed example with initial setup steps.

Knowing how to use lateral joins in Snowflake is crucial for writing efficient and optimized queries, especially when dealing with multiple tables and subqueries. By mastering lateral joins, you can ensure that your queries are more flexible and performant, leveraging the ability to reference previous tables within subqueries for complex data retrieval.

About DataSunrise

DataSunrise provides user-friendly and flexible tools for Snowflake database security, audit, and compliance. Our solutions help organizations protect sensitive data, monitor database activity, and ensure compliance with regulations such as GDPR, HIPAA, and PCI DSS.

If you’re interested in learning more about what DataSunrise has to offer, we invite you to request an online demo. Our team of experts will be happy to showcase our products and discuss how they can assist you in securing and managing your databases effectively.

Next

Data Classification Framework: What Is It & What Are The Benefits

Data Classification Framework: What Is It & What Are The Benefits

Learn More

Need Our Support Team Help?

Our experts will be glad to answer your questions.

Countryx
United States
United Kingdom
France
Germany
Australia
Afghanistan
Islands
Albania
Algeria
American Samoa
Andorra
Angola
Anguilla
Antarctica
Antigua and Barbuda
Argentina
Armenia
Aruba
Austria
Azerbaijan
Bahamas
Bahrain
Bangladesh
Barbados
Belarus
Belgium
Belize
Benin
Bermuda
Bhutan
Bolivia
Bosnia and Herzegovina
Botswana
Bouvet
Brazil
British Indian Ocean Territory
Brunei Darussalam
Bulgaria
Burkina Faso
Burundi
Cambodia
Cameroon
Canada
Cape Verde
Cayman Islands
Central African Republic
Chad
Chile
China
Christmas Island
Cocos (Keeling) Islands
Colombia
Comoros
Congo, Republic of the
Congo, The Democratic Republic of the
Cook Islands
Costa Rica
Cote D'Ivoire
Croatia
Cuba
Cyprus
Czech Republic
Denmark
Djibouti
Dominica
Dominican Republic
Ecuador
Egypt
El Salvador
Equatorial Guinea
Eritrea
Estonia
Ethiopia
Falkland Islands (Malvinas)
Faroe Islands
Fiji
Finland
French Guiana
French Polynesia
French Southern Territories
Gabon
Gambia
Georgia
Ghana
Gibraltar
Greece
Greenland
Grenada
Guadeloupe
Guam
Guatemala
Guernsey
Guinea
Guinea-Bissau
Guyana
Haiti
Heard Island and Mcdonald Islands
Holy See (Vatican City State)
Honduras
Hong Kong
Hungary
Iceland
India
Indonesia
Iran, Islamic Republic Of
Iraq
Ireland
Isle of Man
Israel
Italy
Jamaica
Japan
Jersey
Jordan
Kazakhstan
Kenya
Kiribati
Korea, Democratic People's Republic of
Korea, Republic of
Kuwait
Kyrgyzstan
Lao People's Democratic Republic
Latvia
Lebanon
Lesotho
Liberia
Libyan Arab Jamahiriya
Liechtenstein
Lithuania
Luxembourg
Macao
Madagascar
Malawi
Malaysia
Maldives
Mali
Malta
Marshall Islands
Martinique
Mauritania
Mauritius
Mayotte
Mexico
Micronesia, Federated States of
Moldova, Republic of
Monaco
Mongolia
Montserrat
Morocco
Mozambique
Myanmar
Namibia
Nauru
Nepal
Netherlands
Netherlands Antilles
New Caledonia
New Zealand
Nicaragua
Niger
Nigeria
Niue
Norfolk Island
North Macedonia, Republic of
Northern Mariana Islands
Norway
Oman
Pakistan
Palau
Palestinian Territory, Occupied
Panama
Papua New Guinea
Paraguay
Peru
Philippines
Pitcairn
Poland
Portugal
Puerto Rico
Qatar
Reunion
Romania
Russian Federation
Rwanda
Saint Helena
Saint Kitts and Nevis
Saint Lucia
Saint Pierre and Miquelon
Saint Vincent and the Grenadines
Samoa
San Marino
Sao Tome and Principe
Saudi Arabia
Senegal
Serbia and Montenegro
Seychelles
Sierra Leone
Singapore
Slovakia
Slovenia
Solomon Islands
Somalia
South Africa
South Georgia and the South Sandwich Islands
Spain
Sri Lanka
Sudan
Suriname
Svalbard and Jan Mayen
Swaziland
Sweden
Switzerland
Syrian Arab Republic
Taiwan, Province of China
Tajikistan
Tanzania, United Republic of
Thailand
Timor-Leste
Togo
Tokelau
Tonga
Trinidad and Tobago
Tunisia
Turkey
Turkmenistan
Turks and Caicos Islands
Tuvalu
Uganda
Ukraine
United Arab Emirates
United States Minor Outlying Islands
Uruguay
Uzbekistan
Vanuatu
Venezuela
Viet Nam
Virgin Islands, British
Virgin Islands, U.S.
Wallis and Futuna
Western Sahara
Yemen
Zambia
Zimbabwe
Choose a topicx
General Information
Sales
Customer Service and Technical Support
Partnership and Alliance Inquiries
General information:
info@datasunrise.com
Customer Service and Technical Support:
support.datasunrise.com
Partnership and Alliance Inquiries:
partner@datasunrise.com