Explore outer joins, subqueries, and window functions with practical guidance on features, use cases, and implementation strategies.
Eoin McMillan
January 28, 2026 • 11 min read
Outer joins, subqueries, and window functions are three core SQL patterns analysts use to answer complex questions. Outer joins preserve unmatched rows across tables, subqueries nest one query inside another, and window functions calculate metrics like running totals by partition. Understanding these concepts helps spreadsheet users translate intricate analyses into efficient SQL. For a comprehensive foundation, refer to our guide on Data Analysis with SQL for Spreadsheet-Native Analysts: A 2026 Guide.
Sourcetable's AI data analyst is free to try. Sign up here.
Before mastering advanced patterns, it's crucial to understand SQL's building blocks. Think of a SQL query like setting up a spreadsheet: you first specify where your data comes from.
FROM Clause: This selects the table(s), similar to choosing a sheet or range in Excel. For example, FROM sales tells SQL to use the sales table.
JOIN Clause: To combine data from multiple tables, use JOIN. It's analogous to VLOOKUP or INDEX-MATCH in spreadsheets. An inner join returns only matching rows, while outer joins (covered next) include unmatched rows.
WHERE, GROUP BY, SELECT: These clauses filter, aggregate, and choose columns, mirroring spreadsheet filters, pivot tables, and column selections.
According to SQL education resources, mastering joins and subqueries covers many real-world analytics needs. Start with simple queries and gradually incorporate advanced patterns as you would with complex spreadsheet formulas.
An outer join in SQL extends inner joins by preserving rows from one or both tables that don't have matching rows in the other table. This is essential for analyses where you need to see all data, including gaps. For example, in a LEFT JOIN, all rows from the left table are included, with NULLs for unmatched right table rows.
Simple Example: Imagine a customers table and an orders table. A LEFT JOIN from customers to orders shows all customers, even those who haven't placed orders, with order details as NULL for unmatched rows. According to Vertica documentation, outer joins let you preserve rows of one or both tables that do not have matching rows, which is key for data integrity.
Data indicates that analysts frequently misuse joins, leading to duplicated or missing rows, so understanding outer joins prevents common errors.
SQL Outer Join Types Comparison
| Join Type | Preserved Table | Unmatched Rows |
|---|---|---|
| LEFT JOIN | Left table | Included with NULLs for right table |
| RIGHT JOIN | Right table | Included with NULLs for left table |
| FULL OUTER JOIN | Both tables | Included from both tables with NULLs where no match |
Outer joins are invaluable for specific analyst scenarios where completeness trumps matched data alone. Here are common use cases:
Identifying Missing Data: Use a LEFT JOIN to find customers without orders or products never sold. For instance, SELECT customers.name FROM customers LEFT JOIN orders ON customers.id = orders.customer_id WHERE orders.id IS NULL.
Merging Incomplete Datasets: When combining data from multiple sources with partial overlaps, a FULL OUTER JOIN ensures no row is lost. According to GoodData, outer joins enable advanced metrics and date arithmetic by handling sparse data.
Analyzing Optional Relationships: For analyses like employee-project assignments where not all employees have projects, outer joins reveal unassigned resources.
Research shows that window functions can simplify calculations that are awkward in spreadsheets, but outer joins address the fundamental issue of data alignment.
To visualize how outer joins work in practice, watch this concise tutorial from iNeuron Intelligence.
Subqueries, or inner queries, nest one SQL query inside another, similar to nested functions like IF(VLOOKUP(...)) in Excel. They allow for dynamic filtering, calculations, and data retrieval.
Types of Subqueries:
Scalar Subqueries: Return a single value, used in SELECT or WHERE clauses. Example: SELECT product_name, (SELECT AVG(price) FROM products) AS avg_price FROM sales.
Row Subqueries: Return multiple rows, often with IN or EXISTS. Example: SELECT * FROM customers WHERE id IN (SELECT customer_id FROM orders WHERE amount > 100).
Table Subqueries: Return a full table, used in FROM clauses. Example: SELECT * FROM (SELECT customer_id, SUM(amount) AS total FROM orders GROUP BY customer_id) AS summaries WHERE total > 500.
Step-by-Step Example: To find employees earning more than their department average:
Write a subquery to compute department averages: SELECT department_id, AVG(salary) FROM employees GROUP BY department_id.
Embed it in the main query: SELECT name, salary FROM employees e WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = e.department_id).
This mirrors spreadsheet logic but with SQL's power for large datasets.
Window functions perform calculations across a set of table rows related to the current row, without collapsing them into a single output row like GROUP BY. They are ideal for running totals, rankings, and moving averages.
Key Components:
OVER() Clause: Defines the window or partition for the function. Example: SUM(sales) OVER(PARTITION BY region) calculates total sales per region for each row.
PARTITION BY: Divides data into groups, similar to GROUP BY but without aggregation. It allows row-level calculations within partitions.
ORDER BY: Specifies order within the window, enabling cumulative sums or ranks. Example: SUM(sales) OVER(PARTITION BY region ORDER BY date) gives a running total.
Difference from GROUP BY: GROUP BY aggregates data into summary rows, reducing row count. Window functions compute values while retaining all original rows. For instance, to rank employees within departments, use RANK() OVER(PARTITION BY department ORDER BY salary DESC). 2026 training guides emphasize these advanced patterns as key for intermediate SQL proficiency, especially for analysts transitioning from spreadsheets.
AI-powered tools like Sourcetable revolutionize SQL for spreadsheet-native analysts by assisting with query generation, optimization, and debugging. These tools translate natural language prompts into SQL, similar to how you'd describe an analysis in Excel.
Benefits of AI Assistance:
Query Generation: Describe your goal (e.g., "find monthly sales trends"), and AI suggests SQL with outer joins or window functions.
Debugging: AI identifies errors like missing joins or incorrect subquery logic, reducing trial-and-error.
Optimization: Tools recommend efficient patterns, such as replacing correlated subqueries with joins for better performance.
Sourcetable integrates AI directly into a spreadsheet interface, allowing you to blend SQL with familiar formulas. According to industry insights, AI can boost analyst productivity by 10x by automating complex SQL tasks. This aligns with the trend where research shows that window functions and other advanced patterns are simplified with AI guidance.
An outer join in SQL preserves unmatched rows from one or both tables, unlike inner joins that only return matches. Use it when you need to include all records from a primary table (e.g., all customers) even if related data (e.g., orders) is missing, or to merge datasets with gaps. Common scenarios include finding missing data or combining incomplete sources.
SQL subqueries nest one query inside another, similar to nested Excel formulas like IF(VLOOKUP(...)). They allow dynamic data retrieval and filtering within a main query. For example, a subquery can calculate an average used in a WHERE clause, mirroring how you might use a named range or helper column in spreadsheets, but with SQL's scalability for large datasets.
Practical subquery examples include finding employees above department average salary, listing products never sold, or identifying customers with specific order patterns. For instance, SELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = 1) filters based on a dynamic average, akin to using an Excel array formula.
A SQL window function calculates values across a set of rows related to the current row without grouping, retaining all original rows. Unlike GROUP BY, which aggregates data into summary rows, window functions enable operations like running totals or rankings within partitions. For example, SUM(sales) OVER(PARTITION BY region) adds a column with regional totals per row, similar to a spreadsheet column with cumulative formulas.
AI tools assist by generating SQL from natural language prompts, debugging syntax errors, and optimizing queries for performance. For example, in Sourcetable, you can describe an analysis like 'compare monthly sales with a running average,' and AI produces SQL with window functions. This reduces learning curves and errors, making advanced patterns accessible to spreadsheet users.
Outer joins preserve unmatched rows, crucial for complete data analysis in SQL.
Subqueries enable nested logic, similar to Excel formulas, for dynamic filtering and calculations.
Window functions compute metrics like running totals without collapsing rows, unlike GROUP BY.
AI tools like Sourcetable can generate and debug complex SQL, boosting analyst productivity.
Mastering these patterns is key for intermediate SQL proficiency, as emphasized in 2026 guides.