Hello guys, if you are wondering how to join multiple tables in SQL to produce a combine result which contains columns from all tables but not sure how to do it then you have come to the right place. SQL Join is one of the basic concepts while working in databases or tables but yet less understood and most confusing topic for beginners and even intermediate developers. I compare Joins in SQL with Recursion in programming in terms of confusion because I have found that these two topics are special in their nature and you can't get it right with casual reading until you understand the concept and its various well. Things get worse when the table locked due to such SQL Join queries which were fired without knowing how much time it would and how big the result set could be.
I have read a lot of good articles explaining SQL joins on multiple tables with examples of joins but yet most of them don't focus on the importance of data, which I see as most important for understanding joins.
1. Why we need to Join Tables in SQL?
2. Types of SQL Joins
There are three main categories of Joins in SQL called Inner Join, Outer Join, and Cross Join. Outer Join is further divided into three namely Left Outer Join, Right Outer Join, and Full Outer Join.Inner Join: Only Matching data from two tables included in the result set.
Left Outer Join: All data from left tables and matching data from the right table. null will be used to populate rows that don't have a match on the right side.
Right Outer Join: All data from the Right table and matching data from the left table. again null will be used to fill cells that don't have matching data on the left side.
Full Outer Join: All data from Both Tables
Cross Join: This is multiplication which results in 8x8=64 rows if both tables have 8 columns and are very careful before running this even accidentally on a large table, which could potentially lock the table for a long time.
3. How SQL Joins Works?
To predict the result of SQL Join queries, you need to understand how Join works. My understanding is simple which might not be the way exact way the database will perform join. In SQL Join we have a common column between two tables and data of this column is used in the comparison.4. Implicit Join and Explicit Join
Explicit Join is joined with ON clause:
select a.*, b.* from table a inner join table b on a.id = b.id;
Implicit Join is Join without ON Clause, instead WHERE Clause is used as a condition.
select a.*, b.* from table a, table b where a.id = b.id;
Though their syntax slightly different performance-wise they are the same. I recommend using the "ON" clause or Explicit Join because once you see this "ON" clause you immediately know it's Join and we are joining two tables.
5. Equi Join Example in MySQL
Equi Join is not a different type of join but a term used to refer to queries that involve two instances of the same table for Join in SQL. One of the best examples of Equi Join which I remember is the following. we have the following table Employee which holds its Manager ID.SQL Join Examples for Beginners
mysql> select from Employee;
+--------+----------+---------+
| emp_id | emp_name | dept_id |
+--------+----------+---------+
| 1 | Ashwin | 101 |
| 2 | James | NULL |
| 3 | Kathy | NULL |
| 4 | Harry | 102 |
+--------+----------+---------+
4 rows in set (0.00 sec)
mysql> select from department;
+---------+-----------+
| dept_id | dept_name |
+---------+-----------+
| 101 | Sales |
| 102 | Marketing |
| 103 | NULL|
| 104 | NULL|
+---------+-----------+
4 rows in set (0.00 sec)
1. INNER JOIN Example in MySQL
mysql> select e.emp_name, d.dept_name
from Employee e
INNER JOIN Department d ON e.dept_id=d.dept_id
-> ;
+----------+-----------+
| emp_name | dept_name |
+----------+-----------+
| Ashwin | Sales |
| Harry | Marketing |
+----------+-----------+
2 rows in set (0.00 sec)
2. LEFT OUTER JOIN Example in MySQL
mysql> select e.emp_name, d.dept_name
from Employee e
LEFT OUTER JOIN Department d ON e.dept_id=d.dept_id;
+----------+-----------+
| emp_name | dept_name |
+----------+-----------+
| Ashwin | Sales |
| James | NULL |
| Kathy | NULL |
| Harry | Marketing |
+----------+-----------+
4 rows in set (0.00 sec)
3. RIGHT OUTER JOIN Example in MySQL
mysql> select e.emp_name, d.dept_name
from Employee e
RIGHT OUTER JOIN Department d ON e.dept_id=d.dept_id;
+----------+-----------+
| emp_name | dept_name |
+----------+-----------+
| Ashwin | Sales |
| Harry | Marketing |
| NULL | NULL |
| NULL | NULL |
+----------+-----------+
4 rows in set (0.00 sec)
4. CROSS JOIN Example in MySQL
mysql> select e.emp_name, d.dept_name from Employee e, Department d;
+----------+-----------+
| emp_name | dept_name |
+----------+-----------+
| Ashwin | Sales |
| James | Sales |
| Kathy | Sales |
| Harry | Sales |
| Ashwin | Marketing |
| James | Marketing |
| Kathy | Marketing |
| Harry | Marketing |
| Ashwin | NULL |
| James | NULL |
| Kathy | NULL |
| Harry | NULL |
| Ashwin | NULL |
| James | NULL |
| Kathy | NULL |
| Harry | NULL |
+----------+-----------+
16 rows in set (0.00 sec)
Important points about SQL join worth remembering:
Here are some important points about the SQL Join concept which every programmer should remember:1. Apart from = (equals to) in join which is called equijoin, you can also use other conditional operators like >=, <=, <>, <, >, etc.
2. In T-SQL and Oracle SQL and even in MySQL if you just specify "join" it will be interpreted as "inner join" but it's not guaranteed to all databases.
3. You can join on columns that are not included in the result set.
4. If both tables have a column with the same name then you must specify that column with a table name to avoid ambiguity.
5.You can join more than two tables in SQL as shown in this example.
- How to join three tables in one single SQL query (solution)
- Write a SQL query to find all table names on a database in MySQL (solution)
- 5 Courses to learn Database and SQL Better (courses)
- The real difference between WHERE and HAVING clause in SQL? (answer)
- 5 Courses to learn Oracle and Microsoft SQL Server database (courses)
- Difference between Self and Equi Join in SQL? (answer)
- 4 Free Books to learn Microsoft SQL Server database (books)
- Top 5 Websites to learn SQL online for FREE? (websites)
- 5 Free Courses to learn Database and SQL (free courses)
- What is the difference between View and Materialized View in Database? (answer)
- Difference between clustered and non-clustered indexes in SQL? (answer)
- Difference between Primary and Candidate key in the table? (answer)
- 5 Free Courses to learn T-SQL and SQL Server for Beginners (Courses)
- Difference between the Unique and Primary keys in the table? (answer)
- 5 Free Courses to learn Oracle and SQL Server? (courses)
- Top 5 Courses to learn Microsoft SQL Server in-depth (courses)
- How to migrate SQL queries from Oracle to SQL Server? (answer)
- Top 5 Websites to learn SQL online for FREE? (resource)
- What is the difference between UNION and UNION ALL in SQL? (answer)
- Write a SQL query to copy or backup a table in MySQL (solution)
- How do you find the duplicate rows in a table on a database? (solution)
- 5 Advanced SQL books to level up your SQL skills (books)
- Top 5 Courses to learn MySQL Database for Beginners (Courses)
- Top 5 Courses to learn PostgreSQL in-depth (courses)
- 5 Free Courses to learn T-SQL and SQL Server for Beginners (Courses)
- Difference between Primary and Foreign keys in the table? (answer)
Thanks for reading this article, if you like this SQL Join Tutorial, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.
No comments:
Post a Comment