Filtering rows using BETWEEN operator in SQL, Python and R

Given a table or dataframe named degree as shown below, get all the records from the table or dataframe where the length of pursuing the degree is between 1 and 3 years.

| ----------------- | ---------- | ------------ | -------------- | ------------- |
| student_degree_id | student_id | degree_name  | degree_country | degree_length |
| ----------------- | ---------- | ------------ | -------------- | ------------- |
| 1                 | 1          | B. Arts      | USA            | 3             |
| ----------------- | ---------- | ------------ | -------------- | ------------- |
| 2                 | 2          | B. Tech      | India          | 4             |
| ----------------- | ---------- | ------------ | -------------- | ------------- |
| 3                 | 2          | MS           | USA            | 2             |
| ----------------- | ---------- | ------------ | -------------- | ------------- |
| 4                 | 2          | PhD          | USA            | 5             |
| ----------------- | ---------- | ------------ | -------------- | ------------- |
| 5                 | 3          | B. Sc.       | Germany        | 4             |
| ----------------- | ---------- | ------------ | -------------- | ------------- |
| 6                 | 4          | B. Sc.       | Switzerland    | 4             |
| ----------------- | ---------- | ------------ | -------------- | ------------- |
| 7                 | 4          | M. Sc.       | Germany        | 3             |
| ----------------- | ---------- | ------------ | -------------- | ------------- |
| 8                 | 7          | BS           | China          | 3             |
| ----------------- | ---------- | ------------ | -------------- | ------------- |
| 9                 | 7          | MS           | Australia      | 1             |
| ----------------- | ---------- | ------------ | -------------- | ------------- |
| 10                | 7          | PhD          | USA            | 3             |
| ----------------- | ---------- | ------------ | -------------- | ------------- |
| 11                | 10         | BE           | UK             | 4             |
| ----------------- | ---------- | ------------ | -------------- | ------------- |
| 12                | 6          | BE           | India          | 4             |
| ----------------- | ---------- | ------------ | -------------- | ------------- |
| 13                | 6          | ME           | India          | 2             |
| ----------------- | ---------- | ------------ | -------------- | ------------- |

Filtering rows using BETWEEN operator in SQL:

SELECT * 
FROM degree
WHERE degree_length BETWEEN 1 AND 3

Filtering rows using BETWEEN operator in Python:

You must have the pandas library installed to run this snippet of code.

import pandas as pd

degree[degree['degree_length'].between(1, 3)]

Filtering rows using BETWEEN operator in R:

You must have the dplyr library installed to run this snippet of code.

library('dplyr')

degree %>% filter(between(degree_length, 1, 3))

Result:

    degree_id  student_id degree_name degree_country  degree_length
0           1           1     B. Arts            USA              3
2           3           2          MS            USA              2
6           7           4      M. Sc.        Germany              3
7           8           7          BS          China              3
8           9           7          MS      Australia              1
9          10           7         PhD            USA              3
12         13           6          ME          India              2