LeetCode 184: Department Highest Salary [SQL]

  Рет қаралды 11,212

Frederik Müller

Frederik Müller

Күн бұрын

Пікірлер: 24
@avahome5285
@avahome5285 3 жыл бұрын
Wow, WHERE IN can matches two columns values, that's so cool. good to know, thanks.
@frederikmuller
@frederikmuller 3 жыл бұрын
you can also use CONCAT to combine two columns and then search for matches
@UmaAndLak
@UmaAndLak 2 жыл бұрын
Coming from SQL Server world, I was not aware of the syntax for the where condition that you typed up. Good to know. Thank you. My attempt to solve the problem is below, with join instead of where clause. It works as well. select d.name AS Department, e.name AS Employee, e.salary AS Salary from Employee e inner join department d on e.departmentId = d.id inner join (SELECT departmentid, max(salary) as ms from employee group by departmentid) le ON le.departmentid = e.departmentId and le.ms = e.salary
@lts8936
@lts8936 3 жыл бұрын
Hi Frederik, thanks a lot for your thorough explanation. Your solution made a lot of sense to me. However, after I change the order of the two items in where clause from 'WHERE(tb2.Id, tb1.Salary)' to 'WHERE(tb1.Salary, tb2.Id)', it didn'it work. I don't understand why the order matters in this case. Can you please explain? Thanks again!
@dipeshsaili4468
@dipeshsaili4468 2 жыл бұрын
However, after I change the order of the two items in where clause from 'WHERE(tb2.Id, tb1.Salary)' to 'WHERE(tb1.Salary, tb2.Id)', it didn'it work. I don't understand why the order matters in this case. Can you please explain? Thanks again!
@sidawan8279
@sidawan8279 2 жыл бұрын
with cte as ( select d.name as Department, e.name as Employee, e.salary as Salary, rank() over(partition by e.departmentId order by salary desc) as "ranking" from Employee e left join Department d on e.departmentId = d.id ) select Department, Employee, Salary from cte where ranking = 1
@amarbhogat3486
@amarbhogat3486 3 жыл бұрын
(select departmentid, max(salary) from employee group by departmentid); why does this outputs only 2 salary which are maximum?
@hallomutter1231
@hallomutter1231 3 жыл бұрын
Hi Frederik, why do you not need to ORDER BY Salary DESC here to get the correct solution. That's the part I'm confused about.
@frederikmuller
@frederikmuller 3 жыл бұрын
The problem statement mentions that order of output rows doesn't matter. To get the highest salary we use MAX(), so no need to order by anything.
@faizraina6
@faizraina6 2 жыл бұрын
was it necessary that we include , department id as well in our sub-query . couldn't we just have mentioned select max(salary) and group by department_id . please reply if anyone has the answer
@ambicadronadula6451
@ambicadronadula6451 2 жыл бұрын
Group by groups if department id or salary seems to be same.....in this case as we have two persons with highest salary we need to use group by in subquery
@nchou646
@nchou646 4 жыл бұрын
Great video explanation!! thx a lot
@frederikmuller
@frederikmuller 4 жыл бұрын
thank you so much!
@pranjalmishra2602
@pranjalmishra2602 3 жыл бұрын
Hey, Could You please explain this? WHERE (Employee.DepartmentId , Employee.Salary) IN ( SELECT DepartmentId, MAX(Salary) FROM Employee GROUP BY DepartmentId )
@pranjalmishra2602
@pranjalmishra2602 3 жыл бұрын
specially this part-> (Employee.DepartmentId , Employee.Salary) IN
@frederikmuller
@frederikmuller 3 жыл бұрын
the parentheses create a tuple with the two values of DepartmentId and Salary. we're also selecting two columns in the subquery, we're just using the MAX(Salary) instead of Salary. WHERE IN allows us to check whether a row is in a table (in this case whether this tuple is in the table created from the subquery). we're doing all this to make sure we're selecting rows with employees that earn the department highest salary. If we would try to select the employee and the max salary with just one query, we wouldn't be able look up the employee that corresponds to the highest salary.
@pranjalmishra2602
@pranjalmishra2602 3 жыл бұрын
@@frederikmuller OHHH!!! GOT IT, Thank you so much
@isaac5815
@isaac5815 3 жыл бұрын
Thanks! Much appreciated.
@frederikmuller
@frederikmuller 3 жыл бұрын
Glad it helped!
@vaibhavpatharkar6794
@vaibhavpatharkar6794 Жыл бұрын
Great great great. But i think this question was hard category
@imdeepu7855
@imdeepu7855 2 жыл бұрын
WITH highest_sal AS ( select employee,salary,department, row_number over (partition by department_id order by salary desc) as r from employee join departemnt on employee.department_id=department.id ) select department,employee,salary from temp_table where r = 1
@ignaciogonzalez2619
@ignaciogonzalez2619 2 жыл бұрын
Does Anyone know if this would work? SELECT Employee.Name, MAX(Employee.Salary) AS Salary , Department.Name AS Department FROM Employee JOIN Department ON Employee.DepartmentID = Department.ID GROUP BY Department ;
@mritunzaysingh8978
@mritunzaysingh8978 2 жыл бұрын
create table Employees ( id int not null primary key, name nvarchar(20), salary int, department_id int ); create table department ( id int not null primary key, name nvarchar(20) ); insert into Employees values(1,'Joe',70000,1),(2,'Jim',90000,1),(3,'Marry',80000,2),(4,'Sam',60000,2),(5,'Max',90000,1); insert into department values (1,'IT'),(2,'Sales'); select * from Employees; select * from department; with CTE as ( select E.name as emp_name,D.name as dep_name,E.salary as emp_salary,max(E.salary) over(partition by E.department_id order by E.department_id) as rnk from Employees as E join department as D on E.department_id = D.id ) select dep_name,emp_name,emp_salary from CTE where rnk = emp_salary; -- Second Way select dep_name,emp_name,emp_salary from ( select E.name as emp_name,D.name as dep_name,E.salary as emp_salary,max(E.salary) over(partition by E.department_id order by E.department_id) as rnk from Employees as E join department as D on E.department_id = D.id ) A where rnk = emp_salary;
@sanaayakurup5453
@sanaayakurup5453 2 жыл бұрын
My solution with window functions select Department.name as Department ,lookup.name as Employee,salary as Salary from(select name,salary, departmentId, dense_rank() over (partition by departmentId order by salary desc) as ranks from Employee)lookup join Department on lookup.departmentId=Department.id where ranks=1
LeetCode 185: Department Top Three Salaries [SQL]
9:33
Frederik Müller
Рет қаралды 15 М.
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН
The Best Band 😅 #toshleh #viralshort
00:11
Toshleh
Рет қаралды 22 МЛН
LeetCode 1454: Active Users [SQL]
14:01
Frederik Müller
Рет қаралды 6 М.
LeetCode 176: Second Highest Salary [SQL]
7:19
Frederik Müller
Рет қаралды 20 М.
LeetCode 184 - Department Highest Salary (Python and SQL) [MEDIUM]
13:14
Ryan & Matt Data Science
Рет қаралды 246
you need to learn SQL RIGHT NOW!! (SQL Tutorial for Beginners)
24:25
NetworkChuck
Рет қаралды 1,6 МЛН
SQL indexing best practices | How to make your database FASTER!
4:08
Netflix Interview Question - Top 3 Salaries - MySql
8:14
NeetCode
Рет қаралды 5 М.
Querying 100 Billion Rows using SQL, 7 TB in a single table
9:07
Arpit Agrawal (Elastiq.AI)
Рет қаралды 55 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 685 М.
585. Investments in 2016 - LeetCode SQL Solution
10:08
Code with Carter
Рет қаралды 1,1 М.
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН