0% found this document useful (0 votes)
15 views

Logical Operators SQL File

The document discusses various SQL operators like AND, OR, NOT, BETWEEN, IN, LIKE and ORDER BY. It explains their usage and provides examples to filter records based on conditions.

Uploaded by

Muhammad Hamza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Logical Operators SQL File

The document discusses various SQL operators like AND, OR, NOT, BETWEEN, IN, LIKE and ORDER BY. It explains their usage and provides examples to filter records based on conditions.

Uploaded by

Muhammad Hamza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

SQL AND

SQL AND operator is used when we want to combine multiple conditions as part of the
WHERE clause. The result set will be filtered based on satisfaction of both the
condition. So, if both the conditions are true then only the result will be filtered. To
combine multiple conditions, we can use more than one AND as part of
the WHERE clause.

let’s consider the following Student table for example purpose.

ROLLNO STUDENTNAME STUDENTGENDER STUDENTAGE STUDENTPERCENT

1 George M 14 85

2 Monica F 13 88

3 Jessica F 14 84

4 Tom M 13 78

Scenario: Get the percentage of students whose age is more than 12 years and gender
is female.

SELECT StudentPercent FROM Student WHERE StudentAge>12 AND


StudentGender = "F";

Output:

STUDENTPERCENT

88

84

In the example above, we have used one AND operator to combine two conditions,
StudentAge is greater than 12 and StudentGender is equal to “F”.
Scenario: Get the percentage of students whose age is more than 12 years and gender
are female and percentage is more than 80.

SELECT StudentPercent FROM Student WHERE StudentAge>12 AND


StudentGender = "F" AND StudentPercent>80;
Output:

STUDENTPERCENT

85

88

84

SQL OR Operator
OR operator is used when we want to combine multiple conditions as part of the
WHERE clause. The result set will be filtered based on satisfaction of at least one of the
conditions. So, if at least one of the conditions are true than only the result will be
filtered. To combine multiple conditions, we can use more than one OR as part of the
WHERE clause.

SQL single OR operator example


Syntax:

SELECT column(s) FROM table_name WHERE condition1 OR condition2;


As mentioned in the syntax above for combining two conditions we can use one OR
operator.
We will now try to understand one OR operator through some example. Let’s reuse the
earlier defined Student table for example purpose.
Scenario: Get the percentage of students whose age is more than 12 years or gender
are female.

Query:

SELECT StudentPercent FROM Student WHERE StudentAge>12 OR


StudentGender = "F";
Output:

STUDENTPERCENT

85

88

84

78

Scenario: Get the percentage of students whose age is more than 12 years or gender
is female or percentage is more than 80.

Query:

SELECT StudentPercent FROM Student WHERE StudentAge>12 OR


StudentGender = "F" OR StudentPercent>80;
In the example above, we have used one OR operator to combine three conditions,
StudentAge is greater than 12, StudentGender is equal to “F” and StudentPercent is
greater than 80.
SQL NOT Operator
SQL NOT operator is used when we want to filter result set when the condition is not
satisfied in the WHERE clause.

Let’s try to understand NOT operator in detail with some examples.

SQL NOT operator example


Syntax:

SELECT column(s) FROM table_name WHERE NOT condition;


As mentioned in the syntax above we use NOT operator along with WHERE clause. We
will now try to understand NOT operator through some example.

Scenario: Get the percentage of students whose gender is not female.

Query:

SELECT StudentPercent FROM Student WHERE NOT StudentGender =


"F";
Output:

STUDENTPERCENT

85

78

In the example above, we have used NOT operator to identify if the gender of the
student is not female.
SQL Logical Operators Example
We have tried to understand the three logical operators individually. But, these three
operators can also be used in combination. Let’s try to understand different
combinations.

We will consider the below mentioned Employee table for example purpose for all the
combinations.

EMPID EMPNAME EMPAGE EMPGENDER EMPSTATE

1 Tony 27 M Texas

2 Anjali 32 F California

3 Jason 34 M London

4 Salma 33 F Texas

5 Anuj 26 M California

SQL AND OR Example


Scenario: Get the name of male employees whose state is London or Texas.
Query:
SELECT EmpName FROM Employee WHERE EmpGender = "M" AND (EmpState
= "London" OR EmpState = "Texas");
Output:

EMPNAME

Tony

Jason

SQL AND NOT Example


Scenario: Get the name of the employee whose age is above 30 and gender is not
male.
Query:

SELECT EmpName FROM Employee WHERE EmpAge > 30 AND NOT EmpGender
= "M";
Output:

EMPNAME

Anjali
Salma

SQL OR NOT Example


Scenario: Get the name of the employee whose age is above 30 or gender is not male.
Query:

SELECT EmpName FROM Employee WHERE EmpAge > 30 OR NOT EmpGender =


"M";
Output:

EMPNAME

Anjali

Jason

Salma

That’s all for SQL logical operators, I hope you got good enough ideas through
examples of SQL AND, OR and NOT operators.
The SQL ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set in ascending or
descending order.

The ORDER BY keyword sorts the records in ascending order by default. To sort
the records in descending order, use the DESC keyword.

SELECT
select_list
FROM
table_name
ORDER BY
[column_name | expression] [ASC | DESC ]

SELECT
first_name,
last_name
FROM
sales.customers
ORDER BY
first_name;

SELECT * FROM Customers


ORDER BY Country DESC;

The SQL IN Operator


The IN operator allows you to specify multiple values in a WHERE clause.

SELECT * FROM Customers


WHERE Country IN ('Germany', 'France', 'UK');

SELECT * FROM Customers


WHERE Country NOT IN ('Germany', 'France', 'UK');
SELECT
product_name,
list_price
FROM
production.products
WHERE
list_price IN (89.99, 109.99, 159.99)
ORDER BY
list_price;

The SQL BETWEEN Operator


The BETWEEN operator selects values within a given range. The values can be
numbers, text, or dates.

The BETWEEN operator is inclusive: begin and end values are included.

BETWEEN Example
The following SQL statement selects all products with a price BETWEEN 10 and
20:

Example
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;

BETWEEN with IN Example


The following SQL statement selects all products with a price BETWEEN 10 and
20. In addition; do not show products with a CategoryID of 1,2, or 3:

Example
SELECT * FROM Products
WHERE (Price BETWEEN 10 AND 20)
AND NOT CategoryID IN (1,2,3);

BETWEEN Text Values Example


The following SQL statement selects all products with a ProductName BETWEEN
'Carnarvon Tigers' and 'Mozzarella di Giovanni':
Example
SELECT * FROM Products
WHERE ProductName BETWEEN 'Carnarvon Tigers' AND 'Mozzarella di Giovanni'
ORDER BY ProductName;

The SQL LIKE Operator


The LIKE operator is used in a WHERE clause to search for a specified pattern in
a column.

There are two wildcards often used in conjunction with the LIKE operator:

• % - The percent sign represents zero, one, or multiple characters


• _ - The underscore represents a single character
• Here are some examples showing different LIKE operators with '%' and '_'
wildcards:

LIKE Operator Description

WHERE CustomerName LIKE 'a%' Finds any values that start with "a"

WHERE CustomerName LIKE '%a' Finds any values that end with "a"

WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position

WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second

position

WHERE CustomerName LIKE Finds any values that start with "a" and are at leas
'a_%_%' length

WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with
The following SQL statement selects all customers with a CustomerName
starting with "a":

Example
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';

You might also like