跳至主要内容

如何使用 SQL IN 运算符:高效筛选技巧指南

SQL IN 运算符可将查询结果限定为仅包含满足指定条件的行。探索其全部用法及替代方案。
· 8分钟 阅读

使用 AI 探索

SQL 的IN运算符是一种根据一组取值来筛选数据的实用工具。在较长的查询中使用时,IN运算符有助于简化复杂查询并提升可读性。

如果您正准备从事数据相关工作,建议学习 DataCamp 的Introduction to SQL和SQL Fundamentals课程,掌握用 SQL 筛选数据的基础。想要快速复习,可查看我们的SQL 基础速查表。

什么是 SQL IN 运算符?

SQL 的IN运算符是用于在查询中筛选数据的强大工具。它通过仅返回满足指定条件的行来精炼查询结果。

需要注意的是,IN运算符应放在WHERE子句之后,用于在多条件下返回结果。因此,当筛选的数据列可能取不同值时,可以使用IN运算符。

-- Select the employee ID, employee name, and department columns
SELECT 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');

在上面的示例中,我们从employees表选择employee_id、employee_name和department列。但只返回部门为Sales和HR的员工所在的行。

IN运算符可用于筛选不同的 SQL 数据类型,包括字符串、数字和日期。筛选行时,请务必确保被引用的列具有合适的数据类型。

SQL IN 运算符的语法

IN运算符用于筛选取值,语法简洁。它位于WHERE子句中,可从某一列的取值列表中过滤出多个值。IN运算符的总体语法如下所示。

-- Select the columns you want to retrieve
SELECT column_name(s)
-- Specify the table from which to retrieve the data
FROM table_name
-- Filter the results to include only those rows where the column's value matches any value in the specified list
WHERE column_name IN (value1, value2, ...);

下面是一个在WHERE子句中使用IN运算符的实际示例。

-- Select the employee ID, employee name, and department columns
SELECT 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');

需要记住的要点

IN运算符出现在SELECT语句的WHERE子句中。这种位置使查询可以基于多个取值来筛选行。在 SQL 查询中使用IN运算符能够简化复杂查询并提升可读性。

IN运算符在不同数据库中是通用的,包括 SQL Server、PostgreSQL、MySQL 和 Oracle 等。按照惯例使用大写书写IN运算符有助于提高可读性。

IN 运算符的常见用例

SQL 的IN运算符适用于多种数据分析场景。本节将探讨一些最常见的用法,包括筛选数据、处理多值条件以及使用子查询。

筛选数据

IN运算符用于筛选记录并返回一组指定的取值。例如,下面的查询将从employees表选取所有列,并返回部门为Sales、HR和IT的员工所在的行。

-- Select all columns from employees table
SELECT employee_id, employee_name, department
FROM employees
-- Filter to include only 'Sales', 'HR', and 'IT' departments
WHERE department IN ('Sales', 'HR', 'IT');

处理多值条件

IN运算符也可用于筛选包含多个取值的记录。在使用多个OR条件筛选数据时,这一用法尤为实用。因此,IN运算符提供了一种更简洁的方式来提升查询可读性。

下面是使用OR而非IN运算符时的查询写法。

-- Main query to select employee_id and name from the employees table
SELECT employee_id, name
FROM employees
-- Filter employees based on multiple department names using OR conditions
WHERE department = 'Sales'
OR department = 'Marketing'
OR department = 'HR';

下面是使用IN运算符的查询。请注意,移除重复的OR后,查询更为简洁。

-- Main query to select employee_id and name from the employees table
SELECT employee_id, name
FROM employees
-- Filter employees based on a list of department names using the IN operator
WHERE department IN ('Sales', 'Marketing', 'HR');

子查询

您也可以使用IN运算符在子查询中筛选结果。此操作允许您依据另一查询或关联数据来过滤记录。在下例中,子查询从departments表中选出Sales部门的department_id。因此,主查询将返回employees表中符合该子查询条件的employee_id与姓名。

