Second Highest Salary
why can not i use the ifnull() function? null is a real returnType?
Note, the Select without From is not standard SQL. Some databases don't support it. It is used here as a trick to generate NULL value if empty result set is returned. But will not work in certain databases, e.g. Oracle
@qlan2 Having should be used after group by.
No one has replied
this works great for this example, but in case we have duplicate e.g. id = 4 and salary = 200. then your internal query count will be not equal to N-1
It isn't common.
If choose fourth highest salary
it's useless? why wrong answer?
Since we are sorting by 'Salary DESC', if there is a salary with 0, it will be listed before the null.
@changtan "SELECT NULL" means return NULL install of  if the value doesn't exist.
It would be great to use more explicit syntax LIMIT row_count OFFSET offset, which could help readers to understand the query.
Time Limit Exceeded
How can you get this solution.
when count(Salary) > 1 then (select distinct Salary from Employee
order by Salary DESC limit 1, 1)
else NULL end as SecondHighestSalary
This is wrong.
If an attribute does not display after 'having' and it displays after 'select',it must use aggregate function.
Well when you say LIMIT 2,1 you basically tell SQL that you want 1 row from offset 2, which means you want row number 3 from the results. I don't think that is what you want....
Here is how I did it:
If I'm not wrong, it's only using MySQL as the DB, where "TOP 2" is not correct syntax. Use "LIMIT 2" instead (http://dev.mysql.com/doc/refman/5.0/en/select.html).
And your solution had other bug. You may want to debug on your own.
Disabled Categories are greyed out
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.