sql – Select max em nested queries

Question:

I have to select the candidate who has more publications than any other candidate.

select candidato.nome, Count(1) as nr_publicacoes 
from candidato, cv,ficha_inscricao, publicacao_cv 
where candidato.bi = ficha_inscricao.bi and 
      candidato.bi = cv.bi and cv.cod_cv = publicacao_cv.cod_cv 
group by candidato.nome;

But when I max, Oral SQL Developer does not give results or error messages. I don't want to use rownum , my goal is to solve using max .

Answer:

In a chat conversation, @DanielaMaia said that ROWNUM could not be used, only MAX. So the original answer has been replaced with this one now.

To avoid using ROWNUM, a CTE ( Common Table Expression ) can be used, as shown below:

with publicacao_cte (nome,nr_publicacoes)
AS
(
    select
        candidato.nome,
        Count(1) as nr_publicacoes
    from
        candidato,
        cv,
        ficha_inscricao,
        publicacao_cv
    where
        candidato.bi = ficha_inscricao.bi and
        candidato.bi = cv.bi and
        cv.cod_cv = publicacao_cv.cod_cv
    group by 
        candidato.nome
)

select nome
from publicacao_cte
where publicacao_cte.nr_publicacoes = (select max(nr_publicacoes) from publicacao_cte);
Scroll to Top
AllEscort