You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Join matching rows in both tables:SELECTorders.ID, customers.NameFROM orders
INNER JOIN customers
ONorders.ID=customers.ID;
SELECT*FROM employees e
INNER JOIN departments d
ONe.department_id=d.department_id
2. LEFT Join
Join all the rows from the left table and matching rows from the right table.
If there are no matches in the right table, Null values are returned for the right table columns.
SELECTo.ID, c.NameFROM Orders o
LEFT JOIN Customers c
ONo.ID=c.ID;
SELECT*FROM employees e
LEFT JOIN departments d
ONe.department_id=d.department_id
3. RIGHT Join
Join all the rows from the right table and matching rows from the left table.
If there are no matches in the left table, Null values are returned for the left table columns.
SELECTo.ID, c.NameFROM Orders o
RIGHT JOIN Customers c
ONo.ID=c.ID;
SELECT*FROM employees e
RIGHT JOIN departments d
ONe.department_id=d.department_id;
4. SELF Join
# Join the table with Itself:SELECT DISTINCTE.NameFROM Employees E
INNER JOIN Managers M
ONE.ID=M.ID;
SELECT*FROM employees e
INNER JOIN departments d
ONe.department_id=d.department_id;
SELECT*FROM employees e1, employee e2
WHEREe1.employee_id=e2.employee_id;
5. OUTER Join
Join all records from both tables, even if there is no matching row.
Null values are returned for the columns that do not have a match.
SELECT DISTINCTE.NameFROM Employees E
FULL OUTER JOIN Managers M
ONE.ID=M.ID;
SELECT*FROM employees e
LEFT JOIN departments d
ONe.employee_id=d.employee_idUNIONSELECT*FROM employees
RIGHT JOIN departments
ONe.employee_id=d.employee_id;
6. Joins ( More than 2 Tables )
SELECTE.EmployeeName, D.DepartmentName, E.Manager, S.SalaryFROM Employee E
INNER JOIN Salary S
ONE.E_ID=S.E_IDINNER JOIN Department D
OND.D_ID=S.D_ID;
7. Join using Child Parent Relationship
SELECTE.EmployeeName, D.DepartmentName, E.Manager, S.SalaryFROM Employee E, Salary S, Department D
WHEREE.E_ID=D.E_IDANDD.D_ID=S.D_ID;
Example
SELECT
PositionName, Channel, COUNT(ApplicantID) AS ApplicantCount
FROM Applicants AS App
INNER JOIN Positions AS Po ONApp.Position=Po.PositionIndex
INNER JOJN Channels AS Ch ONApp.ApplicantID=Ch.IDGROUP BY1, 2HAVINGCOUNT(ApplicantID) >4ORDER BY1, 3DESC;
SELECT
Manager, PositionName, Recruiter, ApplicantName
FROM Applicant AS App
INNER JOIN Positions AS Po ONApp.Position=Po.PositionIndex
FULL JOIN Channels AS Ch ONApp.ApplicantID=Ch.IDINNER JOIN Managers AS Mn ONPo.Department=mn.departmentORDER BY1, 2, 3