0% found this document useful (0 votes)
0 views4 pages

And or Not in Between

The document provides SQL examples for querying a 'Products' table using various operators such as AND, OR, NOT, IN, BETWEEN, and LIKE. It includes examples of how to combine multiple conditions in queries to filter products based on categories, prices, and stock quantities. Each operator is explained with practical SQL statements demonstrating its usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views4 pages

And or Not in Between

The document provides SQL examples for querying a 'Products' table using various operators such as AND, OR, NOT, IN, BETWEEN, and LIKE. It includes examples of how to combine multiple conditions in queries to filter products based on categories, prices, and stock quantities. Each operator is explained with practical SQL statements demonstrating its usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Example Table: Products

CREATE TABLE Products (


ProductID INT PRIMARY KEY,
ProductName VARCHAR(100),
Category VARCHAR(50),
Price DECIMAL(10, 2),
StockQuantity INT
);

INSERT INTO Products (ProductID, ProductName, Category, Price, StockQuantity)


VALUES (1, 'Laptop', 'Electronics', 899.99, 50),
(2, 'Smartphone', 'Electronics', 699.99, 120),
(3, 'Desk', 'Furniture', 199.99, 10),
(4, 'Chair', 'Furniture', 89.99, 30),
(5, 'Pen', 'Stationery', 1.99, 500),
(6, 'Notebook', 'Stationery', 4.99, 200);

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

Example: Find products that are not in the 'Stationery' category:

SELECT *
FROM Products
WHERE NOT Category = 'Stationery';

Using IN

Example: Find products that are in the categories 'Electronics' or 'Furniture':

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;

Note: The BETWEEN operator includes the boundary values.

Using LIKE

Example: Find products with a name that starts with 'Smar':

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%';

Combining Multiple Conditions

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;

Example 2: Find products that are either 'Laptop' or 'Smartphone':

SELECT *
FROM Products
WHERE ProductName = 'Laptop'
OR ProductName = 'Smartphone';

Using NOT

Example 1: Find products that are not in the 'Electronics' category:

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');

Example 2: Find products with ProductID 1, 3, or 5:

SELECT *
FROM Products
WHERE ProductID IN (1, 3, 5);

Using BETWEEN

Example 1: Find products with a stock quantity between 10 and 100:

SELECT *
FROM Products
WHERE StockQuantity BETWEEN 10 AND 100;

Example 2: Find products with a price between 50 and 300:

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%';

Combining Multiple Conditions

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.

You might also like