TGViewer
Data Science & Machine Learning Data Science & Machine Learning @datasciencefun · 77.7K subscribers
Post #4632 821
SELECT *
FROM customers
WHERE city = 'Pune'
OR city = 'Mumbai';


This returns customers from either Pune or Mumbai.

🔹 14. AND vs OR

Consider:

WHERE age > 30
AND city = 'Pune'


A customer must satisfy both conditions.

But:

WHERE age > 30
OR city = 'Pune'


A customer only needs to satisfy one or both conditions.

This difference is extremely important.

🔹 15. NOT

NOT reverses a condition.

Example:

SELECT *
FROM customers
WHERE NOT city = 'Pune';


This returns customers who aren't from Pune.

You can also commonly write:

SELECT *
FROM customers
WHERE city <> 'Pune';


🔹 16. Combining AND and OR

You can combine multiple logical operators.

Example:

SELECT *
FROM employees
WHERE department = 'Finance'
AND salary > 60000;


Another example:

SELECT *
FROM employees
WHERE department = 'Finance'
OR department = 'Analytics'
AND salary > 60000;


When conditions become complex, use parentheses to make your intended logic explicit.

For example:

SELECT *
FROM employees
WHERE
(department = 'Finance' OR department = 'Analytics')
AND salary > 60000;


This means:



Employees from Finance or Analytics who earn more than 60,000.



🔹 17. Why Parentheses Matter

Consider:

WHERE city = 'Pune'
OR city = 'Mumbai'
AND age > 30


SQL's logical evaluation rules can make this behave differently from what a beginner might expect.

A safer and clearer version is:

WHERE
(city = 'Pune' OR city = 'Mumbai')
AND age > 30;


This clearly communicates the intended logic.

Best practice:



Use parentheses whenever combining AND and OR in a complex condition.



🔹 18. WHERE with Dates

You can also filter dates.

Example:

SELECT *
FROM orders
WHERE order_date >= '2026-01-01';


This retrieves orders on or after January 1, 2026.

Another example:

SELECT *
FROM orders
WHERE order_date < '2026-07-01';


This retrieves orders before July 1, 2026.

Date syntax can vary slightly across database systems, so always consider the SQL dialect you're using.

🔹 19. Filtering a Date Range

Suppose you want orders during a particular period.

You can use:

SELECT *
FROM orders
WHERE order_date >= '2026-01-01'
AND order_date < '2026-04-01';


This retrieves orders from January through March.

Using a half-open range like this is particularly useful when working with timestamps because it avoids accidentally excluding records with time components.

🔹 20. BETWEEN

SQL provides BETWEEN for range filtering.

Example:

SELECT *
FROM products
WHERE price BETWEEN 1000 AND 5000;


BETWEEN is inclusive of both boundaries in standard SQL.

So this includes:

1000

and:

5000

as well as values between them.

🔹 21. BETWEEN with Dates

Example:

SELECT *
FROM orders
WHERE order_date BETWEEN '2026-01-01' AND '2026-01-31';


For a date-only column, this can be useful.

However, if order_date contains timestamps, using:

order_date >= '2026-01-01'
AND order_date < '2026-02-01'


is often safer because it includes the entire final day regardless of the timestamp.

🔹 22. IN Operator

Suppose you want customers from:

Pune

Mumbai

Delhi

You could write:

SELECT *
FROM customers
WHERE city = 'Pune'
OR city = 'Mumbai'
OR city = 'Delhi';
More from @datasciencefun
  1. Oct 2, 2026Data Visualisation tips for beginners
  2. Sep 29, 2026𝗙𝗥𝗘𝗘 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲𝘀 𝗧𝗼 𝗟𝗲𝗮𝗿𝗻 𝗔𝗜 𝗶𝗻 𝟮𝟬𝟮𝟲🚀 ​ Explore 6 free resource…
  3. Sep 28, 2026🔹 Real-World Data Science Example Suppose you are analyzing sales data. You want to ident…
  4. Sep 28, 2026🚀 Data Science Roadmap 2026 📍 Phase 3: SQL for Data Science 📖 Topic 3 — ORDER BY ORDER…
  5. Sep 28, 2026🎓 𝗛𝗔𝗥𝗩𝗔𝗥𝗗 𝗨𝗡𝗜𝗩𝗘𝗥𝗦𝗜𝗧𝗬 𝗙𝗥𝗘𝗘 𝗢𝗡𝗟𝗜𝗡𝗘 𝗖𝗢𝗨𝗥𝗦𝗘𝗦 😍 Dreaming of…
  6. Sep 27, 2026Complete Data Analytics Mastery: From Basics to Advanced 🚀 Begin your Data Analytics jour…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →