Boolean operators AND, OR and NOT: a guide for search engines, databases and code
Boolean operators are the words AND, OR and NOT used to combine search terms or conditions. AND narrows a search to results with every term, OR widens it to results with any of them, and NOT removes results with a term. Parentheses group terms and quotes keep phrases together.
This guide explains the three operators with diagrams in words and truth tables, shows how Google, library databases, PubMed and LinkedIn differ on operators, precedence, quotes and truncation, works through real search strings, and covers the mistakes that cost the most results. Every claim that two search strings mean the same thing links to a free checker that tests them on every combination of matching terms.
The three Boolean operators
| Operator | Example | Finds | In a Venn diagram |
|---|---|---|---|
| AND | cats AND dogs | results that mention both terms | only the overlap of the two circles |
| OR | cats OR dogs | results that mention either term, or both | both circles, overlap included |
| NOT | cats NOT dogs | results that mention cats but not dogs | the cats circle with the overlap cut out |
The operators are named after the mathematician George Boole, whose algebra of true and false they come from. Some search tools write NOT as AND NOT, or as a minus sign.
Each row of a truth table is one kind of result. With two terms there are four kinds: results that mention neither, only dogs, only cats, or both. T means the result is returned:
| # | cats | dogs | cats AND dogs | cats OR dogs | cats NOT dogs |
|---|---|---|---|---|---|
| 0 | FALSE | FALSE | FALSE | FALSE | FALSE |
| 1 | FALSE | TRUE | FALSE | TRUE | FALSE |
| 2 | TRUE | FALSE | FALSE | TRUE | TRUE |
| 3 | TRUE | TRUE | TRUE | TRUE | FALSE |
Computed by the Tangibool engine
AND returns one kind of result out of four, OR three and NOT one. The truth table generator draws cats NOT dogs as an AND gate with a NOT on one input. Two facts follow and are worth remembering:
- OR always gives at least as many results as either term alone. Every result for cats is also a result for cats OR dogs: cats → (cats OR dogs) is true on every row.
- AND always gives at most as many results as either term alone. Every result for cats AND dogs is also a result for cats: (cats AND dogs) → cats is true on every row.
Parentheses, quotes and truncation
Three more tools turn a list of terms into a precise search:
- Parentheses group terms so the operators apply in the order you mean:
teen AND (sleep OR insomnia)finds results about teenagers and either sleep or insomnia. Without them, many systems readteen AND sleep OR insomniaas(teen AND sleep) OR insomnia, which also returns every result about insomnia, teenagers or not. The equivalence checker shows the two searches differ, with the counterexample of a result about insomnia that does not mention teenagers. - Quotes keep a phrase together and in order:
"social media"finds the phrase, not results that mention social and media separately. - Truncation with an asterisk finds every ending of a word stem:
educat*finds education, educator and educational. Most library databases support it; Google does not.
OR is the operator for synonyms, and parentheses keep each set of synonyms together. A search usually has one group per concept, joined by AND:
(teenagers OR adolescents) AND (sleep OR insomnia) AND "social media"
Grouping does not change what a search means when it only spreads an AND over an OR: (teen OR adolescent) AND sleep is the same as (teen AND sleep) OR (adolescent AND sleep), the distributive law. That is useful when a search box limits how long a string can be.
How the main search tools differ
The same operators behave differently from one tool to the next, so a string that works in one can fail in another.
| Library databases (EBSCOhost, ProQuest and similar) | PubMed | |||
|---|---|---|---|---|
| AND | implied by a space | AND | AND, in capitals | AND, in capitals |
| OR | OR in capitals, or a vertical bar | OR | OR, in capitals | OR, in capitals |
| NOT | a minus sign before the word: -snake | NOT, sometimes AND NOT | NOT, in capitals | NOT, in capitals |
| Phrases | quotes | quotes | quotes | quotes |
| Parentheses | not documented; keep searches simple | supported | supported | supported |
| Truncation | not supported; * stands for a whole word inside quotes | * at the end of a stem | * at the end of a stem | not supported |
| Order of operations | not documented | varies by platform | left to right, no precedence | quotes, parentheses, NOT, AND, OR |
Two rows deserve a closer look.
Order of operations. PubMed reads its operators strictly from left to right, so aspirin OR heparin AND stroke means (aspirin OR heparin) AND stroke. In standard Boolean logic AND binds first, giving aspirin OR (heparin AND stroke), and the equivalence checker shows the two differ, for example on an article about aspirin that does not mention stroke. LinkedIn applies NOT before AND and AND before OR. Library platforms differ from each other. The safe rule everywhere: whenever a search mixes AND and OR, add parentheses.
Capitals. Several tools treat lowercase “and”, “or” and “not” as ordinary words to search for. Writing the operators in capitals works everywhere.
Worked search strings
A research question: does social media use affect teenagers' sleep?
(teen* OR adolescents) AND (sleep OR insomnia) AND ("social media" OR Instagram OR TikTok)
One group per concept, synonyms joined by OR inside each group, groups joined by AND, phrases in quotes, truncation for the ending of teen. In PubMed, check truncated terms with care, because PubMed needs at least four letters before an asterisk and leaves truncated terms out of its automatic term mapping.
A product search on Google:
"noise cancelling" headphones -wireless
Google applies AND between the words, keeps the phrase in quotes together, and removes pages that mention wireless.
A recruiter's search on LinkedIn:
("software engineer" OR developer) AND (Python OR Golang) NOT manager
The parentheses keep each set of alternatives together. With short names for the terms, spelling the search out as four combinations, (engineer AND python) OR (engineer AND golang) OR (developer AND python) OR (developer AND golang), then excluding managers, finds exactly the same people.
Excluding two topics: pets NOT (dogs OR cats) removes every result that mentions dogs or cats, and is the same as pets NOT dogs NOT cats. It is not the same as pets AND (NOT dogs OR NOT cats), which only removes results that mention both, and the checker gives a result it would keep: one about pets and cats that does not mention dogs.
Building a Boolean search, step by step
- Write the question as a sentence. “Does social media use affect how teenagers sleep?”
- Pick out the concepts. Usually two to four: teenagers, sleep, social media. Leave out words that every result will contain anyway, such as “effect”.
- List synonyms and related terms for each concept. Teenagers, adolescents, young people; sleep, insomnia, bedtime; social media, Instagram, TikTok. Note spelling variants such as behaviour and behavior.
- Join each list with OR inside parentheses, and the lists with AND. One group per concept.
- Run it, read the first results, and adjust. Too many results: add a concept with AND, or use a narrower phrase. Too few: add synonyms with OR, or drop the least important concept. Add NOT last, and only for a term that marks results you never want.
Write the final string down with the date and the database. Systematic reviews in medicine and the social sciences report their search strings exactly, so that others can run them again.
Proximity and field searching
Most library databases go beyond AND with proximity operators, which require two terms to appear near each other: for example N5 or W5 in EBSCOhost and NEAR/5 in ProQuest and Web of Science, meaning within five words. They are narrower than AND and wider than a phrase in quotes. The syntax differs between platforms, so check the help page of the one you use.
Field searching limits a term to one part of a record, such as the title, the abstract or the author. Library databases offer it in their advanced search forms, PubMed with tags such as [ti] for title and [tiab] for title and abstract, and Google with operators such as site: (one website), filetype: (one file format) and intitle: (a word in the page title): "sleep hygiene" site:nhs.uk searches one website for a phrase.
Boolean operators beyond search
The same three operators appear wherever conditions are combined:
| Where | AND | OR | NOT |
|---|---|---|---|
| Search engines and databases | AND | OR | NOT, AND NOT or a minus sign |
| SQL | AND | OR | NOT |
| Python | and | or | not |
| JavaScript, Java, C | && | || | ! |
| Excel and Google Sheets | AND(a, b) | OR(a, b) | NOT(a) |
| Logic and mathematics | ∧ | ∨ | ¬ |
The operators page also covers XOR, “one or the other but not both”, which search tools rarely offer, and the symbols page lists every spelling.
Common mistakes
- Using OR to narrow a search. “cats OR dogs” finds more than either alone, never fewer. To narrow, use AND.
- Leaving out parentheses. A search that mixes AND and OR without parentheses means different things in different tools. See the AND–OR grouping slip.
- Removing too much with NOT.
jaguar NOT cardrops every article about the animal that happens to mention a car, and keeps pages about jaguars of any other kind; it is not the same search asjaguar AND animal. Use NOT for terms that only appear in the results you do not want. - Writing the operators in lowercase. Some tools search for the word “and” instead.
- Mixing tools' syntax. A minus sign works on Google but is not supported on LinkedIn, which wants NOT; truncation works in library databases and not on Google.
Check two search strings
Before running a long search, or when rewriting someone else's, check that two versions mean the same thing:
- Replace each term or phrase with a short name:
teen,sleep,insomnia. - Put the two versions into the equivalence checker with AND, OR, NOT and parentheses, for example
teen AND (sleep OR insomnia)andteen AND sleep OR insomnia. - If they are not equivalent, the checker shows a kind of result that one search returns and the other does not.
The checker reads AND before OR, as standard logic does; for PubMed, add the parentheses that its left-to-right reading implies before comparing.
Frequently asked questions
What are Boolean operators?
The words AND, OR and NOT, used to combine search terms or conditions. AND requires every term, OR accepts any, and NOT excludes a term.
What is a list of Boolean operators?
AND, OR and NOT, with AND NOT as a variant of NOT. Searches also use parentheses for grouping, quotes for phrases and an asterisk for truncation, which are not operators but are used with them.
How do Boolean operators work on Google?
Google puts AND between words automatically, accepts OR in capitals, uses a minus sign for NOT and quotes for exact phrases. It does not support truncation.
What is the difference between AND and OR in a search?
AND returns only results that contain every term, so it narrows a search. OR returns results that contain any of the terms, so it widens it.
What does NOT do in a search?
It removes results that contain the term after it. Use it carefully: it also removes useful results that happen to mention that term.
Why use parentheses in a Boolean search?
To control the order in which the operators apply. Without them, tools differ: some apply AND before OR and PubMed reads left to right.
What are examples of Boolean searches?
(teenagers OR adolescents) AND sleep, "climate change" AND agriculture NOT livestock, and on Google, "noise cancelling" headphones -wireless.
What is the order of Boolean operators?
In standard logic, NOT first, then AND, then OR. Search tools differ: LinkedIn follows that order after quotes and parentheses, PubMed reads strictly from left to right, and library platforms vary. Parentheses make the order explicit everywhere.
How do I search for a phrase?
Put it in quotes: "social media". Without quotes, most tools treat the words as separate terms joined by AND.
Are Boolean operators case sensitive?
In many tools, yes: PubMed and LinkedIn need AND, OR and NOT in capitals, and Google needs OR in capitals. Capitals work everywhere.
Related pages
- Guides: Boolean logic
- Operator precedence and the AND–OR grouping slip
- Boolean logic symbols and spellings
- Operators: AND, OR, NOT
- Pitfalls: inclusive vs exclusive or and neither … nor
- Laws: De Morgan and the distributive law
- The equivalence checker
Written by Muaz Notiar. The Tangibool engine (semantics tangibool-semantics/1, engine 1.0.0) computed the truth table marked “Computed by the Tangibool engine”, and the 10 tool results the guide links to were re-run through the tools' own code in the build of and match what the text says. © 2026 Foreranger LTD.