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 MySQL - LIKE() operator
Next article icon

Python MySQL - BETWEEN and IN Operator

Last Updated : 29 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 operations:

The SQL BETWEEN condition is used to test if an expression is within a range of values (inclusive). The values include text, date, or numbers.

  1. It can be used along with the SELECT, INSERT, UPDATE, or DELETE statement.
  2. The SQL BETWEEN Condition will return the records where the expression is within the range of value1 and value2.

Syntax:

SELECT column1,column2,....,column n

FROM table_name

WHERE column_name BETWEEN value1 AND value2;

Example 1: Python MySQL program to demonstrate the use of BETWEEN operator.

Python3
# Import mysql.connector module import mysql.connector  # give connection with xampp database = mysql.connector.connect(     host="localhost",     user="root",     password="",     database="gfg" )  # Creating cursor object cur_object = database.cursor() print("between operator demo")  #  query find = "SELECT cus_id,purchase from geeks where\ purchase between 10 and 100 "  # Execute the query cur_object.execute(find)  # fetching results data = cur_object.fetchall() print(" ") print("Purchase between 10 to 100 ") print(" ")  for res in data:     print(res[0], res[1], sep="--") print(" ")  # Close database connection database.close() 

Output:

The SQL IN operator allows you to easily test if the expression matches any value in the list of values. It is used to remove the need for multiple OR conditions in SELECT, INSERT, UPDATE or DELETE. You can also use NOT IN to exclude the rows in your list.

Syntax:

SELECT column1,column2,...,column n

FROM tablename

WHERE column IN (val1,val2,...,valn);

Example 2: Python MySQL program to demonstrate the use of IN operator.

Python3
# Import mysql.connector module import mysql.connector  # give connection with xampp database = mysql.connector.connect(     host="localhost",     user="root",     password="",     database="gfg" )  # Creating cursor object cur_object = database.cursor()  #  query print("in operator demo") find = "SELECT cus_id,purchase from geeks where \ purchase in (67) "  # Execute the query cur_object.execute(find)  # fetching all results data = cur_object.fetchall() print("customer id with purchase 67: ") print(" ")  for res in data:     print(res[0], res[1], sep="--") print(" ") print("not in operator demo") print(" ")  find = "SELECT cus_id,purchase from geeks where purchase\ not in (67) "  # Execute the query cur_object.execute(find)  # fetching all results data = cur_object.fetchall() print(" ") print("customer id with purchase except 67 purchase ") print(" ")  for res in data:     print(res[0], res[1], sep="--")  # Close database connection database.close() 

Output:


Next Article
Python MySQL - LIKE() operator
author
sravankumar_171fa07058
Improve
Article Tags :
  • Python
  • SQL
  • Web Technologies
  • HTML
  • Python-mySQL
Practice Tags :
  • python

Similar Reads

  • Python MySQL - LIKE() operator
    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’, o
    3 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 Membership and Identity Operators
    There is a large set of Python operators that can be used on different datatypes. Sometimes we need to know if a value belongs to a particular set. This can be done by using the Membership and Identity Operators. In this article, we will learn about Python Membership and Identity Operators. Python M
    5 min read
  • Comparison Operators in Python
    Python operators can be used with various data types, including numbers, strings, boolean and more. In Python, comparison operators are used to compare the values of two operands (elements being compared). When comparing strings, the comparison is based on the alphabetical order of their characters
    5 min read
  • PostgreSQL - NOT BETWEEN operator
    PostgreSQL NOT BETWEEN operator is used to match all values against a range of values excluding the values in the mentioned range itself. Syntax: value NOT BETWEEN low AND high; Or, Syntax: value high; The NOT BETWEEN operator is used generally with WHERE clause with association with SELECT, INSERT,
    1 min read
  • Chaining comparison operators in Python
    Checking multiple conditions in a single expression is common in programming. In Python, comparison operator chaining allows us to write cleaner, more readable code when evaluating multiple conditions. Instead of using multiple and conditions, Python enables chaining comparisons directly in a mathem
    4 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
  • PostgreSQL - BETWEEN Operator
    The PostgreSQL BETWEEN operator is an essential tool for filtering data within a specific range. Often used in the WHERE clause of SELECT, INSERT, UPDATE, and DELETE statements, this operator simplifies range-based conditions, making queries faster and easier to read. In this article, we will explai
    3 min read
  • 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 MySQL - GROUP BY and HAVING Clause
    In this article, we will see how to perform groupby() and HAVING() operations on SQL using Python. Here we will consider a college database to perform group by operation on the department with respect to student strength. GROUP BY The GROUP BY statement groups rows that have the same values into sin
    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