Hone logo
Hone
Problems

Linking Customer Orders: An INNER JOIN Challenge

You've been tasked with analyzing sales data by combining information from two separate tables: one containing customer details and another containing order information. The goal is to retrieve a consolidated view of orders, showing only those orders that are associated with existing customers. This is a fundamental operation in relational databases, crucial for understanding relationships between different data entities.

Problem Description

Your challenge is to write an SQL query that retrieves all columns from both the Customers table and the Orders table for all orders that have a matching customer in the Customers table. You need to use an INNER JOIN clause to achieve this.

Requirements:

  • Retrieve all columns from both tables.
  • Ensure that only orders with a corresponding customer in the Customers table are included in the result.
  • The join condition should be based on the common CustomerID column present in both tables.

Expected Behavior:

The query should return rows where a CustomerID from the Orders table exists in the Customers table. If an order has a CustomerID that does not exist in the Customers table, that order should not appear in the output. Similarly, if a customer exists but has no orders, they will not appear in the output.

Edge Cases:

  • Customers with no orders: These customers should not appear in the final result because they don't have any matching entries in the Orders table.
  • Orders with invalid CustomerIDs: Orders with CustomerID values that do not exist in the Customers table should not appear in the final result.
  • Empty tables: If either table is empty, the result of an INNER JOIN will be an empty set.

Examples

Example 1:

Input Tables:

Customers table:

CustomerIDCustomerNameEmail
1Alice Smithalice@example.com
2Bob Johnsonbob@example.com
3Charlie Browncharlie@test.com

Orders table:

OrderIDCustomerIDOrderDateTotalAmount
10112023-10-26150.00
10222023-10-27220.50
10312023-10-2775.25
10442023-10-2850.00

Output:

OrderIDCustomerIDOrderDateTotalAmountCustomerIDCustomerNameEmail
10112023-10-26150.001Alice Smithalice@example.com
10222023-10-27220.502Bob Johnsonbob@example.com
10312023-10-2775.251Alice Smithalice@example.com

Explanation: The INNER JOIN successfully links orders to their respective customers. Order 104 is excluded because its CustomerID (4) does not exist in the Customers table. Charlie Brown is not included because he has no orders.

Example 2:

Input Tables:

Customers table:

CustomerIDCustomerNameEmail
5David Leedavid@mail.net

Orders table:

OrderIDCustomerIDOrderDateTotalAmount
20152023-11-01300.00
20252023-11-02120.75

Output:

OrderIDCustomerIDOrderDateTotalAmountCustomerIDCustomerNameEmail
20152023-11-01300.005David Leedavid@mail.net
20252023-11-02120.755David Leedavid@mail.net

Explanation: Both orders have a valid CustomerID that exists in the Customers table, so both are included in the result.

Example 3: Edge Case - Empty Orders Table

Input Tables:

Customers table:

CustomerIDCustomerNameEmail
1Alice Smithalice@example.com

Orders table:

(empty)

Output:

(empty result set)

Explanation: An INNER JOIN requires matching records in both tables. Since there are no records in the Orders table, no matches can be found, resulting in an empty output.

Constraints

  • The Customers table will have at least 0 rows and at most 1,000,000 rows.
  • The Orders table will have at least 0 rows and at most 5,000,000 rows.
  • The CustomerID column in both tables is of an integer type.
  • The CustomerID column is the primary key in the Customers table and a foreign key in the Orders table (conceptually, for this problem).
  • The query should be efficient enough to run within a reasonable time frame for the given constraints.

Notes

  • Pseudocode for the SQL query might look like: SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column;
  • Pay close attention to the table names and column names provided in the examples.
  • Successfully completing this challenge demonstrates your understanding of how to combine data from related tables using INNER JOIN, a foundational skill in SQL.
Loading editor...
plaintext