SQL
The IN
command, when combined with the SELECT
statement, enables us to filter data based on a predefined list of values
The basic syntax for using the IN
command in a SELECT
statement is as follows:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
SELECT
statement specifies the columns you want to retrieve from the database.FROM
clause identifies the table from which to retrieve the data.WHERE
clause, combined with IN
, filters the results based on the specified list of values.Consider a simple database table named Students
:
StudentID | Name | Grade |
---|---|---|
1 | John | A |
2 | Sarah | B |
3 | Michael | C |
4 | Emily | A |
5 | Robert | B |
To retrieve information about students with specific grades (A and B), we can use the SELECT IN
command as follows:
SELECT StudentID, Name, Grade
FROM Students
WHERE Grade IN ('A', 'B');
Output:
StudentID | Name | Grade |
---|---|---|
1 | John | A |
2 | Sarah | B |
4 | Emily | A |
5 | Robert | B |
We can enhance the power of the IN
command by combining it with other logical operators like AND
or OR
for more complex queries.