The operators ALL and ANY are used with subqueries.

  • ALL: returns true if the condition is satisfied for every row of the subquery.
  • ANY: returns true if the condition is satisfied for at least one row of the subquery.

Here’s a query that returns the most common name (the name that appears most frequently compared to all others in the Persona table):

--Il numero delle volte che ogni nome è presente
select Nome
from Persona
group by Nome
--Prende solamente il nome che ha il conteggio maggiore o uguale a tutti gli altri
having count(*) >= ALL (
	--Il numero delle volte che ogni nome è presente
	select count(*)
	from Persona
	group by Nome
	);

Here’s a query that returns the data of people who are older (strictly) than those who have a job:

--Dati delle persone
select p.*
from Persona p
--Seleziona la persona solo se ha un'età maggiore di almeno una delle persone presenti in PersoneLavoro
where p.Eta > any (
	--Eta delle persone che sono presenti in PersoneLavoro
	select p.eta
	from Persona p, PersonaLavoro pl
	where p.CF = pl.CFPersona
);

tags: databases SQL