I wrote a quick query
select dno from Employee having max(salary)
In my table, the maximum salary in 96000 and is associated with dno 8. But for some reason, that select statement gives dno 6.
http://hastebin.com/wewahiloki.vhdl
I feel like this is really obvious but I can't figure it out.
having don't work properly without group by
If you want all the dno with max salary you can use this
select dno from Employee where salary = (select max(salary) from Employee);
If you one to get the "dno" that have the highest salary you can use the following query:
SELECT dno from employee ORDER BY Salary DESC LIMIT 1
The reason is that "having" is used when your are using aggregation operations. See: https://stackoverflow.com/a/2905312/1641558
Related
I am trying to solve below leet code problem.
https://leetcode.com/problems/second-highest-salary
What is wrong with this answer? Below answer is not accepted :(
select t.salary as SecondHighestSalary from
(
select salary
from employee
order by salary desc
limit 1 offset 1
) as t
Both the below answers are accepted
We can use subquery as shown below:
SELECT MAX(salary) AS secondhighestsalary
FROM employee
WHERE salary < (SELECT MAX(salary)
FROM employee);
or alternatively you can use temporary table:
with temp as
(
SELECT MAX(salary) as salary FROM employee
)
select max(salary) as secondhighestsalary from employee where salary <(select salary from temp);
This is Accepted.
select salary as SecondHighestSalary
from employee a
where 1 = (select count(1) from employee b where b.salary < a.salary )
Your query will not work if the lowest salary appears twice in the table.
Above query uses co-related subquery, means, for each row in outer table the subquery (or inner query) will be executed once.
Moreover, this will work for Nth highest salary. If 10th highest salary would have been asked then just replace the 1 in the above query with 9.
I am using a nested SQL query for finding the third lowest salary but the query doesn't show the correct name of the employee instead it shows gives the highest-paid employee name with the third-lowest salary. This is my query.
SELECT first_name, MIN(salary)
FROM employee
WHERE salary > (SELECT min(salary)
FROM employee
WHERE salary > (SELECT min(salary)
FROM employee)
);
You can use OFFSET/LIMIT to get the third lowest:
SELECT first_name, salary
FROM employee
WHERE salary
ORDER BY salary
LIMIT 1 OFFSET 2
The reason why your original query didn't work, is firstly, your select is:
SELECT first_name, MIN(salary)
which means that there is an implicit "group everything" here, and MySQL interprets the first_name as ANY(first_name).
Furthermore, it's extremely inefficient to do it that way.
SELECT firstname, salary
FROM employees
ORDER BY salary ASC
OFFSET 2 LIMIT 1;
select the data and order by salary ascending order. The minimum salary will be the first row, so limit the result to just one row, that is the person with the minimum salary!
SELECT first_name, salary
FROM employee
ORDER BY salery
LIMIT 1;
I have table tbl_emply containing fields Salary, DNO, and EID for each employ. I need to find average Salary of each DNO that has more than two employees.
I have tried queries like
select avg(salary),DNO
from tbl_emply
where count(select * from tbl_emply group by(DNO)>2);
select avg(salary),DNO
from tbl_emply
group by(DNO);
But these all gave me invalid use of group by. How to get the result?
Use HAVING
SELECT AVG(salary), DNO
FROM tbl_emply
GROUP BY DNO
HAVING COUNT(*) > 2
try this,
select avg(Salary),DNo from tbl_emply group by DNo having count(*)>2;
I am trying to find the salary of the second-highest paid employee.
My expected output is 9000 but it prints 14000. I am unable to find my mistake. Kindly help me
Try this query
select max(salary) from table_name
where salary< (select max(salary) from table_name)
Fiddle
You could use this query also ,
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees)
Simply use sub query to get max salary from the employees table and check against same to get second highest.
Select * from table order by salary desc limit 2,1
second highest salary in table in sql
select * from emp e where
2 =(select count(distinct sal) from emp where e.sal<=sal)
I am unable to understand this query...can anyone help me.
Let's analyze the inner query: you are selecting all the distinct salaries there are more or equal than a predefined salary, which is the salary of the outer query.
So, for EVERY row, you are searching and counting all the other rows with a salary greater or equal that one, and you finally select the one which have a value of 2, which is exactly the second highest salary (because the second highest salary has just 2 salary greater or equal itself: the greater salary at all, and itself).
Tremendous inefficent, because for every row you re-scan the entire table, but funny :)
This is a very inefficient way of doing it.
It uses a correlated sub query. Conceptually for each row in the outer query it does a self join back into the emp table and counts the number of distinct salary values less than or equal to the current salary. If this is 2 then this salary is the 2nd highest salary in the table.
This is known as a "triangular join" and as the number of rows increase the work required grows exponentially.
We can get nth highest salary from table, see below sql:
SELECT DISTINCT salary, name, id
FROM emp_salary
GROUP BY salary
ORDER BY salary DESC
LIMIT N-1,1;
For example : Find second highest salary
SELECT DISTINCT salary, name, id
FROM emp_salary
GROUP BY salary
ORDER BY salary DESC
LIMIT 1,1;
We can get nth lowest salary from table, see below sql:
SELECT DISTINCT salary, name, id
FROM emp_salary
GROUP BY salary
ORDER BY salary ASC
LIMIT N-1,1;
For example : Find second lowest salary
SELECT DISTINCT salary, name, id
FROM emp_salary
GROUP BY salary
ORDER BY salary ASC
LIMIT 1,1;