The AND, OR, and NOT operators in SQL

The AND and OR operators are used to combine multiple conditions to narrow down the data in an SQL statement. These two operators are called conjugate operators in SQL. AND and OR allow for multiple comparisons with other operators within the same SQL statement.

The AND and OR operators are used to combine multiple conditions to narrow down the data in an SQL statement. These two operators are called conjugate operators in SQL . AND and OR allow for multiple comparisons along with other operators in a single SQL statement.

 

In this SQL lesson, we will explore the AND/OR and NOT operators - how to use them, as well as specific examples.

ANDThe and operators ORare used to filter records based on more than one condition:

  • The AND operator ANDdisplays a record if all conditions separated by AND are TRUE.
  • The operator ORdisplays a record if any of the conditions separated by OR are TRUE.

The operator NOTdisplays a record if the condition(s) are NOT TRUE.

The AND operator in SQL

AND allows the use of multiple conditions in the WHERE clause of an SQL statement. As you saw in the previous lesson, we can only use one condition in WHERE when combined with SELECT . But with AND, you can add other conditions to ensure more accurate data retrieval. The retrieved data must simultaneously satisfy all the conditions in the WHERE clause.

Syntax of AND in WHERE:

SELECT cot1, cot2, cotN FROM TEN_BANG WHERE [DIEU_KIEN1] AND [DIEU_KIEN2].AND [DIEU_KIENN];

Examples of AND:

In this example, we will retrieve employee information from the EMPLOYEE table, filtering out employees whose salary is higher than 2000 and who are younger than 29.

This is the EMPLOYEE table:

 

The command will be as follows:

SQL> SELECT * FROM NHANVIEN WHERE LUONG > 2000 AND TUOI < 29;

The result will look like this:

The OR operator in SQL

The OR operator is used to combine multiple conditions in the WHERE clause of an SQL statement. When using OR in the WHERE clause in conjunction with SELECT, the retrieved data only needs to satisfy one of the listed conditions. Do you notice the difference between OR and AND?

Syntax of OR in WHERE:

SELECT cot1, cot2, cotN FROM TEN_BANG WHERE [DIEU_KIEN1] OR [DIEU_KIEN2].OR [DIEU_KIENN]

You can combine N conditions using OR, and if just one of these N conditions is true, the data will be retrieved.

Examples of OR:

We will retrieve employee information from the EMPLOYEE table below, provided that the SALARY is greater than 6500 and the age is less than 24.

The command would be:

SQL> SELECT * FROM NHANVIEN WHERE LUONG > 6500 OR age < 24;

When executing the above command, any records that meet the conditions of having a salary higher than 6500 or being under 24 years old will be returned in the results table, as shown below:

The NOT operator in SQL

The NOT operator is used to negate the conditions that follow it in the WHERE clause. When using NOT in a SELECT statement combined with a WHERE clause, you will retrieve data that does NOT satisfy the condition.

Syntax of NOT in WHERE:

SELECT cot1, cot2,. cotN FROM TEN_BANG WHERE NOT [DIEU_KIEN]

You can combine the NOT operator with AND/OR when necessary.

 

Examples of NOT:

We will retrieve employee information from the EMPLOYEE table below, provided that the SALARY is not less than 4000 and the age is 24 or greater.

The command would be:

SQL> SELECT * FROM NHANVIEN WHERE NOT LUONG < 4000 AND age >= 24;

After executing the command, anyone with a salary higher than 4000 and aged 24 or older will be selected, with the following results:

+----+----------+-----+-----------+---------+ | ID | TEN | TUOI| DIACHI | LUONG | +----+----------+-----+-----------+---------+ | 4 | Mạnh | 29 | Hue | 6500.00 | | 5 | Huy | 28 | Hatinh | 8500.00 | +----+----------+-----+-----------+---------+

By now, are you confident in using the AND, OR, and NOT operators in the WHERE clause? It's quite simple, isn't it? In the next lesson, we'll learn about a very important SQL statement: the UPDATE statement.

