Database Programming With SQL: 2-2 Limit Rows Selected
Database Programming With SQL: 2-2 Limit Rows Selected
2-2
Limit Rows Selected
DPS2L2
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 2
Limit Rows Selected
SELECT Statement
DPS2L2
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 3
Limit Rows Selected
WHERE Clause
DPS2L2
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 4
Limit Rows Selected
WHERE Clause
DPS2L2
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 5
Limit Rows Selected
Comparison Operators in the WHERE
Clause
• As you saw on the previous slide, the = sign can be used in
the WHERE clause.
• In addition to the "equal to" operator (=), other comparison
operators can be used to compare one expression to another:
– = equal to
– > greater than
– >= greater than or equal to
– < less than
– <= less than or equal to
– <> not equal to (or != or ^=)
DPS2L2
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 6
Limit Rows Selected
Comparison Operators in the WHERE
Clause
• In the example below, the department_id column is used in
the WHERE clause, with the comparison operator = .
• All employees with a department_id of 90 are returned.
SELECT employee_id, last_name, department_id
FROM employees
WHERE department_id = 90;
DPS2L2
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7
Limit Rows Selected
Character and Date Strings in the WHERE
Clause
• Character strings and dates in the WHERE clause must be
enclosed in single quotation marks ' '.
• Numbers, however, should not be enclosed in single
quotation marks.
DPS2L2
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 10
Limit Rows Selected
Character and Date Strings in the WHERE
Clause
• Look at the following example from the Employees database.
• The WHERE clause contains a string and is enclosed in single
quotation marks.
SELECT first_name, last_name
FROM employees
WHERE last_name = 'Taylor';
DPS2L2
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 9
Limit Rows Selected
Character and Date Strings in the WHERE
Clause
• What do you think will happen if the WHERE clause is written
as:
WHERE last_name = 'jones';
DPS2L2
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 10
Limit Rows Selected
Character and Date Strings in the WHERE
Clause
• This is an important point to remember.
• In another lesson, you will learn to use other SQL keywords
UPPER, LOWER, and INITCAP that will make it easier to avoid
a case-sensitive mistake.
DPS2L2
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 11
Limit Rows Selected
Comparison Operators in the WHERE
Clause
• Comparison operators can be used in all of the following ways
in the WHERE clause:
WHERE hire_date < '01-Jan-2000'
DPS2L2
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12
Limit Rows Selected
Comparison Operators in the WHERE
Clause
• In the following example from the Employees database,
which rows will be selected?
• Will salaries of 3000 be included in the result set?
SELECT last_name, salary
FROM employees
WHERE salary <= 3000;
DPS2L2
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 13
Limit Rows Selected
Summary
DPS2L2
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 14
Limit Rows Selected