Logo Wand.Tools

SQL WHERE Clause Generator

Use AI to generate WHERE clauses for filtering data in SQL queries

SQL WHERE Clause Tutorial

SQL WHERE Clause Tutorial

The SQL WHERE clause is used to filter records that meet specific conditions. It is an essential part of SQL queries, allowing you to retrieve only the data that you need.

Syntax

The basic syntax of the WHERE clause is as follows:

SELECT column1, column2, ...
FROM table_name
WHERE condition;

Examples

Example 1: Simple Condition

SELECT * FROM Customers
WHERE Country = 'USA';

This query retrieves all customers from the USA.

Example 2: Using AND Operator

SELECT * FROM Customers
WHERE Country = 'USA' AND City = 'New York';

This query retrieves customers who are from the USA and live in New York.

Example 3: Using OR Operator

SELECT * FROM Customers
WHERE Country = 'USA' OR Country = 'Canada';

This query retrieves customers who are either from the USA or Canada.

Example 4: Using NOT Operator

SELECT * FROM Customers
WHERE NOT Country = 'USA';

This query retrieves customers who are not from the USA.

Example 5: Using IN Operator

SELECT * FROM Customers
WHERE Country IN ('USA', 'Canada', 'Mexico');

This query retrieves customers from the USA, Canada, or Mexico.

Example 6: Using BETWEEN Operator

SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;

This query retrieves products with a price between 10 and 20.

Example 7: Using LIKE Operator

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

This query retrieves customers whose names start with ‘A’.

Conclusion

The WHERE clause is a powerful tool in SQL that allows you to filter data based on specific conditions. By mastering the WHERE clause, you can write more efficient and precise queries.

For more advanced filtering, you can combine the WHERE clause with other SQL clauses like GROUP BY, HAVING, and ORDER BY.