Solution- Inner Join in SQL
Master SQL Inner Joins with Real-World Examples
Finding names and emails of customers who purchased individual line items worth $700 or more requires joining multiple tables and strategic thinking about data relationships.
Database Tables in Our Example
Users Table
Contains customer information including names and email addresses. This is our source for customer identity data.
Orders Table
Acts as the bridge table connecting users to their purchases. Contains user_id and order_id relationships.
Line Items Table
Stores individual product purchases with price and quantity data. Essential for calculating purchase values.
If I want my data to go from users to line items, I need to go through orders. This is the bridge that I need to go through because there's no way to directly connect users to line items.
Building the Multi-Table Join
Start with Line Items
Begin with the line_items table since it contains the price and quantity data needed for our $700 filter condition.
Join to Orders via order_id
Connect line_items to orders using the common order_id field to establish which customer placed each order.
Join to Users via user_id
Connect orders to users using user_id to access customer names and email addresses.
When connecting tables, identify the common fields that create relationships. Order_id connects line_items to orders, and user_id connects orders to users.
Query Refinement Process
Reduces results from all line items to only those worth $700 or more
Eliminates unnecessary data to focus on the specific information requested
Ensures customers who made multiple high-value purchases appear only once
With vs Without DISTINCT
| Feature | Without DISTINCT | With DISTINCT |
|---|---|---|
| Reginald Pfeffer appearances | 2 rows | 1 row |
| Total results | 11 customers | 10 unique customers |
| Data accuracy | Shows all transactions | Shows unique customers |
You must specify the table name when a column exists in multiple tables (like order_id). For unique column names, table specification is optional but can improve code clarity.
Specifying Table Names for All Columns
This lesson is a preview from our SQL Course Online (includes software) and SQL Certification Online (includes software & exam). Enroll in a course for detailed lessons, live instructor support, and project-based training.
Key Takeaways