InterviewSolution
| 1. |
What is Pattern Matching in SQL? |
|
Answer» SQL pattern matching provides for pattern search in data if you have no clue as to what that word should be. This kind of SQL query uses wildcards to match a string pattern, rather than writing the exact word. The LIKE operator is used in CONJUNCTION with SQL Wildcards to fetch the required information.
The % wildcard matches zero or more characters of any type and can be used to define wildcards both before and after the pattern. Search a student in your database with FIRST name beginning with the letter K: SELECT *FROM studentsWHERE first_name LIKE 'K%'
Use the NOT keyword to select records that don't match the pattern. This query returns all students whose first name does not begin with K. SELECT *FROM studentsWHERE first_name NOT LIKE 'K%'
Search for a student in the database where he/she has a K in his/her first name. SELECT *FROM studentsWHERE first_name LIKE '%Q%'
The _ wildcard matches exactly one character of any type. It can be used in conjunction with % wildcard. This query fetches all students with letter K at the third position in their first name. SELECT *FROM studentsWHERE first_name LIKE '__K%'
The _ wildcard plays an important role as a LIMITATION when it matches exactly one character. It limits the length and position of the matched results. For example - SELECT * /* Matches first names with three or more letters */FROM studentsWHERE first_name LIKE '___%'SELECT * /* Matches first names with exactly four characters */FROM studentsWHERE first_name LIKE '____' |
|