0% found this document useful (0 votes)
11 views5 pages

Join Pracyice

The document outlines the creation of two tables, 'table_department' and 'table_employee', with their respective fields and relationships. It includes SQL commands for inserting data into these tables and demonstrates various SQL join operations (INNER JOIN, LEFT JOIN, RIGHT JOIN) to retrieve employee and department information. The results of these queries show how employees are associated with departments and how to handle cases where employees or departments may not have corresponding entries.

Uploaded by

sushankc19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Join Pracyice

The document outlines the creation of two tables, 'table_department' and 'table_employee', with their respective fields and relationships. It includes SQL commands for inserting data into these tables and demonstrates various SQL join operations (INNER JOIN, LEFT JOIN, RIGHT JOIN) to retrieve employee and department information. The results of these queries show how employees are associated with departments and how to handle cases where employees or departments may not have corresponding entries.

Uploaded by

sushankc19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Creating table of employee and department

create table table_department(


-> dept_id int auto_increment primary key,
-> dept_code varchar (20) not null,
-> dept_name varchar (100) not null);

CREATE TABLE table_employee (


employee_id INT AUTO_INCREMENT PRIMARY KEY,
employee_code VARCHAR(20),
employee_name VARCHAR(100),
employee_address VARCHAR(100),
employee_phno VARCHAR(100),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES table_department(dept_id)
);

Inserting data in employee and department


insert into table_department (dept_code, dept_name) values ('S01', 'financial'),
('S02', 'HR'), ('S03', 'Database'), ('S04', 'coding'), ('S05', 'management');

insert into table_employee (employee_code, employee_name, employee_address,


employee_phno, dept_id) values ('E01', 'Sushan Kc', 'Balkot', '9808201519', 3),
('E02', 'Yenush khatri', 'Satdobato', '9845698712', 2), ('E03', 'Kritish basnet',
'imadole', '9765487612', 5), ('E04', 'Sundar Adhikari', 'Thimi', '9764581232', 1),
('E05', 'Aaryas karki', 'Dulikhel', '9845123698',4), ('E06', 'Milan joshi', 'Pokhara',
'9768457321', 5), ('E07', 'arjun shah', 'Baneshwor', '9654789600', null), ('E08',
'Rupesh lama', 'Balkumari', '9741842089', 2), ('E09', 'Saroj karki', 'Kusunti',
'9860247589', null), ('E10', 'Subib bhattarai', 'Pipple bot', '976420045', 5);
Inner join
Write a query to retrieve a list of employees along with
their department names. Only include employees who are
assigned to a department.
SELECT e.employee_id, e.employee_name, e.employee_code, e.employee_address,
e.employee_phno, d.dept_name
-> FROM table_employee e
-> INNER JOIN table_department d
-> ON e.dept_id = d.dept_id;
+-------------+-----------------+---------------+------------------+---------------+------------+
| employee_id | employee_name | employee_code | employee_address |
employee_phno | dept_name |
+-------------+-----------------+---------------+------------------+---------------+------------+
| 4 | Sundar Adhikari | E04 | Thimi | 9764581232 | financial |
| 2 | Yenush khatri | E02 | Satdobato | 9845698712 | HR |
| 8 | Rupesh lama | E08 | Balkumari | 9741842089 | HR |
| 1 | Sushan Kc | E01 | Balkot | 9808201519 | Database |
| 5 | Aaryas karki | E05 | Dulikhel | 9845123698 | coding |
| 3 | Kritish basnet | E03 | imadole | 9765487612 | management
|
| 6 | Milan joshi | E06 | Pokhara | 9768457321 | management
|
| 10 | Subib bhattarai | E10 | Pipple bot | 976420045 |
management |
+-------------+-----------------+---------------+------------------+---------------+------------+


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 |
+---------+-----------+------------+-----

You might also like