**
๐ Phase 3: SQL for Data Science**
๐ Topic 2: SQL Basics โ WHERE
After learning SELECT, the next essential SQL concept is WHERE.
In real-world Data Science, databases can contain millions or billions of records. You usually don't want to retrieve everything.
You want to answer questions such as:
Which customers are from Mumbai?
Which orders are above โน10,000?
Which employees joined after 2023?
Which transactions were successful?
Which products belong to a particular category?
The WHERE clause allows you to filter rows based on conditions.
๐น 1. What Is WHERE?
WHERE is used to filter records based on a specified condition.
Basic syntax:
SELECT column1, column2
FROM table_name
WHERE condition;
Example:
SELECT *
FROM customers
WHERE city = 'Mumbai';
This returns only customers whose city is Mumbai.
๐น 2. WHERE with Text Values
Text values are generally written inside single quotes.
Example:
SELECT customer_id, name
FROM customers
WHERE city = 'Pune';
This retrieves customers from Pune.
Another example:
SELECT *
FROM employees
WHERE department = 'Finance';
๐น 3. WHERE with Numbers
For numeric values, quotes are generally not required.
Example:
SELECT *
FROM customers
WHERE age > 30;
This returns customers older than 30.
Another example:
SELECT *
FROM orders
WHERE amount > 10000;
This returns orders where the amount is greater than 10,000.
๐น 4. Comparison Operators
The most commonly used comparison operators are:
Operator Meaning
= Equal to
<> Not equal to
!= Not equal to
Greater than
< Less than
= Greater than or equal to
<= Less than or equal to
Example:
SELECT *
FROM employees
WHERE salary >= 50000;
This returns employees whose salary is at least 50,000.
๐น 5. Equal To =
The = operator checks whether two values are equal.
SELECT *
FROM customers
WHERE city = 'Delhi';
Only records where city equals Delhi are returned.
๐น 6. Not Equal <>
You can retrieve records that don't match a value.
SELECT *
FROM customers
WHERE city <> 'Delhi';
This returns customers whose city isn't Delhi.
You may also see:
WHERE city != 'Delhi'
Both are commonly supported, although <> is the standard SQL operator.
๐น 7. Greater Than >
Example:
SELECT *
FROM orders
WHERE amount > 50000;
Returns orders above 50,000.
๐น 8. Less Than <
Example:
SELECT *
FROM products
WHERE price < 1000;
Returns products priced below 1,000.
๐น 9. Greater Than or Equal To >=
Example:
SELECT *
FROM employees
WHERE experience >= 5;
This includes employees with exactly 5 years as well as those with more than 5 years.
๐น 10. Less Than or Equal To <=
Example:
SELECT *
FROM products
WHERE price <= 500;
This includes products priced exactly at 500.
๐น 11. WHERE with Multiple Conditions
Real-world queries often require more than one condition.
For this, SQL provides logical operators:
โข AND
โข OR
โข NOT
๐น 12. AND
AND means all conditions must be true.
Example:
SELECT *
FROM customers
WHERE city = 'Pune'
AND age > 30;
This returns customers who:
1.
Are from Pune
2.
Are older than 30
Both conditions must be satisfied.
๐น 13. OR
OR means at least one condition must be true.
Example: