The SQL LIKE Operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator:
The percent sign (%) represents zero, one, or multiple characters
The underscore sign (_) represents one, single character
Note: MS Access uses an asterisk (*) instead of the percent sign (%), and a question mark (?) instead of the underscore (_).
The percent sign and the underscore can also be used in combinations!
LIKE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
---------
You can also combine any number of conditions using AND or OR operators.
Here are some examples showing different LIKE operators with '%' and '_' wildcards:
--------------------
LIKE Operator Description
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"
---------------
33 SQL LIKE Operator sql videos in telugu
Setting environment for using XAMPP for Windows.
Mounika@MOUNIKA-PC c:\xampp
# ram.cmd
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.4.18-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use vlrinst;
Database changed
MariaDB [vlrinst]> select * from emp where name like 'r%';
+----+---------+------+------+-------+
| id | name | loc | age | sal |
+----+---------+------+------+-------+
| 13 | revathi | jntu | 26 | 20000 |
+----+---------+------+------+-------+
1 row in set (0.001 sec)
MariaDB [vlrinst]> select * from emp where name like '%r';
Empty set (0.001 sec)
MariaDB [vlrinst]> select * from emp where name like '%t';
+----+--------+------+------+-------+
| id | name | loc | age | sal |
+----+--------+------+------+-------+
| 10 | venkat | kphb | 35 | 30000 |
+----+--------+------+------+-------+
1 row in set (0.001 sec)
MariaDB [vlrinst]> select * from emp where name like '%t%';
+----+---------+------+------+-------+
| id | name | loc | age | sal |
+----+---------+------+------+-------+
| 10 | venkat | kphb | 35 | 30000 |
| 13 | revathi | jntu | 26 | 20000 |
+----+---------+------+------+-------+
2 rows in set (0.001 sec)
MariaDB [vlrinst]> select * from emp where name like '%ra%';
+----+---------+------+------+-------+
| id | name | loc | age | sal |
+----+---------+------+------+-------+
| 11 | praveen | pune | 28 | 35000 |
| 16 | praveen | jntu | 33 | 100 |
+----+---------+------+------+-------+
2 rows in set (0.001 sec)
MariaDB [vlrinst]> select * from emp where name like 'ra%';
Empty set (0.001 sec)
MariaDB [vlrinst]> select * from emp where name like 'r%';
+----+---------+------+------+-------+
| id | name | loc | age | sal |
+----+---------+------+------+-------+
| 13 | revathi | jntu | 26 | 20000 |
+----+---------+------+------+-------+
1 row in set (0.001 sec)
MariaDB [vlrinst]> select * from emp where name like '%nk%';
+----+--------+------+------+-------+
| id | name | loc | age | sal |
+----+--------+------+------+-------+
| 10 | venkat | kphb | 35 | 30000 |
+----+--------+------+------+-------+
1 row in set (0.001 sec)
MariaDB [vlrinst]> select * from emp where name like 'nk%';
Empty set (0.001 sec)
MariaDB [vlrinst]> select * from emp where name like '%n%';
+----+---------+-----------+------+-------+
| id | name | loc | age | sal |
+----+---------+-----------+------+-------+
| 10 | venkat | kphb | 35 | 30000 |
| 11 | praveen | pune | 28 | 35000 |
| 12 | mounika | hyderabad | 22 | 39000 |
| 14 | anji | gutta | 28 | 20000 |
| 16 | praveen | jntu | 33 | 100 |
| 17 | pandu | munipeda | 35 | 30300 |
| 18 | pandu | pune | 23 | 39300 |
+----+---------+-----------+------+-------+
7 rows in set (0.002 sec)
MariaDB [vlrinst]> select * from emp where name like '__a%';
+----+---------+------+------+-------+
| id | name | loc | age | sal |
+----+---------+------+------+-------+
| 11 | praveen | pune | 28 | 35000 |
| 16 | praveen | jntu | 33 | 100 |
+----+---------+------+------+-------+
2 rows in set (0.001 sec)
MariaDB [vlrinst]> select * from emp where name like '__a';
Empty set (0.001 sec)
MariaDB [vlrinst]> select * from emp where name like '____u';
+----+-------+----------+------+-------+
| id | name | loc | age | sal |
+----+-------+----------+------+-------+
| 17 | pandu | munipeda | 35 | 30300 |
| 18 | pandu | pune | 23 | 39300 |
+----+-------+----------+------+-------+
2 rows in set (0.001 sec)
MariaDB [vlrinst]> select * from emp where name like '_a%';
+----+--------+----------+------+-------+
| id | name | loc | age | sal |
+----+--------+----------+------+-------+
| 15 | harika | lb nagar | 26 | 30100 |
| 17 | pandu | munipeda | 35 | 30300 |
| 18 | pandu | pune | 23 | 39300 |
+----+--------+----------+------+-------+
3 rows in set (0.002 sec)
MariaDB [vlrinst]> select * from emp where name like 'h%a';
+----+--------+----------+------+-------+
| id | name | loc | age | sal |
+----+--------+----------+------+-------+
| 15 | harika | lb nagar | 26 | 30100 |
+----+--------+----------+------+-------+
1 row in set (0.001 sec)
MariaDB [vlrinst]>
No comments:
Post a Comment
Note: only a member of this blog may post a comment.