Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query Syntax : Using table alias in a count is invalid? Why?

Could someone please explain to me why the following query is invalid? I'm running this query against an Oracle 10g database.

select count(test.*) from my_table test;

I get the following error: ORA-01747: invalid user.table.column, table.column, or column specification

however, the following two queries are valid.

select count(test.column) from my_table test;

select test.* from my_table test;
like image 924
contactmatt Avatar asked Dec 12 '25 07:12

contactmatt


2 Answers

COUNT(expression) will count all rows where expression is not null. COUNT(*) is an exception, it returns the number of rows: * is not an alias for my_table.*.

like image 199
Vincent Malgrat Avatar answered Dec 14 '25 22:12

Vincent Malgrat


As far as I know, Count(Table.*) is not officially supported in the SQL specification. Only Count(*) (count all rows returned) and Count(Table.ColumnName) (count all non-null values in the given column). So, even if the DBMS supported it, I would recommend against using it.`

like image 26
Thomas Avatar answered Dec 14 '25 22:12

Thomas