Problem in SQL selection

Asked

Viewed 36 times

1

Why even if I filter to return only the fiat manufacturer, keeps returning other values?

 SELECT FABRICANTE, MODELO, VALOR_DIARIA FROM VEICULO 
    WHERE VALOR_DIARIA = (SELECT MIN(VALOR_DIARIA) FROM VEICULO WHERE FABRICANTE = 'FIAT')

Upshot:

FABRICANTE                     MODELO                         VALOR_DIARIA
------------------------------ ------------------------------ ------------
VOLKSWAGEN                     GOL                                      80
FIAT                           PALIO                                    80

1 answer

4


The problem is that you used a sub-volume to filter the values, which is not necessary. In this case, the value of FIAT’s daily rate is equal to that of VOLKSWAGEN and, as the sub-volume returns "80", it brings both. The correct would simply be:

SELECT FABRICANTE, MODELO, VALOR_DIARIA FROM VEICULO WHERE FABRICANTE = 'FIAT';

Ideally, you should use a filter that uses fields that are primary keys and avoid filtering directly into text to find exact values. Either way this is gonna work.

In case you need to choose the lowest value, you can add one more criterion, type like this:

SELECT FABRICANTE, MODELO, VALOR_DIARIA FROM VEICULO WHERE FABRICANTE = 'FIAT' AND VALOR_DIARIA = (SELECT MIN(VALOR_DIARIA) FROM VEICULO WHERE FABRICANTE = 'FIAT');

Or any other criteria prioritizing the record you need.

It can be done also the limitation of lines, forcing the bank to bring the first line. Type so:

SELECT FABRICANTE, MODELO, VALOR_DIARIA FROM VEICULO WHERE FABRICANTE = 'FIAT' AND VALOR_DIARIA = (SELECT MIN(VALOR_DIARIA) FROM VEICULO WHERE FABRICANTE = 'FIAT') LIMIT 1  

Hug!

  • but the exercise asks me to present the manufacturer and the vehicle model with lower daily value and I wanted to filter to appear only fiat car

  • In this case you will have to use one more criterion. Ask yourself the question: If there is more than one with the same daily rate, what to do? Present both? Prioritize some?

  • haha gave right friend, thank you very much, by the way I could not pass in the correct way what I want to the bank, I appreciate the attention!!

  • Glad you decided! You’re welcome! What you need we’re here! Hug!

Browser other questions tagged

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