+ 1
How to use like,in and between in sql ? Give answer with code example
sql
2 Answers
+ 2
LIKE, IN, and BETWEEN are used for filtering records, all these three are used in conjunction with the WHERE clause.
Assuming we have a table named cars_table with the following fields:
* brand - varchar
* maker - varchar
* year - int
We can filter the records as follows:
*** LIKE
This will select all records from the table, where 'brand' field value begins with letter 'a', with anything following it.
SELECT * FROM cars_table WHERE brand LIKE 'a%';
*** IN
This will select all records from the table where the maker field value equals to 'Audi' or 'BMW'.
SELECT * FROM cars_table WHERE maker IN('Audi', 'BMW');
*** BETWEEN
This will select all records from the table where the car manufacturing year is between 2015 and 2017
SELECT * FROM cars_table WHERE year BETWEEN 2015 AND 2017;
Hth, cmiiw
+ 1
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column for example:
Select * FROM table WHERE name LIKE 'Da%'
This will return all fields where the name starts with 'Da'.
The WHERE clause when used together with the IN keyword only affects the rows whose values matches the list of values provided in the IN keyword
Select * FROM table WHERE name IN ('Dave', 'Mike')
This returns all fields where the name is either Dave or Mike
BETWEEN Condition is used to retrieve values within a range in a SELECT, INSERT, UPDATE, or DELETE statement.
Select * FROM table WHERE age BETWEEN 10 and 20.
This will return all fields where the age is between 10 and 20.