FAQ: Queries - Between

Community%20FAQs%20on%20Codecademy%20Exercises

This community-built FAQ covers the “Between” exercise from the lesson “Queries”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development
Data Science

Learn SQL

FAQs on the exercise Between

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

So how do you include ‘Z’, if last letters are not inclusive?

5 Likes

If we look at the ordinals, we see that a comes after Z, so,

WHERE name IS BETWEEN 'W' AND 'a';
Query Results
id 	name 	genre 	year 	imdb_rating
142 	Warm Bodies 	horror 	2013 	6.9
89 	Wedding Crashers 	comedy 	2005 	7.0
99 	What Lies Beneath 	horror 	2000 	6.6
162 	What Women Want 	romance 	2000 	6.4
144 	Wolf 	horror 	1994 	6.2
98 	World War Z 	horror 	2013 	7.0
48 	X-Men: Days of Future Past 	action 	2014 	8.1
47 	X-Men: The Last Stand 	action 	2006 	6.8
130 	Zombieland 	horror 	2009 	7.7
3 Likes

how to use between operator to search a movies where imdb_rating between 8 to 7?

Hi! In the second exercise, the 70’s are between 1971 and 1980. Decades, centuries start in “XXX1” and end in “XXY0”. So, it’s not between 1970 and 1979.

2 Likes

You would use after the WHERE clause to define a range for the condition. Remember that the BETWEEN clause takes 2 numbers (“from” and “up to but not including”) :

SELECT * FROM movies WHERE imdb_rating BETWEEN 7 AND 9;

This takes all data from a table called movies that have an imdb_rating between 7 and 8

In this statement, the BETWEEN operator is being used to filter the result set to only include movies with year s between 1990 up to, and including 1999, describes BETWEEN as being upper-bound inclusive.

So, then why is SELECT * FROM movies WHERE name BETWEEN 'D' AND 'G'; the correct solution to the first problem. If it was up to and including F, it would be.

SELECT * FROM movies WHERE name BETWEEN 'D' AND 'F';

The assignment is; Using the BETWEEN operator, write a query that selects all information about movies whose name begins with the letters ‘D’, ‘E’, and ‘F’.

1 Like

Even easier is simply using either greater than or less than operators. IE

WHERE name >= 'W'
1 Like

So think about what it would mean to be inclusive when looking at a string of text. The BETWEEN keyword is indeed inclusive, but when you include an upper bound of 'F' that means that everything up to and including 'F' would be included. However, including 'F' does not mean including 'Farm' or any other word that begins with 'F'. Since 'Farm' > 'F', BETWEEN being inclusive of its upper bound of 'F' does not also include other words beginning with 'F'.

In the previous lesson, we learn that we can use wildcards to select entries or records that begin with a certain letter/phrase (e.g., ‘A%’ or ‘%man%’ to find records that start with ‘A’ or that have ‘man’ somewhere in them). Why doesn’t BETWEEN need to use this syntax too? Is it just inferred because ranges imply that entries are consecutive?

1 Like

If we can use inequality operators for string values, then I don’t see the point of using BETWEEN operator, which is just confusing…

1 Like

So I understand why the BETWEEN operator pulls up movies up to the letter G, but why does it not work to do a % wildcard to achieve the same effect? Ex:

SELECT * FROM movies WHERE name BETWEEN ‘D’ AND ‘F%’;

Shouldn’t that give all movies that begin with F? It doesn’t work like that when I have tried, at least.

1 Like
BETWEEN 'D' and 'F'

will only pull out all the D and E entries. F is excluded.

I get that, but why is a wildcard not able to be used in this instance?

‘F%’

In order to cover everything that begins with ‘F’. Is it just a limitation of the WHERE clause or BETWEEN operator?

Between is not well geared to wildcards. The arguments are lower and upper bounds. Like is more suited to wildcards since we merely extend a comparison model.

LIKE '%BCD%'

We can hardly change-up the endpoints of BETWEEN.

BETWEEN 'AAA' AND 'AAD'

The D in AAD means the query can include AZC. Both last characters are essentially wildcards.

I hope you will test this query and confirm or reject this assumption.

1 Like

Thank you for this explanation, which actually makes sense.

Codecademy’s explanation is very unsatisfactory:

When the values are text, BETWEEN filters the result set for within the alphabetical range.

In this statement, BETWEEN filters the result set to only include movies with name s that begin with the letter ‘A’ up to, but not including ones that begin with ‘J’.

SELECT * FROM movies WHERE name BETWEEN ‘A’ AND ‘J’;

However, if a movie has a name of simply ‘J’, it would actually match. This is because BETWEEN goes up to the second value — up to ‘J’. So the movie named ‘J’ would be included in the result set but not ‘Jaws’.

This is, quite simply, confusing to say the least. Especially since in the immediate exercise following this explanation they ask you to use the BETWEEN on numbers by including the upper bound in the query and somewhat contradicting their own prior explanation.

I would really wish sometimes that they would explain things better and more clearly.

1 Like
SELECT *
FROM movies
WHERE name BETWEEN 'D' AND 'G'
  AND name != 'G';

I added the last line to make it more precisely.

although it is moot. We know that G is not included in the query.

Hi!
I tried different variations of BETWEEN and found that I don’t quite understand what means the comment "BETWEEN goes up to the second value — up to ‘J’".

If I quire WHERE name BETWEEN ‘Aa’ AND ‘Az’
I get al movie starting from A
And there will be Anaconda & Annabelle|.

What is interesting is that I quire WHERE name BETWEEN ‘Aa’ AND ‘An’
I got non of those two, BUT if I change to
WHERE name BETWEEN ‘Aa’ AND ‘Ann’ I see Anaconda in the resulting table.

Which is logical, but after that I stoped understanding why I get name J when I quire BETWEEN goes up to the second value — up to ‘J’

Why I get J? :slight_smile:

1 Like

Hi, is it possible to checking a data that released between the year 1990 until 1999, but also has a NULL value?

Because i tried to use this but it seems that the program has an error

SELECT * FROM movies
WHERE imdb_rating IS NULL
WHERE year BETWEEN 1990 AND 1999;