Lynda Celli
About
-
Posted Questions
No Question(s) posted yet!
Posted Answers
Answer
Summary: in this tutorial, you will learn how to use the PostgreSQL IN operator in the WHERE clause to check if a value matches any value in a list.
You use IN operator in the WHERE clause to check if a value matches any value in a list of values.
The syntax of the IN operator is as follows:
The IN operator returns true if the value matches any value in the list i.e., value1 , value2 , …
The list of values can be a list of literal values such as numbers, strings or a result of a SELECT statement like this:
The query inside the parentheses is called a subquery, which is a query nested inside another query. Note that you will learn more about the subquery in the subsequent tutorial
Suppose you want to know the rental information of customer id 1 and 2, you can use the IN operator in the WHERE clause as follows:
The following query uses the equal (=) and OR operators instead of the IN operator. It is equivalent to the query above:
The query that uses the IN operator is shorter and more readable than the query that uses equal (=) and OR operators. In addition, PostgreSQL executes the query with the IN operator much faster than the same query that uses a list of OR operators.
You can combine the IN operator with the NOT operator to select rows whose values do not match the values in the list.
For example, the following statement finds all rentals with the customer id is not 1 or 2.
Similar to the IN operator, you can use the not equal (<>) and AND operators to write the NOT IN operator:
This query returns the same output as above query that use the NOT IN operator.
The following query returns a list of customer ids from the rental table with the return date is 2005-05-27:
Because this query returns a list of values, you can use it as the input of the IN operator like this:
For more information on the subquery, check it out the subquery tutorial.
In this tutorial, you have learned how to use the PostgreSQL IN operator to check if a value matches any value in a list of values.
Answer is posted for the following question.
Answer
El cajon is a lovely and accommodating environment, not everyone has bad eggs.
Answer is posted for the following question.
Are people friendly in El Cajon, CA?