What you will be able to do
- Choose between INNER, LEFT, RIGHT, FULL, SEMI, ANTI and CROSS joins by predicting which rows each one keeps
- Rewrite a RIGHT JOIN as an equivalent LEFT JOIN by swapping the table order
- Join two tables on several key columns with ON ... AND ... or USING (a, b)
- Stack result sets with UNION or UNION ALL and predict whether duplicates survive
- Recognise the column-count and column-type rules that make a set operation fail
Key concept
Join criteria and the preserved side — A join pairs rows from a left and a right table wherever the join criteria evaluate to true. The join type then decides what happens to rows that find no partner: drop them (inner), keep them from one side or both and pad the other side with NULL (outer), or use the match only as a filter (semi, anti).
1.Two ways to combine tables: side by side or stacked
SQL gives an analyst two different ways to combine tables. A join combines them side by side. Each output row is built from a row of one table plus a matching row of the other, so the result gains columns. A set operation such as UNION stacks them. The rows of the second query go underneath the rows of the first, so the result gains rows but keeps the same columns. Most mistakes here come from using one when the problem needs the other: an orders report that needs the customer's name needs a join, and a report that combines this year's orders with an archive table of the same shape needs a union.
Databricks SQL uses the ANSI join syntax that any SQL analyst already knows. A join reads as left_table [join_type] JOIN right_table ON condition. The table named first is the left table and the table after JOIN is the right table. That left/right distinction does all the work in the outer, semi and anti joins below, so read every join from left to right. If you leave out the join type entirely, JOIN means INNER JOIN.
All the examples on this page use the same two small views from the Databricks SQL reference. Six employees belong to departments 1 to 6, but only departments 1, 2 and 3 exist. Chloe (5), Evan (4) and Amy (6) therefore point at departments that aren't there. Those three rows are how you tell the join types apart.
-- Use employee and department tables to demonstrate different type of joins.
> CREATE TEMP VIEW employee(id, name, deptno) AS
VALUES(105, 'Chloe', 5),
(103, 'Paul' , 3),
(101, 'John' , 1),
(102, 'Lisa' , 2),
(104, 'Evan' , 4),
(106, 'Amy' , 6);
> CREATE TEMP VIEW department(deptno, deptname) AS
VALUES(3, 'Engineering'),
(2, 'Sales' ),
(1, 'Marketing' );Checkpoint 1 of 9· Check yourself
An analyst has two tables with the same columns: sales_2026 and sales_archive. They need one result that lists every sale from both tables, under the existing column set. Which approach fits?
The tables have the same shape and the goal is more rows, not more columns, so this calls for a set operation that stacks one result under the other. Every join, FULL OUTER included, puts rows side by side and adds columns.
“Returns the result of subquery1 plus the rows of subquery2.”Source: docs.databricks.com
Checkpoint 2 of 9· Check yourself
Using the page's employee and department views, what does this query return?
SELECT id, name, deptname FROM employee JOIN department ON employee.deptno = department.deptno;
If you leave out the join type, JOIN means INNER JOIN. An inner join keeps only the employees whose deptno matches a department (1, 2 or 3), so Chloe, Evan and Amy are dropped.
“[ INNER ] Returns the rows that have matching values in both table references. The default join-type.”Source: docs.databricks.com
2.Inner, left, right and full outer joins
An inner join keeps only the pairs that match, and that's all it keeps. Joining employee to department on deptno returns Paul, John and Lisa. Chloe, Evan and Amy disappear because no department row has their number. Use an inner join when an unmatched row is meaningless for the report, such as an order that points at a customer who no longer exists and should be dropped from a revenue-by-customer list. Inner is also the default join type.
A left outer join keeps every row of the left table whether or not it matches. Where a match exists, the right table's columns are filled in. Where none exists, those columns are NULL. LEFT JOIN and LEFT OUTER JOIN are the same thing, because OUTER is optional. The same employee query as a left join returns all six employees, and three of them have a NULL department name:
-- Use employee and department tables to demonstrate left join.
> SELECT id, name, employee.deptno, deptname
FROM employee
LEFT JOIN department ON employee.deptno = department.deptno;
105 Chloe 5 NULL
103 Paul 3 Engineering
101 John 1 Marketing
102 Lisa 2 Sales
104 Evan 4 NULL
106 Amy 6 NULLA right outer join is a left join seen from the other side. It keeps every row of the right table and pads the left side with NULL. That means A RIGHT JOIN B ON ... returns exactly the same rows as B LEFT JOIN A ON ... with the same condition. Only the order of the tables in the FROM clause changes. The join key, the matching logic and the set of rows stay the same. Many teams write only left joins and put the table they want to preserve first, because it's easier to read.
A full outer join keeps unmatched rows from both sides. Every employee appears and every department appears. Wherever a row has no partner, the other side's columns are NULL. In the sample data every department has an employee, so the full join here looks like the left join. If a department 7 with no staff existed, it would appear too, with NULL for id and name. A full join is the tool for reconciliation work, such as comparing two systems' customer lists and listing what each one is missing.
| Join type | Unmatched left rows | Unmatched right rows | Rows returned here |
|---|---|---|---|
| [ INNER ] JOIN | Dropped | Dropped | 3 |
| LEFT [ OUTER ] JOIN | Kept, right columns NULL | Dropped | 6 |
| RIGHT [ OUTER ] JOIN | Dropped | Kept, left columns NULL | 3 |
| FULL [ OUTER ] JOIN | Kept, right columns NULL | Kept, left columns NULL | 6 |
Checkpoint 3 of 9· Fill the gap
Run against the page's employee and department views, this query returned the six rows shown under it. Which keyword fills the blank?
> SELECT id, name, employee.deptno, deptname
FROM employee
? JOIN department ON employee.deptno = department.deptno;
105 Chloe 5 NULL
103 Paul 3 Engineering
101 John 1 Marketing
102 Lisa 2 Sales
104 Evan 4 NULL
106 Amy 6 NULLThe result keeps all six employees and puts NULL in deptname for Chloe, Evan and Amy. That means the left table (employee) is the preserved side. INNER would drop those three employees. RIGHT keeps only the department side, so it also returns just the three matched rows.
Source: docs.databricks.comCheckpoint 4 of 9· Exam question
An analyst needs a report of every orders row paired with the matching customers row, using orders.customer_id = customers.customer_id. Orders that reference a customer_id no longer present in customers must be dropped from the result entirely. Which query returns exactly that result set?
SELECT o.order_id, c.customer_name
FROM orders o
___ customers c ON o.customer_id = c.customer_id
Correct answer: A — INNER JOIN, because it returns only rows whose join key matches in both tables, dropping unmatched orders
- A. This is correct: an inner join only returns rows where the join predicate is satisfied on both sides, so any order whose customer_id has no matching row in customers is excluded, exactly as the scenario requires.
- B. A left join preserves every row from the left table (orders) even when there is no match, producing NULL customer_name values instead of dropping the unmatched orders, which does not meet the requirement.
- C. A full outer join keeps unmatched rows from both orders and customers with NULLs filling the missing side, so orphaned orders would still appear in the output rather than being dropped.
- D. A cross join produces the Cartesian product of both tables and ignores the join predicate entirely, generating far more rows than intended and pairing orders with unrelated customers.
Checkpoint 5 of 9· Exam question
A finance analyst writes SELECT o.order_id, c.customer_name FROM orders o RIGHT JOIN customers c ON o.customer_id = c.customer_id. How does the result set differ from writing the equivalent query as SELECT o.order_id, c.customer_name FROM customers c LEFT JOIN orders o ON o.customer_id = c.customer_id?
Correct answer: A — The two queries return the same rows, because a right join preserves every customer row, matching a left join from customers
- A. RIGHT JOIN keeps every row from the table on the right side of the join, so
orders RIGHT JOIN customerspreserves all customers; this is semantically identical tocustomers LEFT JOIN orders, which preserves all customers on the left, so the row sets match. - B. This describes an inner join's behavior, not a right join; a right join still preserves unmatched rows from the preserved side, so customers with zero orders remain in the output with NULL order_id values.
- C. Neither join type introduces duplicate rows on its own; duplicates only occur if the join key is non-unique on one side, which is unrelated to choosing RIGHT versus LEFT here.
- D. Column order in the SELECT list is controlled by the query author, not by which side is designated LEFT or RIGHT, and the join key itself is unchanged between the two equivalent formulations.
Sources2
3.Semi, anti and cross joins
Semi and anti joins use the right table only as a test and never add its columns. A left semi join returns each left row that has at least one match on the right. An employee with five matching rows still appears once, which a plain inner join would not guarantee. A left anti join returns each left row that has no match at all. In Databricks SQL SEMI JOIN and ANTI JOIN default to the left variant, and the output contains only the left table's columns:
-- Use employee and department tables to demonstrate semi join.
> SELECT *
FROM employee
SEMI JOIN department ON employee.deptno = department.deptno;
103 Paul 3
101 John 1
102 Lisa 2
-- Use employee and department tables to demonstrate anti join.
> SELECT *
FROM employee
ANTI JOIN department ON employee.deptno = department.deptno;
105 Chloe 5
104 Evan 4
106 Amy 6"Customers who have never placed an order" is the classic anti-join question: customers LEFT ANTI JOIN orders ON customer_id. The older pattern is a left join from customers to orders followed by WHERE orders.order_id IS NULL. It gives the same customers, but it carries the orders columns along as NULLs and says less clearly what it's for. An inner join followed by the same IS NULL filter can never work. An inner join has already discarded every customer without an order, so no row can be NULL on the orders side, and the result is empty.
A cross join ignores keys and pairs every left row with every right row, which is the Cartesian product. Six employees crossed with three departments gives 18 rows. That's sometimes what you want, for example building a grid of every store against every calendar day before you fill in sales. More often it's an accident. If you write a join and forget the ON clause, Databricks doesn't raise an error. It treats the join as a cross join. On two million-row tables that's a trillion-row query. Cross joins are expensive because the output grows as left rows × right rows, so remove them from queries that need low latency or frequent recomputation.
Without join criteria, the join behaves as a CROSS JOIN, so every order was paired with every customer. The fix is to add ON orders.customer_id = customers.customer_id.
Checkpoint 6 of 9· Fill the gap
An analyst needs the employees whose deptno has no matching row in department. The output should contain only the employee columns, with no department columns padded with NULL. Which keyword completes the query?
> SELECT *
FROM employee
? JOIN department ON employee.deptno = department.deptno;A (left) anti join returns each left row that has no match on the right, and only the left table's columns, so it returns Chloe, Evan and Amy. SEMI returns the matched employees instead. LEFT keeps all six employees and adds NULL department columns. INNER drops exactly the unmatched rows the analyst wants.
Source: docs.databricks.comCheckpoint 7 of 9· Exam question
A retail team wants to identify customers in the customers table who have never placed an order in the orders table, joining on customer_id. The output should only include columns from customers, with one row per customer that has zero matching orders. Which approach satisfies this requirement most directly?
Correct answer: A — Use a LEFT ANTI JOIN from customers to orders on customer_id, which returns only customer rows that have no matching row in orders
- A. A LEFT ANTI JOIN returns rows from the left table that have no matching row in the right table and never includes columns from the right side, so it directly returns customers with zero orders in one step.
- B. An inner join drops unmatched rows before any filter can run, so a customer with no orders is never present in the joined result and the IS NULL filter would never see it; the predicate is also contradictory with an inner join's matching semantics.
- C. This works correctly for a left join, but the question asks for the most direct approach, and a left join followed by a NULL filter still requires selecting and then discarding matched rows rather than producing exactly the anti-join result in one construct.
- D. A right join preserves all rows from orders, not from customers, so this would keep every order and would not isolate customers lacking any order at all.
4.Joining on one key or several
The ON clause is any boolean expression, so joining on several keys means combining equalities with AND. Say a sales table and a targets table both have one row per region per month. Joining only on region matches each sales row with every month's target for that region, which multiplies the rows and inflates any sum. The right condition is ON s.region = t.region AND s.month = t.month. More generally, when the key you join on is not unique on one side, each left row is repeated once for every match. When the row count after a join is suspiciously high, look for a missing key column first.
USING (col1, col2) is the shorthand for an equality join on columns that have the same name in both tables. It has two consequences. Every listed column must exist in both tables, or the query fails with UNRESOLVED_USING_COLUMN_FOR_JOIN. And SELECT * shows each join column only once instead of twice, which avoids ambiguous-column errors later. The reference spells out the equivalence:
SELECT * FROM first JOIN second USING (a, b)
is equivalent to
SELECT first.a, first.b,
first.* EXCEPT(a, b),
second.* EXCEPT(a, b)
FROM first JOIN second ON first.a = second.a AND first.b = second.bUse ON when the key columns have different names (o.cust_id = c.customer_id) or when the condition is more than plain equality. Use USING when the names match and you want a cleaner output. NATURAL JOIN goes further and matches on every column with the same name automatically. That's risky on wide tables, where columns like updated_at happen to share a name and silently become part of the key. Also note that a row with a NULL key never matches anything in an equality join, because NULL = NULL is not true. Those rows survive only on the preserved side of an outer join.
Checkpoint 8 of 9· Fill the gap
The reference shows a two-key USING join and the ON query it is equivalent to. Rows should match only when both key columns are equal. Which keyword fills the blank in the ON clause?
SELECT * FROM first JOIN second USING (a, b)
is equivalent to
SELECT first.a, first.b,
first.* EXCEPT(a, b),
second.* EXCEPT(a, b)
FROM first JOIN second ON first.a = second.a ? first.b = second.bTo join on several keys, you combine the equalities with AND, so a pair of rows matches only when a and b are both equal. OR would match rows where only one key is equal and multiply the rows. USING is a separate shorthand for the whole condition, so it can't join two comparisons inside an ON clause.
Source: docs.databricks.comSources2
5.Stacking results with UNION and UNION ALL
UNION appends the rows of a second query under the first. The one decision is what to do with duplicate rows. Plain UNION (the same as UNION DISTINCT) removes duplicates across the whole combined result, including duplicates that were already inside one input. UNION ALL keeps every row exactly as it came. The examples use two one-column views: number1 holds 3, 1, 2, 2, 3, 4 and number2 holds 5, 1, 1, 2.
> (SELECT c FROM number1) UNION (SELECT c FROM number2);
1
3
5
4
2> SELECT c FROM number1 UNION ALL (SELECT c FROM number2);
3
1
2
2
3
4
5
1
1
2Choose by what a duplicate means. When you stack two monthly extracts of transactions, two identical rows are probably two real purchases, so UNION ALL is correct. UNION would quietly delete one and understate revenue. UNION ALL is also cheaper, because it doesn't have to compare rows to remove duplicates. Use UNION when the goal really is a distinct list, such as every customer ID that appears in either the web or the store table.
Both queries must return the same number of columns. If they don't, Databricks raises NUM_COLUMNS_MISMATCH. Columns are matched by position, not by name. The first column of the second query lands under the first column of the first query, even if one is region and the other is country. The result's column names come from the first query. Each output column takes the least common type of the two inputs, so an INT stacked on a BIGINT becomes BIGINT. Types that can't be reconciled raise INCOMPATIBLE_COLUMN_TYPE. The same rules apply to the related operators INTERSECT (rows in both) and EXCEPT (rows in the first but not the second).
Checkpoint 9 of 9· Fill the gap
This query combines the number1 view (3, 1, 2, 2, 3, 4) with the number2 view (5, 1, 1, 2) and returned the ten rows shown under it. Which set operator fills the blank?
> SELECT c FROM number1 ? ALL (SELECT c FROM number2);
3
1
2
2
3
4
5
1
1
2All ten input rows come back with their duplicates, the six from number1 and then the four from number2 stacked under them. That is UNION ALL. INTERSECT ALL would keep only rows found in both inputs, and EXCEPT ALL would keep only number1 rows that number2 doesn't cancel out. Neither of those would return the 5 from number2 or all ten rows.
Source: docs.databricks.comSources4
Sources
(4)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.https://docs.databricks.com/aws/en/transform/joinOfficial docs
“Databricks supports standard SQL join syntax, including inner, outer, semi, anti, and cross joins.”
↩︎ Two ways to combine tables: side by side or stacked - 2.
“Returns the rows that have matching values in both table references. The default join-type.”
↩︎ Two ways to combine tables: side by side or stacked“or appends NULL if there is no match. It is also referred to as a left outer join.”
↩︎ Inner, left, right and full outer joins“Returns all values from both relations, appending NULL values on the side that does not have a match.”
↩︎ Inner, left, right and full outer joins“Returns values from the left side of the table reference that has a match with the right.”
↩︎ Semi, anti and cross joins“If you omit the join_criteria the semantic of any join_type becomes that of a CROSS JOIN.”
↩︎ Semi, anti and cross joins“Matches the rows by comparing equality for list of columns column_name which must exist in both relations.”
↩︎ Joining on one key or several“Specifies that the rows from the two relations will implicitly be matched on equality for all columns with matching names.”
↩︎ Joining on one key or several“Combines the rows from the preceding left_table reference with the right_table reference based on join criteria.”
↩︎ Key concept“Returns all values from the right table reference and the matched values from the left table reference”
↩︎ Exam trap 1“Returns the values from the left table reference that have no match with the right table reference.”
↩︎ Exam trap 2“If you omit the join_criteria the semantic of any join_type becomes that of a CROSS JOIN.”
↩︎ Exam trap 3“[ INNER ] Returns the rows that have matching values in both table references. The default join-type.”
↩︎ Checkpoint“Returns all values from the right table reference and the matched values from the left table reference”
↩︎ Prediction - 3.
“Cross joins are expensive. Remove cross joins from workloads and queries that require low latency or frequent recomputation.”
↩︎ Semi, anti and cross joins - 4.https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-setopsOfficial docs
“Both subqueries must have the same number of columns and share a least common type for each respective column.”
↩︎ Stacking results with UNION and UNION ALL“If ALL is specified duplicate rows are preserved. If DISTINCT is specified the result does not contain any duplicate rows. This is the default.”
↩︎ Stacking results with UNION and UNION ALL“If ALL is specified duplicate rows are preserved. If DISTINCT is specified the result does not contain any duplicate rows. This is the default.”
↩︎ Exam trap 4“Returns the result of subquery1 plus the rows of subquery2.”
↩︎ Checkpoint“If DISTINCT is specified the result does not contain any duplicate rows. This is the default.”
↩︎ Prediction