Query Top Scores from SQLite
Medium
You have a SQLite table "scores" with columns (id, player, score). Write a function top_players(conn, limit) that returns the top `limit` players ordered by score descending, as a list of (player, score) tuples, using a parameterized query.
Input
A sqlite3 connection object and an integer limit.
Output
A list of (player, score) tuples, highest scores first.
Example
Input
top_players(conn, 2) with rows [("Alice",90), ("Bob",75), ("Carl",95)]
Output
[('Carl', 95), ('Alice', 90)]
Use "SELECT player, score FROM scores ORDER BY score DESC LIMIT ?" with cursor.execute() and fetchall().
15 XP on solve
Back to lesson
Your Solution
Output
Click "Run" to execute your code...
⏳ Loading Python runtime (first run may take a few seconds)...
Log in to submit your solution and earn XP.