超过经理收入的员工

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

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

1.用Where语句 

select B.name as Employee from Employee A, Employee B where A.Id = B.ManagerId and A.Salary < B.Salary;

2. Join是一个更常用也更有效的将表连起来的办法,使用 ON 来指明条件。

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

注意事项 :
1、where 后不能跟聚合函数,因为where执行顺序大于聚合函数。
2、where 子句的作用是在对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,条件中不能包含聚组函数,使用where条件显示特定的行。
3、having 子句的作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚组函数,使用having 条件显示特定的组,也可以使用多个分组标准进行分组。

 

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
employee.h: ```cpp #ifndef EMPLOYEE_H #define EMPLOYEE_H #include <string> using namespace std; class Employee { protected: int number; string name; int position; double monthBonus; double monthIncome; public: Employee(int number, string name, int position, double monthBonus); virtual ~Employee(); virtual void calculateMonthIncome() = 0; void showInfo(); }; #endif ``` employee.cpp: ```cpp #include "employee.h" #include <iostream> using namespace std; Employee::Employee(int number, string name, int position, double monthBonus) { this->number = number; this->name = name; this->position = position; this->monthBonus = monthBonus; this->monthIncome = 0; } Employee::~Employee() {} void Employee::showInfo() { cout << "编号:" << number << endl; cout << "姓名:" << name << endl; cout << "职务级别:" << position << endl; cout << "每月固定奖金:" << monthBonus << endl; cout << "每月月收入:" << monthIncome << endl; } ``` manager.h: ```cpp #ifndef MANAGER_H #define MANAGER_H #include "employee.h" class Manager : public Employee { public: Manager(int number, string name, int position, double monthBonus); ~Manager(); virtual void calculateMonthIncome(); }; #endif ``` manager.cpp: ```cpp #include "manager.h" Manager::Manager(int number, string name, int position, double monthBonus) : Employee(number, name, position, monthBonus) {} Manager::~Manager() {} void Manager::calculateMonthIncome() { monthIncome = 12000 * (3 - position + 1) / 3 + monthBonus + 3500; } ``` developer.h: ```cpp #ifndef DEVELOPER_H #define DEVELOPER_H #include "employee.h" class Developer : public Employee { private: int workHours; public: Developer(int number, string name, int position, double monthBonus, int workHours); ~Developer(); virtual void calculateMonthIncome(); }; #endif ``` developer.cpp: ```cpp #include "developer.h" Developer::Developer(int number, string name, int position, double monthBonus, int workHours) : Employee(number, name, position, monthBonus) { this->workHours = workHours; } Developer::~Developer() {} void Developer::calculateMonthIncome() { if (position == 1) { monthIncome = 6000 * (3 - position + 1) / 3 + monthBonus + workHours * 40 + 500; } else if (position == 2) { monthIncome = 6000 * (3 - position + 1) / 3 + monthBonus + workHours * 40; } else { monthIncome = 6000 * (3 - position + 1) / 3 + monthBonus; } } ``` main.cpp: ```cpp #include "employee.h" #include "manager.h" #include "developer.h" #include <iostream> using namespace std; int main() { Employee *e1 = new Manager(1001, "张三", 1, 5000); Employee *e2 = new Manager(1002, "李四", 2, 3000); Employee *e3 = new Developer(2001, "王五", 3, 2000, 80); Employee *e4 = new Developer(2002, "赵六", 1, 1000, 120); e1->calculateMonthIncome(); e2->calculateMonthIncome(); e3->calculateMonthIncome(); e4->calculateMonthIncome(); e1->showInfo(); e2->showInfo(); e3->showInfo(); e4->showInfo(); delete e1; delete e2; delete e3; delete e4; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值