Syllabus Lesson 67 of 239 · SQL & Databases
SQL & Databases

SELECT: Asking a Database Questions

So far your data vanished when the program ended. A database stores it and lets you ask questions in a language built for exactly that: SQL. Every backend, analytics stack and RAG metadata store speaks it, and it runs right here through Python's built-in sqlite3.

Data lives in tables (rows and columns). You read with SELECT:

SELECT name FROM products;          -- one column
SELECT name, price FROM products;   -- two
SELECT * FROM products;             -- every column

In Python you .execute(sql) on a connection and read the rows; each row is a tuple of the columns you asked for:

cur = con.execute("SELECT name FROM products")
for row in cur.fetchall():
    print(row[0])   # the name

Write all_names(con) that runs SELECT name FROM products and returns a plain list of the name strings, in row order. The connection is handed to you. Press Run.

Your turn

Write all_names(con) that executes SELECT name FROM products and returns a list of the product name strings (e.g. ['Pen', 'Notebook', 'Lamp']), in row order. Unpack column 0 from each row.

Spotted a problem in this lesson? Report it

Code · runs in your browser
Output