SQL joins are used to combine rows from two or more tables. MySQL JOINS are used to retrieve data from multiple tables.
The are two types of SQL JOINS - EQUI JOIN and NON EQUI JOIN
The SQL EQUI JOIN is a simple sql join uses the equal sign(=) as the comparison operator for the condition. It has two types - SQL Outer join and SQL Inner join.
The SQL NON EQUI JOIN is a join uses comparison operator other than the equal sign like >, <, >=, <= with the condition.
1. SQL INNER JOIN
This type of EQUI JOIN returns all rows from tables where the key record of one table is equal to the key records of another table.
2. SQL OUTER JOIN
This type of EQUI JOIN returns all rows from one table and only those rows from the secondary table where the joined condition is satisfying i.e. the columns are equal in both tables.
In order to perform a JOIN query, the required information we need are:
a) The name of the tables
b) Name of the columns of two or more tables, based on which a condition will perform.
SQL EQUI JOIN performs a JOIN against equality or matching column(s) values of the associated tables. An equal sign (=) is used as comparison operator in the where clause to refer equality.
You may also perform EQUI JOIN by using JOIN keyword followed by ON keyword and then specifying names of the columns along with their associated tables to check equality.
Syntax
SELECT column_list FROM table1, table2.... WHERE table1.column_name = table2.column_name;
An equijoin is a join with a join condition containing an equality operator. An equijoin returns only the rows that have equivalent values for the specified columns.
An inner join is a join of two or more tables that returns only those rows (compared using a comparison operator) that satisfy the join condition.
The SQL NON EQUI JOIN uses comparison operator instead of the equal sign like >, <, >=, <= along with conditions.
Syntax
SELECT * FROM table_name1, table_name2 WHERE table_name1.column [> | < | >= | <= ] table_name2.column;
To get 'ord_num' and 'ord_amount' columns from 'orders' table aliased as 'a' and 'cust_name' and 'working_area' columns from 'customer' table aliased as 'b' after joining said two tables with following condition - 1. 'ord_amount' of 'orders' table matches any of the 'opening_amt' of 'customer' table.
the following sql statement can be used : 2. SELECT a.ord_num,a.ord_amount,b.cust_name,b.working_area FROM orders a,customer b WHERE a.ord_amount BETWEEN b.opening_amt AND b.opening_amt;
The INNER JOIN selects all rows from both participating tables as long as there is a match between the columns. An SQL INNER JOIN is same as JOIN clause, combining rows from two or more tables.
Syntax
SELECT * FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name; OR
SELECT * FROM table1 JOIN table2 ON table1.column_name = table2.column_name;
To join 'item_name', 'item_unit' columns from 'foods' table and 'company_name', 'company_city' columns from 'company' table, with following condition -
1. 'company_id' of 'foods' and 'company' table must be same,
the following sql statement can be used :
SELECT foods.item_name, foods.item_unit, company.company_name, company.company_city FROM foods INNER JOIN company ON foods.company_id =company.company_id;
To get 'item_name', 'item_unit' columns from 'foods' table and 'company_name', 'company_city' columns from 'company' table, after joining these mentioned tables, with following condition -
1. 'company_id' of 'foods' and 'company_id' of 'company' table must be same,
the following sql statement can be used :
SELECT foods.item_name,foods.item_unit, company.company_name,company.company_city FROM foods JOIN company ON foods.company_id =company.company_id;
JOIN returns all rows from tables where the key record of one table is equal to the key records of another table. The INNER JOIN selects all rows from both participating tables as long as there is a match between the columns. An SQL INNER JOIN is same as JOIN clause, combining rows from two or more tables.
An inner join of A and B gives the result of A intersect B, i.e. the inner part of a venn diagram intersection. Inner joins use a comparison operator to match rows from two tables based on the values in common columns from each table. For example, retrieving all rows where the student identification number is the same in both the students and courses tables.
Using JOIN Clause
SELECT * FROM Table1 JOIN Table2 ON Table1.column_name=Table2.column_name;
SELECT * FROM Table1 INNER JOIN Table2 ON Table1.column_name= Table2.column_name;
An SQL INNER JOIN returns all rows from multiple tables where the join condition is met.
Returns records that have matching values in both tables
Left Join :-
Returns all records from the left table, and the matched records from the right table
Note: The LEFT JOIN keyword returns all the rows from the left table (Customers), even if there are no matches in the right table (Orders).
The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.
Note: The RIGHT JOIN keyword returns all the rows from the right table (Employees), even if there are no matches in the left table (Orders).
Returns all records from the right table, and the matched records from the left table
The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table (table2). The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.
Returns all records when there is a match in either left or right table
INNER JOIN :-
The inner JOIN is used to return rows from both tables that satisfy the given condition.
Table Order in Inner Joins :-
inner joins only return matching rows if the condition is true. it doesn’t matter which table you specify as the first table. Following both queries return same result.
Table 1 :- movies
table 2 :- members
Inner join example
output :-
LEFT JOIN
The LEFT JOIN returns all the rows from the table on the left even if no matching rows have been found in the table on the right. Where no matches have been found in the table on the right, NULL is returned.
Left joins returns all the rows from first table (left table) for specified relationship and fetch only the matching ones from second table (right table).
First MySQL looks at the table relationship and identifies `id` field of `employee` table as the left part and `employee_id` field of `user` table as the right part. It then selects all the rows from `employee` table that have values for `id` column. In the second step, based on e.id = u.employee_id relationship, it fetches matching rows from `user` table and creates final rows set (These rows contain values from both `employee` and `user` tables). If it can’t find a matching row, NULL is set to the cells of `user` table.
Left join example:
RIGHT JOIN
The RIGHT JOIN returns all the columns from the table on the right even if no matching rows have been found in the table on the left. Where no matches have been found in the table on the left, NULL is returned.
priority is given to right table and fetches all the rows from right table for given relationship.
Following query returns all the rows from `user` table and fills NULL for `employee` table’s cells if no matching row found.
Right join example:
MySql Join
Specifying Multiple Relationships in ON Clause
Some data tables can have composite primary keys. That is, more than one column forms the primary key. In such cases, you can specify multiple relationships in ON clause using AND operator as shown in following syntax.
Using ORDER BY and LIMIT
Joining Multiple Tables
Join with condition
Trending Tutorials