-- Main query to select employee_id and name from the employees table
SELECT employee_id, name
FROM employees
-- Filter employees based on department_id using a subquery
WHERE department_id IN (
    -- Subquery to get department_id for the "Sales" department
    SELECT department_id
    FROM departments
    WHERE department_name = 'Sales'
);

局限性与替代方案

尽管IN运算符在 SQL 查询中使用简便,但其应用仍有一些局限,包括:

  • 大型列表的性能问题:在包含大量取值的列表中使用IN运算符可能导致性能下降,拖慢查询执行。
  • 处理 NULL 值:IN运算符对NULL值的处理并不高效,可能产生不可预期的结果。这是因为NULL值并未包含在取值列表中。
  • 灵活性有限:在处理包含多列的大型数据集时,IN运算符显得受限。通常只能借助子查询来筛选有限的列。

为避免IN运算符的这些限制,可考虑以下替代方案:

使用 EXISTS 运算符

EXISTS运算符用于检查子查询中是否存在指定记录。若使用得当,该运算符通常更高效、更快速。

-- Select employee ID and name for employees in departments located in New York using EXISTS
SELECT employee_id, employee_name
FROM employees e
-- Check if there exists a row in the departments table
WHERE EXISTS (
    -- Select 1 is used here as a placeholder; it simply checks for existence
    SELECT 1
    FROM departments d
    -- Match the department_id in employees with department_id in departments
    WHERE d.department_id = e.department_id
    -- Filter for departments located in New York
    AND d.location = 'New York'
);

使用 JOIN

在执行复杂筛选、且需要基于多个关联列筛选数据时,SQL 的JOIN子句效率更高。下例中,JOIN子句将 employees 表与 departments 表按department_id连接,以筛选居住在纽约的员工记录。

-- Select employee ID and name for employees in departments located in New York using JOIN
SELECT e.employee_id, e.employee_name
FROM employees e
-- Join the employees table with the departments table on department_id
JOIN departments d ON e.department_id = d.department_id
-- Filter the results to include only departments located in New York
WHERE d.location = 'New York';

使用公用表表达式(CTE)或临时表

如果您需要处理包含大量列表的表,可考虑使用 CTE 先存储取值列表,再进行查询。此方法有助于提升查询性能与可读性。

-- Define a CTE (Common Table Expression) to store the list of department IDs located in New York
WITH 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 CTE
SELECT 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 CTE
WHERE e.department_id IN (SELECT department_id FROM DepartmentList);

想要学习更多高级筛选技巧,建议学习 DataCamp 的Associate Data Analyst in SQL职业路径。您也可以查看 DataCamp 的Intermediate SQL Server课程,其中包含关于公用表表达式及其他高级主题的章节。

结语

理解 SQL 的IN运算符对数据从业者非常重要,它能帮助您在分析中筛选并转换数据。为提升在IN运算符方面的 SQL 技能,建议您使用不同的数据集进行练习。

若您希望将 SQL 知识提升到中级水平,推荐学习Reporting in SQL课程,探索其他复杂的查询技巧。此外,SQL Associate Certification认证将通过展示您在高级分析中运用 SQL 的实战能力,助您在众多数据分析师中脱颖而出。

Allan Ouko

作者

Allan Ouko

数据科学技术写作者,具备数据分析、商业智能和数据科学的一线实践经验。我撰写以行业为导向的实用内容,涵盖 SQL、Python、Power BI、Databricks 和数据工程,并以真实的分析工作为基础。我的写作兼顾技术深度与业务影响,帮助专业人士将数据转化为自信的决策。

常见问题

我可以用 IN 运算符筛选不同的数据类型吗?

IN 运算符可用于筛选不同的 SQL 数据类型,包括字符串、数字和日期。

可以在子查询中使用 IN 运算符吗?

IN 运算符可在子查询中使用,是从主查询中筛选数据的一种高级方法。

IN 运算符的替代方案有哪些?

用于替代 IN 运算符的最佳方案包括 EXISTS 运算符、JOIN 子句与公用表表达式(CTE)。

哪些数据库兼容 IN 运算符?

常见数据库均支持 IN 运算符,包括 SQL Server、MySQL、PostgreSQL 与 Oracle 等。

主题