SQL WHERE Clause
The SQL WHERE clause is used to filter records based on specific conditions. It allows you to specify criteria for retrieving rows from a table that meet those conditions.
You can use a variety of operators, such as =, <, >, <=, >=, !=, AND, OR, NOT, within the WHERE clause to create complex conditions.
Table Structure
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
Product VARCHAR(50),
CustomerID INT,
Price DECIMAL(10, 2)
);
Example
Consider the Orders table. We will select orders where the Product is 'Laptop' and the Price is greater than 500.
Code Example
-- Select orders where product is 'Laptop' and price is greater than 500
SELECT * FROM Orders
WHERE Product = 'Laptop' AND Price > 500;
Output
OrderID | Product | CustomerID | Price |
---|---|---|---|
101 | Laptop | 1 | 750.00 |
103 | Laptop | 3 | 800.00 |
Explanation
- The query filters the Orders table for records where the Product is 'Laptop' and the Price is greater than 500.
- The AND
operator ensures that both conditions are met.