How to select the name and salary of the employee of 'MG' who receives the highest salary?

Asked

Viewed 296 times

2

I have a table with columns nome, salario and alocacao.

How do I select only the worker who has the highest salary and who has the value mg in the column alocacao, unused MAX()?

1 answer

6

SELECT 
     nome, salario 
FROM empregados 
WHERE alocacao="mg" 
ORDER BY salario DESC 
LIMIT 1

Split:

  • nome, salario, the columns you want to appear in the result
  • FROM empregados, the table name
  • WHERE alocacao="mg", the condition to check, ie. only bring result(s) that had(m) the value of the column alocacao equal to `mg``
  • ORDER BY salario, sort by column value salario
  • DESC, make the ordination be in descending order (from the largest to the smallest)
  • LIMIT 1, bring only one result

Browser other questions tagged

You are not signed in. Login or sign up in order to post.