Lab 02: Querying Database Tables: 1. WHERE Clause
Lab 02: Querying Database Tables: 1. WHERE Clause
Objective(s) :
To learn the WHERE and ORDER BY clauses in SELECT statement.
1. WHERE Clause
The next thing we want to do is to start limiting, or filtering, the data we fetch from the database. By
adding a WHERE clause to the SELECT statement, we add one (or more) conditions that must be
met by the selected data. This will limit the number of rows that answer the query and are fetched. In
many cases, this is where most of the "action" of a query takes place.
We can continue with our previous query, and limit it to only those employees living in London:
If you wanted to get the opposite, the employees who do not live in London, you would write
Of course, we can write more complex conditions. The obvious way to do this is by having multiple
conditions in the WHERE clause. If we want to know which employees were hired between two
given dates, we could write
Note that SQL also has a special BETWEEN operator that checks to see if a value is between two
values (including equality on both ends). This allows us to rewrite the previous query as
If we want to check if a column value is equal to more than one value? If it is only two values, then it
is easy enough to test for each of those values, combining them with the OR operator and writing
something like
As with the BETWEEN operator, here too we can reverse the results obtained and query for those
rows where City is not in the specified list:
Finally, the LIKE operator allows us to perform basic pattern-matching using wildcard characters.
For Microsoft SQL Server, the wildcard characters are defined as follows:
Wildcard Description
[] matches any single character within the specified range (e.g. [a-f]) or set (e.g. [abcdef]).
[^] matches any single character not within the specified range (e.g. [^a-f]) or set (e.g. [^abcdef]).
WHERE FirstName LIKE '_im' finds all three-letter first names that end with 'im' (e.g. Jim,
Tim).
WHERE LastName LIKE '%stein' finds all employees whose last name ends with 'stein'
WHERE LastName LIKE '%stein%' finds all employees whose last name includes 'stein'
anywhere in the name.
WHERE FirstName LIKE '[JT]im' finds three-letter first names that end with 'im' and begin
with either 'J' or 'T' (that is, only Jim and Tim)
WHERE LastName LIKE 'm[^c]%' finds all last names beginning with 'm' where the
following (second) letter is not 'c'.
Departemnent of Computer Sciences 4/7 Semester Spring 2016
CSL-220: Database Management System Lab 02: Querying Database Tables
Here too, we can opt to use the NOT operator: to find all of the employees whose first name does not
start with 'M' or 'A', we would write
Until now, we have been discussing filtering the data: that is, defining the conditions that determine
which rows will be included in the final set of rows to be fetched and returned from the database.
Once we have determined which columns and rows will be included in the results of our SELECT
query, we may want to control the order in which the rows appear—sorting the data.
To sort the data rows, we include the ORDER BY clause. The ORDER BY clause includes one or
more column names that specify the sort order. If we return to one of our first SELECT statements,
we can sort its results by City with the following statement:
By default, the sort order for a column is ascending (from lowest value to highest value), as shown
below for the previous query:
Departemnent of Computer Sciences 5/7 Semester Spring 2016
CSL-220: Database Management System Lab 02: Querying Database Tables
If we want the sort order for a column to be descending, we can include the DESC keyword after the
column name.
The ORDER BY clause is not limited to a single column. You can include a comma-delimited list of
columns to sort by—the rows will all be sorted by the first column specified and then by the next
column specified. If we add the Country field to the SELECT clause and want to sort by Country
and City, we would write:
Note that to make it interesting, we have specified the sort order for the City column to be
descending (from highest to lowest value). The sort order for the Country column is still ascending.
We could be more explicit about this by writing
but this is not necessary and is rarely done. The results returned by this query are
It is important to note that a column does not need to be included in the list of selected (returned)
columns in order to be used in the ORDER BY clause. If we don't need to see/use the Country
values, but are only interested in them as the primary sorting field we could write the query as
Exercises
Now by using the EMP table, formulate the following queries in SQL