Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

Python SQL Where Clause

by suresh

In this chapter, we explain how to write a SQL Where in the Python Programming language. And how to filter the Table records in Python with an example. 

Before we get into the Python SQL Where example, let me show you the data that we are going to use.

Python SQL Where Example 1

Python SQL Where Clause Example 1

In this Python example, we show how to use the Where Clause to filter the Data or restrict the records based on condition.

TIP: Please refer to Connect Python to SQL Server article to understand the steps involved in establishing a connection.

# Python SQL Where Example
import pyodbc
WhereConn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
                      "Server=PRASAD;"
                      "Database=SQL Tutorial;"
                      "Trusted_Connection=yes;")

WhereCursor = WhereConn.cursor()
WhereCursor.execute('SELECT * FROM CustomerSale WHERE YearlyIncome >= 60000')

for row in WhereCursor:
    print('row = %r' % (row,))

OUTPUT

Python SQL Where Example 2

ANALYSIS

The below Python program selects all the records from the Customer Sales table whose Yearly Income is greater than or equal to 60000.

OrderCursor.execute('SELECT * FROM CustomerSale ORDER BY YearlyIncome')

Next, we used the For loop to iterate each row present in the Where Cursor. Within the For Loop, we used the print statement to print records.

for row in WhereCursor:    
    print('row = %r' % (row,))

Python SQL Where Example 2

In this Python example, we are using Wildcards to filter the data. The below Python program returns all the records from a table whose Occupation ends with l. 

# Python SQL Where Example
import pyodbc
WhereConn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
                      "Server=PRASAD;"
                      "Database=SQL Tutorial;"
                      "Trusted_Connection=yes;")

WhereCursor = WhereConn.cursor()
WhereCursor.execute("SELECT * FROM CustomerSale WHERE Occupation LIKE N'%l' ")

for row in WhereCursor:
    print('row = %r' % (row,))

OUTPUT

Python SQL Where Example 3

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python Numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python Numpy concatenate
  • Python Numpy Array shape
  • Python Pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python Substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy