1. 显示所有员工的姓名,部门号和部门名称。(所有,外连接问题)

      1. SELECT last_name, e.department_id, department_name
      2. FROM employees e
      3. LEFT OUTER JOIN departments d
      4. ON e.`department_id` = d.`department_id`;
    2. 查询 90 号部门员工的 job_id 和 90 号部门的 location_id

      1. SELECT job_id, location_id
      2. FROM employees e, departments d
      3. WHERE e.`department_id` = d.`department_id`
      4. AND e.`department_id` = 90;
      1. SELECT job_id, location_id
      2. FROM employees e
      3. JOIN departments d
      4. ON e.`department_id` = d.`department_id`
      5. WHERE e.`department_id` = 90;
    3. 选择所有有奖金的员工的 last_name , department_name , location_id , city

      1. SELECT last_name , department_name , d.location_id , city
      2. FROM employees e
      3. LEFT OUTER JOIN departments d
      4. ON e.`department_id` = d.`department_id`
      5. LEFT OUTER JOIN locations l
      6. ON d.`location_id` = l.`location_id`
      7. WHERE commission_pct IS NOT NULL;
    4. 选择 city 在 Toronto 工作的员工的 last_name,job_id,department_id,department_name

      1. SELECT last_name , job_id , e.department_id , department_name
      2. FROM employees e, departments d, locations l
      3. WHERE e.`department_id` = d.`department_id`
      4. AND d.`location_id` = l.`location_id`
      5. AND city = 'Toronto';
      1. SELECT last_name , job_id , e.department_id , department_name
      2. FROM employees e
      3. JOIN departments d
      4. ON e.`department_id` = d.`department_id`
      5. JOIN locations l
      6. ON l.`location_id` = d.`location_id`
      7. WHERE l.`city` = 'Toronto';
    5. 查询员工所在的部门名称、部门地址、姓名、工作、工资,其中员工所在部门的部门名称为 Executive

      1. SELECT department_name, street_address, last_name, job_id, salary
      2. FROM employees e JOIN departments d
      3. ON e.department_id = d.department_id
      4. JOIN locations l
      5. ON d.`location_id` = l.`location_id`
      6. WHERE department_name = 'Executive';
    6. 选择指定员工的姓名,员工号,以及他的管理者的姓名和员工号,结果类似于下面的格式

      1. employees Emp# manager Mgr#
      2. kochhar 101 king 100
      1. SELECT emp.last_name employees, emp.employee_id "Emp#", mgr.last_name manager,
      2. mgr.employee_id "Mgr#"
      3. FROM employees emp
      4. LEFT OUTER JOIN employees mgr
      5. ON emp.manager_id = mgr.employee_id;
    7. 查询哪些部门没有员工

      1. SELECT d.department_id
      2. FROM departments d LEFT JOIN employees e
      3. ON e.department_id = d.`department_id`
      4. WHERE e.department_id IS NULL;
      1. # 子查询
      2. SELECT department_id
      3. FROM departments d
      4. WHERE NOT EXISTS (
      5. SELECT *
      6. FROM employees e
      7. WHERE e.`department_id` = d.`department_id`
      8. );
    8. 查询哪个城市没有部门

      1. SELECT l.location_id,l.city
      2. FROM locations l LEFT JOIN departments d
      3. ON l.`location_id` = d.`location_id`
      4. WHERE d.`location_id` IS NULL;
    9. 查询部门名为 Sales 或 IT 的员工信息

      1. SELECT employee_id,last_name,department_name
      2. FROM employees e,departments d
      3. WHERE e.department_id = d.`department_id`
      4. AND d.`department_name` IN ('Sales','IT');