SQL query - count with subquery
source link: https://www.codesd.com/item/sql-query-count-with-subquery.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
SQL query - count with subquery
I want to get the count of all the columns that are retrieved in the query.I have used the below code:
select count (*)
from (
select distinct ID,salary,name,location
from test
) ;
I am getting an error message:
Incorrect error at ; expecting AS,ID or quoted_ID
When I add as below:
select count (*)
from (
select distinct ID,salary,name,location
from test
) as count;
The query works now but the column name is not renamed to the alias given. What is the logic behind this?
In Sql you have to give alias to subquery. So that's the reason the second query works and first one fails
Related Articles
SQL Query COUNT with Two Joins
Rewrite the SQL query without subqueries (subquery in a clause)
SQL combining counts with joins
SQL query, count two columns based on the same identifier
The LINQ to SQL query fails with two statements Contains
Why does the following linq to sql query generate a subquery?
Fusion table The SQL query fails with the numbers
Format a SQL query result with an arbitrary number of decimals
SQL query help with bridge table
T-SQL migration of Access SQL Query running with a problem writing replacement CASE for IIF
SQL Delete from with subquery error
How to convert SQL select Count with group by clauses in LINQ?
SQL Query - Select with Total Column
Sql query custom with sql with pagination
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK