You are on page 1of 8

Kingdom of Saudi Arabia

Ministry of Higher Education


King Faisal University
College of Computer Sciences &
Information Technology

IS222 – DATABASE
CONCEPTS AND DESIGN
Lab sheet
Lab 9: WHERE and ORDER BY

IS222
Table of Contents
LAB 9: WHERE and ORDER BY............................................................................................2
Objectives:.................................................................................................................................2
Tools/Software:.........................................................................................................................2
Concepts & Descriptions:............................................................................................................2
SELCET Statement with Order by................................................................................................2
Examples and explanations of SELECT statement with ORDER BY...............................................3
Lab Activities:.............................................................................................................................5
Deliverables:..............................................................................................................................7
LAB 9: WHERE and ORDER BY

Objectives:
This lesson covers the following objectives:

 Create SELECT Statements using logical operators


 Create SELECT Statements using ORDER BY
 Create SELECT Statements using column aliases
 Create SELECT Statements using sorting with other columns
 Create SELECT Statements using sorting with multiple columns

Tools/Software:
To accomplish this session, students should have access to APEX.

Concepts & Descriptions:


SELCET Statement with ORDER BY
 ORDER BY is used to sort the results of SELECT statement.
 ORDER BY is sort the results either ASC or DESC order.
 ASC is standard of ascending. It is used with ORDER BY to sort output from lowest
to highest.
 DESC is standard of descending. It is used with ORDER BY to sort the output from
highest to lowest.
 The default sorting of ORDER BY is an ascending.
 The alias used in the SELECT statement is referenced in the ORDER BY clause.
Examples and explanations of WHERE and ORDER BY

Syntax of SELECT statement with ORDER BY

SELECT <column1>, … <columnN> FROM <table_name>


WHERE <condition> ORDER BY <column1>;

Example Explanation
Using Logical Operators
SELECT last_name, department_id, salary In the query below, the results returned
FROM employees will be rows that satisfy BOTH
WHERE department_id > 50 AND salary > 12000; conditions specified in the WHERE
clause.

SELECT last_name, hire_date, job_id Another example of using AND in the


FROM employees where clause.
WHERE hire_date > '01/jan/1998' AND job_id LIKE
'SA%';

SELECT department_name, manager_id, all rows returned have a location_id of


location_id 2500 OR they have a manager_id equal
FROM departments to 124
WHERE location_id = 2500 OR manager_id=124;

SELECT department_name, location_id The NOT operator will return rows that
FROM departments do NOT satisfy the condition in the
WHERE location_id NOT IN (1700,1800); WHERE clause.

SELECT last_name||' '||salary*1.05 Rule of precedence is Arithmetic,


As "Employee Raise" Concatenation, Comparison, IS NULL,
FROM employees LIKE, IN, BETWEEN, NOT, AND, and the
WHERE department_id IN(50,80) last is OR.
AND first_name LIKE 'C%'
OR last_name LIKE '%s%';

Using Order By
SELECT last_name, hire_date uses the ORDER BY clause to order
FROM employees hire_date in ascending (default) order
ORDER BY hire_date;

SELECT last_name, hire_date uses the ORDER BY clause to order


FROM employees hire_date in descending (DESC) order
ORDER BY hire_date DESC;
Using Column Aliases
SELECT last_name, hire_date AS Date_Started The alias used in the SELECT statement
Example Explanation
FROM employees is referenced in the ORDER BY clause.
ORDER BY Date_Started;

Sorting with Other Columns


SELECT employee_id, first_name the data is sorted by the last_name
FROM employees column even though this column is not
WHERE employee_id < 105 listed in the SELECT statement
ORDER BY last_name;

Sorting with Multiple Columns


SELECT department_id, last_name Employees are first ordered by
FROM employees department number (from lowest to
WHERE department_id <= 50 highest), then for each department, the
ORDER BY department_id, last_name; last names are displayed in alphabetical
order (A to Z).

SELECT department_id, last_name If you want to reverse the sort order of


FROM employees a column, add DESC after its name.
WHERE department_id <= 50
ORDER BY department_id DESC, last_name;
Lab Activities:

1. Display the last names of all Fast Foods staff who have “e” and “i” in their last
names.

SELECT Last_Name
FROM F_Staffs
WHERE Last_Name LIKE '%e%' AND Last_Name LIKE '%i%';

2. Display the first names and last names of all Fast Foods staff that make more than
$6.50 per hour and their position is not order taker.

SELECT FIRST_NAME, LAST_NAME


FROM F_STAFFS
WHERE Not STAFF_TYPE = 'Order Taker'
AND SALARY / 8 > 6.50;
3. Using the employees table, write a query to display all employees whose last names
start with “D” and have “a” and “e” anywhere in their last name.

SELECT *
FROM employees
WHERE LAST_NAME LIKE 'D%'
AND LAST_NAME LIKE '%a%'
AND LAST_NAME LIKE '%e%';

4. Which list of operators is in the correct order from highest precedence to lowest
precedence?
1. AND, NOT, OR
2. NOT, OR, AND
3. NOT, AND, OR
The correct order of operators from highest precedence to lowest
precedence is:3. NOT, AND, OR
5. Display all employees last names, salaries and salary*2 named Extra Credit and order
it in ascending order based the Extra Credit.

SELECT LAST_NAME, SALARY, SALARY * 2 AS "Extra Credit"


FROM employees
ORDER BY "Extra Credit" ASC;
Deliverables:
At the end of this session, students are expected to:
 Solve the lab activities and submit their work.

You might also like