Yandex

SQL Operators
AND, OR, NOT, IN, BETWEEN, LIKE



Introduction

SQL becomes truly powerful when you start combining conditions. Want students from Delhi *and* Class 10A? Students *not* from Chennai? Names that start with “S”? You’ll need logical operators — the core building blocks of intelligent filtering.

Sample Table – Students

CREATE TABLE students (
  roll_no INT PRIMARY KEY,
  name VARCHAR(50),
  class VARCHAR(10),
  age INT,
  city VARCHAR(30)
);

INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Rohit Menon', '10A', 15, 'Kochi'),
(4, 'Sneha Patil', '8C', 13, 'Pune'),
(5, 'Mehul Agarwal', '9B', 14, 'Delhi'),
(6, 'Saira Bano', '10A', 15, 'Lucknow');

1. AND Operator

Returns rows where both conditions are true.

SELECT name, class, city FROM students
WHERE class = '10A' AND city = 'Delhi';
name           | class | city
----------------+-------+--------
Aarav Sharma   | 10A   | Delhi

2. OR Operator

Returns rows where at least one condition is true.

SELECT name, class FROM students
WHERE class = '10A' OR class = '8C';
name           | class
----------------+-------
Aarav Sharma   | 10A
Rohit Menon    | 10A
Sneha Patil    | 8C
Saira Bano     | 10A

3. NOT Operator

Negates the condition — filters out matches.

SELECT name, city FROM students
WHERE NOT city = 'Delhi';

4. IN Operator

Tests if a value is in a list of values — useful for multiple ORs.

SELECT name, city FROM students
WHERE city IN ('Delhi', 'Kochi');
name           | city
----------------+--------
Aarav Sharma   | Delhi
Rohit Menon    | Kochi
Mehul Agarwal  | Delhi

5. BETWEEN Operator

Filters results within a range (inclusive).

SELECT name, age FROM students
WHERE age BETWEEN 14 AND 15;
name           | age
----------------+-----
Aarav Sharma   | 15
Diya Iyer      | 14
Rohit Menon    | 15
Mehul Agarwal  | 14
Saira Bano     | 15

6. LIKE Operator

Used for pattern matching with wildcards:

  • %: matches any number of characters
  • _: matches a single character

Names starting with 'S':

SELECT name FROM students
WHERE name LIKE 'S%';
name
----------
Sneha Patil
Saira Bano

Combining Operators

Complex conditions often require mixing AND, OR, NOT:

SELECT name FROM students
WHERE (class = '10A' OR class = '9B')
AND city = 'Delhi';
name
----------
Aarav Sharma
Mehul Agarwal

Best Practices

  • Use parentheses to control logical flow when combining AND/OR.
  • Use IN instead of multiple ORs for cleaner queries.
  • LIKE is case-insensitive in most SQL databases unless configured otherwise.

Summary

SQL operators let you refine and sharpen your queries. Whether you’re checking for specific values, creating ranges, or matching patterns — AND, OR, NOT, IN, BETWEEN, and LIKE give you the precision to get just the data you need.

What’s Next?

Up next, we’ll explore Subqueries — a powerful way to use one query inside another for advanced logic and filtering.

QUIZ

Question 1:What does the following SQL query return?
SELECT * FROM students WHERE class = '10A' AND marks > 80;

Question 2:The OR operator in SQL returns results where either one or both conditions are true.

Question 3:Which of the following are valid uses of the IN operator?

Question 4:Which query will return students with marks between 70 and 90 (inclusive)?

Question 5:The NOT operator in SQL can be used to exclude specific results that match a condition.

Question 6:Which queries correctly use the LIKE operator for pattern matching?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M