BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

SQL – Combining AND, OR and NOT together in where clause

Last Updated: November 28, 2018 by Chaitanya Singh | Filed Under: SQL

In the last tutorial, we learned how to use logical operators AND, OR and NOT in SQL where clause. In this guide, we will learn how to combine these operators together in SQL where clause.

SQL AND & OR Operators together

Table: EMPLOYEE

+----+--------------+-----+-----------+----------+
| ID | EMP_NAME     | AGE | LOCATION  | SALARY   |
+----+--------------+-----+-----------+----------+
|123 | Daryl        |  41 | Chennai   |  120000  |
|124 | Jon          |  40 | Delhi     |   80000  |
|125 | Saru         |  43 | Agra      |  110000  |
|126 | Hemant       |  39 | Shimla    |  110000  |
|127 | Devesh       |  42 | Goa       |   90000  |
+----+--------------+-----+-----------+----------+

The following SQL statement will fetch the details of those employees, who are more than 40 years old and their location is either “Chennai” or “Agra”.

SELECT * FROM EMPLOYEE
WHERE AGE>40 AND (LOCATION='Chennai' OR LOCATION='Agra');

Result:

+----+--------------+-----+-----------+----------+
| ID | EMP_NAME     | AGE | LOCATION  | SALARY   |
+----+--------------+-----+-----------+----------+
|123 | Daryl        |  41 | Chennai   |  120000  |
|125 | Saru         |  43 | Agra      |  110000  |
+----+--------------+-----+-----------+----------+

SQL AND, OR and NOT Operators together

Lets see an example where we are using all three AND, OR and NOT operators in a single SQL statement.

Table: EMPLOYEE

+----+--------------+-----+-----------+----------+
| ID | EMP_NAME     | AGE | LOCATION  | SALARY   |
+----+--------------+-----+-----------+----------+
|123 | Daryl        |  41 | Chennai   |  120000  |
|124 | Jon          |  40 | Delhi     |   80000  |
|125 | Saru         |  43 | Agra      |  110000  |
|126 | Hemant       |  39 | Shimla    |  110000  |
|127 | Devesh       |  42 | Goa       |   90000  |
+----+--------------+-----+-----------+----------+

The following SQL statement will fetch the details of those employees, who are more than 40 years old and their location is either “Chennai” or NOT “Agra”.

SELECT * FROM EMPLOYEE
WHERE AGE>40 AND (LOCATION='Chennai' OR NOT LOCATION='Agra');

Result:

+----+--------------+-----+-----------+----------+
| ID | EMP_NAME     | AGE | LOCATION  | SALARY   |
+----+--------------+-----+-----------+----------+
|123 | Daryl        |  41 | Chennai   |  120000  |
|127 | Devesh       |  42 | Goa       |   90000  |
+----+--------------+-----+-----------+----------+
❮ PreviousNext ❯

Top Related Articles:

  1. SQL – DROP Table Statement to delete the entire table
  2. SQL Syntax
  3. DELETE Query in SQL
  4. Introduction to SQL
  5. SQL WHERE Clause

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

SQL Tutorial

  • SQL Tutorial
  • SQL Introduction
  • SQL Syntax
  • SQL Data Types

SQL Database

  • SQL CREATE DB
  • SQL DROP DB
  • SQL Rename DB
  • SQL USE DB

SQL Queries

  • SQL Select
  • SQL Select Distinct
  • SQL Select Count
  • SQL Select Top
  • SQL Where
  • SQL AND, OR & NOT
  • SQL Order By
  • SQL Insert Into
  • SQL Insert Into SELECT
  • SQL Select Random
  • SQL Alias
  • SQL NULL Check
  • SQL Update
  • SQL Delete
  • SQL MIN, MAX
  • SQL SUM
  • SQL AVG

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap