-- Select the employee ID, employee name, and department columnsSELECT employee_id, employee_name, department
FROM employees
-- Filter the results to include only employees whose department is 'Sales' and 'HR'WHERE department IN('Sales','HR');技术支持
-- Select the columns you want to retrieveSELECT column_name(s)-- Specify the table from which to retrieve the dataFROM table_name
-- Filter the results to include only those rows where the column's value matches any value in the specified listWHERE column_name IN(value1, value2,...);技术支持
下面是一个在WHERE子句中使用IN运算符的实际示例。
-- Select the employee ID, employee name, and department columnsSELECT employee_id, employee_name, department
FROM employees
-- Filter the results to include only employees whose department is 'Sales' and 'HR'WHERE department IN('Sales','HR');技术支持
-- Select all columns from employees tableSELECT employee_id, employee_name, department
FROM employees
-- Filter to include only 'Sales', 'HR', and 'IT' departmentsWHERE department IN('Sales','HR','IT');技术支持
-- Main query to select employee_id and name from the employees tableSELECT employee_id, name
FROM employees
-- Filter employees based on multiple department names using OR conditionsWHERE department ='Sales'OR department ='Marketing'OR department ='HR';技术支持
下面是使用IN运算符的查询。请注意,移除重复的OR后,查询更为简洁。
-- Main query to select employee_id and name from the employees tableSELECT employee_id, name
FROM employees
-- Filter employees based on a list of department names using the IN operatorWHERE department IN('Sales','Marketing','HR');技术支持
-- Main query to select employee_id and name from the employees tableSELECT employee_id, name
FROM employees
-- Filter employees based on department_id using a subqueryWHERE department_id IN(-- Subquery to get department_id for the "Sales" departmentSELECT department_id
FROM departments
WHERE department_name ='Sales');技术支持
-- Select employee ID and name for employees in departments located in New York using EXISTSSELECT employee_id, employee_name
FROM employees e
-- Check if there exists a row in the departments tableWHEREEXISTS(-- Select 1 is used here as a placeholder; it simply checks for existenceSELECT1FROM departments d
-- Match the department_id in employees with department_id in departmentsWHERE d.department_id = e.department_id
-- Filter for departments located in New YorkAND d.location ='New York');技术支持
-- Select employee ID and name for employees in departments located in New York using JOINSELECT e.employee_id, e.employee_name
FROM employees e
-- Join the employees table with the departments table on department_idJOIN departments d ON e.department_id = d.department_id
-- Filter the results to include only departments located in New YorkWHERE d.location ='New York';技术支持
-- Define a CTE (Common Table Expression) to store the list of department IDs located in New YorkWITH DepartmentList AS(SELECT department_id
FROM departments
WHERE location ='New York')-- Select employee ID and name for employees who belong to the departments listed in the CTESELECT e.employee_id, e.employee_name
FROM employees e
-- Filter the results to include only employees whose department_id is in the list of department IDs from the CTEWHERE e.department_id IN(SELECT department_id FROM DepartmentList);技术支持
SQL 的
IN运算符是一种根据一组取值来筛选数据的实用工具。在较长的查询中使用时,IN运算符有助于简化复杂查询并提升可读性。如果您正准备从事数据相关工作,建议学习 DataCamp 的Introduction to SQL和SQL Fundamentals课程,掌握用 SQL 筛选数据的基础。想要快速复习,可查看我们的SQL 基础速查表。
什么是 SQL IN 运算符?
SQL 的
IN运算符是用于在查询中筛选数据的强大工具。它通过仅返回满足指定条件的行来精炼查询结果。需要注意的是,
IN运算符应放在WHERE子句之后,用于在多条件下返回结果。因此,当筛选的数据列可能取不同值时,可以使用IN运算符。在上面的示例中,我们从
employees表选择employee_id、employee_name和department列。但只返回部门为Sales和HR的员工所在的行。IN运算符可用于筛选不同的 SQL 数据类型,包括字符串、数字和日期。筛选行时,请务必确保被引用的列具有合适的数据类型。SQL IN 运算符的语法
IN运算符用于筛选取值,语法简洁。它位于WHERE子句中,可从某一列的取值列表中过滤出多个值。IN运算符的总体语法如下所示。下面是一个在
WHERE子句中使用IN运算符的实际示例。需要记住的要点
IN运算符出现在SELECT语句的WHERE子句中。这种位置使查询可以基于多个取值来筛选行。在 SQL 查询中使用IN运算符能够简化复杂查询并提升可读性。IN运算符在不同数据库中是通用的,包括 SQL Server、PostgreSQL、MySQL 和 Oracle 等。按照惯例使用大写书写IN运算符有助于提高可读性。IN 运算符的常见用例
SQL 的
IN运算符适用于多种数据分析场景。本节将探讨一些最常见的用法,包括筛选数据、处理多值条件以及使用子查询。筛选数据
IN运算符用于筛选记录并返回一组指定的取值。例如,下面的查询将从employees表选取所有列,并返回部门为Sales、HR和IT的员工所在的行。处理多值条件
IN运算符也可用于筛选包含多个取值的记录。在使用多个OR条件筛选数据时,这一用法尤为实用。因此,IN运算符提供了一种更简洁的方式来提升查询可读性。下面是使用
OR而非IN运算符时的查询写法。下面是使用
IN运算符的查询。请注意,移除重复的OR后,查询更为简洁。子查询
您也可以使用
IN运算符在子查询中筛选结果。此操作允许您依据另一查询或关联数据来过滤记录。在下例中,子查询从departments表中选出Sales部门的department_id。因此,主查询将返回employees表中符合该子查询条件的employee_id与姓名。局限性与替代方案
尽管
IN运算符在 SQL 查询中使用简便,但其应用仍有一些局限,包括:IN运算符可能导致性能下降,拖慢查询执行。IN运算符对NULL值的处理并不高效,可能产生不可预期的结果。这是因为NULL值并未包含在取值列表中。IN运算符显得受限。通常只能借助子查询来筛选有限的列。为避免
IN运算符的这些限制,可考虑以下替代方案:使用 EXISTS 运算符
EXISTS运算符用于检查子查询中是否存在指定记录。若使用得当,该运算符通常更高效、更快速。使用 JOIN
在执行复杂筛选、且需要基于多个关联列筛选数据时,SQL 的
JOIN子句效率更高。下例中,JOIN子句将 employees 表与 departments 表按department_id连接,以筛选居住在纽约的员工记录。使用公用表表达式(CTE)或临时表
如果您需要处理包含大量列表的表,可考虑使用 CTE 先存储取值列表,再进行查询。此方法有助于提升查询性能与可读性。
想要学习更多高级筛选技巧,建议学习 DataCamp 的Associate Data Analyst in SQL职业路径。您也可以查看 DataCamp 的Intermediate SQL Server课程,其中包含关于公用表表达式及其他高级主题的章节。
结语
理解 SQL 的
IN运算符对数据从业者非常重要,它能帮助您在分析中筛选并转换数据。为提升在IN运算符方面的 SQL 技能,建议您使用不同的数据集进行练习。若您希望将 SQL 知识提升到中级水平,推荐学习Reporting in SQL课程,探索其他复杂的查询技巧。此外,SQL Associate Certification认证将通过展示您在高级分析中运用 SQL 的实战能力,助您在众多数据分析师中脱颖而出。