Join Pracyice
Join Pracyice
‘
LEFT JOIN
Write a query to retrieve a list of all employees, including their
department names. If an employee is not assigned to a department,
the department name should be NULL..
SELECT e.employee_id, e.employee_name, e.employee_code, e.employee_address,
e.employee_phno, d.dept_name
-> FROM table_employee e
-> LEFT JOIN table_department d
-> ON e.dept_id = d.dept_id;
+-------------+-----------------+---------------+------------------+---------------+------------+
| employee_id | employee_name | employee_code | employee_address |
employee_phno | dept_name |
+-------------+-----------------+---------------+------------------+---------------+------------+
| 1 | Sushan Kc | E01 | Balkot | 9808201519 | Database |
| 2 | Yenush khatri | E02 | Satdobato | 9845698712 | HR |
| 3 | Kritish basnet | E03 | imadole | 9765487612 | management
|
| 4 | Sundar Adhikari | E04 | Thimi | 9764581232 | financial |
| 5 | Aaryas karki | E05 | Dulikhel | 9845123698 | coding |
| 6 | Milan joshi | E06 | Pokhara | 9768457321 | management
|
| 7 | arjun shah | E07 | Baneshwor | 9654789600 | NULL |
| 8 | Rupesh lama | E08 | Balkumari | 9741842089 | HR |
| 9 | Saroj karki | E09 | Kusunti | 9860247589 | NULL |
| 10 | Subib bhattarai | E10 | Pipple bot | 976420045 |
management |
+-------------+-----------------+---------------+------------------+---------------+------------+
RIGHT JOIN
Write a query to retrieve a list of all departments,
including the employees in those departments. If a
department has no employees, the employee details
should be NULL.
SELECT d.dept_id, d.dept_code, d.dept_name, e.employee_id, e.employee_name,
e.employee_code, e.employee_address, e.employee_phno
-> FROM table_department d
-> RIGHT JOIN table_employee e
-> ON e.dept_id = d.dept_id;
+---------+-----------+------------+-------------+-----------------+---------------+------------------
+---------------+
| dept_id | dept_code | dept_name | employee_id | employee_name |
employee_code | employee_address | employee_phno |
+---------+-----------+------------+-------------+-----------------+---------------+------------------
+---------------+
| 3 | S03 | Database | 1 | Sushan Kc | E01 | Balkot |
9808201519 |
| 2 | S02 | HR | 2 | Yenush khatri | E02 | Satdobato |
9845698712 |
| 5 | S05 | management | 3 | Kritish basnet | E03 | imadole
| 9765487612 |
| 1 | S01 | financial | 4 | Sundar Adhikari | E04 | Thimi |
9764581232 |
| 4 | S04 | coding | 5 | Aaryas karki | E05 | Dulikhel |
9845123698 |
| 5 | S05 | management | 6 | Milan joshi | E06 | Pokhara
| 9768457321 |
| NULL | NULL | NULL | 7 | arjun shah | E07 | Baneshwor
| 9654789600 |
| 2 | S02 | HR | 8 | Rupesh lama | E08 | Balkumari |
9741842089 |
| NULL | NULL | NULL | 9 | Saroj karki | E09 | Kusunti |
9860247589 |
| 5 | S05 | management | 10 | Subib bhattarai | E10 | Pipple bot
| 976420045 |
+---------+-----------+------------+-----