1

My question is why when I input the query

SELECT * FROM movies WHERE year BETWEEN 1990 AND 2000;

includes the year 2000 however when I input the query:

SELECT * FROM movies WHERE name BETWEEN 'A' and 'J'

movies that begin with J are not included. I don't understand the inconsistency with how the between function works when looking at letters versus numbers. I understand that you could use the % to include it but I would like to know why it works this way.

MattCom
  • 241
  • 6
  • 19

3 Answers3

2

In short, because J is not equal to Jumanji. The BETWEEN A AND B keyword is equivalent to >= A AND <= B

Stavr00
  • 3,219
  • 1
  • 16
  • 28
1

It has nothing to do with SQL.
Everything starts with J (e.g. JAVA) is alphabetically greater than J itself.

David דודו Markovitz
  • 42,900
  • 6
  • 64
  • 88
0

As Stavr mentions, SQL does not infer additional characters other than the literals you used.

SELECT * FROM
(
    SELECT 'A Few Good Men' As Movie
        UNION ALL 
    SELECT 'Back to the Future' As Movie
        UNION ALL 
    SELECT 'Call of the Wild' As Movie
        UNION ALL 
        SELECT 'Captain America' As Movie
        UNION ALL 
    SELECT 'D Movie' As Movie
        UNION ALL 
    SELECT 'E Movie' As Movie
        UNION ALL 
    SELECT 'F Movie' As Movie
        UNION ALL 
    SELECT 'G Movie' As Movie
        UNION ALL 
    SELECT 'H Movie' As Movie
        UNION ALL 
    SELECT 'I Movie' As Movie
        UNION ALL 
    SELECT 'J Movie' As Movie
        UNION ALL 
    SELECT 'J Movie 2' As Movie
) it 
WHERE Movie BETWEEN 'A' AND 'J%'
DanielG
  • 1,669
  • 1
  • 12
  • 26