我想知道我的代码是否有效? this w3school exercise上的问题3:
SELECT first_name, last_name
FROM employees
WHERE
manager_id IS NOT NULL
AND manager_id IN (
SELECT manager_id
FROM departments
WHERE location_id IN (
SELECT location_id
FROM locations
WHERE country_id='US'
)
)
;
答案 0 :(得分:1)
是的,您的代码应该可以使用。
请注意,SQL中检查manager_id是否不为null的部分是多余的,可以删除(IN子句已经确保已设置manager_id)。
可以使用一组JOIN来优化查询,例如:
SELECT e.first_name, e.last_name
FROM employees e
INNER JOIN departments d ON d.manager_id = e.manager_id
INNER JOIN locations l ON l.department_id = d.department_id AND l.country_id='US'