twegner
twegner

Reputation: 443

sqlite - return all columns for max of one column without repeats

Im using Python to query a SQL database. I'm fairly new with databases. I've tried looking up this question, but I can't find a similar enough question to get the right answer.

I have a table with multiple columns/rows. I want to find the MAX of a single column, I want ALL columns returned (the entire ROW), and I want only one instance of the MAX. Right now I'm getting ten ROWS returned, because the MAX is repeated ten times. I only want one ROW returned.

The query strings I've tried so far:

sql = 'select max(f) from cbar'  
# this returns one ROW, but only a single COLUMN (a single value)

sql = 'select * from cbar where f = (select max(f) from cbar)'  
# this returns all COLUMNS, but it also returns multiple ROWS

I've tried a bunch more, but they returned nothing. They weren't right somehow. That's the problem, I'm too new to find the middle ground between my two working query statements.

Upvotes: 0

Views: 436

Answers (1)

CL.
CL.

Reputation: 180280

In SQLite 3.7.11 or later, you can just retrieve all columns together with the maximum value:

SELECT *, max(f) FROM cbar;

But your Python might be too old. In the general case, you can sort the table by that column, and then just read the first row:

SELECT * FROM cbar ORDER BY f DESC LIMIT 1;

Upvotes: 1

Related Questions