0
0
SQLquery~5 mins

INNER JOIN syntax in SQL

Choose your learning style9 modes available
Introduction
INNER JOIN helps you combine rows from two tables when they have matching values in a related column.
You want to see orders along with the customer details who placed them.
You need to list employees with their department names from two separate tables.
You want to find products and their suppliers where the supplier exists.
You want to combine student records with their enrolled courses where enrollment exists.
Syntax
SQL
SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
INNER JOIN returns only rows where the join condition matches in both tables.
Use the ON clause to specify how the tables are related.
Examples
This gets employee names with their department names by matching department IDs.
SQL
SELECT employees.name, departments.name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.id;
This lists order IDs with the names of customers who placed them.
SQL
SELECT orders.id, customers.name
FROM orders
INNER JOIN customers
ON orders.customer_id = customers.id;
This shows product names along with their supplier names where the supplier exists.
SQL
SELECT products.name, suppliers.name
FROM products
INNER JOIN suppliers
ON products.supplier_id = suppliers.id;
Sample Program
This example creates two tables: customers and orders. It inserts some data, then uses INNER JOIN to show orders with customer names only where the customer exists.
SQL
CREATE TABLE customers (id INT, name VARCHAR(20));
CREATE TABLE orders (id INT, customer_id INT, product VARCHAR(20));

INSERT INTO customers VALUES (1, 'Alice'), (2, 'Bob');
INSERT INTO orders VALUES (101, 1, 'Book'), (102, 3, 'Pen');

SELECT orders.id, customers.name, orders.product
FROM orders
INNER JOIN customers
ON orders.customer_id = customers.id;
OutputSuccess
Important Notes
If no matching rows exist in both tables, INNER JOIN returns no rows for those entries.
INNER JOIN is the most common join type used to combine related data.
Make sure the columns used in ON have compatible data types.
Summary
INNER JOIN combines rows from two tables based on matching column values.
It returns only rows where the join condition is true in both tables.
Use INNER JOIN to see related data from multiple tables in one result.