SELECT Statements SQL Topic exerciseEasyVerified answerSQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

SELECT with IN Operator

Return employees in departments 10, 20, or 30.

  • Filtering

Exercise brief

Understand the request

Workforce planner A cross-functional review only covers IT, HR, and Finance.

Show first_name, last_name, and department_id for employees who belong to IT (10), HR (20), or Finance (30). Use the IN operator instead of three separate OR conditions.

Return

  • Return first_name, last_name, and department_id.

Constraints

  • Use the IN operator in the filter.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

employees

  • first_nameVARCHAR(50)
  • last_nameVARCHAR(50)
  • department_idINTEGER

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

IN takes a parenthesised list of values. The row passes if the column equals any one of them.

Hint 2

WHERE department_id IN (10, 20, 30) is equivalent to WHERE department_id = 10 OR department_id = 20 OR department_id = 30.

Hint 3

Project the two name fields and department_id, then filter department_id with a three-value IN list.

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
SELECT first_name, last_name, department_id FROM employees WHERE department_id IN (10, 20, 30);

Why this works

IN is syntactic sugar over a chain of equality ORs. It is cleaner, less bug-prone, and easier for the optimiser. Use NOT IN for the inverse — but be careful: NOT IN with a list that contains NULL returns no rows because of three-valued logic.

Expected result

Use this output to verify values, aliases, ordering, and row count.

first_namelast_namedepartment_id
JohnSmith10
AliceJohnson10
BobWilson10
CarolDavis20
DavidBrown30
GraceWhite10
HenryClark20
IvyMartinez30

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.