Logical Operators SQL File
Logical Operators SQL File
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.
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.
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.
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.
Query:
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:
Query:
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.
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
EMPNAME
Tony
Jason
SELECT EmpName FROM Employee WHERE EmpAge > 30 AND NOT EmpGender
= "M";
Output:
EMPNAME
Anjali
Salma
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;
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;
Example
SELECT * FROM Products
WHERE (Price BETWEEN 10 AND 20)
AND NOT CategoryID IN (1,2,3);
There are two wildcards often used in conjunction with the LIKE operator:
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%';