How to use IN operator in SQL

SQL (Structured Query Language) comes with many operators that allow for flexible queries. The IN operator is synonymous with multiple OR conditions. It is used in the WHERE clause.

Let’s see an example.

Table 'Student'

s_ID

s_name

s_major

s_level

s_age

89237

Joseph

CS

SO

19

87496

Chris

Econ

JR

20

83927

Lisa

CS

FM

17

83216

Paul

Psych

SH

18

Suppose we only wish to see data on students that are either seniors or juniors. We would first create the table ‘Student’ using CREATE TBALE, and insert the values accordingly. We then use the IN operator in the WHERE clause to specify that the student level must be ‘SO’ or ‘JR’.

CREATE TABLE Student (
s_ID int,
s_name varchar(255),
s_major varchar(255),
s_level varchar(255),
s_age int
);
INSERT INTO Student
VALUES (89237,'Joseph','CS','SO','19');
INSERT INTO Student
VALUES (87496,'Chris','Econ','JR','20');
INSERT INTO Student
VALUES (83927,'Lisa','CS','FM','17');
INSERT INTO Student
VALUES (83216,'Paul','Psych','SH','18');
SELECT * FROM Student
WHERE s_level IN ('SO','JR');

The above query returns the following table. The only rows that are returned have an s_level of either ‘SO’ or ‘JR’, meaning we have achieved our objective and now have data on seniors and juniors exclusively.

s_ID

s_name

s_major

s_level

s_age

89237

Joseph

CS

SO

19

87496

Chris

Econ

JR

20

Free Resources