@@frederikmuller Thanks sir, its so great I tried to study a lot sources I feel I understand them but when I actually work on real problem I feel I dont know anything and really appreciate your explanation for every single problems.
@@khoanguyen1194 I'm glad it's helpful! I think the most important thing is to learn how to approach a problem so you can apply your knowledge to any problem.
Thanks for explaining the solution really well. Just thought of another solution with rank function. SELECT case when b.salary is null then null else b.salary end as salary FROM (select 2 as rnk) as a LEFT OUTER JOIN (select salary,rnk from (select salary, rank() over(order by salary desc) as rnk from dbo.Employee ) a where rnk = 2 ) as b on a.rnk = b.rnk Got bit messy as the requirement was if there is no second highest salary , return NULL.
select ifnull((select distinct salary from table_name ordre by salary desc limit 1 offset 1),null) as second_highest_salary This will give you NULL as return incase if there's no second highest. simple but effective one
I think the second approach did not work because salary column can contain duplicate salaries so when you write offset 1 it will skip 1 row but because of duplicate entries your limit 1 will give you again same salary.. I think you are getting me.??
-- Write your PostgreSQL query statement below select max(salary) as SecondHighestSalary from Employee where salary < (select max(salary) from Employee)
What an Amazing explanation. Pl take more problems from leetcode, scratascatch and hackerrank. You explain perfectly. May Shree Raam bless you with joy and happiness..❤
Hello Frederik, Thank you so much for your content; very helpful. I have a question for this video, why did you use the WHERE Clause with an aggregation 'AVG.'? I am a bit confused here.
Do you mean MAX()? Since the aggregation is in a separate subquery that will be evaluated first, it’s fine. But you’re right, you’re not able to use aggregation functions in a where clause otherwise.
Not at the moment. I feel like I’ve covered all major question types on LeetCode and I have a sponsorship with StrataScratch which is why I’m prioritizing SS. Also more questions on there!
Second highest salary create table Person (id int not null primary key, Salary INt );
insert into person VALUES(1,1000),(2,2000),(3,3000),(4,4000),(5,5000);
select * from person;
select salary FROM ( select id,salary,rank() over(order by salary desc) as rnk from person ) A where A.rnk = 2; This is a right approach or not please any one can tell me
If you want to have access to more free SQL problems, check out StrataScratch: stratascratch.com/?via=frederik
Watching Frederik explain feels like a meditative session ❤
Very good explanation. Really solved all my confusion about this problem. Keep going like. Really enjoy content like this!
5: 14 updated
Use DISTINCT Keyword
I am really appreciate your work. Are you going to make all SQL problems on Leetcode (153 questions) ? Thank you!
yes, I'm planning to go through all of them!
@@frederikmuller Thanks sir, its so great I tried to study a lot sources I feel I understand them but when I actually work on real problem I feel I dont know anything and really appreciate your explanation for every single problems.
@@khoanguyen1194 I'm glad it's helpful! I think the most important thing is to learn how to approach a problem so you can apply your knowledge to any problem.
Thanks for explaining the solution really well. Just thought of another solution with rank function.
SELECT
case when b.salary is null then null else b.salary end as salary
FROM
(select 2 as rnk) as a
LEFT OUTER JOIN
(select salary,rnk from
(select
salary,
rank() over(order by salary desc) as rnk
from
dbo.Employee
) a
where rnk = 2
) as b
on a.rnk = b.rnk
Got bit messy as the requirement was if there is no second highest salary , return NULL.
select ifnull((select distinct salary from table_name ordre by salary desc limit 1 offset 1),null) as second_highest_salary
This will give you NULL as return incase if there's no second highest. simple but effective one
while selecting " where salary not in select MAX(Salary) from employee , why it does not includes 3rd also as we did not set any limit
I think the second approach did not work because salary column can contain duplicate salaries so when you write offset 1 it will skip 1 row but because of duplicate entries your limit 1 will give you again same salary.. I think you are getting me.??
Try "Select distinct"
-- Write your PostgreSQL query statement below
select max(salary) as SecondHighestSalary
from Employee
where salary < (select max(salary) from Employee)
Wonderful explaination. As a recommendation,
Please zoom in. It is difficult to see the letters clearly
Thank you for the input!
Nice explanation my brother
What an Amazing explanation. Pl take more problems from leetcode, scratascatch and hackerrank. You explain perfectly. May Shree Raam bless you with joy and happiness..❤
now this is a medium level question ...tricky but fun
Whats gonna happen to the 100? as it is also less than the max salary 300?
man these are too good 🙌
Great explanation
Hello Frederik,
Thank you so much for your content; very helpful.
I have a question for this video, why did you use the WHERE Clause with an aggregation 'AVG.'? I am a bit confused here.
Do you mean MAX()? Since the aggregation is in a separate subquery that will be evaluated first, it’s fine. But you’re right, you’re not able to use aggregation functions in a where clause otherwise.
@@frederikmuller Thank you so much; I appreciate your quick feedback.
Are you not making anymore videos on SQL ?
I'll start with the premium SQL questions soon ;)
@@frederikmuller That's great !!
Hello Frederik, Are you still making premium leetcode SQL problems? Thank you
Not at the moment. I feel like I’ve covered all major question types on LeetCode and I have a sponsorship with StrataScratch which is why I’m prioritizing SS. Also more questions on there!
Second highest salary
create table Person
(id int not null primary key,
Salary INt
);
insert into person VALUES(1,1000),(2,2000),(3,3000),(4,4000),(5,5000);
select * from person;
select salary
FROM
(
select id,salary,rank() over(order by salary desc) as rnk
from person
) A
where A.rnk = 2;
This is a right approach or not please any one can tell me
Hey I did this but it failed for me in Case 2, because it doesn't print NULL in case there is no data in the table
SELECT MAX Salary as SecondHighestSalary
FROM employes
WHERE salary NOT IN(SELECT MAX Salary) FROM employees
Wasn't aware Fardeen khan taught SQL in his younger days..
AWESOME !
very nice thanks!"
Tricky indeed
Thanks
Thanks