0% found this document useful (0 votes)
2 views37 pages

Lec2 Restricting and Sorting Data

The document provides guidelines on restricting and sorting data using SQL, focusing on the use of the WHERE clause to limit rows based on specific conditions. It explains various comparison conditions, logical operators, and the ORDER BY clause for sorting results. Additionally, it covers the syntax for using character strings, dates, and null values in queries, as well as the rules of precedence for combining conditions.

Uploaded by

pakizaasif0031
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views37 pages

Lec2 Restricting and Sorting Data

The document provides guidelines on restricting and sorting data using SQL, focusing on the use of the WHERE clause to limit rows based on specific conditions. It explains various comparison conditions, logical operators, and the ORDER BY clause for sorting results. Additionally, it covers the syntax for using character strings, dates, and null values in queries, as well as the rules of precedence for combining conditions.

Uploaded by

pakizaasif0031
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Restricting and Sorting Data

Limiting Rows Using a Selection


Limiting the Rows Selected
⚫ Restrict the rows returned by using the WHERE
clause

⚫ The WHERE clause follows the FROM clause


In this syntax:

KEYWORD MEANING
WHERE restricts the query to rows that meet a
condition

condition is composed of column names,


expressions, constants, and a
comparison operator
The WHERE clause can compare values in columns, literal
values, arithmetic expressions, or functions. It
consists of three elements:

⚫ Column name
⚫ Comparison condition
⚫ Column name, constant, or list of values
Using the WHERE Clause
Character Strings and Dates
⚫ Character strings and date values are enclosed in
single quotation marks
⚫ Character values are case sensitive, and date values are
format sensitive
⚫ The default date format is DD-MON-RR
Character Strings and Dates(Contd..)

Character strings and dates in the WHERE clause must be


enclosed in single quotation marks (''). Number
constants, however, should not be enclosed in single
quotation marks.
All character searches are case sensitive. In the following
example, no rows are returned because the EMPLOYEES
table stores all the last names in the proper case
Comparison Conditions
OPERATOR MEANING
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
Comparison Conditions
Comparison conditions are used in conditions that compare
one expression to another value or expression. They are used
in the WHERE clause in the following format:
... WHERE expr operator value
Example
... WHERE hire_date='01-JAN-95'
... WHERE salary>=6000
... WHERE last_name='Smith‘
An alias cannot be used in the WHERE clause.
Using Comparison Conditions

NOTE: The symbol != and ^= can also represent the not equal to condition.
Other Comparison Conditions
OPERATOR MEANING
BETWEEN Between two values (inclusive)
...AND...
IN(set) Match any of a list of values
LIKE Match a character pattern
IS NULL Is a null value
Using the BETWEEN Condition
Use the BETWEEN condition to display rows based on a
range of values.

Values specified with the BETWEEN condition are inclusive. You must
specify the lower limit first.
Using the IN Condition
Use the IN membership condition to test for values in a
list.
Using the IN Condition(Contd..)
The IN condition can be used with any data type. The
following example returns a row from the EMPLOYEES
table for any employee whose last name is included in the
list of names in the WHERE clause:

If characters or dates are used in the list, they must be


enclosed in single quotation marks ('').
Using the LIKE Condition
⚫ Use the LIKE condition to perform character
pattern-matching operation

⚫ Search conditions can contain either literal characters


or numbers:
⚫ – % denotes zero or many characters.
⚫ – _ denotes one character.
Using the LIKE Condition
Using the LIKE Condition
You can combine pattern-matching characters.
Using the NULL Conditions
Test for nulls with the IS NULL operator.
Logical Conditions
⚫ A logical condition combines the result of two component
conditions to produce a single result based on them or
inverts the result of a single condition.
⚫ A row is returned only if the overall result of the condition
is true
⚫ Three logical operators are available in SQL:
• AND
• OR
• NOT
Logical Conditions(Contd..)
OPERATOR MEANING
AND Returns TRUE if both component
conditions are true
OR Returns TRUE if either component
condition is true
NOT Returns TRUE if the following
condition is false

You can use several conditions in one WHERE clause using the AND and OR
operators.
Using the AND Operator
AND requires both conditions to be true.
Using the OR Operator
OR requires either condition to be true.
Using the NOT Operator
SELECT
Rules of Precedence
Rules of Precedence
2

Therefore, the SELECT statement reads as follows:


1“Select the row if an employee is a president and earns more

than $15,000, or if the employee is a sales representative.”


2
Rules of Precedence
Use parentheses to force priority.

Therefore, the SELECT statement reads as follows:


1
“Select the row if an employee is a president or a sales
representative, and if the employee earns more than
$15,000.”2
Rules of Precedence
AND has a higher priority than the OR
SELECT first_name, last_name, salary, department_id
FROM employees
WHERE salary > 7000 1
AND department_id = 50
OR department_id = 20; 2

Select the row if an employee earns more than 7000 AND is


working in department 50
OR
Select the row if an employee is working in department 20 2
Rules of Precedence
Use parentheses to force priority.
SELECT first_name, last_name, salary, department_id
FROM employees
WHERE salary > 7000 1

AND (department_id = 50
2
OR department_id = 20);

Select the row if an employee earns more than 7000 1

AND
is working in department 50 OR department 20 2
ORDER BY Clause
⚫ Sort rows with the ORDER BY clause
A 1
⚫ – ASC: ascending order (the default order) Z 9

⚫ – DESC: descending order Z 9


A 1

⚫ The ORDER BY clause comes last in the SELECT


statement.
Default Ordering of Data
⚫ The default sort order is ascending:
• Numeric values are displayed with the lowest values first:
for example, 1–999

• Date values are displayed with the earliest value first: for
example, 01-JAN-92 before 01-JAN-95

• Character values are displayed in alphabetical order: for


example, A first and Z last
ORDER BY Clause
Sorting in Descending Order
Sorting by Column Alias
Sorting by Column Position in SELECT List

1 2 3 4
SELECT employee_id, first_name, last_name, department_id
FROM employees
ORDER BY 2 ;


Sorting by Multiple Columns
The order of ORDER BY list is the order of sort.
⚫ You can also order by columns that are not included in the
SELECT clause.

Example
Display the last names and salaries of all employees. Order
the result by department number, and then in
descending order by salary.

SELECT last_name, salary


FROM employees
ORDER BY department_id, salary DESC;

You might also like