⬅ Previous Topic
DISTINCT Keyword in SQLNext Topic ⮕
SQL Aliases⬅ Previous Topic
DISTINCT Keyword in SQLNext Topic ⮕
SQL AliasesSQL 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.
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');
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
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
Negates the condition — filters out matches.
SELECT name, city FROM students
WHERE NOT city = 'Delhi';
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
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
Used for pattern matching with wildcards:
%
: matches any number of characters_
: matches a single characterSELECT name FROM students
WHERE name LIKE 'S%';
name
----------
Sneha Patil
Saira Bano
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
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.
Up next, we’ll explore Subqueries — a powerful way to use one query inside another for advanced logic and filtering.
SELECT * FROM students WHERE class = '10A' AND marks > 80;
⬅ Previous Topic
DISTINCT Keyword in SQLNext Topic ⮕
SQL AliasesYou 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.