Skip to main content

Posts

Showing posts with the label SELF JOIN

Joining Tables in SQL

 Joining Tables Joining tables is a powerful feature in SQL that allows you to combine rows from two or more tables based on a related column. Here’s a detailed breakdown of the various types of joins, including INNER JOIN , LEFT JOIN (OUTER JOIN) , RIGHT JOIN , FULL JOIN , and SELF JOIN . 1. INNER JOIN The INNER JOIN returns only the rows where there is a match in both tables. It excludes rows without matches in either table. Syntax: SELECT column1, column2, ... FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name; Example: Consider two tables: employees and departments . employees : id name department_id 1 Alice 101 2 Bob 102 3 Carol 103 departments : department_id department_name 101 HR 102 IT To get a list of employees and their departments: SELECT employees.name, departments.department_name FROM employees INNER JOIN departments ON employees.department_id = departments.department_id; Result : name | department_name ------------------------ A...