Logical Operators operate on two conditions and returns a logical output as True or False.
Following are the logical operators.
Table: Product
ProductID |
ProductName |
Price |
1 |
HD TV |
1000 |
2 |
Play Station |
400 |
3 |
MacBook |
2000 |
4 |
IMAC |
1500 |
5 |
IPOD |
200 |
6 |
XBOX |
500 |
7 |
MODEM |
100 |
8 |
Nintendo |
200 |
9 |
Speakers |
100 |
10 |
Printer |
200 |
11 |
SLR Camera |
1000 |
12 |
Tablet |
600 |
Example 1 (Logical Operator - AND)
Query :
SELECT ProductID, ProductName, Price
FROM Product
WHERE ProductID > 5
AND Price > 500
Output :
Description :
Here both the conditions in the WHERE clause are evaluated for each row in the Product table and returns back only those rows where both the conditions returns true.
Example 2 (Logical Operator - OR)
Query :
SELECT ProductID, ProductName, Price
FROM Product
WHERE ProductName = 'MacBook'
OR ProductID > 10
Output :
Description :
Here both the conditions in the WHERE clause are evaluated for each row in the Product table and returns back those rows where either one of the condition returns true.
Example 3 (Logical Operator - NOT)
Query :
SELECT ProductID, ProductName, Price
FROM Product
WHERE NOT (Price < 999)
Output :
Description :
Here the NOT operator reverses the Boolean expression and returns back all the rows from the Product table where the condition returns false.
Example 4 (Logical Operator - ALL)
Query :
SELECT ProductID, ProductName, Price
FROM Product
WHERE Price > ALL (SELECT Price
FROM Product
WHERE ProductID > 5)
Output :
Description :
In the above query, the sub query will return back 7 records having ProductID > 5.
The outer query uses the ALL operator to return back those records where the price of the product is greater than all the records returned by the sub query.
Since the max price in the sub query is 1000(from ProductID 11), only those records in the outer query with price >1000 will be returned as the output.
In the output, notice that only two records (ProductID 3 and 4) have price greater than all the prices returned by the sub query.
Example 5 (Logical Operator - ANY)
Query :
SELECT ProductID, ProductName, Price
FROM Product
WHERE Price > ANY (SELECT Price
FROM Product
WHERE ProductID > 5)
Output :
Description :
In the above query, the sub query will return back 7 records having ProductID > 5.
The outer query uses the ANY operator to return back those records where the price of the product is greater than at least one of the records returned by the sub query.
Since the minimum price in the sub query is 100(from ProductID 7 and 9), all the records in the outer query with price >100 will be returned as the output.
In the output, notice that the only products which are not part of the output are ProductID 7 and 9, because their price is not greater than 100.
If we update the comparison operator in the outer query from >(less than) to >=(greater than or equal to), then all the products will be returned back by the outer query
because all products have price greater than or equal to 100.
Example 6 (Logical Operator - BETWEEN)
Query :
SELECT ProductID, ProductName, Price
FROM Product
WHERE Price BETWEEN 500 AND 1000
Output :
Description :
Here those products having price greater than or equal to 500 and less than or equal to 1000 are returned back.
Example 7 (Logical Operator - EXISTS)
Query :
SELECT ProductID, ProductName, Price
FROM Product AS P
WHERE EXISTS (SELECT ProductID, ProductName, Price
FROM Product AS P1
WHERE P1.ProductID = P.ProductID
AND P1.Price > 1000)
Output :
Description :
Here since we are using only one table, the example does not make much sense.
But in general, as you can see the outer query returns back only those records which exist in the inner query.
Example 8 (Logical Operator - IN)
Query :
SELECT ProductID, ProductName, Price
FROM Product
WHERE ProductName IN (SELECT ProductName
FROM Product AS P1
WHERE P1.Price > 1000)
Output :
Description :
Just like previous example, IN Operator is usually used with multiple tables.
In the above example, the outer query returns back those products which has product name matching the product name returned by the sub query.