BETWEEN demands arguments in ascending order? Why?

Asked

Viewed 268 times

6

I have the following dummy table.

inserir a descrição da imagem aqui

I created the following query:

SELECT * FROM ALUNOS
WHERE IDADE BETWEEN 10 AND 15

Students A, C and D return.

Now, because when I invert the order in the BETWEEN

SELECT * FROM ALUNOS
WHERE IDADE BETWEEN 15 AND 10

Returns nothing?

  • because the between always picks up [higher] => [lower] as 15 > 10, it invalidates beetween. because there is no 15 to 10 interval.

  • It’s okay that the command has as default pick the smaller - > larger. Now between 10 and 15 I have {10,11,12,13,14,15} and between 15 and 10 I have {15,14,13,12,11,10}.. the same interval however in another order... Thank you for the reply

  • Thanks for the answers. Doubt clarified after a mathematical understanding. Thank you

  • 1

    Enio, the problem is that it is so because it is so. I don’t know if you have a better answer than that. I could consider the opposite too, this would be up to a hand on the wheel in certain cases, but that’s not how it works in SQL.

4 answers

7


First of all, I understand that there is some logical motivation in the question. If something is between 1 and 3, in principle it is between 3 and 1.

What is not true if it is a trade that opens from 22h to 6h in the morning. Between 6h and 22h it will be closed. Between 22h and 6h will be open. In other words, concepts are often relativized.

That said, let’s answer the question:

It is so, because who defined the function wanted so. The interpretation of the creator(s) of the function is that the data will be in ascending order. It’s not about them having a better mind than you or the other way around. If you create a language and define that your BETWEEN will work independent of parameter order, I believe you will be right too.

Anyway, if you need to know independent order, just do this:

a BETWEEN( b, c ) OR a BETWEEN( c, b )

It may not be as elegant as calling a function only, but in return, knowing the "limitation" of the function, it may be useful at some point when the order is important.

Exaggerating: I could make a third language with a BETWEEN that gave inverted result if the parameters were reversed, it would be perfect in the case of the example I gave of working hours. 10 BETWEEN( 5, 15 ) => true and 20 BETWEEN ( 15, 5 ) => true (as if reversing the parameters, worth the "turn" outside the numbering, starting from 15 and going to the positive infinity, and from the negative infinity ending in 5)

In the end, what counts is documentation. And when it’s up to date ;)

  • 1

    Saved the day. Thank you very much for the reply

5

1

It’s just a pattern.

But I believe it was so standardized because the reverse would give the same result.

It would be redundant and generate process costs when implementing in the compiler which parameter is higher or lower.

However simple and imperceptible the gain in performance is, saving 00000000000000.1s is a good saving.

1

Your break is incorrect.

There is no interval from 15 to 10.

BETWEEN is used to select values in a range.

If your goal is to select records that are not in this range you should use NOT BETWEEN so it will bring all the records that NAY are in their interval (remembering that the initial interval should be less than the final interval).

  • 1

    It’s okay that the command has as default pick the smaller - > larger. Now between 10 and 15 I have {10,11,12,13,14,15} and between 15 and 10 I have {15,14,13,12,11,10}.. the same interval however in another order... Thank you for the reply

Browser other questions tagged

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