Monday 18 June 2012

Lesson 11: Retrieving Specific Rows in SQL (Logical Operators)


So far, you have been retrieving all of the rows in the table. You can, however, specify which rows you wish to retrieve. For example, you could retrieve only those vendors who are in Chicago.


Syntax:
Select */fieldname ...
from tablename
where fieldname =/!=/<>/>/>=/</<=/in/not in/between/not between/begins with/contains/not contains/ is null/is not null/like/not/like value
order by fieldname <desc>...

Explanation:
  • You can use any of the following logical operators in your Where clause to restrict the rows you retrieve.
Logical Operators
=Equal to
!= or <>Not equal to
>Greater than
>=Greater than or equal to
<Less than
<=Less than or equal to
inEqual to any item in a list
not in Not equal to any item in a list
betweenBetween two values,
greater than or equal to one and less than or equal to the other
not betweenNot between two values
begins withBegins with specified value
containsContains specified value
not containsDoes not contain specified value
is nullIs blank
is not nullIs not blank
likeLike a specified pattern.
% means any series of characters.
_ means any single character.
not likeNot like a specified pattern.
% means any series of characters.
_ means many single character.
  • In the Where clause, when referring to variables in character fields, you must enclose the values in singlequotes.
Example:
where City = 'Chicago'  
  • Variables that refer to numeric fields should not be enclosed in quotes.
Example:
where CurrBal > 1200
Examples:Retrieve all vendors located in Chicago.
  1. In the SQL text box, type:
Select City, Name, VendId
from TrnVendor
where City = 'Chicago';'  
  1. Execute the SQL statement.
Results:
 
CityNameVendId
ChicagoBedMakers LinenTV003
ChicagoMusic MakerTV016
ChicagoDistant HorizonsTV028
ChicagoPaltry PlayTV029
ChicagoMake Shift TiltsTV030

Retrieve all vendors who are not located in Chicago. Order the results by city.
  1. In the SQL text box, type:
Select City, Name, VendId
from TrnVendor
where City <> 'Chicago'
order by City;
  1. Execute the SQL statement.
Results:
 
CityNameVendId
BayshoreBayshore ConsultingTV019
BurlingtonBayCon GroupTV020
DallasCooperative OperativesTV021
DallasClampett OilTV026
DetroitRay BlockTV005
DetroitSpot OutTV007
FreeportFood FourTV017
FreeportWorld Wide Learning UTV023
FreeportEnterprise TransportTV024
HoustonLegal LookupTV022
MercerBed Room Furniture, Inc.TV004
MercerThe FreelanceTV011




Retrieve all vendors in Mercer, New York, or Park Ridge.
  1. In the SQL text box, type:
Select City, Name, VendId
from TrnVendor
where City in ( 'Mercer', 'New York', 'Park Ridge')
order by City;
  1. Execute the SQL statement.
Results:
 
CityNameVendId
MercerBed Room Furniture, Inc.TV004
MercerThe FreelanceTV011
New YorkPaper PeopleTV012
Park RidgeMosquito No BiteTV006
Park RidgeNo Waste DisposalTV015

Retrieve all vendors whose names begin with "Co."
  1. In the SQL text box, type:
Select Name, City, VendId
from TrnVendor
where Name begins with 'Co'
order by Name;
  1. Execute the SQL statement.
Results:
 
NameCityVendId
Computer BytesYoungstownTV018
Cooperative OperativesDallasTV021
Counter ProductiveToledoTV014


Retrieve all vendors whose city ends with the letters "do."
  1. In the SQL text box, type:
Select Name, City, VendId
from TrnVendor
where City like '%do'
order by City;
  1. Execute the SQL statement.
Results:
 
NameCityVendId
The Soda FactoryOrlandoTV010
Against the TideOrlandoTV025
Wet Off TowelsToledoTV001
Hit the DeckToledoTV009
Counter ProductiveToledoTV014
Narrow NestToledoTV027


Retrieve all vendors with a current balance between 500 and 1000 dollars.
  1. In the SQL text box, type:
Select VendId, Name, CurrBal [ZZ,ZZZ.99]
from TrnVendor
where CurrBal between 500 and 1000
order by CurrBal;
  1. Execute the SQL statement.
Results:
 
VendIdName
CurrBal
TV003BedMakers Linen
500
TV012Paper People
617
TV017Food Four
642.98
TV023World Wide Learning U
771.06
TV006Mosquito No Bite
967.24
TV029Paltry Play
1,000

No comments:

Post a Comment