IN Operator

SQL · Filtering Rows

Theory

IN checks whether a value matches any item in a list. dept IN ('IT', 'HR', 'Finance') is shorthand for dept = 'IT' OR dept = 'HR' OR dept = 'Finance', and it is easier to read and extend. The list can also be a subquery: WHERE id IN (SELECT customer_id FROM orders) keeps customers who have placed orders.

NOT IN keeps values that are not in the list, but it hides a trap: if the list or subquery contains a NULL, NOT IN returns no rows at all, because x <> NULL is UNKNOWN. When a subquery might return NULLs, use NOT EXISTS or filter NULLs out. Values in the list must be the same type as the column. Writing NULL inside an IN list does not match NULL rows; use IS NULL for that.

Why we use it

Filtering by a set of allowed values is very common: a few cities, a list of IDs, a set of statuses. IN replaces long OR chains and lets the list come straight from another query.

When to use it

Use IN for a fixed list of values or a subquery returning one column. For ranges use BETWEEN. For anti-matches against a subquery that may contain NULLs, prefer NOT EXISTS over NOT IN.

How it works

  1. Read a row
  2. Take the column value
  3. Does it equal any item in the list? Yes: Keep the row. No: Discard the row.
  4. Repeat for every row
  5. Pass kept rows on

Syntax

SELECT columns FROM table
WHERE column IN (val1, val2, ...);

Example

SELECT * FROM employees WHERE department IN ('IT','HR','Finance');

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic