SQL Where Statement

Introduction 

In this article, we will learn about the WHERE statement in SQL. This statement is used to filter the results of a SELECT, UPDATE, or DELETE statement based on specified conditions in SQL.

The syntax of the WHERE clause is as follows

[ WHERE <search_condition> ]    

What is a WHERE statement in SQL? 

The WHERE statement specifies the condition that each row must satisfy in order to be included in the query result. The condition can be a simple comparison using an operator such as equals (=), greater than (>), or less than (<), or it can be a more complex expression involving logical operators such as AND and OR.

 For example, if you have a table called "OrderDetails" with columns for "OrderName," "orderAddress," and "OrderDate," you could find a specific customer by searching for their OrderName like this.

SELECT OrderName, orderAddress,OrderDate    
FROM OrderDetails    
WHERE OrderName  = 'Apple' ; 

The SQL query you provided selects the columns 'OrderName,' 'orderAddress,' and 'OrderDate' from the 'OrderDetails' table where the 'OrderName' equals 'Apple.' Assuming that the 'OrderName' column contains the name of the product being ordered and the 'orderAddress' column contains the address where the order is being delivered, this query would return the 'OrderName,' 'orderAddress,' and 'OrderDate' information for all orders of the product 'Apple.'

Note. that the syntax and exact query result would depend on the specific schema and data in the 'OrderDetails' table. 

Output 

Output

Using WHERE and condition in SQL

1. Start by writing your SQL statement. For example

SELECT * FROM table_name WHERE condition;
  1. Replace "table_name" with the name of the table you want to query.
  2. Replace "condition" with the condition you want to use to filter the results. For example,

2. you can use the following SQL query to find a row in a database table.

SELECT OrderName, OrderAddress    
FROM OrderDetails     
WHERE OrderId <= 5; 

The SQL query you provided selects the OrderName and OrderAddress columns from the OrderDetails table where the OrderId is less than or equal to 5. The WHERE clause filters the rows of the table based on a specified condition. In this case, the condition is OrderId <= 5, which means that only rows where the OrderId column has a value less than or equal to 5 will be returned. 

The result of this query will be a table containing the OrderName and OrderAddress values for the rows where the OrderId is less than or equal to 5. The number of rows returned will depend on how many rows in the OrderDetails table satisfy the condition.

Output 

Output

3. You can also use comparison operators such as =, !=, <, >, <=, and >= in the WHERE clause. For example  

SELECT * FROM OrderDetails WHERE Id >=2 AND Id <= 5; 

This query will select all the rows from the "OrderDetails" table where the value of the "Id" column is between 2 and 5.

Note. When using string values in the WHERE clause, you must enclose them in single quotes (' ').

Output

Using Three conditions in WHERE statement in SQL

To find rows that meet any of the three conditions, you can use the logical operator "OR" to combine the conditions. Here's an example using SQL Example. 

SELECT OrderName, orderAddress   
FROM OrderDetails    
WHERE OrderId = 1 OR OrderName  = 'Mango' OR orderAddress = 'Noida'; 

The SQL query you provided will retrieve data from the "OrderDetails" table based on the specified conditions in the WHERE clause.

The query will return the "OrderName" and "orderAddress" columns from the "OrderDetails" table where any of the following conditions are met.

  • "OrderId" is equal to 1
  • "OrderName" is equal to 'Mango.'
  • "orderAddress" is equal to 'Noida.'

Output 

Output

How do you write a query in SQL in the WHERE clause? 

To find rows that must meet several conditions in SQL, you can use the AND operator in the WHERE clause. The AND operator allows you to combine two or more conditions that must be true for a row to be included in the query result.

SELECT OrderName, orderAddress    
FROM OrderDetails     
WHERE OrderId <= 1 AND OrderName LIKE '%App%' AND orderAddress LIKE '%N%'; 

This SQL query selects the OrderName and orderAddress columns from the OrderDetails table, where the OrderId is less than or equal to 1, the OrderName contains the substring "App," and the orderAddress contains the letter "N." In other words, it will return the order details for any orders with an OrderId of 1 or less, a name containing "App," and an address containing the letter "N." Please note that the syntax and functionality of this query may vary depending on the specific SQL implementation being used.

Output 

Output

Which option is used with the WHERE clause?

The WHERE statement is used in SQL (Structured Query Language) to filter records that meet a specific condition.

To specify the condition in the WHERE clause, you can use various operators and functions such as:

  • Comparison operators- = (equal), <> (not equal), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to).
  • Logical operators- AND, OR, NOT.
  • Pattern matching- LIKE, IN, BETWEEN.
  • NULL handling- IS NULL, IS NOT NULL.
  • Functions- CONCAT, UPPER, LOWER, SUBSTRING, etc.

For example, the following SQL query uses the WHERE clause to select all the employees from the "OrderDetails" table whose id is greater than 2.

SELECT * FROM OrderDetails
WHERE Id > 2; 

This query returns all the records from the "OrderDetails" table where the "Id" column value is greater than 2.

Output 

Output

Conclusion  

This article taught us learned the basics of the SQL Where Statement.  

FAQs   

Q- What is the WHERE statement in SQL?

A- The WHERE statement is a clause in SQL that is used to filter data based on a specific condition. It is used in conjunction with the SELECT statement to retrieve data from one or more tables in a database. 

Q- What are some common operators used in the WHERE statement?

A- Some common operators used in the WHERE statement include

  • Equals (=)
  • Not equals (<> or !=)
  • Less than (<)
  • Greater than (>)
  • Less than or equal to (<=)
  • Greater than or equal to (>=)
  • LIKE (used for pattern matching)
  • IN (used to check if a value is in a list)  

Q- Can you use multiple conditions in a WHERE statement?

A- Yes, you can use multiple conditions in a WHERE statement using logical operators such as AND, OR, and NOT. 

Q- What is the order of precedence for logical operators in a WHERE statement?

A- The order of precedence for logical operators in a WHERE statement is as follows

  1. NOT
  2. AND
  3. OR


Similar Articles