Related posts
  • DROP DATABASE command in SQL

    how is a database drop used in sql? this article will guide you in detail on how to delete a database in sql.
  • Handling copy - HANDLING DUPLICATE in SQL

    this article will show you in detail how to handle copy - handling duplicate with specific examples to make it easier to visualize and capture.
  • Boolean operators AND and OR in SQL

    the and and or operators are used to combine multiple conditions to narrow the data in sql statements. these two operators are called conjugate operators in sql. and and or allow multiple comparisons with other operators in the same sql statement.
Other SQL articles
  • DROP DATABASE command in SQL

    drop database trong sql được dùng như thế nào? bài viết sẽ hướng dẫn bạn chi tiết cách xóa database trong sql.
  • Learn about the most popular RDBMSs

    rdbms có nhiều loại như mysql, oracle, sql server, mongo db, sybase,... Để hiểu hơn về rdbms, trong bài viết này chúng ta sẽ tìm hiểu về một số rdbms phổ biến nhất, so sánh tính năng cơ bản của chúng. mời các bạn cùng theo dõi.
  • Window Functions in SQL: Everything you need to know

    window function trong sql là gì? hãy cùng nhau khám phá cách dùng các window function để tiến hành phân tích thống kê chỉ bằng một truy vấn sql.
  • DROP TABLE or DELETE TABLE command in SQL

    xóa bảng trong sql được sử dụng khá nhiều. thuật ngữ này còn được gọi là drop table trong sql. dưới đây là mọi điều bạn cần biết về xóa table trong sql.
  • CREATE TABLE command in SQL to create a database table

    lệnh create table trong sql có tác dụng gì? Ở bài viết này, hãy cùng nhau tìm hiểu mọi điều bạn cần biết về câu lệnh create table trong sql nhé!
  • Expressions in SQL

    một biểu thức là sự kết hợp của một hoặc nhiều giá trị, toán tử và hàm sql để đánh giá 1 giá trị. những biểu thức sql này giống như công thức và chúng được viết bằng ngôn ngữ truy vấn.
Category

System

Windows XP

Windows Server 2012

Windows 8

Windows 7

Windows 10

Wifi tips

Virus Removal - Spyware

Speed ​​up the computer

Server

Security solution

Mail Server

LAN - WAN

Ghost - Install Win

Fix computer error

Configure Router Switch

Computer wallpaper

Computer security

Mac OS X

Mac OS System software

Mac OS Security

Mac OS Office application

Mac OS Email Management

Mac OS Data - File

Mac hardware

Hardware

USB - Flash Drive

Speaker headset

Printer

PC hardware

Network equipment

Laptop hardware

Computer components

Advice Computer

Game

PC game

Online game

Mobile Game

Pokemon GO

information

Technology story

Technology comments

Quiz technology

New technology

British talent technology

Attack the network

Artificial intelligence

Technology

Smart watches

Raspberry Pi

Linux

Camera

Basic knowledge

Banking services

SEO tips

Science

Strange story

Space Science

Scientific invention

Science Story

Science photo

Science and technology

Medicine

Health Care

Fun science

Environment

Discover science

Discover nature

Archeology

Life

Travel Experience

Tips

Raise up child

Make up

Life skills

Home Care

Entertainment

DIY Handmade

Cuisine

Christmas

Application

Web Email

Website - Blog

Web browser

Support Download - Upload

Software conversion

Social Network

Simulator software

Online payment

Office information

Music Software

Map and Positioning

Installation - Uninstall

Graphic design

Free - Discount

Email reader

Edit video

Edit photo

Compress and Decompress

Chat, Text, Call

Archive - Share

Electric

Water heater

Washing machine

Television

Machine tool

Fridge

Fans

Air conditioning

Program

Unix and Linux

SQL Server

SQL

Python

Programming C

PHP

NodeJS

MongoDB

jQuery

JavaScript

HTTP

HTML

Git

Database

Data structure and algorithm

CSS and CSS3

C ++

C #

AngularJS

Mobile

Wallpapers and Ringtones

Tricks application

Take and process photos

Storage - Sync

Security and Virus Removal

Personalized

Online Social Network

Map

Manage and edit Video

Data

Chat - Call - Text

Browser and Add-on

Basic setup