Fatskills
Practice. Master. Repeat.
Study Guide: Data Analytics: SQL Fundamentals Joins
Source: https://www.fatskills.com/data-science/chapter/data-analytics-sql-fundamentals-joins

Data Analytics: SQL Fundamentals Joins

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~9 min read

What Is This?

A join is a database operation that combines rows from two or more tables based on a related column between them. This topic appears in exams to test your understanding of data management, query optimization, and database design.

Why It Matters

This topic is crucial in database management and appears frequently in exams such as Oracle Certified Professional (OCP), Microsoft Certified Database Administrator (MCDA), and IBM Certified Database Administrator (ICDA). It typically carries 20-30% of the total marks and tests your ability to write efficient SQL queries.

Core Concepts

To master joins, you must understand the following foundational ideas:


  • Inner Join: Returns only the rows that have a match in both tables.
  • Left Join: Returns all the rows from the left table and the matched rows from the right table.
  • Right Join: Similar to left join but returns all the rows from the right table and the matched rows from the left table.
  • Full Join: Returns all the rows from both tables, with null values in the columns where there are no matches.
  • Cross Join: Returns the Cartesian product of both tables, with each row of one table combined with each row of the other table.

Prerequisites

Before tackling joins, you must understand the following concepts:


  • SQL basics: You should be familiar with basic SQL syntax, including SELECT, FROM, WHERE, and GROUP BY clauses.
  • Table relationships: You should understand how tables are related to each other through primary and foreign keys.
  • Data types: You should be familiar with different data types, including integers, strings, and dates.

The Rule-Book (How It Works)

The primary rule for joins is to specify the type of join and the tables involved. The syntax for joins is as follows:

SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column;

Sub-rules and exceptions:


  • Use the INNER JOIN clause to return only the rows that have a match in both tables.
  • Use the LEFT JOIN or RIGHT JOIN clause to return all the rows from one table and the matched rows from the other table.
  • Use the FULL JOIN clause to return all the rows from both tables, with null values in the columns where there are no matches.
  • Use the CROSS JOIN clause to return the Cartesian product of both tables.

Visual pattern: Imagine two tables, A and B, with a common column, C. An inner join would return only the rows where the values in column C match in both tables.

Exam / Job / Audit Weighting

Frequency: 30% Difficulty Rating: 7/10 Question Type or Real-World Task Type: Multiple-choice questions, short-answer questions, and case studies.

Difficulty Level

Intermediate

Must-Know Rules, Formulas, Standards, or Principles

The following rules and formulas are essential for joins:


  • Join syntax: SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column;
  • Join types: Inner join, left join, right join, full join, and cross join.
  • Join conditions: Use the ON clause to specify the join condition.

Worked Examples (Step-by-Step)

Here are three solved examples that escalate in difficulty:

Example 1: Easy

Question: Write a query to return all the orders from the orders table that have a match in the customers table.


SELECT * FROM orders INNER JOIN customers ON orders.customer_id = customers.customer_id;

Answer: The query returns all the orders from the orders table that have a match in the customers table.
Key rule applied: Inner join.

Example 2: Medium

Question: Write a query to return all the customers from the customers table and their corresponding orders from the orders table. If a customer has no orders, return null for the order columns.


SELECT * FROM customers LEFT JOIN orders ON customers.customer_id = orders.customer_id;

Answer: The query returns all the customers from the customers table and their corresponding orders from the orders table. If a customer has no orders, return null for the order columns.
Key rule applied: Left join.

Example 3: Hard

Question: Write a query to return all the products from the products table and their corresponding prices from the prices table. If a product has no price, return null for the price column.


SELECT * FROM products FULL JOIN prices ON products.product_id = prices.product_id;

Answer: The query returns all the products from the products table and their corresponding prices from the prices table. If a product has no price, return null for the price column.
Key rule applied: Full join.

Common Exam Traps & Mistakes

Here are four common mistakes that cost marks in exams:


  • Mistaking inner join for left join: This mistake occurs when you use the INNER JOIN clause instead of the LEFT JOIN clause.
  • Failing to specify the join condition: This mistake occurs when you forget to specify the join condition using the ON clause.
  • Using the wrong join type: This mistake occurs when you use the wrong join type, such as using a left join when you need a right join.
  • Failing to handle null values: This mistake occurs when you fail to handle null values in the columns where there are no matches.

Shortcut Strategies & Exam Hacks

Here are two practical techniques to solve questions faster or more accurately under time pressure:


  • Use the EXPLAIN statement: This statement helps you understand the execution plan of your query and identify potential performance issues.
  • Use the JOIN clause with the USING keyword: This keyword allows you to specify the join condition using the USING clause instead of the ON clause.

Question-Type Taxonomy

Here are four distinct question formats that this topic appears in across different exams:


