Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server subquery syntax

Tags:

sql

sql-server

When I run the query :

select count(*) from  (select idCover from x90..dimCover group by idCover having count(*) > 1)  

I get the error :

Server: Msg 170, Level 15, State 1, Line 2 Line 2: Incorrect syntax near ')' 

How do I formulate this query correctly?

I'm on SQL Server 2000

like image 275
cindi Avatar asked Jul 09 '09 09:07

cindi


People also ask

What is subquery syntax in SQL?

A Subquery or Inner query or a Nested query is a query within another SQL query and embedded within the WHERE clause. A subquery is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved.

What is subquery in SQL Server with examples?

A subquery is a query that is nested inside a SELECT , INSERT , UPDATE , or DELETE statement, or inside another subquery. The samples in this article use the AdventureWorks2016 database available for download at AdventureWorks sample databases. A subquery can be used anywhere an expression is allowed.

How use subquery FROM clause in SQL Server?

From clause can be used to specify a sub-query expression in SQL. The relation produced by the sub-query is then used as a new relation on which the outer query is applied. Sub queries in the from clause are supported by most of the SQL implementations.


2 Answers

Add an alias after your last bracket.

select count(*) from  (select idCover from x90..dimCover group by idCover having count(*) > 1) a 
like image 136
D'Arcy Rittich Avatar answered Sep 29 '22 11:09

D'Arcy Rittich


SELECT COUNT (*) FROM  ( SELECT IdCover FROM x90..dimCover group by idCover having count(*) > 1) AS a 

(note the alias at the end)

like image 36
Frederik Gheysels Avatar answered Sep 29 '22 11:09

Frederik Gheysels