To find the maximum and minimum salary in each department from a database, you can use an SQL query that groups the results by the department. Assuming you have a table named employees with columns department_id (or department_name) and salary, you can write the following SQL query:
SELECT
department_id,
MAX(salary) AS max_salary,
MIN(salary) AS min_salary
FROM
employees
GROUP BY
department_id;
If your table uses department_name instead of department_id, you can adjust the query like this:
SELECT
department_name,
MAX(salary) AS max_salary,
MIN(salary) AS min_salary
FROM
employees
GROUP BY
department_name;
Explanation:
- SELECT: This part of the query specifies the columns you want to retrieve. In this case, it's the department ID/name and the maximum and minimum salary.
- MAX(salary) AS max_salary: This calculates the maximum salary for each department and labels it as max_salary.
- MIN(salary) AS min_salary: This calculates the minimum salary for each department and labels it as min_salary.
- FROM employees: Specifies the table from which to retrieve the data.
- GROUP BY department_id (or department_name): This groups the results by the department, allowing the aggregate functions (MAX and MIN) to produce results for each department separately.
Make sure to adjust the table name and column names according to your schema.