Most recent backup for each database

This technical problem deals with finding the most recent backup for each database on a server. Given a table of backup information, the goal is to write a query that returns the most recent backup for each database.

Problem

Write a query to find the most recent backup for each database on a server.
Example input:
Backup table:
id | database | start_time | end_time
1 | db1 | 2020-01-01 12:00:00 | 2020-01-01 12:05:00
2 | db2 | 2020-01-01 12:10:00 | 2020-01-01 12:15:00
3 | db1 | 2020-01-02 12:00:00 | 2020-01-02 12:05:00
4 | db2 | 2020-01-02 12:10:00 | 2020-01-02 12:15:00
Example output:
id | database | start_time | end_time
3 | db1 | 2020-01-02 12:00:00 | 2020-01-02 12:05:00
4 | db2 | 2020-01-02 12:10:00 | 2020-01-02 12:15:00

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by AskAI
SELECT *
FROM backup
WHERE start_time = (SELECT MAX(start_time) FROM backup GROUP BY database)

/*

The solution above is optimal because it uses a subquery to find the most recent start_time for each database.

*/

A.I. Evaluation of the Solution

The solution above is optimal because it uses a subquery to find the most recent start_time for each database.

Evaluated at: 2022-11-07 23:34:50