You are on page 1of 19

Lesson 2: SQL Data Types & Operators

Copyright 2016 RESTRICTED CIRCULATION


Basic Logical Operators

Copyright 2016 RESTRICTED CIRCULATION


Objectives

Basic Logical operators

Wildcard Characters

Practice Questions

Copyright 2016 RESTRICTED CIRCULATION 3


Basic Logical Operators

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

Copyright 2016 RESTRICTED CIRCULATION 4


Example – AND 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

Sales for Product ID 1 in January 2016: Where Product ID=1 and Month ID=201601

Product ID City ID Month ID Sales Quantity Sales Value

1 9 201601 659 9,432.00

Copyright 2016 RESTRICTED CIRCULATION 5


Example – AND Operator
True/False

Q: Write a query to fetch the Sales Person details for


Territory 4 having commission percent of 1.5

A: Select * from Sales.SalesPerson where Territory ID =4


AND1 commission pct = 0.015

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

Fetch Sales if Volume>=650 or Value>=8500: Where Sales Quantity>=650 or Sales Value>=8500

Product ID City ID Month ID Sales Quantity Sales Value


1 1 201512 100 8,000.00
1 9 201601 659 9,432.00
2 5 201602 110 8,999.00
2 12 201602 785 8,324.00
Copyright 2016 RESTRICTED CIRCULATION 7
Example – OR Operator
True/False

Q: Write a query to fetch Employees having more than


50 vacation hours or more than 30 sick hours

A: Select * from HumanResources.Employee where


VacationHours>=50 OR1 SickLeaveHours>=30

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

Product ID City ID Month ID Sales Quantity Sales Value


1 1 201512 100 8,000.00
1 7 201603 55 3,433.00
2 5 201602 110 8,999.00

Copyright 2016 RESTRICTED CIRCULATION 9


Example – BETWEEN Operator
Test Expression Begin Expression End Expression

Q: Write a query to fetch Purchase order details where


Received quantity is between 550 and 600

A: Select * from Purchasing.PurchaseOrderDetail where


ReceivedQty BETWEEN1 550 and 600

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)

Product ID City ID Month ID Sales Quantity Sales Value


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
Copyright 2016 RESTRICTED CIRCULATION 11
Example – IN Operator
Test Expression Expression

Q: W ite a ue y to fetch E ployees ho a e Tool


Desig e , Se io Tool Desig e a d Desig E gi ee

A: Select * from HumanResources.Employee where


[Job Title] IN1
Tool Desig e , Se io Tool Desig e , Desig E gi ee

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

Find bearing products: Where Product Description like %Bearing%

Product ID Product Code Product Description Color


2 BA-8327 Bearing Ball Silver
3 BE-2349 BB Ball Bearing Grey
4 BE-2908 Headset Ball Bearings Black

Copyright 2016 RESTRICTED CIRCULATION 13


Example – LIKE Operator
Match Pattern – Regular/
Expression Wildcard characters

Q: W ite a ue y to fetch Bea i g Ball P oduct f o


Products table

A: Select * from Production.Product where Name LIKE1


Bea i g Ball

Wait a second!! We could have used = operator here for


the same result. So why Like operator?

1 Like operator, returns true if match expression value matches with the Pattern
Copyright 2016 RESTRICTED CIRCULATION 14
Wildcard Characters

Power of Like operator is with wildcard characters

Q: W ite a ue y to fetch p oducts that ha e Bea i g i


its Name

A: Select * from Production.Product where Name like


%Bea i g% 1

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

– Refers to single character

[] Refers to single character within the


specified range or set. Example:- [f:g]
or [xyz]

[^] Reverse of []. Single character not within


the specified range or set
Example:- [^f-g] or [^xyz]

Copyright 2016 RESTRICTED CIRCULATION 16


Examples

Q: Write a query to fetch Address information from


Address table where State Province ID is 3 digits and end
with 5

A: Select * from Person.Address where StateProvinceID


like __1

1 _ used to match single character, in this case we have used two _ to match 2 single characters
Copyright 2016 RESTRICTED CIRCULATION 17
Examples

Q: Write a query to fetch Address information from


Address table where Postal Code ends with either 2 or 6

A: Select * from Person.Address where PostalCode like


%1[2 ]

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

Copyright 2016 RESTRICTED CIRCULATION

You might also like