Subquery Within the WHERE Clause

A subquery refers to the inclusion of one or more nested queries within an original query.

select CFPersona
from PersonaLavoro
where stipendio = (select max(stipendio) from PersonaLavoro)

Within the where clause, there is a nested query designed to retrieve the maximum value of the stipendio field from the PersonaLavoro table.

Subquery Within the SELECT Clause

Consider the case where you want to display a table of the following format:

LavoroPercentualeLavoratori
Sviluppatore Web Senior33,333
Sviluppatore Web Junior33,333
Chirurgo33,333
Segretaria/o0
Manager0

This forms pairs (Job, Percentage of people doing that job relative to the total number of people who have a job in the database).

select l.Nome
	( (
		--Sottoquery che ritorna il numero di persone che effetutano il lavoro l
		select count(*) 
		from PersonaLavoro pl
		where l.ID = pl.IDLavoro
	) as PersoneCheEffettuanoIlLavoro
	/
	(
		--Sottoquery che ritorna il numero di persone (distinte) presenti in PersonaLavoro
		--Ovvero il numero totale di persone che lavorano
		select count(distinct CFPersona)
		from PersonaLavoro
	) as PersoneCheLavorano
	* 100) as PercentualeLavoratori
	--Il valore (PersoneCheEffettuanoIlLavoro / PersoneCheLavorano) * 100 
	--ritornala percentuale di persone che effettuano quel lavoro
	--Tale valore viene chiamato PercentualeLavoratori
from Lavoro l

Subquery Within the FROM Clause

A subquery within the from clause is usually used when you want to perform an (implicit) join with a table created on-the-fly.

Here is an example where the count of the cheeses sold by the dairy that processed the largest quantity of milk is returned.

  • Schema logico dell’Esercizio Caseificio Caseificio(PK: id_caseificio, nome, indirizzo, lati, longi, nome_titolare, provincia) GiornoProduzione(q_lat_lavorato, q_forme, forme_vendute, PK: data, PK: cod_caseificio) Forme(PK: id_forma, data_produzione, cod_caseificio) FormeVendute(qualita, nome_acquirente, tipo_acquirente, data_ve, stagionatura, PK: cod_forma) Immagine(FK: cod_caseificio, PK: URL)
--Query finale
select count(*)
from FormaVenduta fv, Forma f, (
    select c.CodiceID, c.Nome
    from Caseificio c, Lavorazione lav
    where c.CodiceID = lav.CodiceIDCaseificio
    group by c.CodiceId, c.Nome
    order by sum(lav.LatteLavorato)
    limit 1
) as clm
--In clm è presente il CodiceID e il Nome del caseificio 
--che ha lavorato la maggior quantitĂ  di latte
where fv.NumeroProgressivo = f.NumeroProgressivo and
    f.CodiceIDCaseificio = clm.CodiceID

Here is an equivalent solution using subqueries within the where clause

select count(*)
from Caseificio c, Forma f, 
	FormaVenduta fv, Lavorazione lav
where c.CodiceId = lav.CodiceIdCaseificio and
	fv.NumeroProgressivo = f.NumeroProgressivo and
	f.CodiceIdCaseificio = lav.CodiceIdCaseificio and
	lav.LatteLavorato = (select max(LatteLavorato) from Lavorazione)
  --Seleziono solo le forme del caseificio che ha lavorato la maggior quantitĂ  di latte