SQL AND, OR, NOT

Combine conditions.

✍️ 3 exercises + code challenge in this lesson
  • AND: every condition must be true.
  • OR: at least one condition must be true.
  • NOT: reverses a condition.

Use parentheses to control the order when mixing AND and OR: WHERE Country = 'Pakistan' AND (City = 'Lahore' OR City = 'Karachi').

Key Takeaways: SQL AND, OR, NOT

Mastering SQL AND, OR, NOT helps you write clean and reliable SQL code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

SQL AND, OR, NOT Code Example

SQL
SELECT * FROM Customers
WHERE Country = 'Pakistan' AND (City = 'Lahore' OR City = 'Karachi');

SELECT ProductName, Category FROM Products
WHERE NOT Category = 'Electronics';
Free sign-in required · Google login available

SQL AND, OR, NOT Practice Exercises

Check what you learned in “SQL AND, OR, NOT”.

/ 3 solved
Fill in the blank

Both conditions must be true.

WHERE Country = 'Pakistan'  City = 'Karachi'
✓ Correct! Not quite. Try again.
Multiple choice

Which operator reverses a condition?

✓ Correct! Not quite. Try again.
Code challenge

Select ProductName of Electronics products that cost less than 5000.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services