SQL LIKE query Command

SQL LIKE By using LIKE query we can match part of the full data present in a column. Here our search word need not exactly match.

Using Like Query with wildcard in different combinations, we can match our keyword with the pattern of the data present in columns.

The best way to use LIKE command is to apply it against a text or varchar field along with wildcard % or _

  • Video Tutorial on LIKE Query with AND , OR NOT combinations.


Here is our table with all the records.

1 John Deo Four 75
2 Max Ruin Three 85
3 Arnold Three 55
4 Krish Star Four 60
5 John Mike Four 60
6 Alex John Four 55
We will apply the LIKE command here like this to our name field.
SELECT * FROM student WHERE name LIKE '%John%'
id name class mark
1 John Deo Four 75
5 John Mike Four 60
6 Alex John Four 55
The above result shows that we will get all the names where John word is present in the record. It can be any where. Please note the use of  symbol % in the query. This will match any number of character even zero character before or after the word John. So we are getting the names those starts with John also. We may require the names which only starts with John not inside the name. In other words we want records starting with John only. In this case we will remove the use of  % at the left of word John. Here is the SQL with LIKE command.     

Read how Regular expression is used to Pattern matching

SELECT * FROM student WHERE name LIKE 'John%'
id name class mark
1 John Deo Four 75
5 John Mike Four 60
We can see the result above list out all the names starting with name John. To display the records which does not have specific word John as the beginning we have change our LIKE sql command a little by changing the % position to the end of the word. Here we will allow any character even zero character to the left of the desired word not to the right 
SELECT * FROM student WHERE name LIKE '%John'
6 Alex John Four 55

The above result have desired word John at the end only.

Matching string in name column starting with A and ending with n.
SELECT * FROM student WHERE name LIKE 'A%n'
6	Alex John	Four	55	male
Here is a summary of string matching using % along with LIKE query
'%John'Matches string ending with John
'John%'Matches string starting with John
'%John%'Matches string anywhere with John
'j%n'Matches string starting with j and ending with n

Use of underscore ( _) as wildcard in string matching

We can use underscore as wildcard for one character space and use them along with LIKE statement and apply to table columns. For example we want to collect all the account numbers ending with 044 in a five digit account number field. Here is the query for this.
SELECT * FROM account_master WHERE acc_no LIKE '__044'
We have used two underscores in our query to tell that first two digits can be any thing and it should end with 044.

Underscores as wildcard can be used at any location but one can replace one character only. We can use more than one underscore also inside our query.

Using NOT with LIKE

SELECT * FROM student WHERE name NOT LIKE '%John%'
This way we can use LIKE command with many other commands to get desired output.

Case Sensitive query

As we have seen all the above cases are case insensitive. To match lower only or upper only cases we have to use binary command to make binary matching. Here is an example.
select * from student where name LIKE binary '%A'
Try the same query by not using binary inside it.

Name of the students having letter 'a' and letter 'e' ( without single quotes )
SELECT * FROM student WHERE name LIKE  '%a%' AND name LIKE  '%e%'
Find all courses from the Section table that start with the character, C, but do not have h, as the second character.
SELECT * FROM student WHERE name LIKE  'C%' AND name NOT LIKE  '_h%'

Searching keyword across multiple columns by using AND , OR

Keyword should present in both columns ( by using AND )
WHERE Column1 Like '%keyword%' AND Column2 LIKE '%keyword%'
Keyword should present in any columns ( by using OR )
WHERE Column1 Like '%keyword%' OR Column2 LIKE '%keyword%'
Same way the query can be extended to search across more than two columns.


In the string supplied the name is matched. (Reverse way of matching)
' Our best student is Mr  John Deo  of 5th Class'   
LIKE  CONCAT('%',name,'%')

CONCAT to join strings in Query

Limitation of Like query

Like condition has to match the entire data where as by using Regular Expression REGEXP we can match anywhere within the data

Displaying records using PHP Script

Query using PHP script All above queries can be used by using PHP script. First the Script should connect to MySQL database and then records can be displayed.

Here is the sample code.
require "config.php";

$count="SELECT * FROM student WHERE name LIKE '%John%'";

echo "<table>";
echo "<tr><th>id</th><th>name</th><th>class</th><th>mark</th></tr>";
foreach ($dbo->query($count) as $row) {
echo "<tr ><td>$row[id]</td><td>$row[name]</td><td>$row[class]</td><td>$row[mark]</td></tr>";
echo "</table>";
Locate command is used to search for records having a string in any place LIKE query is used in keyword search script
Read how LIKE command is used in MSSQL database




I wonder if I can use '%' as below for LIKE command: declare X nvarchar declare Y nvarcahr select * from dbo.~ where(X '%' like Y)-- <-this line is mentioned


hey all I am using like command for accesing name as alphabetics I have written for that as select * from tbl)country where country like " Textbox1.text " but it is not working properly please guide me


### to manoj SELECT * FROM TABLE_NAME WHERE country like '%HERE_GOES_MASK%' In that query, you should replace the TABLE_NAME with the name of the table you query data from. Also, HERE_GOES_MASK should be changed to the string you want to find


@manoj - This is how your command should be: "Select * from tblcountry where country like '%" Textbox1.Text "%'" OR you could also remove the "%" before the Textbox1.Text or remove it after, it will depend on your usage. Hope this helps.


How will i get names of all those people who have the string 'in' in their names?


I need to find a specific literal in an Oracle table column which is described as a "long" datatype. How do I do this, since "like" command can't be used on "long" type columns (can't use "long" column in a where clause of a select statement)? I get the error message:"inconsistent datatypes: expected NUMBER got LONG". Please help!


I want to selet more than one row at a time ...I think query should be... SELECT * FROM TABLENAME WHERE FIELDNAME LIKE 'A%' But it selecting only the first one ...I 'm working in MSAccess...with Core JAVA-Swing Can u please help me SIR..........


To Kichu.. select * from Table_Name where first_name like '%in%'


Question PHP/SQL: I have an EXEC that take all my contact list from my Database and put them into a variable. Now I want to select all contact starting with "A" as the last name. And show them into my contact list. I want to do it with all letters "A" , "B" , "C" ... Something like Iphone Contact list , all familly name are in alphatical order. So, can I select and show all name starting by a specific letter? how? thx for answerying
Regalla Naresh Reddy


Hi...! The information is useful. I need somehelp. I want to get the information like SELECT * FROM employee WHERE name LIKE '%ram' where '%ram' is the value of other table which is similar to the value in the current table. Means i need to retrieve the information from the current table based up on the value of other table. Can you help how to acheive this .....?


select t1.field1,t1.field2,t2.field1.t2.field2 from t1,t2 where t1.keyfield=t2.keyfield and t2.name like '%Ram'
Here t1 and t2 and two tables and linked by a keyfeild ( t1.keyfield=t2.keyfield)
Perwez Akhter


Dear all, Hi! Can I run two like in single command line. If Yes Tell me how
Shah Viral


Hello EveryOne i want to find the Partucular Person name, WHen i write Down in TextBox .. Thanks


You can run two like commands by using AND or OR combinations.


how can i use like query to search for two words (statement) not only when word example : select # from table_name where column like'% hello world%'


"SELECT * FROM video WHERE category LIKE '%'."$_GET['category']".'%' ORDER BY video_id DESC LIMIT ...... I have no idea where is the problem but it seems that this sql can not work with this statement because the webpage is not loading after this statement can someone tell me where is the problem I know that get[category] is funny or animal


Try by printing the SQL to see what the value of category you are getting. You can also print the error message to see what has gone wrong.


Like not working in vb 6.... please help code: Me.Data1.RecordSource = "select * from phone where name LIKE '% " Me.Text1.Text "%'" Me.Data1.Refresh all >,<,>=,<=,and,or,between etc works correctly but like not working..... what can i do?


I want to check 4th digit of the account no is zero using sql query


I like to search a general one suppose not the Exact one for that what query is use for Eg SELECT * FROM [reg1] WHERE ([Name] LIKE '%' @Name '%' ) This will display the name what u search in database I need the query for one are more search wht is that


what if i would like to find person's name which contain 2 words only?


Hi, I am having the table with the column First Name. Where ever the first name contains 'is' I just wanted to remove these word in that record.


