Very Famous SQL Interview Question | Department Highest Salary

  Рет қаралды 24,872

Sumit Mittal

Sumit Mittal

Күн бұрын

Пікірлер: 37
@sumitmittal07
@sumitmittal07 2 жыл бұрын
Checkout the Big Data course details here: trendytech.in/?referrer=youtube_lc5
@arunsundar3739
@arunsundar3739 8 ай бұрын
'where' clause with more than one column, along with 'in' clause is a combination i am learning first time, very interesting, gaining more confidence in sql questions now :)
@rokibchi
@rokibchi 9 ай бұрын
Finished the whole playlist. Really helpful. I've learned How to approach any SQL problem.
@sonumahto1364
@sonumahto1364 2 жыл бұрын
select Department, Employee, Salary from ( select d.name as Department,e.name as Employee,e.salary, rank() over (partition by departmentid order by salary desc) rnk from Employee e join department d on e.departmentid=d.id ) x where x.rnk=1 It is more optimised
@himanchalchandra6202
@himanchalchandra6202 6 ай бұрын
use dense_rank over rank as it will allow more than 1 highest salary in each dept
@angelnadar6451
@angelnadar6451 2 жыл бұрын
Thanks @ helpful !!!
@sumitmittal07
@sumitmittal07 2 жыл бұрын
Glad that it was helpfull
@RITURAJRANJAN-st7dv
@RITURAJRANJAN-st7dv 7 ай бұрын
Thanks for the wonderful solution Sir. I have tried to solve in a different way. select Department,Employee,salary from (select Department,Employee,salary,dense_rank() over(partition by Department order by salary desc) as denserank from (select e.name as Employee,salary,d.name as Department from Employee e join Department d on e.departmentId = d.id) temp) temp1 where denserank=1;
@ashitosh501
@ashitosh501 2 жыл бұрын
sir waiting for next session
@sumitmittal07
@sumitmittal07 2 жыл бұрын
Very soon
@HARSHRAJ-gp6ve
@HARSHRAJ-gp6ve 7 күн бұрын
with cte as( select Employee.name,Employee.salary,Department.name as dept_name FROM Employee JOIN Department ON Employee.departmentId=Department.id ),cte1 as( select cte.*,DENSE_RANK()OVER(PARTITION BY dept_name ORDER BY salary DESC) as r1 FROM cte ) select dept_name as Department,name as Employee,salary as Salary FROM cte1 where r1=1;
@vijaybabukommuri5125
@vijaybabukommuri5125 4 күн бұрын
# Write your MySQL query statement below select B.name as Department, A.name as Employee, A.salary as Salary from ( select name,departmentId,salary, DENSE_RANK() OVER (PARTITION BY departmentId ORDER BY salary desc) as rn from Employee) as A left join Department B on A.departmentId=B.id where A.rn=1 ; But your approach is much better and straightforward, Unnecessarily used the DENSE_RANK and made it more complex SELECT B.name, A.name, A.salary FROM Employee A JOIN Department B ON A.departmentId=B.id WHERE (A.departmentId,A.salary) in (SELECT departmentId, MAX(salary) FROM Employee GROUP BY departmentId)
@venkataram6460
@venkataram6460 2 жыл бұрын
Please upload more videos to this playlist
@adnanmulla6640
@adnanmulla6640 10 ай бұрын
Alternate way: select Department,Employee,Salary from (select d.name as Department, e.name as Employee, dense_rank() over (partition by d.name order by salary desc) as Sal, Salary from Employee e inner join Department d on e.departmentId=d.id)x where x.Sal=1
@sathiyanr3992
@sathiyanr3992 2 жыл бұрын
More Useful Sir. Kindly upload these kinds of problems continuously.
@subhajitroy5850
@subhajitroy5850 2 жыл бұрын
Please bring few more problem statements showing the usage of full outer join & Cartesian product as well
@sukanyaiyer2671
@sukanyaiyer2671 2 жыл бұрын
pls solve prob 627
@sumitmittal07
@sumitmittal07 2 жыл бұрын
Noted :) will cover that in the upcoming sessions
@sukanyaiyer2671
@sukanyaiyer2671 2 жыл бұрын
@@sumitmittal07 thank you sir
@lucifieramit1
@lucifieramit1 2 жыл бұрын
@@sukanyaiyer2671 you need to use update with case statement in that problem
@Foodie_Cooking_Loverz
@Foodie_Cooking_Loverz Жыл бұрын
Looks complicated querry
@lucifieramit1
@lucifieramit1 2 жыл бұрын
select temp.name as Department,temp.employeename as Employee ,temp.salary as Salary from (select d.name,e.name as employeename,salary,dense_rank() over ( partition by d.name order by salary desc ) as dr from employee e join department d on e.departmentId=d.id) temp where temp.dr=1
@thomsondcruz5456
@thomsondcruz5456 2 жыл бұрын
Window functions are not always cost effective
@florincopaci6821
@florincopaci6821 2 жыл бұрын
Thank you for all the videos! can you please please do a video about leetcode sql question 2153-number of passengers in each bus 2?Please.Thank you
@faizraina6
@faizraina6 2 жыл бұрын
why are we using department id in sub-query . why do we need to include it , can't we just use max(salary) only
@razeenahmed7334
@razeenahmed7334 2 жыл бұрын
No, we can't use only max(salary) in the sub-query, because that will return only one maximum salary, but here we want to retrieve max salaries for both of the departments!
@vishwamgupta1329
@vishwamgupta1329 2 жыл бұрын
WITH Solution as (SELECT Employee.name as Employee, Employee.salary as Sal ,Department.name as Department, DENSE_RANK() OVER(PARTITION BY Department.name ORDER BY Employee.salary DESC) as re FROM Employee INNER JOIN Department ON Employee.departmentID=Department.id) SELECT Department, Employee ,Sal as Salary FROM Solution WHERE re=1; #This one also worked using Dense rank window function
@103himajapoluri6
@103himajapoluri6 2 жыл бұрын
Can anybody help with identifying what is wrong in this query select d.name as Department,e1.name as Empolyee ,max(salary) as Salary from employee e1,department d where e1.departmentId = d.id group by d.name ;
@ShivaKumar-dj8bj
@ShivaKumar-dj8bj 2 жыл бұрын
This will not work because here in each department for each employee there will be only one row for salary...your query gives result department, employee name and the highest salary of him, here there is only one row so basically no aggregation will happen considering there is only one row for each employee in each department. the logic is you need to first partition the data department wise then order the data by salary in descending order for each department and get the first row in each partitioned data set. you can use either Row number or dense rank to achieve this....check once.
@karthikbs8457
@karthikbs8457 2 жыл бұрын
Sir I saw all the previous 5 video. This problem I paused and solved the problem on my own. Thanks. But I used Partition method involving 3 select statements. SELECT temp1.Department,temp1.name as Employee,temp1.Salary FROM( SELECT *, RANK() OVER(PARTITION by temp.departmentId ORDER BY temp.Salary DESC) AS rnk FROM( SELECT e.*,d.name as "Department" FROM Employee e INNER JOIN Department d ON e.departmentId=d.id) temp ) temp1 WHERE temp1.rnk=1;
@ShivaKumar-dj8bj
@ShivaKumar-dj8bj 2 жыл бұрын
for this highest salary rank will work but if you want to get nth highest salary you need to use dense rank...correct me if I'm wrong
@slyroy7562
@slyroy7562 2 жыл бұрын
Select * from (Select employee.department, employee.name employee, salary, dense_rank() over(partition by employee.department order by salary desc) as r from employee, department where employee. Id = department. Id) where r=1; Will this works?
@sukanyaiyer2671
@sukanyaiyer2671 2 жыл бұрын
Yes. It works
@dikshagupta2795
@dikshagupta2795 2 жыл бұрын
There are some changes I have made and after that the query gives expected results: select Department, Employee, Salary from ( Select Department.name as Department, employee.name as Employee, Salary, dense_rank() over(partition by employee.departmentId order by salary desc) as r from employee join Department on employee.departmentId = Department.id ) as r where r = 1
@slyroy7562
@slyroy7562 2 жыл бұрын
@@dikshagupta2795 Thanks
@dikshagupta2795
@dikshagupta2795 2 жыл бұрын
Hello Sir, If we are using sql server then what will be the alternative for "WHERE (departmentId, salary)" as we can't use two columns in WHERE in sql server
@AnweshDash
@AnweshDash 2 жыл бұрын
use window functions
SQL Interview Question - Find Nth Highest Salary | LeetCode
30:40
Sumit Mittal
Рет қаралды 40 М.
Leetcode SQL Interview Questions  | Practise SQL questions
17:50
Sumit Mittal
Рет қаралды 22 М.
Don’t Choose The Wrong Box 😱
00:41
Topper Guild
Рет қаралды 40 МЛН
А я думаю что за звук такой знакомый? 😂😂😂
00:15
Денис Кукояка
Рет қаралды 6 МЛН
Accompanying my daughter to practice dance is so annoying #funny #cute#comedy
00:17
Funny daughter's daily life
Рет қаралды 26 МЛН
Lazy days…
00:24
Anwar Jibawi
Рет қаралды 8 МЛН
Top 5 Excel Hacks & Productivity Tricks
6:54
Innozant
Рет қаралды 2,6 М.
LeetCode 175 | SQL Interview Questions on LeetCode
20:59
Sumit Mittal
Рет қаралды 56 М.
Practice SQL Interview Query | Big 4 Interview Question
14:47
SQL Query | How to find employees with highest salary in a department
10:38
SQL Interview Questions and Answers | Intellipaat
42:05
Intellipaat
Рет қаралды 483 М.
Don’t Choose The Wrong Box 😱
00:41
Topper Guild
Рет қаралды 40 МЛН