Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT *, COUNT(*) in SQLite

Tags:

If i perform a standard query in SQLite:

SELECT * FROM my_table 

I get all records in my table as expected. If i perform following query:

SELECT *, 1 FROM my_table 

I get all records as expected with rightmost column holding '1' in all records. But if i perform the query:

SELECT *, COUNT(*) FROM my_table 

I get only ONE row (with rightmost column is a correct count). Why is such results? I'm not very good in SQL, maybe such behavior is expected? It seems very strange and unlogical to me :(.

like image 855
grigoryvp Avatar asked Sep 01 '10 06:09

grigoryvp


People also ask

What does COUNT do in SQLite?

The COUNT(*) function returns the number of rows in a table, including the rows including NULL and duplicates.

What does select COUNT (*) mean in SQL?

COUNT(*) returns the number of rows in a specified table, and it preserves duplicate rows. It counts each row separately. This includes rows that contain null values.

How do I COUNT records in SQLite?

In SQLite the Count(*) function will return total number of rows available in a table, including the rows which contain NULL values. The Count(*) will not take any parameters other than the asterisk symbol (*). Now we will see how to use SQLite count(*) on emp_master to get total number of records in table.

How do I select multiple columns in SQLite?

To select multiple columns from a table, simply separate the column names with commas! For example, this query selects two columns, name and birthdate , from the people table: SELECT name, birthdate FROM people; Sometimes, you may want to select all columns from a table.


2 Answers

SELECT *, COUNT(*) FROM my_table is not what you want, and it's not really valid SQL, you have to group by all the columns that's not an aggregate.

You'd want something like

SELECT somecolumn,someothercolumn, COUNT(*)     FROM my_table  GROUP BY somecolumn,someothercolumn 
like image 189
nos Avatar answered Oct 06 '22 18:10

nos


If you want to count the number of records in your table, simply run:

    SELECT COUNT(*) FROM your_table; 
like image 25
rafaelbattesti Avatar answered Oct 06 '22 19:10

rafaelbattesti