单行比较操作符

操作符 含义
= equal to
> greater than
>= greater than or equal to
> less than
<= less than or equal to
<> not equal to

案例:

查询工资大于149号员工工资的员工的信息

  1. SELECT employee_id,last_name,salary
  2. FROM employees
  3. WHERE salary > (
  4. SELECT salary
  5. FROM employees
  6. WHERE employee_id = 149
  7. );

返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id和工资

  1. SELECT last_name,job_id,salary
  2. FROM employees
  3. WHERE job_id = (
  4. SELECT job_id
  5. FROM employees
  6. WHERE employee_id = 141
  7. )
  8. AND salary > (
  9. SELECT salary
  10. FROM employees
  11. WHERE employee_id = 143
  12. );

返回公司工资最少的员工的last_name,job_id和salary

  1. SELECT last_name,job_id,salary
  2. FROM employees
  3. WHERE salary = (
  4. SELECT MIN(salary)
  5. FROM employees
  6. );

查询与141号员工的manager_id和department_id相同的其他员工的employee_id,manager_id,department_id。

  1. SELECT employee_id,manager_id,department_id
  2. FROM employees
  3. WHERE manager_id = (
  4. SELECT manager_id
  5. FROM employees
  6. WHERE employee_id = 141
  7. )
  8. AND department_id = (
  9. SELECT department_id
  10. FROM employees
  11. WHERE employee_id = 141
  12. )
  13. AND employee_id <> 141;
  14. # 法二:成对查询
  15. SELECT employee_id,manager_id,department_id
  16. FROM employees
  17. WHERE (manager_id,department_id) = (
  18. SELECT manager_id,department_id
  19. FROM employees
  20. WHERE employee_id = 141
  21. )
  22. AND employee_id <> 141;