WikiGalaxy

Personalize

SQL WHERE Clause

Introduction to SQL WHERE Clause

Purpose of WHERE Clause

The SQL WHERE clause is used to filter records that meet a specified condition. It is essential in SQL queries to target specific rows of data in a table.

Basic Syntax

The basic syntax of the WHERE clause involves specifying a condition after the WHERE keyword. This condition determines which rows will be affected by a query.

Example with Numeric Condition

Filter employees with a salary greater than 50000.


SELECT * FROM Employees WHERE Salary > 50000;
    

Using WHERE with Strings

To filter rows based on string values, you can use the WHERE clause in conjunction with string conditions.

Example with String Condition

Select customers from the city of 'New York'.


SELECT * FROM Customers WHERE City = 'New York';
    

Combining Conditions with AND

The WHERE clause allows combining multiple conditions using the AND operator to refine your query results.

Example with AND Operator

Select orders placed by customer ID 1234 with a total greater than 100.


SELECT * FROM Orders WHERE CustomerID = 1234 AND Total > 100;
    

Combining Conditions with OR

The OR operator in the WHERE clause helps in selecting rows that satisfy at least one of several conditions.

Example with OR Operator

Select products that are either in category 'Electronics' or 'Furniture'.


SELECT * FROM Products WHERE Category = 'Electronics' OR Category = 'Furniture';
    

Using WHERE with IN

The IN operator allows specifying multiple values in a WHERE clause, simplifying queries that involve multiple OR conditions.

Example with IN Operator

Select employees from departments 1, 2, or 3.


SELECT * FROM Employees WHERE DepartmentID IN (1, 2, 3);
    

Using WHERE with BETWEEN

The BETWEEN operator is used to filter the result set within a certain range of values.

Example with BETWEEN Operator

Select orders placed between '2023-01-01' and '2023-12-31'.


SELECT * FROM Orders WHERE OrderDate BETWEEN '2023-01-01' AND '2023-12-31';
    

Using WHERE with LIKE

The LIKE operator is used for pattern matching in strings, allowing flexible search criteria.

Example with LIKE Operator

Select customers whose names start with 'A'.


SELECT * FROM Customers WHERE Name LIKE 'A%';
    

Console Output:

[Filtered Results]

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025