In this tutorial, we will work with a products database to demonstrate how to use SQL pattern matching with the LIKE operator. We will begin by retrieving all records from the database, add new entries, and then explore filtering records using various LIKE patterns. This guide is perfect for developers and database administrators seeking to enhance their SQL querying skills.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Retrieving and Adding Records
Start by retrieving all products to check the current state of the database:Ensure that you have the necessary permissions to insert and retrieve records from your products database.
Filtering Records Using the LIKE Operator
Standard comparison operators (such as equals, greater than, or less than) are not flexible enough for partial text matching. The LIKE operator, combined with wildcard characters, offers the ability to filter text columns—similar to regular expressions in Python.Finding Products That Start with “TV”
To fetch products whose names start with “TV”, use the percent sign (%) as a wildcard representing any sequence of characters after “TV”. For example:Changing the Pattern for Different Searches
You can modify the pattern to search for products starting with any other letter. For example, to find products starting with “A”, use:Finding Products That End with a Specific Letter
To filter products ending with a particular letter, place the wildcard at the beginning of the pattern. For instance, to select names ending with “E”:Excluding Certain Patterns
To exclude records that contain a specific substring, combine the NOT operator with LIKE. For example, to exclude rows containing ‘jené’, use:| name | price | id | is_sale | inventory | created_at |
|---|---|---|---|---|---|
| TV | 200 | 1 | false | 0 | 2021-08-20 00:49:58.021274-04 |
| DVD Players | 80 | 2 | false | 0 | 2021-08-20 00:49:58.021274-04 |
| Car | 40 | 3 | false | 0 | 2021-08-20 00:49:58.021274-04 |
| pencil | 4 | 4 | true | 0 | 2021-08-20 00:49:58.021274-04 |
| keyboard | 28 | 5 | false | 0 | 2021-08-20 00:49:58.021274-04 |
| soda | 2 | 6 | false | 0 | 2021-08-20 00:49:58.021274-04 |
| pizza | 30 | 7 | true | 0 | 2021-08-20 00:49:58.021274-04 |
| toothbrush | 5 | 8 | false | 0 | 2021-08-20 00:49:58.021274-04 |
| xbox | 380 | 9 | false | 0 | 2021-08-20 00:49:58.021274-04 |
| toilet paper | 4 | 10 | false | 0 | 2021-08-20 00:49:58.021274-04 |
| TV BLUE | 100 | 11 | false | 45 | 2021-08-20 00:49:58.021274-04 |
| TV RED | 20 | 12 | true | 0 | 2021-08-20 00:49:58.021274-04 |
| TV YELLOW | 50 | 20 | false | 0 | 2021-08-21 01:03:42.829215-04 |
Matching a Substring Anywhere in the Name
To match any product that contains a specific substring (for example, “EN”), enclose the substring with wildcards on both sides:If you encounter an error due to a possible typo or syntax issue, retyping the query (for example, updating ‘%jené%’ to ‘%enn%’) might resolve the problem.