SQL WHERE Clause

SQL WHERE Clause

SQL WHERE Clause

The SQL WHERE clause is used in conjunction with the SELECT statement to filter data based on specified conditions. It allows you to retrieve only the rows that meet the specified condition or conditions. Here is the basic syntax of a SELECT statement with a WHERE clause:

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

				
			
  • The SELECT clause specifies the columns to retrieve data from in the specified table(s).
  • The FROM clause specifies the table(s) to retrieve data from.
  • The WHERE clause is used to filter data based on a specified condition. If the condition evaluates to true, the row is included in the result set. If it evaluates to false, the row is excluded.

The WHERE clause can use various comparison operators to specify the condition, such as:

  • = : equal to
  • <> or != : not equal to
  • : greater than
  • < : less than
  • = : greater than or equal to
  • <= : less than or equal to
  • BETWEEN : between two values
  • IN : matches any value in a list
  • LIKE : matches a pattern
  • IS NULL : is a null value

Here are some examples of using the WHERE clause with various operators:

				
					SELECT * FROM customers
WHERE age > 25;

				
			

This statement retrieves all columns and rows from the customers table where the age is greater than 25.

				
					SELECT * FROM orders
WHERE order_date BETWEEN '2022-01-01' AND '2022-12-31';

				
			

This statement retrieves all columns and rows from the orders table where the order date is between January 1, 2022, and December 31, 2022.

				
					SELECT * FROM products
WHERE category IN ('Electronics', 'Home Appliances');

				
			

This statement retrieves all columns and rows from the products table where the category is either Electronics or Home Appliances.

				
					SELECT * FROM customers
WHERE first_name LIKE 'J%';

				
			

This statement retrieves all columns and rows from the customers table where the first name starts with the letter J.

Overall, the WHERE clause is a powerful tool for filtering data based on specified conditions and is essential in constructing more complex SQL queries.

Join To Get Our Newsletter
Spread the love