SQL Inner Join Tutorial
Preface
I often think to myself, "If only this material was presented as xyz, I could grasp it better!" Now, with the power of AI, I can generate my own training aids for topics I need additional depth on. As a result, I developed this short tutorial to wrap my head around some SQL Joins concepts. If you learn like I do, give it a try yourself.
Introduction
The Inner Join command is used to combine data from two or more tables based on a related column between them. It returns only the rows that have matching values in both tables. The syntax for the Inner Join command is:
SELECT column1, column2, … FROM table1 INNER JOIN table2 ON table1.column = table2.column;
Where column1, column2, … are the columns you want to select from the tables, table1 and table2 are the tables you want to join, and column is the column that both tables have in common.
Example 1 - Joining Two Tables:
Suppose you have two tables: "employees" and "departments". The employees table contains information about the employees, while the departments table contains information about the departments in the company. To join these two tables, you can use the Inner Join command as follows:
Syntax:
SELECT employees.employee_name, departments.department_name FROM employees INNER JOIN departments ON employees.department_id = departments.department_id;
Output:
Example 2 - Joining Three or More Tables:
Suppose you have three tables: "customers", "orders", and "order_details". The customers table contains information about the customers, the orders table contains information about the orders, and the order_details table contains information about the details of each order. To join these three tables, you can use the Inner Join command as follows:
Syntax:
SELECT customers.customer_name, orders.order_date, order_details.quantity FROM customers INNER JOIN orders ON customers.customer_id = orders.customer_id INNER JOIN order_details ON orders.order_id = order_details.order_id;
Output:
Example 3 - Business Scenario:
Suppose you have two tables: "salespeople" and "sales". The salespeople table contains information about the salespeople, while the sales table contains information about their sales. You want to find out the total sales amount for each salesperson. To solve this business scenario, you can use the Inner Join command as follows:
Syntax:
SELECT salespeople.salesperson_name, SUM(sales.amount) AS total_sales FROM salespeople INNER JOIN sales ON salespeople.salesperson_id = sales.salesperson_id GROUP BY salespeople.salesperson_name;
Output:
These were three examples of how to use the Inner Join command to combine data from multiple tables. This tutorial was developed with the assistance of AI!
Passionner de la TIC | Informaticien de formation | Reconversion en developpement Web | Consultant Independant en Administration Système & Réseau
10moThis article is very intesting. Big up Matthew L. 🤗
Senior Automation Engineer for finance at @ BASF | Lean| SAP| Databricks| MS Power Platform
11mogreat job. help me to have a clear understanding =)