In a given question,
Write a SQL query to get the second highest salary from the Employee table.
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
For example, given the above Employee table, the query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null.
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
I wrote a code for this question, however, the expected output is different.
My Code:
SELECT
CASE
WHEN COUNT(*) = 1 THEN NULL
ELSE (SELECT Salary FROM Employee HAVING Salary < MAX(Salary) ORDER BY Salary DESC LIMIT 1)
END AS SecondHighestSalary
FROM Employee;
I think there's something wrong with my code but I cannot find what is the actual problem here. My code returns 100 not 200. What have I gotten wrong?
+---------------------+
| SecondHighestSalary |
+---------------------+
| 100 |
+---------------------+
You can utilize LIMIT {[offset,] row_count}. Refer https://dev.mysql.com/doc/refman/8.0/en/select.html
Order by Salary in descending order, and get the second row by defining OFFSET as 1. We will use DISTINCT on Salary as there is a possibility to have multiple rows for the highest salary.
SELECT DISTINCT
Salary
FROM Employee
ORDER BY Salary DESC
LIMIT 1,1
You can try below
SELECT MAX(salary) From Employee WHERE salary < ( SELECT Max(salary) FROM Employee);
Try that out:
SELECT salary FROM Employee ORDER BY salary DESC LIMIT 1,1
or to a deeper approach you can use something like:
SELECT salary FROM Employee GROUP BY salary ORDER BY salary DESC LIMIT 1,1
All that queries have high performance, since they does not have any subqueries.
If you want to display a empty_row(null) in case there is no highest salary then the following does it, if there is a value then it gets shown
select (select salary
from Employee ORDER BY salary DESC LIMIT 1,1
) as x
Here is the solution.
SELECT MAX(salary) From Employee WHERE salary < ( SELECT Max(salary) FROM Employee);
Related
In a given question,
Write a SQL query to get the second highest salary from the Employee table.
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
For example, given the above Employee table, the query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null.
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
I submited the query below
(window function to rank the salary then identify ifnull or not):
WITH salary_ranked
AS (
SELECT
salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS rank_num
FROM
Employee
)
SELECT
IFNULL(
(SELECT salary_ranked.salary
FROM salary_ranked
WHERE rank_num =2), 'null') AS SecondHighestSalary;
But it showed the wrong answer as below.
THE LATEST UPDATE
Revised the code below:
WITH salary_ranked
AS (
SELECT
salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS rank_num
FROM
Employee
WHERE
salary > 0
)
SELECT
IFNULL(
(SELECT salary_ranked.salary
FROM salary_ranked
WHERE rank_num =2), null) AS SecondHighestSalary;
It passed 7 test cases of 8. But it still leave the last situation to be solved.
SELECT id, salary FROM employees ORDER BY salary DESC LIMIT 1, 1
The first number after "LIMIT" is the offset when using the comma. The offset is zero-indexed. So, this query selects one row, starting at the second highest.
See the section on "offset" in the MySQL docs on SELECT
SQL Question:
Write a SQL query to get the nth highest salary from the Employee table (SQL Server)
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
For this example, the nth highest salary where n = 2 is 200. If there is no nth highest salary, then the query should return null.
| getNthHighestSalary(2) |
+------------------------+
| 200 |
My code is the following:
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
RETURN (
select distinct a.salary from Employee a, Employee b where
a.id =b.id-n+1 order by a.id desc
limit 1
);
END
My question is: The code works for most of the cases except for the situation when there are multiple same values before the nth place. For example, whe n is 2 and the table is the following. (The expected result should be NULL and my code returns 100. ) So, how can I change my code so that it can work for all situations? Thank you!
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
| 2 | 100 |
| 3 | 100 |
+----+--------+
Try this
Select distinct salary
From (
Select salary, dense_rank() over
(order by salary desc) r
from employee
) where r=n
1st 2nd and 3rd highest salary query
SELECT MAX(salary) AS ThirdHighestSalary
FROM emp
WHERE (salary <
(SELECT MAX(salary) AS SecondHighestSalary
FROM emp
WHERE (salary <
(SELECT MAX(salary) AS HighestSalary
FROM emp))))
i have table A in mysql
+-------------------------+
| ID Name Month Salary |
+-------------------------+
| 1 Joe 4 300 |
| 2 Kim 3 100 |
| 3 Moko 2 150 |
| 4 Loli 5 80 |
| 5 Tom 3 400 |
+-------------------------+
how do i get total number of salary based on month and the person who earned max
example
Joe = 4*300 = 1200
Kim = 300
Moko = 300
Loli = 400
Tom = 1200
The output will be : 1200, 2
1200 is the max salary and 2 is people who earned which is Joe and Tom
You can compute all the total salaries in a subquery, then count the number of occurrences of each total value, ordering by the total descending with a LIMIT 1 to restrict to the highest value:
SELECT total, COUNT(*) AS num
FROM (
SELECT Month * Salary AS total
FROM data
) t
GROUP BY total
ORDER BY total DESC
LIMIT 1
Output:
total num
1200 2
Demo on SQLFiddle
try like below using subquery
select max(salary),count(*) from (select name, sum(Month*Salary) as salary
from data group by name
) b where b.salary=
(select max(salary) from
(select sum(Month*Salary) as salary
from data group by name
)b
)
Demo link
SELECT Month*Salary, COUNT(*)
FROM A WHERE Month*Salary = (SELECT MAX(Month*Salary)
FROM A)
The second query will give you the maximum total salary earned and then you can filter the entries in table A with Month*Salary being equal to that amount.
I have a table called tbl_user_sal:
| id | user_id | salary | date |
| 1 | 1 | 1000 | 2014-12-01 |
| 2 | 1 | 2000 | 2014-12-02 |
Now I want to get the id of the maximum date. I used the following query:
SELECT MAX(date) AS from_date, id, user_id, salary
FROM tbl_user_sal
WHERE user_id = 1
But it gave me this output:
| id | user_id | salary | from_date |
| 1 | 1 | 2000 | 2014-12-02 |
Which is correct as far as the max date being 2014-12-02, but the corresponding id is not correct. This happens for other records as well. I used order by to check but that was not successful either. Can anyone shed some light on this?
Note: Its not necessary that max date will have max id, according to my needs. Records can have max date but id may be older.
If you only want to retrieve that information for a single user, which you seem to, because of your WHERE clause, just use ORDER BY and LIMIT:
SELECT *
FROM tbl_user_sal
WHERE user_id = 1
ORDER BY date DESC
LIMIT 1
If you want to do that for every user, however, you will have to get a little bit fancier. Something like that should do it:
SELECT t2.id, user_id, date
--find max date for each user_id
FROM (SELECT user_id, MAX(date) AS date
FROM tbl_user_sal
GROUP BY user_id) AS t1
--join ids for each max date/user_id combo
JOIN tbl_user_sal AS t2
USING (user_id, date)
--limit to 1 id for every user_id
GROUP BY
user_id
You are missing group by clause Try this:
select max(awrd_date) as from_date,awrd_id
from tbl_user_sal
where awrd_user_id = 106
group by awrd_id
What I believe you should do here is have a subquery that pulls the max date, and your outer query looks for the row with that date.
It looks like this:
SELECT *
FROM myTable
WHERE date = (SELECT MAX(date) FROM myTable);
Additional things may need to be added if you want to search for a specific user_id, or get the largest date for each user_id, but this gives your expected results for this example here.
Here is the SQL Fiddle.
I was practicing some SQL when this hit me. I wanted to see how many times a certain commodity came up and from there get the commodity which came up the most.
This shows how many times each commodity comes up:
mysql> SELECT commodity, COUNT(commodity) count FROM orders GROUP BY commodity ORDER BY count;
+----------------------+------------+
| commodity | count |
+----------------------+------------+
| PERSIAN MELON | 4 |
| BEANS | 6 |
| CASABA | 10 |
| ASPARAGUS | 11 |
| EGGPLANT | 12 |
| TOMATOES, CHERRY | 16 |
| GALIA MELON | 18 |
+-----------------------------------+
I'm trying to get the row with the highest but it's all wrong:
mysql> SELECT commodity, MAX(COUNT(commodity)) count FROM orders GROUP BY commodity ORDER BY count;
What's the right way of doing this?
CAUTION: the query will not handle duplicate records having the maximum COUNT
SELECT commodity, COUNT(commodity) `count`
FROM orders
GROUP BY commodity
ORDER BY `count` DESC
LIMIT 1
But this will,
SELECT commodity, COUNT(commodity) `count`
FROM orders
GROUP BY commodity
HAVING COUNT(commodity) =
(
SELECT MAX(`COUNT`)
FROM
(
SELECT COUNT(commodity) `count`
FROM orders
GROUP BY commodity
) s
)
Try this query
SELECT commodity,COUNT(commodity) AS count
FROM orders
GROUP BY commodity
ORDER BY count desc
LIMIT 1;
I would write:
select * from (SELECT commodity, COUNT(commodity) count FROM orders GROUP BY commodity ORDER BY count desc) where rownum <2;
It is fine just add desc to order by
SELECT commodity, COUNT(commodity) count FROM orders GROUP BY commodity ORDER BY count DESC;
first row will have max value and add limit to get just this one record
SELECT commodity, COUNT(commodity) count FROM orders GROUP BY commodity ORDER BY count DESC LIMIT 1;
It looks like you're doing it right. Except ORDER BY orders them in ASC order. make it descending
mysql> SELECT commodity, COUNT(commodity) count FROM orders GROUP BY commodity ORDER BY count DESC;