Pattern interview question

Optimize this SQL query so it doesn't have to call the database one row at a time.