Boolean logic: How to get what you want

A visual exercise in writing queries for any purpose

Technology has become increasingly accessible to people outside technical fields through lighter weight Saas offerings and designed interfaces. However, with this comes the need to educate users how to best leverage the tools. While understanding how to use highly designed interfaces is great, you will be able to better speak to results and understand how to create more complex solutions to fit your need.

Boolean logic is one of the base concepts to many search-focused tools out there: Google search and online libraries are common examples of tools based on this concept. In the digital marketing world, tools you use to listen to mentions of your brand on social media, press release sites, blogs and more are more often than not using Boolean logic as the base for how they pull in and filter that data. While the tools come with wizards to help you write the search queries, they have limitations and are not as flexible as writing the logic yourself.

You could tap a developer...or learn yourself! This comes with practice and can help with any code or analysis you do--it is less about writing code and much more about understanding how to categorize and define what you really want. Being able to do this will help not only save time and resources in your searches, whatever tool you use for your purposes, but it will help you to better understand what else you may want to know and how to communicate what it is you've found and how.

Below is a tutorial that will take you through simple to intermediate level Boolean logic. As you go through this, there are a couple things to keep in mind:

  • Practice "clean" code
    • Start from the most general way to write something and narrow from there--you'll find you write less. For example, if you have "New York" in a query, there is no need to also have "New York City," as it will already be included with the broader term.
    • Try using each element or phrase as few times as possible. It helps in changing the code later, as you only have to change a few pieces. It's also easier to understand the intention, if you are sharing your code with someone else.
  • Be intentional
    • If you are trying to find something specific, it helps to be descriptive in your logic. If you are looking for something in New York City, make sure to have "New York City," not just "New York."
    • If you are exploring what anomalies there may be out there, look to leveraging the AND NOT operator more to exclude what you do know about your data set.

Let's get started

First, you will need to know the Boolean operators and a little about the "data" you're working with each exercise will be determining a query that will identify a certain subset of this.

If you were to write a query for this, it would just be "*," as star means "all."

If you were to write a query for this, it would just be "*," as star means "all."

shape: circle AND fill_color: blue

shape: circle AND fill_color: blue

Boolean operators/elements to start out with:

  • OR: Either variable can be true
  • AND: Both variables must be true
  • NOT: The variable cannot be true
  • ( ): Parenthesis are used to denote phrasing and order in processing. You must separate using OR, AND, and NOT within one query with these.

Exercise variables:

  • Shape: circle, square, triangle, star, hexagon
  • Fill: none, solid, "polka dot"
  • Fill_color: red, blue, yellow, green
  • Line_color: red, blue, yellow

Combine operators and variables to create a query. It's as simple as "shape: circle AND fill_color: blue."

Grab a pen and piece of paper and for each image of a shape sets, write out your guess first. Then hover your mouse over the image to see the answer. Know that there are several ways to write a correct query, so if it doesn't match, evaluate why. You still may be right and in that case, evaluate which way was written most clean.

 

Exercise 1

line_color: yellow

line_color: yellow

Exercise 2

line_color: yellow AND fill: "polka dot" AND shape: star

line_color: yellow AND fill: "polka dot" AND shape: star

Exercise 3

shape: star NOT fill_color: green

shape: star NOT fill_color: green

 

Exercise 4

line_color: yellow NOT fill_color: greenORline_color: yellow AND fill_color:(yellow OR none)

line_color: yellow NOT fill_color: green

OR

line_color: yellow AND fill_color:(yellow OR none)

Exercise 5

(shape: (star OR circle OR square) AND fill: solid) NOT fill_color: green

(shape: (star OR circle OR square) AND fill: solid) NOT fill_color: green

Exercise 6

 (shape: triangle AND fill: none) OR (shape: star AND fill: (none OR solid)) OR (shape: square AND (fill: solid OR (fill: none NOT fill_color: yellow))) OR (shape: circle AND (fill: none OR (fill: solid NOT fill_color: red)))NOT (fill: “polka dot” O…

 

(shape: triangle AND fill: none) OR (shape: star AND fill: (none OR solid)) OR (shape: square AND (fill: solid OR (fill: none NOT fill_color: yellow))) OR (shape: circle AND (fill: none OR (fill: solid NOT fill_color: red)))

NOT (fill: “polka dot” OR fill_color: green OR shape: hexagon)

 

 

Want more practice?

Download the PDF worksheet and grab a friend and have them circle one or more pieces of data and see if you can write a query to describe it. Each data piece in the data set is unique, so you can choose any combination to inspire a new logic challenge.

Click on the image to download the worksheet PDF.

Click on the image to download the worksheet PDF.