SELECT col BETWEEN lower_val AND higher_val
Both lower_val and higher_val are included in the range. q="SELECT * FROM student WHERE mark between 65 and 70 "
try:
my_cursor=my_conn.execute(q)
for row in my_cursor:
print(row[0],row[1],row[2],row[3],row[4],row[5])
except sqlite3.Error as my_error:
print("error: ",my_error)
Output
20 Jackly Nine 65 female 2020-10-11
21 Babby John Four 69 female 2020-10-13
22 Reggid Seven 70 female 2020-05-18
34 Gain Toe Seven 69 male 2020-11-14
my_data=(65,70)
q="SELECT * FROM student WHERE mark between ? and ? "
try:
my_cursor=my_conn.execute(q,my_data)
for row in my_cursor:
print(row[0],row[1],row[2],row[3],row[4],row[5])
except sqlite3.Error as my_error:
print("error: ",my_error)
Output ( same as above )
20 Jackly Nine 65 female 2020-10-11
21 Babby John Four 69 female 2020-10-13
22 Reggid Seven 70 female 2020-05-18
34 Gain Toe Seven 69 male 2020-11-14
my_data=(25,90)
q="SELECT * FROM student WHERE mark NOT between ? and ? "
Output (The rows having mark not between 25 and 90 are returned. )
12 Recky Six 94 female 2020-11-18
19 Tinny Nine 18 male 2020-12-18
33 Kenn Rein Six 96 female 2020-10-25
from sqlalchemy.exc import SQLAlchemyError
my_data=(60,75)
q="SELECT * FROM student WHERE mark between ? AND ? "
try:
r_set=my_conn.execute(q,my_data)
r_list=r_set.fetchall()
for row in r_list:
print(row)
except SQLAlchemyError as e:
error=str(e.__dict__['orig'])
print(error)
else:
print("Total Number of rows : ",len(r_list))
my_data=('2020-05-01','2020-07-20')
q="SELECT * FROM student WHERE dot between ? and ? "
try:
my_cursor=my_conn.execute(q,my_data)
for row in my_cursor:
print(row[0],row[1],row[3],row[2],row[4],row[5])
except sqlite3.Error as my_error:
print("error: ",my_error)
Output
22 Reggid 70 Seven female 2020-05-18
27 Big Nose 81 Three female 2020-07-18
28 Rojj Base 86 Seven female 2020-07-19
More on SQLite Date query
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.