SELECT — Python Technical Interview Questions & Answers

SELECT

SELECT

  • SELECT is about talking to a database from Python. Pattern is always: connect → cursor → execute → fetch or commit → close.
  • For SELECT: use ? placeholders (or the driver’s params). Never build SQL with + user text — that is injection. sqlite3 is in the stdlib for practice; MySQL/Mongo need a driver.
  • SELECT on the board: insert Asha, select her back, print the name. Same idea on MySQL. Close the connection.
  • SELECT — output: [('Asha',)]. fetchall reads rows.

Technical Interview Tip: Steps in order: connect → execute → fetch/commit → close. Mention placeholders.

Code Examples for SELECT

Example

# SELECT
import sqlite3
con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.execute("create table t(name text)")
cur.execute("insert into t values('Asha')")
print(cur.execute("select name from t").fetchall())
con.close()

SELECT — output: [('Asha',)]. fetchall reads rows.

How to Explain SELECT to an Interviewer

Explain SELECT as if you are teaching a junior — definition, then one tiny script.

SELECT uses connect + cursor + SQL/API. SELECT — parameterized queries. Never + user text into SQL.

What does the example print, and why?

SELECT — output: [('Asha',)]

What mistake do freshers make with SELECT?

SELECT — commit after write. close when done.