LIKE is another keyword that is used in the WHERE clause. Basically, LIKE allows you to do a search based on a pattern rather than specifying exactly what is desired (as in IN) or spell out a range (as in BETWEEN). The syntax for is as follows:
SELECT "column_name"
FROM "table_name"
WHERE "column_name" LIKE {PATTERN}
{PATTERN} often consists of wildcards. Here are some examples:
- 'A_Z': All string that starts with 'A', another character, and end with 'Z'. For example, 'ABZ' and 'A2Z' would both satisfy the condition, while 'AKKZ' would not (because there are two characters between A and Z instead of one).
- 'ABC%': All strings that start with 'ABC'. For example, 'ABCD' and 'ABCABC' would both satisfy the condition.
- '%XYZ': All strings that end with 'XYZ'. For example, 'WXYZ' and 'ZZXYZ' would both satisfy the condition.
- '%AN%': All string that contain the pattern 'AN' anywhere. For example, 'LOS ANGELES' and 'SAN FRANCISCO' would both satisfy the condition.
Let's say we have the following table:
Table Store_Information
store_name | Sales | Date |
LOS ANGELES | $1500 | Jan-05-1999 |
SAN DIEGO | $250 | Jan-07-1999 |
SAN FRANCISCO | $300 | Jan-08-1999 |
BOSTON | $700 | Jan-08-1999 |
We want to find all stores whose name contains 'AN'. To do so, we key in,
SELECT *
FROM Store_Information
WHERE store_name LIKE '%AN%'
Result:
store_name | Sales | Date |
LOS ANGELES | $1500 | Jan-05-1999 |
SAN DIEGO | $250 | Jan-07-1999 |
SAN FRANCISCO | $300 | Jan-08-1999 |
No comments:
Post a Comment