CertSafari
    Databricks Certified Data Analyst Associate· Lessons

    Domain 4 · Lesson 14/39

    SQL joins and UNION in Databricks SQL

    Write queries to combine tables using various join operations (inner, left, right, and so on) with single or multiple keys, as well as set operations like union and union all, including the differences between the joins (inner, left, right, and so on).

    19 min read
    2.22% of exam
    4 sources
    Published 22 Oct 2026
    Docs as of 30 Sep 2026

    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.

    The employee and department views used throughout this page: three employees have no matching departmentsql
    -- 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?

    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;

    Sources12

    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:

    LEFT JOIN keeps all six employees; the three with no department get NULL for deptnamesql
    -- 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        NULL

    A 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.

    What each join keeps, on the employee/department data (6 employees, 3 departments, 3 matches)
    Join typeUnmatched left rowsUnmatched right rowsRows returned here
    [ INNER ] JOINDroppedDropped3
    LEFT [ OUTER ] JOINKept, right columns NULLDropped6
    RIGHT [ OUTER ] JOINDroppedKept, left columns NULL3
    FULL [ OUTER ] JOINKept, right columns NULLKept, left columns NULL6

    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        NULL

    Checkpoint 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

    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?

    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:

    SEMI JOIN keeps employees whose department exists; ANTI JOIN keeps the ones whose department does notsql
    -- 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.

    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;

    Checkpoint 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?

    Sources23

    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:

    A two-key USING join and the ON ... AND ... query it is equivalent tosql
    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.b

    Use 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.b

    Sources2

    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.

    UNION returns each distinct value oncesql
    > (SELECT c FROM number1) UNION (SELECT c FROM number2);
      1
      3
      5
      4
      2
    UNION ALL returns all ten input rows, duplicates includedsql
    > SELECT c FROM number1 UNION ALL (SELECT c FROM number2);
      3
      1
      2
      2
      3
      4
      5
      1
      1
      2

    Choose 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
      2

    Sources4

    Sources

    (4)

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 1.
      “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. 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. 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. 4.
      “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

    Spotted a mistake, or was something unclear? Tell us.