Tutorial Gateway

  • C
  • C#
  • Python
  • SQL
  • Java
  • JS
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs
  • MySQL

Python SQL Select Statement

In this section, we explain to you how to write a SQL Select Statement in the Python Programming language. And how to extract or select the records from a SQL Server Table. 

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

Python SQL Select Statement Example 1

Python SQL Select statement Example 1

In this example, we show how to use the select statement to select records from a SQL Table.

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

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

cursor = conn.cursor()
cursor.execute('SELECT * FROM CustomerSale')

for row in cursor:
    print('row = %r' % (row,))
Python SQL Select Statement Example 2

First, we imported or selected data from the Customer Sales table present in SQL Tutorial Database.

cursor = cursor.execute('SELECT * FROM CustomerSale')

Next, we used the For loop to iterate each row present in the Customer Sales table. Within the For Loop, we used the print statement to print rows.

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

Python SQL Select statement Example 2

Instead of selecting all the unnecessary columns (using * ), you can select the required columns.

This Python example selects the Employee Id, Occupation, Yearly Income, and Sales columns from the Customer Sales table.

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

cursor = conn.cursor()
cursor.execute('SELECT EmpID, Occupation, YearlyIncome, Sales FROM CustomerSale')

for row in cursor:
    print('row = %r' % (row,))
Python SQL Select Statement Example 3

Select Database records in Python Example 3

In Python, the cursor has many functions. You can use these Python cursor functions to alter the result provided by the select statement.

Python SQL Select Statement Example 4

For example, Python fetchone function fetches only one row or record from a table

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

cursor = conn.cursor()
cursor.execute('SELECT * FROM CustomerSale')
result = cursor.fetchone()
print(result)
Python SQL Select Statement Example 5

Filed 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 Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File Handling
  • 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

Copyright © 2021· All Rights Reserved by Suresh.
About | Contact | Privacy Policy