% : Matches zero or more chars _ : Matches one char
q="SELECT * FROM student WHERE name like '%John'"
try:
my_cursor=my_conn.execute(q)
for row in my_cursor:
print(row[0],row[1],row[2],row[3],row[4])
except sqlite3.Error as my_error:
print("error: ",my_error)
Output, these three records ends with the name John
6 Alex John Four 55 male
10 Big John Four 55 female
21 Babby John Four 69 female
my_data=('%john%',)
q="SELECT * FROM student WHERE name like ?"
try:
my_cursor=my_conn.execute(q,my_data)
for row in my_cursor:
print(row[0],row[1],row[3],row[2],row[4])
except sqlite3.Error as my_error:
print("error: ",my_error)
Output ( John is present in the string , not only at staring or ending of the name column )
1 John Deo 75 Four female
5 John Mike 60 Four female
6 Alex John 55 Four male
7 My John Rob 78 Five male
10 Big John 55 Four female
21 Babby John 69 Four female
To match John only at ending of the string we can change the query like this
my_data=('%john',)
q="SELECT * FROM student WHERE name like ?"
To match John only at starting of the string we can change the query like this.
my_data=('john%',)
q="SELECT * FROM student WHERE name like ?"
Rows where name column starting with b and ending with n
my_data=('b%n',) # starting with b and ending with n
q="SELECT * FROM student WHERE name like ?"
Output
10 Big John 55 Four female
21 Babby John 69 Four female
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 |
my_data=('%john%','%ow%')
q="SELECT * FROM student WHERE name like ? or name like ?"
my_data=('%john%','%ro%')
q="SELECT * FROM student WHERE name like ? AND name like ?"
Output ( having john and ro )
7 My John Rob 78 Five male
my_data=('%john%','%Big%')
q="SELECT * FROM student WHERE name like ? AND name NOT like ?"
Output ( by using NOT , we have removed 10th records )
1 John Deo 75 Four female
5 John Mike 60 Four female
6 Alex John 55 Four male
7 My John Rob 78 Five male
21 Babby John 69 Four female
my_data=('fe____',)
q="SELECT * FROM student WHERE sex like ? "
We can get records having marks in nineties. In mark column we can get records where first digit is 9, here we are excluding mark equal to or above 100 and marks less than 90.
my_data=('9_',)
q="SELECT * FROM student WHERE mark like ? "
Output
12 Recky 94 Six female
32 Binn Rott 90 Seven female
33 Kenn Rein 96 Six female
Some time we have to enter last 4 digits of our Credit Card number or account number. The query can be prepared using this part of the string. SELECT * FROM account_master WHERE acc_no LIKE '__044'
my_data=('b%n',) # starting with b and ending with n
q="SELECT * FROM student WHERE name like ?"
try:
my_conn.execute('PRAGMA case_sensitive_like = ON')
my_cursor=my_conn.execute(q,my_data)
for row in my_cursor:
print(row[0],row[1],row[3],row[2],row[4])
except sqlite3.Error as my_error:
print("error: ",my_error)
We will not get any matching rows as now LIKE is case sensitive ( there are two records starting with B ) . We can change the code to make it case insensitive.
my_conn.execute('PRAGMA case_sensitive_like = OFF')
Output is here
10 Big John 55 Four female
21 Babby John 69 Four female
View and Download like.ipynb file
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.