HI all, I have 3 tables (A,B,C)and 2 querys in the first query I join two tables (A


hi, thanks for the submission of this since this has helped me a lot... i was confused with the usage of % and _. but now its clear for me... thanks again... hope i will be more benefitted in future from here


@ sf09 try combination of builtin functions like select name from tableName where len(name) - len(replace(name,, ')) = 1 this will work for you


hi, pls how do i write a query that should give me a result for all last_names that start with J, A and M?


Hi, can you please tell me how to retrieve names that are starting from either A or B from names table?


i want a query which will in output give the name of those employees whose last name ends with e and the query should be written without usibg the like statement


i want a query , how to find 3rd highest earner in a table


hai... i am trying to execute below stored procedure. i will take two arguments as a parameters. ALTER PROCEDURE S_ListVendor ( @searchFieldValue varchar(128), @searchInField varchar(120) ) AS SELECT * FROM S_Vendor where @searchInField like @searchFieldValue; its not executed so plz.. help me Thank you


I want to print one field that should not start with the digits. How can i write that logic using the NOT LIKE keyword. or is there any otehr comand can we use. pls answer this question.
srikar vandanapu


how to get the zero record table name in a database


I am trying to match first four digits(text)of a column values in a table with another table values(text). Pls write an sql query...


I want the list of studebts wgose ids are even...lik 2,4,6...pls help


hello all, i want the name of the employees whose name starts with j or k or l or m...............what should be the query........plz help......


Hello Every0ne: I Want To See The Third Letter In Name ..... In MySql Whos Could I ?


have a column named Name.I want to make a query where i just enter initials of any name and i get the full name.Plz solve my problem.


Very simple and helpful.I have been benefited from here massively.Thanks a lot.


hiiii what if i would like to find the data of a person having _ as 2nd letter in his name ex.s_sharma............... using wild cards


Use locate command


Hi, If I have the second table like Student.Table but the First part of name or Second Part of name in the name field, Now guide how to compare both the table, My database is a huge one near about 1million record.


I am trying to find a string at the end of a field using %XYZ as my search criteria. How do I condition the LIKE operator not to consider trailing blanks? I saw some information on the strip BIF but am not familiar with it (novice using SQL).


Hi, I was create table with 3 columns and insert data, by using SELECT QUERY i get all the 3rd column data, from that i want particular column data. what can i do for that. if any body knows plz update. Thanks in advance.


Hi, I was created one company based webapplication in that case i want to apply any one company that result wil be updated in my database and one vacancy will be reduced in my jsp page how can i do what query wil be use it please help me
midhun sudhakar


how will i delete records whose name starts with a or j


how can i get a data from a table where it should be match with its username.
something like this table:
table 1 (username,password,name)

all i want is when i type the username in a textbox and click a command button, the password will be on the other textbox that correspond to the username.

please write the php source code. thanks alot


please refer a book in which all simple and useful sql queries are given, as u write in ur tutorials, and thanx its very beneficial


what is the code of to fetch data from mysql table according to alphabet


How to display the names that end with a particular letter in sql?
Veerbhadra Singh


Please tell me how to find a particular one employer salary in mysql table.
for example find second employer salary.
bapu bardoli vada


my problem is i dont understand in sql statement with jquery & ajax... what am i do?? plz help me>>????
bapu bardoli vada


thanks for solved my problem...i realy thankyu for ur advice..and now i m doing every thing in sql n jquery..because u r amazing...!!!!


question:: to display employees whose names are containing 'A' at 2nd occurrence without using wild characters


How to employee name with third letter
L in the name without using like operator


search name with third letter.

where name like '__L%';
Hyder Ali


Hi i want to get the details the first letter starts with space how to write the query in mysql


SELECT * FROM student WHERE name NOT LIKE ' %'

note that there is a space followed by % inside the quote


SELECT * FROM student WHERE name LIKE '%John%'

how to get same answer in above qry with out using LIKE keyword


Try using <a href=sql_locate.php>LOCATE query</a>


how to get answer "list the name from student whose name start with 'J' or end with 'N'.


SELECT name FROM student where name LIKE 'J%' OR name LIKE '%N'


Find all courses from the Section table that start with the character, “C”, but do not have “h”, as the second character.
Prashant K


Kindly let me know the syntax for the below question

Select Name of the person from the tables where Name should contains letter 'a' and 'e' ?
Prashant K


Kindly let me know the syntax for the below question.

Select Name from tables where Name should contains letter "a" and "e".

Please note the provided letters are just an example.


It is added to the tutorial


I need the list of names from a student relation which ends with a particular character say 'a' or a pattern say 'ram' . you give an explanation above but it is not working when i try it in sql. Give me explanation regarding it.


I need a list of tickets, which contains 3 matching number, e.g say the ticket is 12,13,56,49,20,30 any ticket that contains any of the number should be listed in regardless of the order eg 45, 20, 36,34,56,49 can be one of the ticket listed as it contains 49,56 and 20


I have a, b, c, D, e, f alphabet .
I use this query = select * from table where Colume like ='a%' ;
Echo "coum(0)";

So what I do then my one php select ... Like query query work For all alphabet
Mens I click a then just a alphabet data fetch not other then I click I then just I data fetch please give me answer please solve my problem fast


Your Query part is ok. You need to pass the alphabet to the query through a variable. There are several ways to do it.
You can keep hyperlinks with variable passing the alphabet to the page.
<a href=your_page.php?var_alpha=a>a</a>, <a href=your_page.php?var_alpha=b>b</a> ...
Now read the variable and pass it to query.
You can use drop down list box and on select reload the page with variable.

Take care of injection attack here.
Lakshmi priya


Is the output of '%E%' and 'E%' is same??


No , '%E%' means it will match presence of E any where. 'E%' means it will match E at the starting of the string.
Dion James Smith


Hey there ! I want a command line that finds all names whose name containing the first character 'N' and the fourth character 'o' and the rest may be of any character.
Please help !


SELECT * from table_name WHERE column_name like 'N___o%'

Post your comments , suggestion , error , requirements etc here

SQL Video Tutorials

We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer