![]() |
MySQL joins combined rows from two or more tables based on a related column. MySQL, a popular relational database management system, offers two main approaches to perform joins: explicit and implicit. In this article, we will explore these two methodologies, understanding their syntax, use cases, and the implications for code readability and performance, when to use, and the Difference between these two approaches. Explicit JoinsExplicit joins are explicitly specified in the SQL query using the JOIN keyword followed by the type of join (e.g., INNER JOIN, LEFT JOIN, RIGHT JOIN, etc.) and the condition for joining the tables. Features of Explicit Joins:
Syntax:SELECT columns Examples of Explicit MySQL Joins1. INNER JOIN
Syntax:
Example: Suppose we have two tables: Employees and Departments. The employees table contains information about employees, and the departments table contains details about different departments in a company. The common column between these tables is department_id. Creating the employees table: -- Creating the employees table Output: Similarly we will create Department table with two columns also we will insert data into Department Table. Creating the departments table: -- Creating the departments table Output:Till now we have created employees and Departments table. Now, let’s perform an explicit INNER JOIN to retrieve information about employees and their corresponding departments. Following is the query to this task. SELECT Output:
Explanation: In the above example The INNER JOIN clause is used to combine rows from both the employees and departments tables based on the specified condition (employees.department_id = departments.department_id). SELECT statement specifies the columns you want to retrieve in the result set (employee_id, employee_name, and department_name). The result set will include only the rows where there is a match in both tables based on the department_id. 2. Left JoinA LEFT JOIN retrieves all rows from the left table (table1) and the matching rows from the right table (table2). If there are no matching rows in the right table, the columns from the right table will be filled with NULL values. Syntax:
Example: To perform an explicit Left Join between the employees and departments tables based on the department_id column, Following is the SQL query: SELECT The above query retrieves the employee_id, employee_name, department_id, and department_name from both tables. If there is a match on the department_id column, it includes the corresponding values from the departments table. If there is no match, it still includes the row from the employees table, but the columns from the departments table will be filled with NULL values. The LEFT JOIN ensures that all rows from the employees table are included in the result set. Output:
3. Right JoinThe RIGHT JOIN returns all rows from the right table, even if there are no matching values in the left table. Syntax:
Example: To perform an explicit Right Join between the employees and departments tables based on the department_id column, Following is the SQL query: SELECT The above SQL query is using a RIGHT JOIN to retrieve specific columns from both the employees and departments tables where there is a match based on the common department_id column. Output: Explanation: The above result includes columns from both tables based on the RIGHT JOIN condition. The rows are determined by the departments table, so all departments are included in the result. For the matching rows, employee information is included. For example, department_id 1 (Engineering) has two employees (John Doe and Bob Johnson), and department_id 2 (Marketing) has two employees (Jane Smith and Alice Brown). If there is no match in the employees table for a particular department, NULL values are returned for the columns from the employees table (as seen in the last row for department_id 2). Implicit JoinsImplicit joins are joins that are specified implicitly in the SQL query using the WHERE clause. The WHERE clause is used to filter the rows in the table based on a condition. If the condition in the WHERE clause involves columns from two or more tables, then an implicit join is performed. Features of Implicit Joins:
Syntax:
Examples of Implicit Joins1. Implicit INNER JOINFirst we will create two tables students and courses tables. then we will perform implicit inner join on the tables. Creating the students table: CREATE TABLE students ( Output:
Creating the courses table: CREATE TABLE courses ( Output:
So till now we have created two tables students and courses tables. Now we will perform implicit inner join on students and courses table. Following is the query to perform the implicit join. SELECT students.student_id, students.student_name, students.course_id, courses.course_name In above query FROM clause lists both the students and courses tables and WHERE clause implicitly specifies the join condition by equating the course_id in the students table to the course_id in the courses table. Output:
Explanation: This result set combines information from both the students and courses tables, showing the student_id, student_name, course_id, and course_name for each student based on the matching course_id. The query essentially links the two tables on the common course_id column, providing a list of students along with the names of the courses they are enrolled in. Comparison of Explicit and Implicit Joins
ConclusionExplicit joins should be used when you want to have more control over the join operation. For example, you might use an explicit join if you want to specify a specific join type or if you want to use a complex join condition. Implicit joins should be used when you want to keep your SQL queries simple and easy to read. Implicit joins can also be used to improve performance in some cases. |
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 9 |