Write a query to find maximum salary from employee table w.r.t. dept name and gender. (Every dept has a male & a female record)
dept.name | gender | salary |
---|---|---|
Finance | M | 4000 |
Finance | F | 4500 |
HR | M | 3000 |
HR | F | 2000 |
Accounts | M | 6000 |
Accounts | F | 7000 |
CodePudding user response:
You could this:
Select max(salary), dept.name from the_table
CodePudding user response:
You have some way of doing this:
If you want to use max
function you have to group by
, for example dept_name
in your case. And you will find the max salary for the dept_name.
select max(salary) as max_salary ,
dept_name
from employee
group by dept_name
order by max_salary DESC;
If you do not want to use max
with group by
, use limit
like:
select dept_name,
gender,
salary
from employee
order by salary desc limit 1;
Demo: https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/88
CodePudding user response:
You may add the conditions, as follows:
SELECT MAX(salary)
FROM employee
WHERE gender= 'F'
AND dept.name='Accounts';