Task 6 - SQL AND, OR and NOT Operators

Post Reply
User avatar
Mayank
Site Admin
Posts: 229
Joined: 06 Oct 2017, 11:19
Status: Online

12 Oct 2017, 11:08

The SQL AND, OR and NOT OperatorsThe WHERE clause can be combined with AND, OR, and NOT operators.
 The AND and OR operators are used to filter records based on more than one condition:
 
  • The AND operator displays a record if all the conditions separated by AND is TRUE.
  • The OR operator displays a record if any of the conditions separated by OR is TRUE.
The NOT operator displays a record if the condition(s) is NOT TRUE.AND Syntax

Code: Select all

SELECT [i]column1[/i],[i] column2, ...[/i]
FROM [i]table_name[/i]
WHERE [i]condition1[/i] AND [i]condition2[/i] AND [i]condition3 ...[/i];
OR Syntax

Code: Select all

SELECT [i]column1[/i],[i] column2, ...[/i]
FROM [i]table_name[/i]
WHERE [i]condition1[/i] OR [i]condition2[/i] OR [i]condition3 ...[/i];
NOT Syntax

Code: Select all

SELECT [i]column1[/i],[i] column2, ...[/i]
FROM [i]table_name[/i]
WHERE NOT [i]condition[/i];
AND ExampleThe following SQL statement selects all fields from "Customers" where country is "Germany" AND city is "Berlin":
 Example

Code: Select all

SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';
Try it Yourself »

OR ExampleThe following SQL statement selects all fields from "Customers" where city is "Berlin" OR "München":Example

Code: Select all

SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
Try it Yourself »

NOT ExampleThe following SQL statement selects all fields from "Customers" where country is NOT "Germany":
 Example

Code: Select all

SELECT * FROM Customers
WHERE NOT Country='Germany';
Try it Yourself »

Combining AND, OR and NOTYou can also combine the AND, OR and NOT operators.
The following SQL statement selects all fields from "Customers" where country is "Germany" AND city must be "Berlin" OR "München" (use parenthesis to form complex expressions):Example

Code: Select all

SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR City='München');
Try it Yourself »
 The following SQL statement selects all fields from "Customers" where country is NOT "Germany" and NOT "USA":Example

Code: Select all

SELECT * FROM Customers
WHERE NOT Country='Germany' AND NOT Country='USA';
Try it Yourself »
Mayank Goyal

Post Reply
Actuarial Blogs Insurology

icon icon