Joins SQL
Joins SQL
Joins SQL
The SQL JOIN statement is used to combine rows from two tables based on a common
column and selects records that have matching values in these columns.
SQL JOIN Syntax
SELECT columns_from_both_tables
FROM table1
JOIN table2
ON table1.column1 = table2.column2
Here, table1 and table2 are the two tables that are to be joined
column1 is the column in table1 that is related to column2 in table2
Example1: join the Customers and Orders tables
-- based on the common values of their customer_id columns
Here, the SQL command joins the Customers and Orders tables based on the common
column, customer_id of both tables.
The result set will consist of: customer_id and first_name columns from the Customers table
item column from the Orders table
Example2: Join Two Tables Based on Common Column
-- join Customers and Orders tables based on
-- customer_id of Customers and customer column of Orders
This SQL command joins three tables and selects relevant columns from each, based on the
matching customer_id.
1. INNER JOIN
2. LEFT JOIN
3. RIGHT JOIN
4. FULL OUTER JOIN
SELECT columns_from_both_tables
FROM table1
INNER JOIN table2
ON table1.column1 = table2.column2
Here,table1 and table2 - two tables that are to be joined,column1 and column2 - columns
common to in table1 and table2
Example1:
-- join Customers and Orders tables with their matching fields customer_id
Here, the SQL command joins the Customers and Orders tables.
The result includes customer_id (from Customers) and item (from Orders) of rows where
customer IDs match (Customer.customer_id = Orders.customer_id).
Example 2:
-- join the Customers and Orders tables,with customer_id and customer fields
The result set has the cat_name column from Categories and the prod_title column from
Products.
SQL LEFT JOIN Syntax
SELECT columns_from_both_tables
FROM table1
LEFT JOIN table2
ON table1.column1 = table2.column2
Here, table1 is the left table to be joined, table2 is the right table to be joined
column1 and column2 are the common columns in the two tables
Example1:
-- left join the Customers and Orders tables
SELECT columns_from_both_tables
FROM table1
RIGHT JOIN table2
ON table1.column1 = table2.column2
Here, table1 is the left table to be joined, table2 is the right table to be joined
column1 and column2 are the related columns in the two tables.
Example1: SQL RIGHT JOIN
-- join Customers and Orders tables
-- based on customer_id of Customers and customer of Orders
-- Customers is the left table
-- Orders is the right table
And, the result set will contain those rows where there is a match between customer_id (of
the Customers table) and customer (of the Orders table), along with all the remaining rows
from the Orders table.
FULL OUTER JOIN SYNTAX
The syntax of the SQL FULL OUTER JOIN statement is:
SELECT columns
FROM table1
FULL OUTER JOIN table2
ON table1.column1 = table2.column2;
Here, table1 and table2 are the tables to be joined, column1 and column2 are the related
columns in the two tables
Example1: SQL OUTER Join
SELECT Customers.customer_id, Customers.first_name, Orders.amount
FROM Customers
FULL OUTER JOIN Orders
ON Customers.customer_id = Orders.customer;
Here, the SQL command selects the customer_id and first_name columns (from the
Customers table) and the amount column (from the Orders table).
The result set will contain all rows of both the tables, regardless of whether there is a match
between customer_id (of the Customers table) and customer (of the Orders table).
Create required tables(given sample tables here) and solve below Practice questions:
The dataset consists of four tables: author, book, adaptation, and book_review.
AUTHOR table:
BOOK table:
ADAPTATION table:
BOOK_REVIEW Table:
PRODUCT table:
NUTITION_DATA table:
PRODUCER Table:
SALES_HISTORY Table: