beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

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

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 ❯

Enjoyed this post? Try these related posts

  1. SQL SELECT DISTINCT Statement
  2. SQL SELECT SUM() Function
  3. SQL – DROP Table Statement to delete the entire table
  4. SQL SELECT Statement
  5. SQL SELECT AVG() Function
  6. Group By clause in SQL

Leave a Reply Cancel reply

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

SQL Tutorial

  • SQL Introduction
  • SQL Syntax
  • 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

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap