Using String Patterns and Ranges
String Patterns: We can use string patterns with the LIKE predicate in the WHERE clause to search for data rows that match a particular pattern. The percent sign (%) is used as a wildcard character to represent missing letters. For example, SELECT first_name FROM author WHERE first_name LIKE 'R%' retrieves all rows where the author's first name starts with the letter 'R'.
Ranges of Values: Instead of using multiple comparison operators in the WHERE clause to specify a range of values, we can use the BETWEEN and AND operators. This makes the SELECT statement simpler and quicker to write. For example, SELECT * FROM books WHERE pages BETWEEN 290 AND 300 retrieves all books with a page count between 290 and 300, inclusive.
Sets of Values: When dealing with multiple values that need to be checked in the WHERE clause, we can use the IN operator. This allows us to specify a set of values to compare against. For example, SELECT * FROM authors WHERE country IN ('Canada', 'India', 'China') retrieves authors from Canada, India, or China.
Using these techniques, we can simplify our SELECT statements and make them more efficient for retrieving the desired data from the database.
Comments
Post a Comment