ThoughtSpot acquires Mode to define the next generation of collaborative BI >>Learn More

SQL BETWEEN

Starting here? This lesson is part of a full-length tutorial in using SQL for Data Analysis. Check out the beginning.

In this lesson we'll cover:

The SQL BETWEEN operator

BETWEEN is a logical operator in SQL that allows you to select only rows that are within a specific range. It has to be paired with the AND operator, which you'll learn about in a later lesson. Here's what BETWEEN looks like on a Billboard Music Chart Dataset:

SELECT *
  FROM tutorial.billboard_top_100_year_end
 WHERE year_rank BETWEEN 5 AND 10

BETWEEN includes the range bounds (in this case, 5 and 10) that you specify in the query, in addition to the values between them. So the above query will return the exact same results as the following query:

SELECT *
  FROM tutorial.billboard_top_100_year_end
 WHERE year_rank >= 5 AND year_rank <= 10

Some people prefer the latter example because it more explicitly shows what the query is doing (it's easy to forget whether or not BETWEEN includes the range bounds).

Sharpen your SQL skills

Practice Problem

Write a query that shows all top 100 songs from January 1, 1985 through December 31, 1990.

Try it out See the answer

Next Lesson

SQL IS NULL

Get more from your data

Your team can be up and running in 30 minutes or less.