MySQL查询所有员工的邮箱_mysql数据库查询练习二-【比经理工资还高员工姓名】【查找重复的邮箱】...

1.查询工资比他经理的工资还高的员工的姓名

Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。

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

| Id | Name | Salary | ManagerId |

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

| 1 | Joe | 70000 | 3 |

| 2 | Henry | 80000 | 4 |

| 3 | Sam | 60000 | NULL |

| 4 | Max | 90000 | NULL |

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

给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。

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

| Employee |

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

| Joe |

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

s1表: s2表:

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

| Id | Name | Salary | ManagerId | | Id | Name | Salary | ManagerId |

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

| 1 | Joe | 70000 | 3 | | 1 | Joe | 70000 | 3 |

| 2 | Henry | 80000 | 4 | | 2 | Henry | 80000 | 4 |

| 3 | Sam | 60000 | NULL | | 3 | Sam | 60000 | NULL |

| 4 | Max | 90000 | NULL | | 4 | Max | 90000 | NULL |

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

2.解答

(1)方法一

8068971bd2fc35371b4b09c809cc7926.png

select s1.Name as Employee from Employee as s1,Employee as s2 where s1.ManagerId=s2.Id and s1.Salary > s2.Salary

(2)方法二

f450ba7254d8dbf637bd17b34db60844.png

7518b3fa0a2c113efb0801d6b8669e26.png

1c0c9f1a7ef9e24102b35c64dfd29f77.png

Select s1.Name as Employee from Employee s1 join Employee s2 on s1.ManagerId = s2.Id and s1.Salary > s2.Salary

Select s1.Name as Employee from Employee s1 join Employee s2 on s1.ManagerId = s2.Id where s1.Salary > s2.Salary

(3)方法三:这种方法涉及到子查询,所以查询效率低

5d17077ab03c7c4a1c8f60fa31b9156a.png

9f29c96a8d167b8eeb380cf2ca86de35.png

select s1.Name as Employee from Employee as s1 where s1.Salary > (select s2.Salary from Employee as s2 where s1.ManagerId = s2.Id);

3.编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。

编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。

示例:

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

| Id | Email |

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

| 1 | a@b.com |

| 2 | c@d.com |

| 3 | a@b.com |

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

根据以上输入,你的查询应返回以下结果:

+---------+

| Email |

+---------+

| a@b.com |

+---------+

说明:所有电子邮箱都是小写字母。

4.解答

(1)方法一:

select distinct p1.Email from Person as p1,Person as p2 where p1.Email=p2.Email and p1.Id !=p2.Id;

(2)方法二:

select Email

from Person

group by Email

having count(*) > 1;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值