Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Database
  • Python MySQL
  • Python SQLite
  • Python MongoDB
  • PostgreSQL
  • SQLAlchemy
  • Django
  • Flask
  • SQL
  • ReactJS
  • Vue.js
  • AngularJS
  • API
  • REST API
  • Express.js
  • NodeJS
Open In App
Next Article:
Python Logical Operators
Next article icon

Python MySQL - LIKE() operator

Last Updated : 28 Apr, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will discuss the use of LIKE operator in MySQL using Python language.

Sometimes we may require tuples from the database which match certain patterns. For example, we may wish to retrieve all columns where the tuples start with the letter ‘y’, or start with ‘b’ and end with ‘l’, or even more complicated and restrictive string patterns. This is where the LIKE Clause comes to the rescue, often coupled with the WHERE Clause in SQL.

There are two kinds of wildcards used to filter out the results:

  • The percent sign (%): Used to match zero or more characters. (Variable Length)
  • The underscore sign (_): Used to match exactly one character. (Fixed Length)

Syntax:

SELECT column1, column2, ...,columnn

FROM table_name

WHERE columnn LIKE pattern;

The following are the rules for pattern matching with the LIKE Clause: 

PatternMeaning
'a%'Match strings which start with 'a'
'%a'Match strings with end with 'a'
'a%t'Match strings which contain the start with 'a' and end with 't'.
'%wow%'Match strings which contain the substring 'wow' in them at any position.
'_wow%'Match strings which contain the substring 'wow' in them at the second position.
'_a%'Match strings which contain 'a' at the second position.
'a_ _%'Match strings which start with 'a' and contain at least 2 more characters.

In order to use LIKE operations we are going to use the below table:

Below are various examples that depict how to use LIKE operator in Python MySQL.

Example 1:

Program to display rows where the address starts with the letter G in the itdept table.

Python3
# import mysql.connector module import mysql.connector  # establish connection database = mysql.connector.connect(     host="localhost",     user="root",     password="",     database="gfg" )  # creating cursor object cur_object = database.cursor() print("like operator address starts with G")  #  query find = "SELECT * from itdept where Address like 'G%' "  # execute the query cur_object.execute(find)  # fetching all results data = cur_object.fetchall() for i in data:     print(i[0], i[1], i[2], i[3], sep="--")  # Close database connection database.close() 

Output:

Example 2:

Here we display all the rows where the name begins with the letter H and ends with the letter A in the table.

Python3
# import mysql.connector module import mysql.connector  # establish connection database = mysql.connector.connect(     host="localhost",     user="root",     password="",     database="gfg" )  # creating cursor object cur_object = database.cursor() print("like operator name starts with H and ends with A")  #  query find = "SELECT * from itdept where Name like 'H%A' "  # execute the query cur_object.execute(find)  # fetching all results data = cur_object.fetchall() for i in data:     print(i[0], i[1], i[2], i[3], sep="--")  # close database connection database.close() 

Output:

Example 3:

In this program, we display all the rows having three-lettered addressees in the table.

Python3
# import mysql.connector module import mysql.connector  # establish connection database = mysql.connector.connect(     host="localhost",     user="root",     password="",     database="gfg" )  # creating cursor object cur_object = database.cursor() print("like operator address has three letters only")  #  query find = "SELECT * from itdept where Address like '___' "  # execute the query cur_object.execute(find)  # fetching all results data = cur_object.fetchall() for i in data:     print(i[0], i[1], i[2], i[3], sep="--")  # close database connection database.close() 

Output:


Next Article
Python Logical Operators
author
sravankumar_171fa07058
Improve
Article Tags :
  • Python
  • SQL
  • Python-mySQL
Practice Tags :
  • python

Similar Reads

  • Python Logical Operators
    Python logical operators are used to combine conditional statements, allowing you to perform operations based on multiple conditions. These Python operators, alongside arithmetic operators, are special symbols used to carry out computations on values and variables. In this article, we will discuss l
    6 min read
  • Python NOT EQUAL operator
    In this article, we are going to see != (Not equal) operators. In Python, != is defined as not equal to operator. It returns True if operands on either side are not equal to each other, and returns False if they are equal. Python NOT EQUAL operators SyntaxThe Operator not equal in the Python descrip
    3 min read
  • not Operator in Python
    The not keyword in Python is a logical operator used to obtain the negation or opposite Boolean value of an operand. It is a unary operator, meaning it takes only one operand and returns its complementary Boolean value. For example, if False is given as an operand to not, it returns True and vice ve
    3 min read
  • Python MySQL - BETWEEN and IN Operator
    In this article, we are going to see the database operations BETWEEN and IN operators in MySQL using Python. Both of these operators are used with the WHERE query to check if an expression is within a range or in a list of values in SQL. We are going to use the below table to perform various operati
    3 min read
  • Python 3 - Logical Operators
    Logical Operators are used to perform certain logical operations on values and variables. These are the special reserved keywords that carry out some logical computations. The value the operator operates on is known as Operand. In Python, they are used on conditional statements (either True or False
    4 min read
  • Python Bitwise Operators
    Python bitwise operators are used to perform bitwise calculations on integers. The integers are first converted into binary and then operations are performed on each bit or corresponding pair of bits, hence the name bitwise operators. The result is then returned in decimal format. Note: Python bitwi
    6 min read
  • Python Operators
    In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators. OPERATORS: These are the special symbols. Eg- + , * , /,
    6 min read
  • Python OR Operator
    Python OR Operator takes at least two boolean expressions and returns True if any one of the expressions is True. If all the expressions are False then it returns False. Flowchart of Python OR OperatorTruth Table for Python OR OperatorExpression 1Expression 2ResultTrueTrueTrueTrueFalseTrueFalseTrueT
    4 min read
  • Python IF with NOT Operator
    We can use if with logical not operator in Python. The main use of the logical not operator is that it is used to inverse the value. With the help of not operator, we can convert true value to false and vice versa. When not applied to the value so it reverses it and then the final value is evaluated
    6 min read
  • numpy.ones_like() in Python
    The numpy.one_like() function returns an array of given shape and type as a given array, with ones. Syntax: numpy.ones_like(array, dtype = None, order = 'K', subok = True) Parameters : array : array_like input subok : [optional, boolean]If true, then newly created array will be sub-class of array; o
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences