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)
                                
WHERE
Filter
Precise
Results
Fast
Search

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

Find Students by Age

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

Find by Name

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

Using Placeholders

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

Different Comparison 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

Pattern Matching

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)

Combine Conditions

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'

🧠 Test Your Knowledge

What does WHERE do?

Which finds names starting with 'B'?