0% found this document useful (0 votes)
51 views

Leet Code

1. Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id. 2. Write a SQL query that finds employees who earn more than their managers by joining the Employee table to itself on the ManagerId and Id columns and checking that the employee's salary is greater than the manager's salary. 3. Write a SQL query to get the second highest salary from the Employee table.

Uploaded by

Xinyuan Gao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Leet Code

1. Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id. 2. Write a SQL query that finds employees who earn more than their managers by joining the Employee table to itself on the ManagerId and Id columns and checking that the employee's salary is greater than the manager's salary. 3. Write a SQL query to get the second highest salary from the Employee table.

Uploaded by

Xinyuan Gao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Write a SQL query to delete all duplicate email

LeetCode entries in a table named Person, keeping only unique


1/12/2020 emails based on its smallest Id.

1. +----+------------------+

The Employee table holds all employees | Id | Email |


including their managers. Every employee has
an Id, and there is also a column for the +----+------------------+
manager Id.
| 1 | [email protected] |
+----+-------+--------+-----------+
| 2 | [email protected] |
| Id | Name | Salary | ManagerId |
| 3 | [email protected] |
+----+-------+--------+-----------+
+----+------------------+

| 1 | Joe | 70000 | 3 | Id is the primary key column for this table.

| 2 | Henry | 80000 | 4 | For example, after running your query, the


above Person table should have the following rows:
| 3 | Sam | 60000 | NULL |
+----+------------------+
| 4 | Max | 90000 | NULL |
| Id | Email |
+----+-------+--------+-----------+
+----+------------------+
Given the Employee table, write a SQL query
that finds out employees who earn more than | 1 | [email protected] |
their managers. For the above table, Joe is the
only employee who earns more than his | 2 | [email protected] |
manager.
+----+------------------+
+----------+
Note: Your output is the whole Person table after
executing your sql. Use delete statement.
| Employee |

+----------+

| Joe | DELETE p1 FROM Person p1, Person p2


WHERE p1.Email = p2.Email
+----------+ AND p1.Id > p2.Id;

SELECT
a.NAME AS Employee
FROM Employee AS a JOIN Employee AS b
ON a.ManagerId = b.Id
AND a.Salary > b.Salary;

3.

Write a SQL query to get the second highest


2. Delete Duplicate salary from the Employee table.
+----+--------+

| Id | Salary |

+----+--------+

| 1 | 100 |

| 2 | 200 |

| 3 | 300 |

+----+--------+

For example, given the above Employee table,


the query should return 200 as the second
highest salary. If there is no second highest
salary, then the query should return null.

+---------------------+

| SecondHighestSalary |

+---------------------+

| 200 |

+---------------------+

You might also like