Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select count(*) from select

I am trying to get row count from the following query. I get only row count as 1 but there are 35 records. Could you please let me know how to get the count from inner query?

Thank you

SELECT COUNT(*)(SELECT DISTINCT a.my_id, a.last_name, a.first_name, b.temp_val                 FROM Table_A a INNER JOIN Table_B b on a.a_id = b.a_id) 
like image 240
nav100 Avatar asked May 03 '12 20:05

nav100


People also ask

What is SELECT count (*) in SQL?

SQL SELECT COUNT(*) function The COUNT(*) function represents the count of all rows present in the table (including the NULL and NON-NULL values).

What the will be result of SELECT count (*) from table?

SELECT COUNT(*) FROM table_name; The COUNT(*) function will return the total number of items in that group including NULL values. The FROM clause in SQL specifies which table we want to list. You can also use the ALL keyword in the COUNT function.

How do I write a SELECT count in SQL query?

Syntax of Select Count Function in SQL In the syntax, we have to specify the column's name after the COUNT keyword and the name of the table on which the Count function is to be executed.

How do I count rows from a SELECT query?

The SQL COUNT() function returns the number of rows in a table satisfying the criteria specified in the WHERE clause. It sets the number of rows or non NULL column values. COUNT() returns 0 if there were no matching rows.


1 Answers

You're missing a FROM and you need to give the subquery an alias.

SELECT COUNT(*) FROM  (   SELECT DISTINCT a.my_id, a.last_name, a.first_name, b.temp_val    FROM dbo.Table_A AS a     INNER JOIN dbo.Table_B AS b     ON a.a_id = b.a_id ) AS subquery; 
like image 166
Aaron Bertrand Avatar answered Oct 06 '22 23:10

Aaron Bertrand