Question Format Description Example Exam
Multiple-choice questions Choose the correct join type or syntax. What is the correct syntax for an inner join? OCP
Short-answer questions Write a query to perform a specific join operation. Write a query to return all the orders from the orders table that have a match in the customers table. MCDA
Case studies Analyze a scenario and write a query to solve the problem. A company wants to return all the customers from the customers table and their corresponding orders from the orders table. Write a query to solve this problem. ICDA
Essay questions Write a detailed explanation of a specific join operation or concept. Explain the difference between an inner join and a left join. OCP

Practice Set (MCQs)

Here are five multiple-choice questions at mixed difficulty levels:

Question 1: Easy

Question: What is the correct syntax for an inner join? A) SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column; B) SELECT * FROM table1 LEFT JOIN table2 ON table1.column = table2.column; C) SELECT * FROM table1 RIGHT JOIN table2 ON table1.column = table2.column; D) SELECT * FROM table1 FULL JOIN table2 ON table1.column = table2.column;

Correct answer: A Explanation: The correct syntax for an inner join is SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column; Why the distractors are tempting: The distractors are tempting because they use the LEFT JOIN or RIGHT JOIN clause instead of the INNER JOIN clause.

Question 2: Medium

Question: What is the difference between an inner join and a left join? A) An inner join returns all the rows from both tables, while a left join returns only the rows from the left table.
B) An inner join returns only the rows that have a match in both tables, while a left join returns all the rows from the left table and the matched rows from the right table.
C) An inner join returns only the rows that have a match in the left table, while a left join returns all the rows from the left table and the matched rows from the right table.
D) An inner join returns all the rows from both tables, while a left join returns only the rows from the right table.

Correct answer: B Explanation: An inner join returns only the rows that have a match in both tables, while a left join returns all the rows from the left table and the matched rows from the right table.
Why the distractors are tempting: The distractors are tempting because they use incorrect definitions of inner join and left join.

Question 3: Hard

Question: What is the correct syntax for a full join? A) SELECT * FROM table1 FULL JOIN table2 ON table1.column = table2.column; B) SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column; C) SELECT * FROM table1 LEFT JOIN table2 ON table1.column = table2.column; D) SELECT * FROM table1 RIGHT JOIN table2 ON table1.column = table2.column;

Correct answer: A Explanation: The correct syntax for a full join is SELECT * FROM table1 FULL JOIN table2 ON table1.column = table2.column; Why the distractors are tempting: The distractors are tempting because they use the INNER JOIN or LEFT JOIN clause instead of the FULL JOIN clause.

Question 4: Easy

Question: What is the purpose of the ON clause in a join? A) To specify the join condition.
B) To specify the table names.
C) To specify the column names.
D) To specify the join type.

Correct answer: A Explanation: The ON clause is used to specify the join condition.
Why the distractors are tempting: The distractors are tempting because they use incorrect definitions of the ON clause.

Question 5: Medium

Question: What is the difference between a left join and a right join? A) A left join returns all the rows from the left table and the matched rows from the right table, while a right join returns all the rows from the right table and the matched rows from the left table.
B) A left join returns only the rows that have a match in the left table, while a right join returns only the rows that have a match in the right table.
C) A left join returns all the rows from the left table and the matched rows from the right table, while a right join returns all the rows from the right table and the matched rows from the left table.
D) A left join returns only the rows from the left table, while a right join returns only the rows from the right table.

Correct answer: A Explanation: A left join returns all the rows from the left table and the matched rows from the right table, while a right join returns all the rows from the right table and the matched rows from the left table.
Why the distractors are tempting: The distractors are tempting because they use incorrect definitions of left join and right join.

30-Second Cheat Sheet

Here are the 7 things you must remember walking into the exam hall:


  • Inner join: Returns only the rows that have a match in both tables.
  • Left join: Returns all the rows from the left table and the matched rows from the right table.
  • Right join: Returns all the rows from the right table and the matched rows from the left table.
  • Full join: Returns all the rows from both tables, with null values in the columns where there are no matches.
  • Cross join: Returns the Cartesian product of both tables.
  • Join syntax: SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column;
  • Join conditions: Use the ON clause to specify the join condition.

Learning Path

Here is a suggested study sequence to master this topic from scratch to exam-ready:


  1. Beginner foundation: Learn the basic concepts of SQL, including SELECT, FROM, WHERE, and GROUP BY clauses.
  2. Core rules: Learn the rules and syntax for joins, including inner join, left join, right join, full join, and cross join.
  3. Practice: Practice writing queries using joins and analyzing case studies.
  4. Timed drills: Practice solving join-related questions under timed conditions.
  5. Mock tests: Take mock tests to assess your knowledge and identify areas for improvement.

Related Topics

Here are three closely connected topics that appear alongside this one in exams:


  • Subqueries: Learn how to write subqueries and use them in joins.
  • Set operations: Learn how to perform set operations, including UNION, INTERSECT, and EXCEPT.
  • Indexing: Learn how to create and use indexes to improve query performance.


ADVERTISEMENT