SELECT IN command in SQL

SQL

The IN command, when combined with the SELECT statement, enables us to filter data based on a predefined list of values

Syntax:

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, ...);

How It Works:

  • The SELECT statement specifies the columns you want to retrieve from the database.
  • The FROM clause identifies the table from which to retrieve the data.
  • The WHERE clause, combined with IN, filters the results based on the specified list of values.

Example:

Consider a simple database table named Students:

StudentIDNameGrade
1JohnA
2SarahB
3MichaelC
4EmilyA
5RobertB

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:

StudentIDNameGrade
1JohnA
2SarahB
4EmilyA
5RobertB

We can enhance the power of the IN command by combining it with other logical operators like AND or OR for more complex queries.