开发者

Trouble with oracle sql query

I am trying to make a query of

"What are the names of the producers with a开发者_运维技巧t least 2 properties with areas with less than 10"

I have made the following query that seems to work:

select Producers.name
from Producers
where (
  select count(Properties.prop_id)
  from Properties
  where Properties.area < 10 and Properties.owner = Properties.nif
) >= 2;

yet, my lecturer was not very happy about it. He even thought (at least gave me the impression of) that this kind of queries wouldn't be valid in oracle.

How should one make this query, then? (I have at the moment no way of getting to speak with him btw).

Here are the tables:

Producer (nif (pk), name, ...)

Property (area, owner (fk to producer), area, ... )


The having clause is typically used to filter on aggregate data (like counts, sums, max, etc).

select
  producers.name,
  count(*)
from
  producers,
  property
where
  producers.nif = property.owner and
  property.area < 10
group by
  producers.name
having
  count(*) >= 2


select P.name
from Producers p, Properties pr
where p.nif = pr.Owner
AND Properties.area < 10 
GROUP BY Producers.name
having Count(*) >= 2 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