CASE A_value
WHEN B_value THEN B_statement
[WHEN C_Value THEN C_statement] ...
[ELSE X_statement]
END CASE
Here A_value is matched with different values given under WHEN. If A_value equals to B_value then B_statement is executed. If it is not matched than next WHEN value C_value is checked. If nothing is matched then statement under X_statement is executed.
q="SELECT `id` , `name` , `class` , `mark` , `sex` , \
CASE class \
WHEN 'Four' THEN '1st floor' \
WHEN 'Five' THEN '2nd floor' \
WHEN 'Three' THEN '2nd floor' \
WHEN 'Two' THEN '1st floor' \
ELSE 'Ground floor' \
END AS location \
FROM `student` "
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 ( first 4 records are displayed here , there are more records, check the downloaded file at the end of this tutorial )
1 John Deo Four 75 female - 1st floor
2 Max Ruin Three 85 male - 2nd floor
3 Arnold Three 55 male - 2nd floor
4 Krish Star Four 60 female - 1st floor
----------------
----------------
q="SELECT `id` , `name` , `class` , `mark` , `sex` , \
CASE \
WHEN mark >= 90 THEN 'A' \
WHEN mark >= 80 THEN 'B' \
WHEN mark >= 70 THEN 'C' \
ELSE 'FAIL' \
END AS grade \
FROM `student` "
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 ( check the downloaded file at the end for full output )
1 John Deo Four 75 female - C
2 Max Ruin Three 85 male - B
3 Arnold Three 55 male - FAIL
4 Krish Star Four 60 female - FAIL
----------------
----------------
q="SELECT `id` , `name` , `class` , `mark` , `sex` ,\
CASE WHEN mark BETWEEN 90 AND 100 THEN 'A'\
WHEN mark BETWEEN 80 AND 89 THEN 'B'\
WHEN mark BETWEEN 70 AND 79 THEN 'C'\
ELSE 'FAIL' \
END AS grade FROM `student`"
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 is same as above.
my_data=(90,100,80,89,70,79)
q="SELECT `id` , `name` , `class` , `mark` , `sex` ,\
CASE WHEN mark BETWEEN ? AND ? THEN 'A'\
WHEN mark BETWEEN ? AND ? THEN 'B'\
WHEN mark BETWEEN ? AND ? THEN 'C'\
ELSE 'FAIL' \
END AS grade FROM `student`"
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 )
my_data=('2020-04-01','2020-06-30','2020-07-01','2020-09-30',
'2020-10-01','2020-12-31','2021-01-01','2021-03-31')
q="SELECT id,name,mark,class,dot,\
CASE WHEN dot BETWEEN ? AND ? THEN 'Q1'\
WHEN dot BETWEEN ? AND ? THEN 'Q2'\
WHEN dot BETWEEN ? AND ? THEN 'Q3'\
WHEN dot BETWEEN ? AND ? THEN 'Q4'\
ELSE 'Outside FY' \
END AS Date FROM `student`"
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 ( sample only, all records are included in downloaded page )
1 John Deo Four 75 2020-12-20 - Q3
2 Max Ruin Three 85 2020-12-21 - Q3
3 Arnold Three 55 2019-11-20 - Outside FY
4 Krish Star Four 60 2020-08-16 - Q2
----------------
----------------
View and Download sqlite-case.ipynb file View and Download sqlite-case-between.ipynb file
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.