SQL SELECT Statement SELECT IN
The SQL SELECT statement SELECT IN
allows you to filter data from a table based on a set of values. It works like an IN
clause in SQL, where it selects rows that match any value in a specified list.
Syntax
The basic syntax for the SELECT IN
statement is as follows:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, value3, ...);
Example
Suppose we have a table called employees
with columns id
, name
, department
, and salary
. We want to retrieve the names and salaries of all employees who work in the departments 'Sales' or 'Marketing'. We can use the SELECT IN
statement as follows:
SELECT name, salary
FROM employees
WHERE department IN ('Sales', 'Marketing');
Output
The above SQL query will return a table with two columns: name
and salary
. It will only display the rows where the department
column matches 'Sales' or 'Marketing'.
name | salary |
---|---|
John Doe | 50000 |
Jane Doe | 60000 |
Bob Smith | 55000 |
Jill Lee | 65000 |
Explanation
The SELECT IN
statement is used to filter data from a table based on a set of values. It works like an IN
clause, where it selects rows that match any value in a specified list.
In our example, we are retrieving the names and salaries of all employees who work in the departments 'Sales' or 'Marketing'. We specify the columns we want to retrieve in the SELECT
clause, and use the IN
keyword in the WHERE
clause to filter the rows based on the departments.
Use
The SELECT IN
statement can be used to filter data from a table based on a specified list of values. It is useful when you want to retrieve rows that match any value in a given list.
Some common use cases for the SELECT IN
statement include:
- Filtering data based on a list of IDs.
- Retrieving rows that match any value in a set of categories.
- Selecting data from a table based on a list of dates or time periods.
Important Points
Here are some important points to keep in mind when using the SELECT IN
statement:
- The
IN
keyword can be used with any data type that can be compared using=
. This includes strings, numbers, and dates. - You can specify as many values as you like in the list, separated by a comma.
- The
SELECT IN
statement can be combined with other SQL keywords, such asLIKE
,BETWEEN
, andNOT
.
Summary
The SELECT IN
statement is a powerful SQL filter that allows you to retrieve data from a table based on a set of values. You can specify any number of values in the list, and filter rows based on any column that can be compared using =
. The SELECT IN
statement can be used on its own, or in combination with other SQL keywords to create complex queries.