To fetch employees based on a given department name using SQL, you can use a SQL query with a JOIN clause and a WHERE condition to filter by the department name. Assuming you have two tables, Employee and Department, with appropriate columns.
Department:
| id | name | 
|---|---|
| 1 | HR | 
| 2 | Java | 
Employee:
| id | name | department_id | 
|---|---|---|
| 101 | Gangu | 1 | 
| 103 | Naidu | 2 | 
SQL query to achieve this:
sqlSELECT e.name AS employee_name
FROM Employee e
JOIN Department d ON e.departmentId = d.id
WHERE d.name = 'Java';
In this SQL query:
- We select the nameof the employees from theEmployeetable and alias it asemployee_name.
- We perform an INNER JOINbetween theEmployeeandDepartmenttables based on thedepartmentIdcolumn in theEmployeetable and theidcolumn in theDepartmenttable. This links employees to their respective departments.
- We use a WHEREcondition to filter the results. In this case, we filter employees whose department'snamematches 'Java'. You can replace 'Java' with the desired department name.
This SQL query will return a list of employee names who belong to the specified department.

Comments
Post a Comment