The Wayback Machine - https://web.archive.org/web/20240927183813/https://www.geeksforgeeks.org/sql-and-and-or-operators/
Open In App

SQL AND and OR Operators

Last Updated : 15 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

SQL AND and OR operators are used for data filtering and getting precise results based on conditions. They are used with the WHERE clause and are also called conjunctive operators.

The AND operator returns records where all conditions specified are true and the OR operator returns records where at least one condition specified is true.

AND and OR operators in SQL can be combined to test for multiple conditions in a SELECT, INSERT, UPDATE, or DELETE statement. When combining these conditions, it is important to use parentheses so that the database knows what order to evaluate each condition.

SQL AND Operator

The AND operator in SQL only displays those records where all conditions are true, i.e. both condition1 and condition2 are True. 

Syntax

The syntax to use the AND operator in SQL is:

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

Here,

  • table_name: name of the table
  • condition1,2,..N: first condition, second condition, and so on.

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;

  • table_name: name of the table
  • condition1,2,..N: first condition, second condition, and so on

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

Student Table

SQL AND Operator Example

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

SQL OR Operator Example

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

  • The SQL AND operator is used to combine multiple conditions, where all the conditions must be true for the row to be included in the result set
  • The OR operator is used to combine multiple conditions, where at least one of the conditions must be true for the row to be included in the result set.
  • Any kind of condition, including equality, inequality, comparison, and logical operators, can be utilized with the AND and OR operators.
  • The AND operator is more important than the OR operator. In other words, when both are used in the same SQL statement, the AND operator will be executed first. To change the order of evaluation, parentheses can be used.
  • You can employ the AND and OR operators inside of other conditions because they can both be nested.

Previous Article
Next Article

Similar Reads

