We'll use the same Employees table throughout so it's easy to follow.

Employees

Employee_ID Name Age Department Salary City
101 Rahul 25 IT 60000 Surat
102 Priya 28 HR 55000 Mumbai
103 Amit 30 IT 70000 Surat
104 Neha 24 Sales 50000 Delhi
105 Rohan 29 Finance 65000 Mumbai
106 Ankit 27 IT 60000 Delhi

1. WHERE Clause

The WHERE clause is used to filter rows.

Think of it as asking:

Give me only the rows that satisfy this condition.

Syntax:

SELECT column_name
FROM Employees
WHERE condition;

Example: get all IT employees.

SELECT *
FROM Employees
WHERE Department = 'IT';

Example: employees older than 27.

SELECT *
FROM Employees
WHERE Age > 27;

Comparison Operators

Operator Meaning
= Equal
!= or <> Not Equal
> Greater Than
< Less Than
>= Greater Than or Equal
<= Less Than or Equal

Examples:

WHERE Salary > 60000
WHERE Age <= 25
WHERE Department != 'HR'

2. AND Operator

Returns rows only if all conditions are true.

Example: IT employees earning more than 60,000.

SELECT *
FROM Employees
WHERE Department = 'IT'
AND Salary > 60000;

Think of AND as:

Condition 1 true
AND
Condition 2 true

Return row

Even if one condition is false, the row is excluded.


3. OR Operator

Returns rows if any one condition is true.

SELECT *
FROM Employees
WHERE Department = 'HR'
OR Department = 'Sales';

Think of OR as:

Condition 1 true
OR
Condition 2 false

Still returned

4. NOT Operator

Reverses a condition.

Employees who are not in IT:

SELECT *
FROM Employees
WHERE NOT Department = 'IT';

Combining AND & OR

SELECT *
FROM Employees
WHERE Department = 'IT'
OR Salary > 65000;

SQL evaluates AND before OR.

So this:

SELECT *
FROM Employees
WHERE Department = 'IT'
OR Department = 'HR'
AND Salary > 55000;

is read as:

Department='IT'
OR
(Department='HR' AND Salary>55000)

If your intention is different, use parentheses.

SELECT *
FROM Employees
WHERE
(Department='IT'
OR Department='HR')
AND Salary > 55000;

Interview Tip: always use parentheses when mixing AND and OR to make your intent explicit.


5. DISTINCT

Removes duplicate values.

SELECT DISTINCT Department
FROM Employees;

Without DISTINCT, duplicate departments appear. With DISTINCT, each department appears once.


6. ORDER BY

Sorts data.

Ascending:

SELECT *
FROM Employees
ORDER BY Salary;

Descending:

SELECT *
FROM Employees
ORDER BY Salary DESC;

Multiple columns:

SELECT *
FROM Employees
ORDER BY Department, Salary DESC;

SQL first sorts by Department, then by Salary within each department.


7. LIMIT

Returns only the first N rows.

SELECT *
FROM Employees
LIMIT 3;

Highest salary:

SELECT *
FROM Employees
ORDER BY Salary DESC
LIMIT 1;

LIMIT is used in MySQL and PostgreSQL. SQL Server uses TOP, and Oracle may use FETCH FIRST or ROWNUM.


8. LIKE

Used for pattern matching.

Wildcard Meaning
`%` Zero or more characters
`_` Exactly one character

Starts with R:

SELECT *
FROM Employees
WHERE Name LIKE 'R%';

Ends with t:

WHERE Name LIKE '%t';

Contains ha:

WHERE Name LIKE '%ha%';

Exactly five letters:

WHERE Name LIKE '_____';

Each _ matches exactly one character.


9. IN

Instead of writing multiple OR conditions:

WHERE City='Surat'
OR City='Delhi'
OR City='Mumbai'

write:

SELECT *
FROM Employees
WHERE City IN ('Surat','Delhi','Mumbai');

Much cleaner.


10. BETWEEN

Checks whether a value lies within a range. It is inclusive.

SELECT *
FROM Employees
WHERE Salary BETWEEN 50000 AND 65000;

Equivalent to:

WHERE Salary >= 50000
AND Salary <= 65000;

11. IS NULL

NULL means missing or unknown, not zero or an empty string.

Find employees whose city is missing:

SELECT *
FROM Employees
WHERE City IS NULL;

Find employees whose city is available:

SELECT *
FROM Employees
WHERE City IS NOT NULL;

Interview Tip: never compare NULL using = or !=. Always use IS NULL or IS NOT NULL.


Logical SQL Execution Order

For a query like:

SELECT Name, Salary
FROM Employees
WHERE Department = 'IT'
ORDER BY Salary DESC
LIMIT 2;

The database logically processes it as:

FROM
   |
   v
WHERE
   |
   v
SELECT
   |
   v
ORDER BY
   |
   v
LIMIT

This execution order is a favorite interview question.


Summary Table

Clause Purpose
WHERE Filter rows
=, >, <, >=, <=, != Compare values
AND All conditions must be true
OR At least one condition must be true
NOT Negates a condition
DISTINCT Remove duplicate values
ORDER BY Sort results
LIMIT Return first N rows
LIKE Pattern matching
IN Match any value from a list
BETWEEN Filter within a range
IS NULL Check for NULL values

Interview Questions

1. What is the difference between WHERE and HAVING?

2. What is the difference between LIKE and IN?

3. Does BETWEEN include the boundary values?

Yes. BETWEEN 10 AND 20 includes both 10 and 20.

4. Why can't we use = NULL?

Because NULL represents an unknown value. SQL uses three-valued logic, so comparisons with NULL do not behave like normal equality checks.

Use IS NULL or IS NOT NULL instead.


At this point, you can write and understand a large percentage of everyday SQL queries. The next major topic is Joins, where relational databases really start to shine.