SQLite - getting number of rows in a database
I want to get a number of rows in my table using max(id)
. When it returns NULL
- if there are no rows in the table - I want to return 0. And when there are rows I want to return max(id) + 1
.
My rows are being numbered from 0 and autoincreased.
Here is my statement:
SELECT CASE WHEN MAX(id) != NULL THEN (MAX(id) + 1) ELSE 0 END FROM words
But it is always returning me 0. What have I done wrong?
You can query the actual number of rows with
SELECT Count(*) FROM tblNamesee https://www.w3schools.com/sql/sql_count_avg_sum.asp
If you want to use the MAX(id) instead of the count, after reading the comments from Pax then the following SQL will give you what you want
SELECT COALESCE(MAX(id)+1, 0) FROM words