Example 3: Combining IN Clause with Other Conditions
You can combine the IN clause with other conditions to create more complex queries. For example,
Letβs find students from grades βAβ or βBβ who are enrolled in the βMathematicsβ course:
Query
SELECT student_id, student_name, grade
FROM students
WHERE grade IN ('A', 'B') AND course = 'Mathematics';
The result Looks Like:
Explanation: In the above Query, we fetch all students whose grades whether A or B and enrolled in Mathematics courses.
SQL Server IN Operator
IN clause in SQL Server is a logical operator to check a list of values, if available in a specific table column. This keyword helps in filtering data from the query results, based on a set of values. Using this IN clause we can specify multiple values in a WHERE clause, making your queries more readable and efficient. The IN clause can be used as a replacement for multiple OR conditions in a query.
In this article, we will understand the IN clause in detail with its syntax, usage, and benefits with examples to illustrate its functionality.
Syntax:
SELECT column_name1, column_name2, ...
FROM table_name
WHERE column_name IN (value1, value2, ...);
- SELECT: Specifies the column names that we want in our result.
- FROM: Indicates the table from which we are querying the data to retrieve the result set.
- WHERE: Filters the rows based on a specified condition.
- column_name: Specifies the column that we want to filter.
- IN: The keyword used to specify a list of values.