MySQL BIT_OR Function

MySQL BIT_OR is one of the Aggregate Function, which performs Bitwise OR operation on all bits. First, this MySQL BIT_OR function converts all decimal values into binary values and perform bitwise OR operation on those binary values.

MySQL BIT_OR Syntax

The basic syntax of the BIT_OR Function in MySQL is as shown below:

SELECT BIT_OR (Expression)
FROM [Source]

The Truth Table behind this BIT_OR function is

xyx BIT_OR y
000
011
101
111

MySQL BIT_OR Function Example 1

In this example, we are going to implement the MySQL Bitwise OR function on different columns in a table. To demonstrate the same, we are going to use the dup employee 2 table data that we have shown below

MySQL BIT_OR Function Example 1

The below MySQL statement finds the Bitwise OR values of Sales 1 and Sales 2 columns.

-- MySQL BIT_OR Function Example
USE company;
SELECT BIT_OR(Sales1),
       BIT_OR(Sales2)
FROM dupemploy2;
MySQL BIT_OR Function Example 2

x = 9 = 1001

y = 12 = 1100 and the Bitwise OR for the above bits is 1101 = 8 + 4 + 0 + 1 = 13

MySQL BIT OR Group By Example

In this Aggregate Function example, we are going to use the BIT_OR function along with the Group By clause. For this MySQL Bitwise OR group by example, we are going to use the below shown data

MySQL BIT_OR Example 1

BIT_OR CODE

-- MySQL Aggregate BIT_OR Function Example
USE company;
SELECT Profession,
       BIT_OR(Yearly_Income)
FROM dupcustomerdetails
GROUP BY Profession;
MySQL BIT_OR Function Example 3