# Buggy code
def _atime_expr(self):
    """If max_items is set, return an expression to limit the query."""
    max_items = config.get("completion", "web-history-max-items")
    # HistoryCategory should not be added to the completion in that case.
    assert max_items != 0

    if max_items < 0:
        return ""

    min_atime = (
        sql.Query(
            " ".join(
                [
                    "SELECT min(last_atime) FROM",
                    "(SELECT last_atime FROM CompletionHistory",
                    "ORDER BY last_atime DESC LIMIT :limit)",
                ]
            )
        )
        .run(limit=max_items)
        .value()
    )

    return "AND last_atime >= {}".format(min_atime)


# Traceback (qutebrowser.misc.sql.SqlException)
12:21:36 DEBUG    sql        sql:__init__:80 Preparing SQL query: "SELECT min(last_atime) FROM (SELECT last_atime FROM CompletionHistory ORDER BY last_atime DESC LIMIT :limit)"
12:21:36 DEBUG    sql        sql:run:99 Running SQL query: "SELECT min(last_atime) FROM (SELECT last_atime FROM CompletionHistory ORDER BY last_atime DESC LIMIT :limit)"
12:21:36 DEBUG    sql        sql:run:102 query bindings: {':limit': 1000}
12:21:36 DEBUG    sql        sql:__init__:80 Preparing SQL query: "SELECT url, title, strftime('%Y-%m-%d', last_atime, 'unixepoch', 'localtime') FROM CompletionHistory WHERE (url LIKE :pat escape '\' or title LIKE :pat escape '\') AND last_atime >=  ORDER BY last_atime DESC"
12:21:36 DEBUG    completion debug:__exit__:264 Starting url completion took 0.003652 seconds.
12:21:36 ERROR    misc       crashsignal:exception_hook:205 Uncaught exception
Traceback (most recent call last):
File "/home/florian/proj/qutebrowser/git/qutebrowser/completion/completer.py", line 236, in _update_completion
model = func(*args)
File "/home/florian/proj/qutebrowser/git/qutebrowser/completion/models/urlmodel.py", line 70, in url
hist_cat = histcategory.HistoryCategory(delete_func=_delete_history)
File "/home/florian/proj/qutebrowser/git/qutebrowser/completion/models/histcategory.py", line 54, in __init__
]), forward_only=False)
File "/home/florian/proj/qutebrowser/git/qutebrowser/misc/sql.py", line 83, in __init__
querystr, self.lastError().text()))
qutebrowser.misc.sql.SqlException: Failed to prepare query "SELECT url, title, strftime('%Y-%m-%d', last_atime, 'unixepoch', 'localtime') FROM CompletionHistory WHERE (url LIKE :pat escape '\' or title LIKE :pat escape '\') AND last_atime >=  ORDER BY last_atime DESC": "near "ORDER": syntax error Unable to execute statement"

# Fixed code
def _atime_expr(self):
    """If max_items is set, return an expression to limit the query."""
    max_items = config.get("completion", "web-history-max-items")
    # HistoryCategory should not be added to the completion in that case.
    assert max_items != 0

    if max_items < 0:
        return ""

    min_atime = (
        sql.Query(
            " ".join(
                [
                    "SELECT min(last_atime) FROM",
                    "(SELECT last_atime FROM CompletionHistory",
                    "ORDER BY last_atime DESC LIMIT :limit)",
                ]
            )
        )
        .run(limit=max_items)
        .value()
    )

    if not min_atime:
        # if there are no history items, min_atime may be '' (issue #2849)
        return ""

    return "AND last_atime >= {}".format(min_atime)