SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL OPERATORS SQL COMMANDS SQL Queries

SQL Table

SQL TABLE SQL CREATE TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL UPDATE TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT INTO Statement

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL

SQL SELECT OR Operator

This SQL tutorial explains and helps you understand how to use the OR operator in the SELECT query with examples.

The OR operator in the Structured Query Language is used to retrieve the data from the table if one of the expressions is true in the query.

For example, a student's mark is greater than 80 OR student resides in the 'Hyderabad' city. Here student mark is greater than 80, but the city resides in 'Pune’, and then student data is displayed because one of the given conditions is true, i.e., the mark is greater than 80.

Syntax of the SQL SELECT OR Operator is as follows:

SELECT Column_Name_1, Column_Name_2, Column_Name_3. Column_Name_4, Column_Name_5 FROM Table_Name WHERE Column_Name = Value OR Column_Name =Value;

Column name in the expression can be same or different.

Let’s understand the SQL SELECT OR Operator with the help of examples.

Consider the already existing table named Diploma_Student:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592911
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758075788076775
202118Sakashi Patil807874788077782
202119Sopan Bhore706875758080752
202220Prajwal Lokhande808585757880814
202221Anuja Wanare858886828485855
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202224Aakash Jain807572748580784
202225Akshay Agarwal858078889082845

Example 1: Execute a query to display the student details where student first Semester percentage is greater than 80 OR student name starts with the letter 'A'.

SELECT * FROM Diploma_Student WHERE First_Sem > 80 OR Student_Name LIKE 'A%';

In the above SELECT OR Operator query example, we displayed the student details where the student's first-semester percentage is greater than 80 or the student’s name starts with the letter 'A'. Here, the student percentage is greater than 80, but the name didn't start with the letter 'A' record get the display.

The output of the above query is given below:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592911
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202221Anuja Wanare858886828485855
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202224Aakash Jain807572748580784
202225Akshay Agarwal858078889082845
SQL SELECT OR Operator

Example 2: Execute a query to display student details where the overall percentage is between 80 and 90 OR sixth semester greater than 85.

SELECT * FROM Diploma_Student WHERE Total BETWEEN 80 AND 90 OR Sixth_Sem > 85;

In the above SELECT OR Operator query example, we displayed the student details where the total student percentage is between 80 and 90, OR the sixth-semester percentage is greater than 85. We have used the BETWEEN Operator with the OR operator in this query.

The output of the above query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592911
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202220Prajwal Lokhande808585757880814
202221Anuja Wanare858886828485855
202222Venkatesh Iyer908987909291903
202225Akshay Agarwal858078889082845
SQL SELECT OR Operator

Example 3: Execute a query to display student details where student fifth-semester percentage is greater than 85 OR department name contains 'ec’ anywhere in the name.

SELECT * FROM Diploma_Student WHERE Fifth_Sem > 85 OR Department_Id IN (SELECT Department_Id FROM Department WHERE Department_Name LIKE '%ec%');    

In the above SELECT OR Operator query example, we displayed student details where student fifth-semester percentage is greater than 85 or student department name contain 'ec' anywhere in the name. We have used a subquery in the above example. First, the inner query gets executed, then the outer query is executed.

The output of the above query is:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592911
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202118Sakashi Patil807874788077782
202119Sopan Bhore706875758080752
202222Venkatesh Iyer908987909291903
202225Akshay Agarwal858078889082845
SQL SELECT OR Operator



ADVERTISEMENT
ADVERTISEMENT