IN and BETWEEN Operators
Consider the case of a typical e-commerce scenario. Users generally search for the products that belong to a list of brands, or the products that lie within a particular price range.
In such scenarios, we use the IN operator to check if a value is present in the list of values. And, BETWEEN operator is used to check if a particular value exists in the given range.
Let’s learn about these operators in detail using the following database.
Database The database contains a
IN Operator
Retrieves the corresponding rows from the table if the value of column(c1) is present in the given values(v1,v2,..).
Syntax
Example
Get the details of all the products from
Output
name | category | price | brand | rating |
---|---|---|---|---|
Blue Shirt | Clothing | 750 | Denim | 3.8 |
Blue Jeans | Clothing | 800 | Puma | 3.6 |
Black Jeans | Clothing | 750 | Denim | 4.5 |
... | ... | ... | ... | ... |
Try it Yourself!
- Get all the products fromproducttable, that belong to "Britannia", "Lay's", "Cadbury" brands from the "Food" category.
BETWEEN Operator
Retrieves all the rows from table that have cloumn(c1) value present between the given range(v1 and v2).
Syntax
Example
Find the products with
Output
name | price | brand |
---|---|---|
Blue Shirt | 1000 | Puma |
Smart Cam | 2600 | Realme |
Realme Smart Band | 3000 | Realme |
Possible Mistakes
- When using theBETWEENoperator, the first value should be less than second value. If not, we'll get an incorrect result depending on the DBMS.
Output
name | price | brand |
---|---|---|
- We have to give both lower limit and upper limit while specifying range.
- The data type of the column for which we're using theBETWEENoperator must match with the data types of the lower and upper limits.
Output
name | price | brand |
---|---|---|
Try it Yourself!
- Get all the products fromproducttable withratinggreater than 4.3 and less than 4.8