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.
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 StudentVALUES (89237,'Joseph','CS','SO','19');INSERT INTO StudentVALUES (87496,'Chris','Econ','JR','20');INSERT INTO StudentVALUES (83927,'Lisa','CS','FM','17');INSERT INTO StudentVALUES (83216,'Paul','Psych','SH','18');SELECT * FROM StudentWHERE 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 |