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
name
of the employees from theEmployee
table and alias it asemployee_name
. - We perform an
INNER JOIN
between theEmployee
andDepartment
tables based on thedepartmentId
column in theEmployee
table and theid
column in theDepartment
table. This links employees to their respective departments. - We use a
WHERE
condition to filter the results. In this case, we filter employees whose department'sname
matches '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