How to Add Boolean Operators in MySQL Query

Here is an example to use Boolean datatype in the WHERE clause. It works well in MySQL, and this query is helpful for data science engineers.

IN THIS PAGE

  1. Usage of Boolean datatype
  2. Create tables in MYSQL
  3. Using Boolean operator in WHERE

Usage of Boolean datatype

  • Precisely where it is helpful. When a column in the table has a value of Yes/No and in that cases, you can use the Boolean operator’s technique.
  • Here I will show a simple demo by creating four tables with some data. To this, I have used the online MySQL compiler. The tables I have created are model, make, salesdetails and stock.

Create tables in MYSQL

Create four tables Model, Make, Salesdetails, and Stock in MySQL database. Here, the stock table has a Boolean datatype. Checkout how to create a Data clinic.

create table make(make_name varchar(10));
create table model(model_name varchar(10), model_id integer);
create table stock(stock_code integer, IsRHD Boolean);
create table stockdetails(stock_id integer);
Best selling E-books

Using Boolean operator in WHERE

Here’s an SQL query use the Boolean operator in the WHERE clause to get required data.

SELECT     DISTINCT a.make_name, b.model_name
FROM       make AS a
JOIN       model AS b USING(make_id)
JOIN       stock AS c USING(model_id)
JOIN       salesdetails d ON c.stock_code = d.stock_id
WHERE      c.IsRHD = 1
ORDER BY   a.make_name, b.model_name;

Here IsRHD column contains YES/NO values, which is the part of the stock table. Boolean operators, the ‘0’ speaks for ‘NO,’ and ‘1’ as ‘YES”. This way, you can use Boolean operators in MySQL.

Reference books for MySQL.

Related

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.