Maybe I'm missing something here. How can that ever return 0? If I have understood it correctly, this is what the query will do: Select the rows from TABLES with type "BASE TABLE" Group them by TABLE_NAME Counts the number of rows for each TABLE_NAME If the count is 0, return 0 If the count is not 0, return the identity of the table But, surely the only way COUNT(TABLE_NAME) can be 0 is if there are no rows in TABLES with this TABLE_NAME and type "BASE TABLE". In which case, this query would never pick up that table name, so it would never appear in the result set. So for any TABLE_NAME that this query picks up, the count will always be greater than 0.