MySql Subquery

1

I have an employee table that has only three columns, NOME , CARGO and SALARIO , I want to set up a query that brings me this whole table and contains the highest salary value for the job. Totalizing 4 Columns ( NOME , CARGO , SALARIO and MAIOR_SALARIO_P_CARGO This is to check the variation.

    
asked by anonymous 15.05.2014 / 16:59

2 answers

2
SELECT
    T1.NOME,
    T1.CARGO,
    T1.SALARIO_ATUAL,
    (
        SELECT MAX(SALARIO_ATUAL)
        FROM TABELA T2
        WHERE T2.CARGO = T1.CARGO
    ) AS MAIOR_SALARIO_PARA_CARGO
FROM TABELA T1
    
15.05.2014 / 18:01
1

Answers:

SubQuery

SELECT a.nome, a.cargo, a.salario, (select max(salario) from tbsalarios b where b.cargo = a.cargo) maiorsalario FROM tbsalarios a

JOIN

SELECT a.nome, 
       a.cargo, 
       a.salario, 
       b.maior 
FROM tbsalarios a 
LEFT JOIN 
      (select max(salario) maior, cargo from tbsalarios group by cargo) as b on b.cargo = a.cargo
    
15.05.2014 / 17:56