Python MySQL WHERE Clause
Filter your data - find exactly what you're looking for!
šÆ Filter Your Data!
WHERE is like a filter - it helps you find specific information instead of everything!
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="yourpassword",
database="school"
)
cursor = mydb.cursor()
cursor.execute("SELECT * FROM students WHERE age > 16")
results = cursor.fetchall()
for student in results:
print(student)
What is WHERE?
WHERE helps you find specific data:
- šÆ Filter Results - Show only what matches your condition
- š Search Specific - Find exact records you need
- ā” Save Time - Don't look through everything
- šļø Multiple Conditions - Combine different filters
Basic WHERE Example
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="yourpassword",
database="school"
)
cursor = mydb.cursor()
# Find students older than 16
cursor.execute("SELECT name, age FROM students WHERE age > 16")
results = cursor.fetchall()
print("š Students older than 16:")
for student in results:
print(f" {student[0]} is {student[1]} years old")
WHERE with Text
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="yourpassword",
database="school"
)
cursor = mydb.cursor()
# Find a specific student
cursor.execute("SELECT * FROM students WHERE name = 'John Smith'")
result = cursor.fetchone()
if result:
print(f"ā
Found: {result}")
else:
print("ā Student not found")
Safe WHERE with Variables
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="yourpassword",
database="school"
)
cursor = mydb.cursor()
# Safe way to use variables
student_name = "Alice Johnson"
sql = "SELECT * FROM students WHERE name = %s"
cursor.execute(sql, (student_name,))
result = cursor.fetchone()
if result:
print(f"ā
Found student: {result[1]}")
WHERE Operators
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="yourpassword",
database="school"
)
cursor = mydb.cursor()
# Equal to
cursor.execute("SELECT name FROM students WHERE grade = '10th'")
print("š 10th grade students:")
for student in cursor.fetchall():
print(f" {student[0]}")
# Not equal to
cursor.execute("SELECT name FROM students WHERE grade != '12th'")
print("\nš Not in 12th grade:")
for student in cursor.fetchall():
print(f" {student[0]}")
# Greater than
cursor.execute("SELECT name FROM students WHERE age >= 17")
print("\nšØāš Students 17 or older:")
for student in cursor.fetchall():
print(f" {student[0]}")
LIKE - Find Similar Text
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="yourpassword",
database="school"
)
cursor = mydb.cursor()
# Names starting with 'A'
cursor.execute("SELECT name FROM students WHERE name LIKE 'A%'")
print("š¤ Names starting with 'A':")
for student in cursor.fetchall():
print(f" {student[0]}")
# Names containing 'son'
cursor.execute("SELECT name FROM students WHERE name LIKE '%son%'")
print("\nš„ Names containing 'son':")
for student in cursor.fetchall():
print(f" {student[0]}")
Multiple Conditions (AND, OR)
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="yourpassword",
database="school"
)
cursor = mydb.cursor()
# AND - both conditions must be true
cursor.execute("SELECT name, age FROM students WHERE age > 16 AND grade = '11th'")
print("šÆ Students older than 16 AND in 11th grade:")
for student in cursor.fetchall():
print(f" {student[0]} ({student[1]} years old)")
# OR - either condition can be true
cursor.execute("SELECT name FROM students WHERE grade = '9th' OR grade = '10th'")
print("\nš Students in 9th OR 10th grade:")
for student in cursor.fetchall():
print(f" {student[0]}")
WHERE Operator Reference
=
Equal to
age = 16
!=
Not equal to
grade != '12th'
>
Greater than
age > 15
<
Less than
age < 18
LIKE
Pattern match
name LIKE 'A%'
AND/OR
Multiple conditions
age > 16 AND grade = '11th'