AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
SQL IN |
SQL > SQL Commands >
In
The IN operator in SQL filters the result set based on a list of discrete values. The list of discrete values can be simply be listed out or is provided by a separate SELECT statement (this is called a subquery). The IN operator is always used with the WHERE clause. SyntaxBelow is the syntax for the IN operator when the possible values are listed out directly. SELECT "column_name"
FROM "table_name" WHERE "column_name" IN ('value1', 'value2', ...); The number of values in the parenthesis can be one or more, with each values separated by comma. Values can be numerical or string characters. If there is only one value inside the parenthesis, this commend is equivalent to, WHERE "column_name" = 'value1'
The syntax for the IN operator in a subquery construct is as follows: SELECT "column_name"
FROM "table_name" WHERE "column_name" IN ( [SELECT STATEMENT] ); In the above construct, the subquery statement must retrieve only one column, as the values returned from that column is what will be used to filter the results. If two or more columns are selected in the subquery, an error will result. Please note that the IN operator cannot be used if the filtering criteria is a continuous range. For example, if we are looking for any value that is between 0 and 1, we cannot use the IN operator because it is not possible to list every possible value between 0 and 1. ExampleWe use the following table for our example. Table Store_Information
To select all records for the Los Angeles and the San Diego stores in Table Store_Information, we key in, SELECT *
FROM Store_Information WHERE Store_Name IN ('Los Angeles', 'San Diego'); Result:
For an example of the IN operator used in conjunction with a separate SELECT statement, please see the Subquery section. ExercisesFor these exercises, assume we have a table called Users with the following data: Table Users
1. Which of the following SQL statement is valid? (There can be more than one answer)
2. How many records will be returned by the following query?
3. How many records will be returned by the following query?
|
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.