365 Days of Daily Coding: Day 11

I did an easy Leetcode challenge today. For the moment, I am quite happy with Leetcode cause the challenges although easy is helping me understand the basic concepts very well. I think for the start I will stick to doing easy challenges and then move on to doing medium challenges to curb my frustration of not being able to solve the medium ranked challenges. Doing the easy challenge will first allow me to understand the basic concepts which I can then apply to the medium ranked challenges. Hoping this strategy works well.

Challenge: For today’s challenge, I had to find out Employees who were being paid more than their Managers.

IdNameSalaryManagerId
1Joe700003
2Henry800004
3Sam60000Null
4Max90000Null
Sample of the ‘Employee’ table

Expected Output:

Employee
Joe
Sample of the expected output

My solution:

 SELECT b.Name AS Employee FROM Employee a
 INNER JOIN Employee b
 ON a.Id = b.ManagerId
 WHERE a.Salary < b.Salary;
 Runtime: 756 ms, 15.46% faster 

Breaking down the code:

Query 1

 SELECT * FROM Employee a
 INNER JOIN Employee b
 ON a.Id = b.ManagerId 

Output

IdNameSalaryManagerIdIdNameSalaryManagerId
4Max900002Henry800004
3Sam600001Joe700003
Sample of the output of Query 1

I used an INNER JOIN on self to find the managers with their employees and finally used ‘WHERE’ clause to find manager’s salary that are less than employee’s salary .

Query 2

SELECT * FROM Employee a
INNER JOIN Employee b
ON a.Id = b.ManagerId 
WHERE a.Salary < b.Salary;

Output

IdNameSalaryManagerIdIdNameSalaryManagerId
3Sam600001Joe700003
Sample of output of Query 2

Other Submitted Solution 1:

SELECT Name as Employee 
FROM employee a 
WHERE EXISTS(SELECT 1 FROM employee b 
WHERE a.managerid = b.id 
AND a.salary>b.salary)
Runtime: 932 ms, 5.02% 

The ‘EXISTS’ function used here is an operator is used to test for the existence of any record in a subquery.

Syntax

SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition); 

Breaking down the code

 SELECT * FROM employee a WHERE EXISTS(SELECT * FROM employee b) 

Output

IDNameSalaryManagerId
4Max90000
3Sam60000
2Henry800004
1Joe700003
Sample of the output of Query 1

Query 2

SELECT * FROM employee a 
WHERE EXISTS (SELECT * FROM employee b 
WHERE a.managerid = b.id); 

Output

IdNameSalaryManagerId
2Henry800004
1Joe700003
Sample of the output of Query 2

Query 3

SELECT * FROM employee a 
WHERE EXISTS (SELECT Name FROM employee b 
WHERE a.managerid = b.id 
AND a.salary>b.salary); 

Output

IdNameSalaryManagerId
1Joe700003
Sample of the output of Query 3

Other Submitted Solution 2:

 SELECT emp.name "Employee"
 FROM employee emp, employee mgr
 WHERE mgr.id = emp.managerId
 AND emp.salary > mgr.salary;
 Runtime: 574 ms, 69.88% 

Breaking down the code

Query 1

SELECT * FROM employee emp, employee mgr
ORDER BY emp.Id; 

Output

Id NameSalaryManagerIdIdNameSalaryManagerId
1Joe7000032Henry800004
1Joe7000033Sam60000
1Joe7000034Max90000
1Joe7000031Joe700003
2Henry8000042Henry800004
2Henry8000043Sam60000
2Henry8000044Max90000
2Henry8000041Joe700003
3Sam600002Henry800004
3Sam600003Sam60000
3Sam600004Max90000
3Sam600001Joe700003
4Max900002Henry800004
4Max900003Sam60000
4Max900004Max90000
4Max900001Joe700003
Sample of the output of Query 1

Query 2

 SELECT * FROM employee emp, employee mgr
 WHERE mgr.id = emp.managerId; 

Output

IdNameSalaryManagerIdIdNameSalaryManagerId
1Joe7000033Sam60000
2Henry8000044Max90000
Sample of the output of Query 2

Query 3

 SELECT * FROM employee emp, employee mgr
 WHERE mgr.id = emp.managerId
 AND emp.salary > mgr.salary; 

Output

IdNameSalaryManagerIdIdNameSalaryManagerId
1Joe7000033Sam60000
Sample of the output of Query 3

Leave a comment