Spatial Operators, Dynamic Spatial Operators and Spatial Queries in DBMS
1. Spatial operators : Spatial operators these operators are applied in geometric properties of objects. It is then used in the physical space to capture them and the relation among them. It is also used to perform spatial analysis. Spatial operators are grouped into three categories : Topological operators : Topological properties do not vary when
3 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
SQL vs NO SQL vs NEW SQL
SQL stands for Structured Query Language. Which is based on relational algebra and schema is fixed in this which means data is stored in the form of columns and tables. SQL follows ACID properties which means Atomicity, Consistency, Isolation, and Durability are maintained. There are three types of languages present in SQL : Data Definition Languag
2 min read
SQL | Arithmetic Operators
Prerequisite: Basic Select statement, Insert into clause, Sql Create Clause, SQL Aliases We can use various Arithmetic Operators on the data stored in the tables. Arithmetic Operators are: + [Addition] - [Subtraction] / [Division] * [Multiplication] % [Modulus] Addition (+) : It is used to perform addition operation on the data items, items include
5 min read
SQL - Logical Operators
SQL logical operators are used to test for the truth of the condition. A logical operator like the Comparison operator returns a boolean value of TRUE, FALSE, or UNKNOWN. In this article, we will discuss different types of Logical Operators. Logical operators are used to combine or manipulate the conditions given in a query to retrieve or manipulat
4 min read
SQL Server Arithmetic Operators
Microsoft's SQL Server, a capable relational database management system, has a lot of features to deal with data and make manipulations purely. SQL Server, in addition to the many features available to users, has a set of operators for arithmetic which allows users of databases to perform mathematical operations on data stored within tables. In thi
3 min read
Arithmetic Operators in SQL Server
Arithmetic operators play a crucial role in performing mathematical calculations within SQL Server. These operators allow you to perform addition, subtraction, multiplication, division, and more on numeric data stored in your database tables. In this article, we'll explore the various arithmetic operators available in SQL Server, discuss their usag
4 min read
Relational Operators in SQL Server
In SQL Server, relational operators are used to compare values and establish relationships between data stored in tables. These operators allow us to perform logical comparisons to filter data based on specific conditions. Understanding relational operators is fundamental for querying and manipulating data effectively in SQL Server. In this article
4 min read
SQL Operators
SQL Operators perform arithmetic, comparison, and logical operations to manipulate and retrieve data from databases. In this article, we will discuss Operators in SQL with examples, and understand how they work in SQL. Operators in SQLOperators in SQL are symbols that help us to perform specific mathematical and logical computations on operands. An
5 min read
SQL Comparison Operators
SQL Comparison Operators are used to compare two values and check if they meet the specific criteria. Some comparison operators are = Equal to, > Greater than , < Less than, etc. Comparison Operators in SQLThe below table shows all comparison operators in SQL : OperatorDescription=The SQL Equal Operator checks if the values of two operands ar
3 min read
PL/SQL Operators
The PL/SQL language offers various operators for data manipulation and logical processing. There are several types of these operators which include arithmetic operators, relational operators, comparison operators, and logical operators. In this guide, we will learn about the various PL/SQL operators with the help of examples and so on. PL/SQL Opera
3 min read
SQL | Difference between functions and stored procedures in PL/SQL
Prerequisite: Procedures in PL/SQLFunctions in PL/SQL.Difference between functions and stored procedures in PL/SQL Differences between Stored procedures(SP) and Functions(User-defined functions (UDF)): 1. SP may or may not return a value but UDF must return a value. The return statement of the function returns control to the calling program and ret
3 min read
SQL SERVER – Input and Output Parameter For Dynamic SQL
An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
3 min read
Difference between T-SQL and PL-SQL
1. Transact SQL (T-SQL) : T-SQL is an abbreviation for Transact Structure Query Language. It is a product by Microsoft and is an extension of SQL Language which is used to interact with relational databases. It is considered to perform best with Microsoft SQL servers. T-SQL statements are used to perform the transactions to the databases. T-SQL has
3 min read
Dynamic SQL and Temporary Tables in SQL Server
In SQL Server, creating and using Temp Tables using dynamic SQL is a good feature when we need to temporarily create tables at run time and delete automatically all within a session. They can be very useful when we need to store temporary data in a structured format and do data manipulation using Data Manipulation Language in SQL. In this article l
6 min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced
SQL (Structured Query Language) is a powerful tool used for managing and manipulating relational databases. Whether we are beginners or experienced professionals, practicing SQL exercises is important for improving your skills. Regular practice helps you get better at using SQL and boosts your confidence in handling different database tasks. So, in
15+ min read
Difference between SQL and T-SQL
SQL (Structured Query Language) is the standard language for managing and manipulating relational databases, enabling operations like querying, updating, and deleting data. T-SQL (Transact-SQL), an extension of SQL developed by Microsoft, adds advanced features and procedural capabilities specifically for SQL Server. In this article, We will learn
4 min read
SQL Server | Convert tables in T-SQL into XML
In this, we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command. Let's discuss it one by one. Overview :XML (Extensible Markup Language) is a markup language similar to HTML which was designed to share information between different platforms.
2 min read
SQL - SELECT from Multiple Tables with MS SQL Server
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
3 min read
How to Execute SQL Server Stored Procedure in SQL Developer?
A stored procedure is a set of (T-SQL ) statements needed in times when we are having the repetitive usage of the same query. When there is a need to use a large query multiple times we can create a stored procedure once and execute the same wherever needed instead of writing the whole query again. In this article let us see how to execute SQL Serv
2 min read
SQL Query to Check if Date is Greater Than Today in SQL
In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a ‘YYYY-MM-DD hh:mm: ss. mmm’ pattern. Features: This function is used to find the present date a
2 min read
SQL Query to Add a New Column After an Existing Column in SQL
Structured Query Language or SQL is a standard Database language that is used to create, maintain and retrieve data from relational databases like MySQL, Oracle, SQL Server, Postgres, etc. In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modif
3 min read
SQL Query to Convert Rows to Columns in SQL Server
In this article we will see, how to convert Rows to Column in SQL Server. In a table where many columns have the have same data for many entries in the table, it is advisable to convert the rows to column. This will help to reduce the table and make the table more readable. For example, Suppose we have a table given below: NAMECOLLEGEROLL NUMBERSUB
2 min read
Dynamic SQL in SQL Server
In SQL Server, at times the SQL Queries need to be dynamic and not static, meaning the complete SQL query may be built dynamically at run time as a string using the user inputs and any specific application logic. This can be done in queries run from back-end applications or inside stored procedures. In this article let us look into the details abou
6 min read
How to SQL Select from Stored Procedure using SQL Server?
There may be situations in SQL Server where you need to use a stored procedure to get data from a SQL query. For direct data selection from a stored procedure within a query, SQL Server offers options like OPENQUERY and OPENROWSET. The usual way is running the stored procedure independently and then querying the outcomes. The idea of utilizing SQL
3 min read
SQL Quiz : Practice SQL Questions Online
This SQL quiz covers various topics like SQL basics, CRUD operations, operators, aggregation functions, constraints, joins, indexes, transactions, and query-based scenarios. We've included multiple-choice questions, fill-in-the-blank questions, and interactive coding challenges to keep things interesting and challenging. Whether you're a beginner l
3 min read
BULK INSERT in SQL Server(T-SQL command)
BULK INSERT in SQL Server(T-SQL command): In this article, we will cover bulk insert data from csv file using the T-SQL command in the SQL server and the way it is more useful and more convenient to perform such kind of operations. Let's discuss it one by one.  ConditionSometimes there is a scenario when we have to perform bulk insert data from .cs
3 min read
Queries using AND ,OR ,NOT operators in MySQL
AND, OR, NOT operators are basically used with WHERE clause in order to retrieve data from table by filtering with some conditions using AND, OR, NOT in MySQL.Here in this article let us see different queries on the student table using AND, OR, NOT operators step-by-step. Step-1:Creating a database university: CREATE DATABASE university; Step-2:Usi
2 min read
How to Combine MySQL IN and LIKE Operators?
In database management, MySQL stands as one of the most popular relational database management systems. As developers strive to extract specific data from their databases, the judicious use of SQL operators becomes imperative. Among these operators, the IN and LIKE operators play crucial roles in facilitating flexible and precise data retrieval. Th
4 min read