A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/sql/sql-and-and-or-operators/ below:

SQL AND and OR Operators

SQL AND and OR Operators

Last Updated : 23 Jul, 2025

The SQL AND and OR operators are used to filter data based on multiple conditions. These logical operators allow users to retrieve precise results from a database by combining various conditions in SELECT, INSERT, UPDATE, and DELETE statements.

In this article, we'll learn the AND and OR operators, demonstrate their usage with examples, and provide insights on combining them for complex queries.

SQL AND Operator

The AND operator allows you to filter data based on multiple conditions, all of which must be true for the record to be included in the result set.

Syntax:

The syntax to use the AND operator in SQL is:

SELECT * FROM table_name WHERE condition1 AND condition2 AND ...conditionN;

Here,

SQL OR Operator

The OR Operator in SQL displays the records where any one condition is true, i.e. either condition1 or condition2 is True.

Syntax:

The syntax to use the OR operator in SQL is:

SELECT * FROM table_name WHERE condition1 OR condition2 OR... conditionN;

SQL AND and OR Operator Examples

Let's look at some examples of AND and OR operators in SQL and understand their working.

Now, we consider a table database to demonstrate AND & OR operators with multiple cases.

Student Table Example 1: SQL AND Operator

If suppose we want to fetch all the records from the Student table where Age is 18 and ADDRESS is Delhi.

Query:

SELECT * FROM Student 
WHERE Age = 18 AND ADDRESS = 'Delhi';

Output:

ROLL_NO NAME ADDRESS PHONE Age 1 Ram Delhi XXXXXXXXXX 18 4 SURESH Delhi XXXXXXXXXX 18 Example 2: SQL OR Operator

To fetch all the records from the Student table where NAME is Ram or NAME is SUJIT. 

Query:

SELECT * FROM Student 
WHERE NAME = 'Ram' OR NAME = 'SUJIT';

Output:

ROLL_NO NAME ADDRESS PHONE Age 1 Ram Delhi XXXXXXXXXX 18 3 SUJIT ROHTAK XXXXXXXXXX 20 3 SUJIT ROHTAK XXXXXXXXXX 20 Combining AND and OR Operators in SQL

Combining AND and OR Operators in SQL allows the creation of complex conditions in queries. This helps in filtering data on multiple conditions. 

Syntax:

Syntax to use AND and OR operator in one statement in SQL is:

SELECT * FROM table_name
WHERE condition1 AND (condition2 OR condition3);

Example

Let's look at example of combining AND and OR operators in a single statement. In this example we will fetch all the records from the Student table where Age is 18, NAME is Ram or RAMESH.

Query:

SELECT * FROM Student WHERE Age = 18 AND (NAME = 'Ram' OR NAME = 'RAMESH');

Output:

ROLL_NO NAME ADDRESS PHONE Age 1 Ram Delhi XXXXXXXXXX 18 2 RAMESH GURGAON XXXXXXXXXX 18 Important Points About SQL AND and OR Operators
Conclusion

The AND and OR operators are essential tools for filtering data in SQL. By combining multiple conditions, you can create powerful and efficient queries to retrieve exactly the data you need. Remember to use parentheses for clarity and control the order of evaluation, especially when combining both operators. By mastering these logical operators, you’ll be able to handle complex queries and data filtering tasks more effectively.


AND and OR operators in SQL


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4