Tutorial Gateway

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

Python SQL Select Top

by suresh

In this section, we explain to you how to write a SQL Select Top in the Python Programming language. Or how to write a SQL Server query to select the top 10 records in Python with an example.

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

Python SQL Select Top Example 1

Python SQL Select Top Example 1

In this Python example, we show how to use the Top Clause to select the first 10 records from a customer sales table.

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

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

TopCursor = TopConn.cursor()
TopCursor.execute("SELECT TOP 10 * FROM CustomerSale ")

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

OUTPUT

Python SQL Select Top Example 2

ANALYSIS

The below program selects the first 10 or top 10 records in a Customer Sales table.

TopCursor.execute("SELECT TOP 10 * FROM CustomerSale ")

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

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

Python Select Top 10 records Example 2

In this example, we are using Order By Clause along with Top Clause. Below program sort data in Descending order using Sales, and then it selects the first 10 records from the sorted table. 

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

TopCursor = TopConn.cursor()
TopCursor.execute("SELECT TOP 10 * FROM CustomerSale ORDER BY Sales DESC")

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

OUTPUT

Python SQL Select Top Example 3

Python SQL Select Top Example 3

In this Python example, we are using the Percent to select the top 40 percent records from a customer sales. 

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

TopCursor = TopConn.cursor()
TopCursor.execute("SELECT TOP 40 PERCENT * FROM CustomerSale ORDER BY Sales DESC")

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

OUTPUT

Python SQL Select Top Example 4

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