Trying to find third lowest salary in mysql - mysql

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;

Related

SQL - Second highest

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 trying to get latest month salary from salary table, where multiple entry of employeeID in salary table for each month

Employee table"
empno,
ename,
sal_id,
emp_id,
salary,
month
SALARY table:
sal_id,
emp_id,
salary,
month
I am trying to make query for getting all employee from employee table +
In salary table, there are multiple entry (may be not) of emp_id.
I want employee list with their latest salary( or last month salary)
My current query is :
SELECT * FROM emp LEFT JOIN salary ON emp.empno = salary.emp_id GROUP BY empno ORDER BY salary.sal_id DESC
But I am getting emp list with first salary, I want with latest salary.
Help me :(
Emploee table
Salary table
You are ordering by sal_id instead you might want to order by a field in salary which represents its month.
SELECT emp.empno, emp.name, (
SELECT sal
FROM salary
WHERE salary.empno = emp.empno
ORDER BY salary.sal_id DESC LIMIT 1
)
FROM emp
I have come with solution after a lot R&D and googling .
It is not possible in single query, we have to apply 2 query
SELECT * FROM emp LEFT JOIN salary ON salary.emp_id=emp.empno WHERE salary.sal_id IN ( SELECT MAX(salary.sal_id) FROM salary GROUP BY salary.emp_id ) GROUP BY salary.sal_id ORDER BY salary.sal_id DESC

How to find second highest salary in mysql

How to find second highest salary in mysql.
All record find in second highest salary.
Table : Employee
ID salary emp_name
1 400 A
2 800 B
3 300 C
4 400 D
4 400 C
*** Mysql Query: ***
SELECT * FROM employee ORDER by salary DESC LIMIT 1,2
This return two record.I do not know how many record in second highest salary.
Try this:
SELECT emp_name,salary
FROM Employee
WHERE salary = (SELECT DISTINCT salary FROM Employee as emp1
WHERE (SELECT COUNT(DISTINCT salary)=2 FROM Employee as emp2
WHERE emp1.salary <= emp2.salary))
ORDER BY emp_name
SELECT sal
FROM emp
ORDER BY sal DESC
LIMIT 1, 1;
You will get only the second max salary.
And if you need any 3rd or 4th or Nth value you can increase the first value followed by LIMIT (n-1) ie. for 4th salary: LIMIT 3, 1;
1st one with limit-
SELECT SALARY FROM tbl_name ORDER BY SALARY DESC LIMIT 1,1
2nd one without limit-
SELECT MAX(SALARY) FROM tbl_name WHERE SALARY < (SELECT MAX(SALARY) FROM tbl_name)
SELECT * FROM employee GROUP BY salary ORDER BY salary DESC LIMIT 1, 1
Above query first grouped salary column (for distinct record) and display records in descending order then apply limit function (limit function accept two parameter first one is for index(which is start from 0) and second one is for how many record we want).
If you want third highest salary just change limit 2,1 and so on for next.
Use this to find 2nd highest salary.
SELECT * FROM tablename ORDER BY salary DESC LIMIT 1,1
first 1 in limit is to skipping the rows and second 1 in limit is to display the row.
For third highest skip two rows same scenario to find nth salaries of the employees.
SELECT * FROM tablename ORDER BY salary DESC LIMIT 2,1
select salary
from (
select salary
from Employee
order by desc
limit 2) as minimumTwoSalary
order by minimumTwoSalary.salary
limit 1;
Using GROUP BY with ORDER BY will give you the second highest salary even if there is two same salary
SELECT * FROM employee GROUP BY salary ORDER by salary DESC LIMIT 1,1
Try this:
SELECT MAX(SALARY)
FROM tbl_name
WHERE
SALARY NOT IN
(
SELECT MAX(SALARY)
FROM tbl_name
)
Use the below query to get the 2nd or nth highest salary. Basically, The DENSE_RANK()
assigns a rank to each row within a partition or result set with no gaps in ranking values.
The rank of a row is increased by one from the number of distinct rank values which come before the row.
SELECT salary as highest_salary FROM
(SELECT salary, DENSE_RANK() OVER( ORDER BY salary DESC) row_num FROM Employee) with_dense_rank
WHERE row_num = 2;
select max(salary) from Employee where salary != (select max(salary) from Employee)
Explanation: -
In the above query, we are using the max function of SQL to find the maximum salary then we are comparing max salary using where clause with the help of subquery to filter out the maximum salary from the salary column.
Another example of the same approach is below. I believe it is the easiest logical method but maybe not the fastest because the subquery makes the output a bit slower.
select max(salary) from Employee where salary < (select max(salary) from Employee)
So, I will go with #Renuka Kulkarni Approach, but just a little different in the query, because some employees can also have the same amount of salary. so we need to select Distinct, it is important to use distinct because maybe 2 and multiple employees can have the same amount of salary.
SELECT DISTINCT price
FROM Product
ORDER BY price DESC
LIMIT 1, 1;
We can find Second Highest Salary in many ways.
1.Normal Where Condition Using MAX() Function
SELECT MAX(salary) AS SecondHighestSalary
FROM Employee WHERE salary<(SELECT MAX(salary) FROM Employee);
2.Using LIMIT
SELECT salary AS SecondHighestSalary FROM Employee ORDER BY salary DESC
LIMIT 1,1;
3.Using Self JOIN
SELECT MAX(e2.salary) AS SecondHighestSalary
FROM Employee e1, Employee e2 WHERE e1.salary>e2.salary;
4.Using NOT IN Keyword
SELECT MAX(salary) AS SecondHighestSalary
FROM Employee
WHERE salary NOT IN (SELECT MAX(salary) FROM Employee);
SELECT * FROM employee ORDER by salary DESC LIMIT 1,1;

Using multiple aggregate functions in one SQL query

My table is as below
WORKS ( emp-name, comp-name, salary)
I want to find the comp-name which pays to lowest total salary to it's employees
I tries below query, but it gives SUM of salaries for all comp-name
SELECT comp-name, MIN(sal) as lowest
FROM
(
SELECT comp-name, SUM(salary) as sal from WORKS group by comp-name
)tmt group by comp-name;
How do I find only one company which pays lowest total salary.
You can use LIMIT to get only one company with lowest total salary , also need to need to sort in ascending order
SELECT comp-name,
SUM(salary) as sal
from WORKS
group by comp-name
Order by sal ASC
LIMIT 1

second highest salary in table in sql

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;