<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/sql-cross-apply-when-sql-thinks-like-a-recommendation-engine-bjfnqimc2" -->

---
title: SQL Cross Apply — When SQL Thinks Like a Recommendation...
description: SQL's CROSS APPLY operator is explained through the analogy of an e-commerce recommendation engine. Unlike a standard JOIN, CROSS APPLY runs a subquery for...
canonical: https://daily.dev/posts/sql-cross-apply-when-sql-thinks-like-a-recommendation-engine-bjfnqimc2
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: SQL Cross Apply — When SQL Thinks Like a Recommendation Engine | daily.dev
og:description: SQL's CROSS APPLY operator is explained through the analogy of an e-commerce recommendation engine. Unlike a standard JOIN, CROSS APPLY runs a subquery for...
og:url: https://daily.dev/posts/sql-cross-apply-when-sql-thinks-like-a-recommendation-engine-bjfnqimc2
og:image: https://api.daily.dev/og/posts/BjfnqimC2.png
og:image:alt: SQL Cross Apply — When SQL Thinks Like a Recommendation Engine
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# SQL Cross Apply — When SQL Thinks Like a Recommendation Engine

**[Code Like A Girl](https://daily.dev/sources/colkgirl)** · 5 min read · 1 upvotes · 0 comments

## Summary

SQL's CROSS APPLY operator is explained through the analogy of an e-commerce recommendation engine. Unlike a standard JOIN, CROSS APPLY runs a subquery for each individual row — in this case, fetching the top 3 personalized product recommendations per customer ordered by score. A side-by-side comparison with a CTE + ROW_NUMBER() JOIN approach shows that CROSS APPLY produces cleaner, more readable code for per-row logic. Use cases include top-N results per group, dynamic per-row filtering, and calling table-valued functions.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://code.likeagirl.io/sql-cross-apply-when-sql-thinks-like-a-recommendation-engine-30961470f630>

## Questions this post answers

### How do I get the top 3 recommended products for each customer in SQL using CROSS APPLY?

Use a CROSS APPLY with a correlated subquery that filters by the outer row's CustomerID and selects the top 3 rows ordered by score. For example: SELECT c.CustomerName, r.ProductName FROM Customers c CROSS APPLY (SELECT TOP 3 ProductName FROM Recommendations r WHERE r.CustomerID = c.CustomerID ORDER BY Score DESC) r. This runs the subquery once per customer row, returning only that customer's top 3 products instead of an overall top 3.

_Developers refining per-row SQL queries can compare approaches like this one on daily.dev._

### Why use CROSS APPLY instead of a JOIN for top-N-per-group queries in SQL?

CROSS APPLY lets you run a correlated subquery per row directly, making top-N-per-group logic simpler than a JOIN. Achieving the same top-3-per-customer result with a JOIN requires a common table expression using ROW_NUMBER() with PARTITION BY CustomerID ordered by score, then filtering where the row number is 3 or less - noticeably more verbose than the equivalent CROSS APPLY query.

_When weighing SQL query patterns like CROSS APPLY versus JOIN, daily.dev helps developers compare trade-offs._

## Similar posts on daily.dev

- [10 Rare SQL Features Every Developer Should Know](https://daily.dev/posts/10-rare-sql-features-every-developer-should-know-fvu4qwczx) · Medium · 1 upvotes · 0 comments
- [Table-Valued Functions in SQL: Turning Requests Into Data Tables](https://daily.dev/posts/table-valued-functions-in-sql-turning-requests-into-data-tables-azwqbr2a4) · Code Like A Girl · 1 upvotes · 0 comments
- [Mastering Complex SQL Server Queries: Turning Data Into Insights](https://daily.dev/posts/mastering-complex-sql-server-queries-turning-data-into-insights-ptszhlorr) · C\# Corner · 3 upvotes · 0 comments
- [10 Simple Steps to Solve SQL Problems \[2026\]](https://daily.dev/posts/10-simple-steps-to-solve-sql-problems-2026--uicxpyy02) · Planet MySQL · 0 upvotes · 0 comments

---

Tags: [#backend](https://daily.dev/tags/backend), [#sql](https://daily.dev/tags/sql), [#recommendation-systems](https://daily.dev/tags/recommendation-systems)

[View this post on daily.dev](https://daily.dev/posts/sql-cross-apply-when-sql-thinks-like-a-recommendation-engine-bjfnqimc2)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"SQL Cross Apply — When SQL Thinks Like a Recommendation Engine","url":"https://daily.dev/posts/sql-cross-apply-when-sql-thinks-like-a-recommendation-engine-bjfnqimc2","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/sql-cross-apply-when-sql-thinks-like-a-recommendation-engine-bjfnqimc2"},"datePublished":"2026-07-09T18:21:25.281Z","dateModified":"2026-09-14T07:13:39.436Z","description":"SQL's CROSS APPLY operator is explained through the analogy of an e-commerce recommendation engine. Unlike a standard JOIN, CROSS APPLY runs a subquery for...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/c20845d914dcbce5d5cf32ebb1f1c50d?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/c20845d914dcbce5d5cf32ebb1f1c50d?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Code Like A Girl","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Code Like A Girl","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/82e21f28c59c499f9c47833c55500fd8","url":"https://daily.dev/sources/colkgirl"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/sql-cross-apply-when-sql-thinks-like-a-recommendation-engine-bjfnqimc2","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"backend,sql,recommendation-systems","timeRequired":"PT5M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Code Like A Girl","item":"https://daily.dev/sources/colkgirl"},{"@type":"ListItem","position":3,"name":"SQL Cross Apply — When SQL Thinks Like a Recommendation Engine"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/sql-cross-apply-when-sql-thinks-like-a-recommendation-engine-bjfnqimc2#faq","mainEntity":[{"@type":"Question","name":"How do I get the top 3 recommended products for each customer in SQL using CROSS APPLY?","acceptedAnswer":{"@type":"Answer","text":"Use a CROSS APPLY with a correlated subquery that filters by the outer row's CustomerID and selects the top 3 rows ordered by score. For example: SELECT c.CustomerName, r.ProductName FROM Customers c CROSS APPLY (SELECT TOP 3 ProductName FROM Recommendations r WHERE r.CustomerID = c.CustomerID ORDER BY Score DESC) r. This runs the subquery once per customer row, returning only that customer's top 3 products instead of an overall top 3. Developers refining per-row SQL queries can compare approaches like this one on daily.dev."}},{"@type":"Question","name":"Why use CROSS APPLY instead of a JOIN for top-N-per-group queries in SQL?","acceptedAnswer":{"@type":"Answer","text":"CROSS APPLY lets you run a correlated subquery per row directly, making top-N-per-group logic simpler than a JOIN. Achieving the same top-3-per-customer result with a JOIN requires a common table expression using ROW_NUMBER() with PARTITION BY CustomerID ordered by score, then filtering where the row number is 3 or less - noticeably more verbose than the equivalent CROSS APPLY query. When weighing SQL query patterns like CROSS APPLY versus JOIN, daily.dev helps developers compare trade-offs."}}]}
```

