And or Not in Between
And or Not in Between
Using AND
Example: Find products that are in the 'Electronics' category and have a price less than 800:
SELECT *
FROM Products
WHERE Category = 'Electronics'
AND Price < 800;
Using OR
Example: Find products that are either in the 'Furniture' category or have a stock quantity greater than
100:
SELECT *
FROM Products
WHERE Category = 'Furniture'
OR StockQuantity > 100;
Using NOT
SELECT *
FROM Products
WHERE NOT Category = 'Stationery';
Using IN
SELECT *
FROM Products
WHERE Category IN ('Electronics', 'Furniture');
Using BETWEEN
Example: Find products with a price between 100 and 800:
SELECT *
FROM Products
WHERE Price BETWEEN 100 AND 800;
Using LIKE
SELECT *
FROM Products
WHERE ProductName LIKE 'Smar%';
Example: Find products with a name that contains 'note' (case insensitive):
SELECT *
FROM Products
WHERE ProductName LIKE '%note%';
You can combine multiple conditions using AND, OR, and parentheses for complex queries.
Example: Find products that are either in the 'Electronics' category and have a price less than 800, or are
in the 'Stationery' category with a stock quantity greater than 100:
SELECT *
FROM Products
WHERE (Category = 'Electronics' AND Price < 800)
OR (Category = 'Stationery' AND StockQuantity > 100);
Using AND
Example 1: Find products that are in the 'Furniture' category and have a stock quantity greater than 20:
SELECT *
FROM Products
WHERE Category = 'Furniture'
AND StockQuantity > 20;
Example 2: Find products that have a price greater than 100 and less than 500:
SELECT *
FROM Products
WHERE Price > 100
AND Price < 500;
Using OR
Example 1: Find products that are in the 'Stationery' category or have a price less than 50:
SELECT *
FROM Products
WHERE Category = 'Stationery'
OR Price < 50;
SELECT *
FROM Products
WHERE ProductName = 'Laptop'
OR ProductName = 'Smartphone';
Using NOT
SELECT *
FROM Products
WHERE NOT Category = 'Electronics';
Example 2: Find products that do not have a price between 200 and 800:
SELECT *
FROM Products
WHERE NOT Price BETWEEN 200 AND 800;
Using IN
Example 1: Find products that are in the 'Electronics', 'Furniture', or 'Stationery' categories:
SELECT *
FROM Products
WHERE Category IN ('Electronics', 'Furniture', 'Stationery');
SELECT *
FROM Products
WHERE ProductID IN (1, 3, 5);
Using BETWEEN
SELECT *
FROM Products
WHERE StockQuantity BETWEEN 10 AND 100;
SELECT *
FROM Products
WHERE Price BETWEEN 50 AND 300;
Using LIKE
Example 1: Find products with names that end with 'book':
SELECT *
FROM Products
WHERE ProductName LIKE '%book';
Example 2: Find products with names that contain the letter 'e':
SELECT *
FROM Products
WHERE ProductName LIKE '%e%';
Example 1: Find products that are in the 'Electronics' category, have a price less than 800, and have a
stock quantity greater than 50:
SELECT *
FROM Products
WHERE Category = 'Electronics'
AND Price < 800
AND StockQuantity > 50;
Example 2: Find products that are either in the 'Furniture' category with a price less than 100, or in the
'Stationery' category with a stock quantity greater than 200:
SELECT *
FROM Products
WHERE (Category = 'Furniture' AND Price < 100)
OR (Category = 'Stationery' AND StockQuantity > 200);
Example 3: Find products that are in the 'Electronics' category but do not have a price between 500 and
900:
SELECT *
FROM Products
WHERE Category = 'Electronics'
AND NOT Price BETWEEN 500 AND 900;
Summary
AND: Combines two or more conditions and returns true only if all conditions are true.
OR: Combines two or more conditions and returns true if at least one condition is true.
NOT: Reverses the result of a condition.
IN: Checks if a value is within a set of values.
BETWEEN: Checks if a value is within a range of values.
LIKE: Searches for a specified pattern in a column.