Basic Logical Operators in SQL
Basic Logical Operators in SQL
Wildcard Characters
Practice Questions
Operator Meaning
AND TRUE if both Boolean expressions are
TRUE
BETWEEN TRUE if the operand is within a range
IN TRUE if the operand is equal to one of a
list of expressions
LIKE TRUE if the operand matches a pattern
NOT Reverses the value of any other Boolean
operator
OR TRUE if either Boolean expression is TRUE
Sales for Product ID 1 in January 2016: Where Product ID=1 and Month ID=201601
1 And operator, returns true if both the conditions are met. Therefore the query will only retrieve rows
where Territory ID = 4 and commission pct = 0.015. It is used when there is a need to filter data based
on multiple conditions
Copyright 2016 RESTRICTED CIRCULATION 6
Example – OR Operator
Product ID City ID Month ID Sales Quantity Sales Value
1 1 201512 100 8,000.00
1 2 201605 33 5,640.00
1 6 201511 600 7,590.00
1 7 201603 55 3,433.00
1 9 201601 659 9,432.00
2 5 201602 110 8,999.00
2 12 201602 785 8,324.00
1 OR operator, returns true if either condition is met. Therefore the query will only retrieve rows where
Vacation Hours>= 50 or where Sick Hours >= 30. It is used when there is a need to filter data based on
multiple conditions
Copyright 2016 RESTRICTED CIRCULATION 8
Example – Between Operator
Product ID City ID Month ID Sales Quantity Sales Value
1 1 201512 100 8,000.00
1 2 201605 33 5,640.00
1 6 201511 600 7,590.00
1 7 201603 55 3,433.00
1 9 201601 659 9,432.00
2 5 201602 110 8,999.00
2 12 201602 785 8,324.00
Fetch Sales if Volume is between 50 and 120 Where Sales Quantity BETWEEN 50 and 120
1 Between operator, returns true if test expression value is greater than equal to begin expression value
and less than equal to end expression value
Copyright 2016 RESTRICTED CIRCULATION 10
Example – IN Operator
Product ID City ID Month ID Sales Quantity Sales Value
1 1 201512 100 8,000.00
1 2 201605 33 5,640.00
1 6 201511 600 7,590.00
1 7 201603 55 3,433.00
1 9 201601 659 9,432.00
2 5 201602 110 8,999.00
2 12 201602 785 8,324.00
Fetch Sales for Jan, Feb, Mar 2016: Where MonthID IN (201601,201602,201603)
1 In operator, returns true if test expression value is equal to any of the expression value
Copyright 2016 RESTRICTED CIRCULATION 12
Example – LIKE Operator
Product ID Product Code Product Description Color
1 AR-5381 Adjustable Race Black
2 BA-8327 Bearing Ball Silver
3 BE-2349 BB Ball Bearing Grey
4 BE-2908 Headset Ball Bearings Black
5 BL-2036 Blade Grey
6 CA-5965 LL Crankarm Grey
7 CA-6738 ML Crankarm Silver
8 CA-7457 HL Crankarm Silver
9 CB-2903 Chainring Bolts Silver
10 CN-6137 Chainring Nut Grey
1 Like operator, returns true if match expression value matches with the Pattern
Copyright 2016 RESTRICTED CIRCULATION 14
Wildcard Characters
1 % wildcard, refers to string of 0 or more characters. In this case, it refers to any character before
Bearing and any character after Bearing
Copyright 2016 RESTRICTED CIRCULATION 15
Wildcard Characters
Character Meaning
% Refers to string of zero or more
characters
1 _ used to match single character, in this case we have used two _ to match 2 single characters
Copyright 2016 RESTRICTED CIRCULATION 17
Examples
1 % used for string match of any length and [] used to match single character within the set ,i.e.: 2 and 6
Copyright 2016 RESTRICTED CIRCULATION 18
Thank You