Open In App

BIT_OR() Function in MySQL

Last Updated : 24 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

BIT_OR() function in MySQL is used to return the bitwise OR of all bits in a given expression. It first converts all decimal values into binary values, and then perform bitwise or operation on those binary values.

Syntax :

BIT_OR(expr)

Parameter : This method accepts only one parameter.

  • expr –
    Input expression on which we want to apply BIT_OR function.

Returns : It returns bitwise OR of all bits in a given expression.

Example-1 :
To demonstrate the working of the BIT_OR function, we will have to first create a table named EmployeeDetails.

CREATE TABLE EmployeeDetails(
Employee_Id INT AUTO_INCREMENT,  
Employee_Name VARCHAR(100) NOT NULL,
Working_At VARCHAR(20) NOT NULL,
Work_Location  VARCHAR(20) NOT NULL,
Joining_Date DATE NOT NULL,
Annual_Income INT  NOT NULL,
PRIMARY KEY(Employee_Id )
);

Now, inserting the values into the table –

INSERT INTO EmployeeDetails (Employee_Name, Working_At, 
Work_Location, Joining_Date, Annual_Income)
VALUES
('Amit Khan', 'XYZ Digital', 'Kolkata', '2019-10-06', 350000),
('Shreetama Pal', 'ABC Corp.', 'Kolkata', '2018-12-16', 500000),
('Aniket Sharma', 'PQR Soln.', 'Delhi', '2020-01-11', 300000),
('Maitree Jana', 'XYZ Digital', 'Kolkata', '2019-05-01', 400000),
('Priyanka Ojha', 'ABC Corp.', 'Delhi', '2019-02-13', 350000),
('Sayani Mitra', 'XYZ Digital', 'Kolkata', '2019-09-15', 320000),
('Nitin Dey', 'PQR Soln.', 'Delhi', '2019-10-06', 250000),
('Sujata Samanta', 'PQR Soln.', 'Kolkata', '2020-10-06', 350000),
('Sudip Majhi', 'ABC Corp.', 'Delhi', '2018-10-30', 600000),
('Sanjoy Kohli', 'XYZ Digital', 'Delhi', '2019-04-18', 450000);

To verify used the following command as follows –

Select * FROM EmployeeDetails;

Output :

EMPLOYEE_ID EMPLOYEE_NAME WORKING_AT WORK_LOCATION JOINING_DATE ANNUAL_INCOME
1 Amit Khan XYZ Digital Kolkata 2019-10-06 350000
2 Shreetama Pal ABC Corp. Kolkata 2018-12-16 500000
3 Aniket Sharma PQR Soln. Delhi 2020-01-11 300000
4 Maitree Jana XYZ Digital Kolkata 2019-05-01 400000
5 Priyanka Ojha ABC Corp. Delhi 2019-02-13 350000
6 Sayani Mitra XYZ Digital Kolkata 2019-09-15 320000
7 Nitin Dey PQR Soln. Delhi 2019-10-06 250000
8 Sujata Samanta PQR Soln. Kolkata 2020-10-06 350000
9 Sudip Majhi ABC Corp. Delhi 2018-10-30 600000
10 Sanjoy Kohli XYZ Digital Delhi 2019-04-18 450000

Now we will find the BIT_OR of the annual income of all the employees company-wise from the EmployeeDetails table.

SELECT Working_At, BIT_OR(Annual_Income) AS BITORINCOME 
FROM EmployeeDetails group by Working_At;

Output :

WORKING_AT BITORINCOME
XYZ Digital 524272
ABC Corp. 1046512
PQR Soln. 514032


Example-2 :
Now we will find the BIT_OR of the annual income of all the employee location wise from the EmployeeDetails table.

SELECT Work_Location, BIT_OR(Annual_Income) AS BITORINCOME 
FROM EmployeeDetails 
Group By Work_Location;

Output :

WORK_LOCATION BITORINCOME
Kolkata 524208
Delhi 1048560


Example-3 :
Firstly Creating a StudentMarks table –

CREATE TABLE StudentMarks(
StudentId INT AUTO_INCREMENT,  
StudentName VARCHAR(100) NOT NULL,
Class VARCHAR(20) NOT NULL,
Roll INT NOT NULL,
Sub1Mark INT NOT NULL,
Sub2Mark INT NOT NULL,
Sub3Mark INT NOT NULL,
TotalMarks INT NOT NULL,
PRIMARY KEY(StudentId)
);

Inserting data into the table –

INSERT INTO StudentMarks
(StudentName, Class, Roll, Sub1Mark, 
Sub2Mark, Sub3Mark, TotalMarks)
VALUES
('Amit Jana', 'V', 10100, 85, 80, 95, 260),
('Labanya Mallick', 'VI', 11000, 81, 89, 95, 265),
('Virat Sharma', 'VI', 12000, 75, 83, 90, 248),
('Sayani Samanta', 'V', 13000, 95, 90, 99, 284),
('Riyanka Panda', 'V', 14000, 70, 87, 88, 245),  
('Ritika Shah', 'VI', 15000, 78, 89, 90, 257);

To verify used the following command as follows –

SELECT  * From StudentMarks;

Output :

STUDENTID STUDENTNAME CLASS ROLL SUB1MARK SUB2MARK SUB3MARK TOTALMARKS
1 Amit Jana V 10100 85 80 95 260
2 Labanya Mallick VI 11000 81 89 95 265
3 Virat Sharma VI 12000 75 83 90 248
4 Sayani Samanta V 13000 95 90 99 284
5 Riyanka Panda V 14000 70 87 88 245
6 Ritika Shah VI 15000 78 89 90 257

Now we will find the BIT_OR of total marks of all the student class wise from the StudentMarks table.

SELECT Class, BIT_OR(TotalMarks) AS BITORMARKS 
FROM StudentMarks 
Group By Class;

Output :

CLASS BITORMARKS
V 509
VI 505

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads