Display employee names with their department names from two tables using an equi-join
Expt. No.37
Page No.74
Equi-join
Aim
To display each employee's name with the name of their department, joining the EMPLOYEE and DEPT tables on the department number.
Concepts used
Queries
USE company;SELECT ename, dname FROM employee, dept WHERE employee.deptno = dept.deptno;SELECT ename, salary, dname FROM employee, dept WHERE employee.deptno = dept.deptno AND dname = 'IT';
A marked line continues the line above it. In your file, write it on the same line.
Tables used
Table: employee
+-------+--------+--------+--------+ | empno | ename | deptno | salary | +-------+--------+--------+--------+ | 1 | Anil | 10 | 45000 | | 2 | Charu | 20 | 68000 | | 3 | Farhan | 20 | 75000 | | 4 | Kavita | 30 | 41000 | | 5 | Gauri | 10 | 48000 | +-------+--------+--------+--------+
Table: dept
+--------+-------+ | deptno | dname | +--------+-------+ | 10 | Sales | | 20 | IT | | 30 | HR | +--------+-------+
Output
mysql> USE company;
Database changed
mysql> SELECT ename, dname FROM employee, dept WHERE employee.deptno = dept.deptno;
+--------+-------+
| ename | dname |
+--------+-------+
| Anil | Sales |
| Charu | IT |
| Farhan | IT |
| Kavita | HR |
| Gauri | Sales |
+--------+-------+
5 rows in set
mysql> SELECT ename, salary, dname FROM employee, dept WHERE employee.deptno = dept.deptno AND dname = 'IT';
+--------+--------+-------+
| ename | salary | dname |
+--------+--------+-------+
| Charu | 68000 | IT |
| Farhan | 75000 | IT |
+--------+--------+-------+
2 rows in set
✓ Printed by running this query set, not typed by hand.
Result
The equi-join matched each employee's deptno with the DEPT table and showed the department name; the second query kept the two IT employees, Charu and Farhan.
Viva voce: what the examiner may ask
01What is an equi-join?
A join whose condition uses = on the common column of the two tables.
02Why is employee.deptno written with the table name?
Both tables have a deptno column; without the table name it would be ambiguous.
03What would happen without the WHERE condition?
Every employee would be paired with every department (a cartesian product: 5 × 3 = 15 rows).
Variations for the lab test
- Display the employees of the Sales department.
- Display the department name and salary of employees earning above 45000.
- Count the employees in each department name with GROUP BY dname.
! Common mistake Writing
WHERE deptno = deptno is ambiguous (or always true); both sides must name their tables.