Back to Python Notes
Topic #288

Select Query

Learn how to retrieve data from a database using SQL SELECT queries in Python, ensuring efficient and safe data access.

What it is

A Select Query in Python involves sending an SQL command to a database to read existing records. This process typically uses a database connector library (like sqlite3, psycopg2, or mysql-connector-python). The core mechanism relies on a cursor object, which acts as the interface for executing commands and fetching results. Key terms include execute() for running the query and fetchall(), fetchone(), or fetchmany() for retrieving the data.

Why it matters

  • Data Retrieval: It is the primary method for reading application state, user profiles, or transaction history.
  • Memory Efficiency: Using specific fetch methods allows you to control how much data loads into memory at once.
  • Security: Proper implementation prevents SQL injection attacks by separating code from data.
  • Interactivity: Enables dynamic content generation based on real-time database states.

Syntax or steps

  1. Establish a connection to the database using the appropriate driver.
  2. Create a cursor object from the connection.
  3. Write the SQL SELECT statement as a string.
  4. Execute the statement using cursor.execute().
  5. Fetch the results using one of the fetch methods.
  6. Close the cursor and connection when finished.

Example

import sqlite3

# 1. Connect to database (creates file if not exists)
conn = sqlite3.connect('example.db')
cur = conn.cursor()

# 2. Execute a select query with parameters to prevent SQL injection
query = "SELECT id, name, email FROM users WHERE age > ?"
params = (25,)
cur.execute(query, params)

# 3. Fetch all matching rows
rows = cur.fetchall()

# 4. Process the data
for row in rows:
    print(f"ID: {row[0]}, Name: {row[1]}, Email: {row[2]}")

# 5. Clean up
cur.close()
conn.close()

This example connects to an SQLite database, selects users older than 25, and prints their details. Note the use of ? placeholders and the params tuple. This ensures that the value 25 is treated strictly as data, not executable code.

Common mistakes

  • String Formatting for Queries: Never use f-strings or concatenation like f"SELECT * FROM users WHERE id = {user_id}". This invites SQL injection. Always use parameterized queries.
  • Ignoring Resource Cleanup: Failing to close cursors and connections can lead to memory leaks or locked databases. Use context managers (with) where possible.
  • Loading Too Much Data: Using fetchall() on a table with millions of rows will crash your application due to high memory usage. Use fetchmany(size) or iterate over the cursor directly.
  • Assuming Column Order: Relying on index positions (e.g., row[0]) makes code fragile if the schema changes. Consider mapping results to dictionaries if the driver supports it.

When to use it

MethodBest ForMemory Impact
fetchall()Small result sets (< 10k rows)High (loads everything)
fetchone()Single record lookupsLow
Iterating CursorLarge datasets / StreamingMinimal (row-by-row)

Use fetchall() only when you are certain the dataset is small. For large reports or exports, iterate through the cursor to process rows one at a time without loading the entire table into RAM.

Practice

Guided Exercise: Modify the example above to select only the name column for users whose email ends with '@gmail.com'. Hint: Use LIKE '%@gmail.com' in the SQL string and pass no parameters if the pattern is static, or use parameters for dynamic patterns.

Challenge: Write a function that accepts a minimum age and returns a list of names. Ensure it handles cases where no users match the criteria gracefully (returns an empty list).

Quick check

Question: Why should you avoid using cur.execute("SELECT * FROM users WHERE id = " + str(user_id))?
Answer: This creates a vulnerability to SQL injection attacks because user input is concatenated directly into the SQL command string. Always use parameterized queries instead.

Summary

Select queries are fundamental for reading data in Python applications. By using parameterized queries and appropriate fetch methods, you ensure security, efficiency, and robustness in your database interactions.

Want to go beyond the notes?

Join Coding Now Tech Institute's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Select Query – FAQs

Quick answers about learning Select Query in Python.

This free note from Coding Now Tech Institute explains Select Query in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on Coding Now Tech Institute, including Select Query, is 100% free with no signup required.
With focused practice, most students grasp Select Query in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now