Python Practice › SQLite Programs › Program 4
Select with a condition
Write a Python program to select students who scored above 80.
Output
Asha 91 Meena 88
Approach
WHERE marks > ?.
Solution
import sqlite3 conn = sqlite3.connect(":memory:")conn.execute("CREATE TABLE students (name TEXT, marks INTEGER)")conn.executemany("INSERT INTO students VALUES (?, ?)", [("Asha", 91), ("Ravi", 74), ("Meena", 88)])for name, marks in conn.execute("SELECT name, marks FROM students WHERE marks > ? ORDER BY marks DESC", (80,)): print(name, marks)Output
Asha 91 Meena 88
Learn the concept
New to this topic? Read the lesson, then come back.