The following exercises by LeetCode: https://leetcode-cn.com/problemset/database/ , are interested can go to the above question brush
Exercise 1: income employees than managers
analysis:
Use sql self-join queries, because employees Id and ManagerId managers are in the same table, use a self-join, a table can be seen as two tables to use a table for employees ygb, a manager for the table jlb;
Query idea is: equal Id record selected employees and managers table ManagerId table, and then screened employees table in these records is greater than the manager Salary Salary table records
SELECT * FROM employee ygb,employee jlb WHERE ygb.ManagerId = jlb.Id
SELECT * FROM employee ygb,employee jlb WHERE ygb.ManagerId = jlb.Id AND ygb.Salary>jlb.Salary
SELECT ygb.name AS Employee FROM employee ygb,employee jlb WHERE ygb.ManagerId = jlb.Id AND ygb.Salary>jlb.Salary
Exercise 2: Remove duplicate e-mail
Analysis: The
first use self-join queries, with its own table and she was more to identify duplicate mailbox
SELECT * FROM Person p1, Person p2
This gave a two Cartesian product of the same epitope, i.e., the combined result of two tables
You can query respectively p1. * And p2. *
Then re-check
SELECT p1.* FROM Person p1, Person p2 WHERE p1.Email = p2.Email AND p1.Id>p2.Id
Then you can delete duplicate data
DELETE p1.* FROM Person p1, Person p2 WHERE p1.Email = p2.Email AND p1.Id>p2.Id
2019-09-04 22:47:00