Oracle SQL Fundamentals/Restricting Data
Appearance
This lesson introduces restricting data using the WHERE clause.
Objectives and Skills
[edit | edit source]Objectives and skills for the restricting data portion of Oracle SQL Fundamentals I certification include:[1]
- Restricting and Sorting Data
- Limit the rows that are retrieved by a query
Readings
[edit | edit source]- Read Wikipedia: Where (SQL).
Multimedia
[edit | edit source]- Oracle SQL Tutorial - Querying data - Part 6 - WHERE clause
- Oracle SQL Tutorial - Querying data - Part 7 - WHERE clause continued
- Oracle SQL Tutorial - Querying data - Part 8 - WHERE clause continued
Activities
[edit | edit source]- Select HR data using the WHERE clause.
- Run the query
SELECT * FROM EMPLOYEES;
to see all data in the EMPLOYEES table. - Run the query
SELECT * FROM EMPLOYEES WHERE DEPARTMENT_ID = 20;
to see all employees in department 30. - Run the query
SELECT * FROM EMPLOYEES WHERE JOB_ID = 'SA_REP';
to see all employees with a job id of SA_REP. - Run the query
SELECT * FROM EMPLOYEES WHERE JOB_ID = 'sa_rep';
to see all employees with a job id of sa_rep. Note that Oracle WHERE comparisons are case-sensitive. - Run the query
SELECT * FROM EMPLOYEES WHERE HIRE_DATE >= '1 JAN 2000';
to see all employees hired on or after January 1, 2000. - Run the query
SELECT * FROM EMPLOYEES WHERE HIRE_DATE BETWEEN '1 JAN 1999' AND '31 DEC 1999';
to see all employees hired in 1999. - Run the query
SELECT * FROM EMPLOYEES WHERE MANAGER_ID IN (100, 145, 205);
to see all employees who have manager 100, 145, or 205. - Run the query
SELECT * FROM EMPLOYEES WHERE PHONE_NUMBER LIKE '0%';
to see all employees with a phone number beginning with 0. - Run the query
SELECT * FROM EMPLOYEES WHERE FIRST_NAME LIKE 'J___';
to see all employees with a four-character first name beginning with J. - Run the query
SELECT * FROM EMPLOYEES WHERE DEPARTMENT_ID IS NULL;
to see all employees not assigned to a department.
- Run the query
- Select HR data using logical operators.
- Run the query
SELECT * FROM EMPLOYEES WHERE DEPARTMENT_ID = 80 AND COMMISSION_PCT < 0.2;
to see all employees in department 80 with a commission percentage less than 0.2. - Run the query
SELECT * FROM EMPLOYEES WHERE SALARY > 20000 OR COMMISSION_PCT > 0.3;
to see all employees with a salary greater than 20,000 or with a commission percentage greater than 0.3. - Run the query
SELECT * FROM EMPLOYEES WHERE PHONE_NUMBER NOT LIKE '515%';
to see all employees with phone numbers that do not begin with 515.
- Run the query
- Select OE data using the WHERE clause.
- Select all data in the CUSTOMERS table.
- Select all customers with a credit limit greater than 100000.
- Select all customers with Platinum status.
- Select all customers with Gold status.
- Select all customers with a credit limit between 50000 and 100000.
- Select all customers in Germany (DE), Italy (IT), and Switzerland (CH).
- Select all customers with phone numbers beginning with '+1 '.
- Select all customers with a six-character postal code ending with 0.
- Select all customers not assigned to an account manager.
- Select OE data using logical operators.
- Select all US customers with Silver status.
- Select all customers from India (IN) or China (CN).
- Select all customers who do not have account manager 145.
Lesson Summary
[edit | edit source]- A WHERE clause in SQL specifies that a SQL Data Manipulation Language (DML) statement should only affect rows that meet specified criteria.[2]
- The basic WHERE clause syntax is <SQL-DML-Statement> FROM <table> WHERE <predicate>.[3]
- Simple predicates use one of the operators =, <>, >, >=, <, <=, IN, BETWEEN, LIKE, IS NULL or IS NOT NULL.[4]
- IN will find any values existing in a set of candidates.[5]
- BETWEEN will find any values within a range.[6]
- LIKE will find a string fitting a certain description.[7]
- The underscore character (_) is used as a wildcard character to match any single character for LIKE comparisons.[8]
- The percent character (%) is used as a wildcard character for to match any character string for LIKE comparisons.[9]
- The keywords AND and OR may be used to combine two predicates into a new one.[10]
- The keyword NOT may be used to negate a condition.[11]
- Predicates may be enclosed in parentheses if desired.[12]
- Operator precedence is evaluated in the order (), */, +-, ||, = <> <= >=, IN LIKE IS NULL, BETWEEN, !=, NOT, AND, OR.[13]
Key Terms
[edit | edit source]- operator precedence
- A collection of rules that define which procedures to perform first in order to evaluate a given mathematical expression.[14]
- wildcard
- a single character used to represent a number of characters or an empty string.[15]
Assessments
[edit | edit source]- Flashcards: Quizlet: Oracle 1Z0-051 Exam - Select Statement
See Also
[edit | edit source]References
[edit | edit source]- ↑ Oracle: Database 11g: SQL Fundamentals I Exam Topics
- ↑ Wikipedia: Where (SQL)
- ↑ Wikipedia: Where (SQL)
- ↑ Wikipedia: Where (SQL)
- ↑ Wikipedia: Where (SQL)
- ↑ Wikipedia: Where (SQL)
- ↑ Wikipedia: Where (SQL)
- ↑ Wikipedia: Where (SQL)
- ↑ Wikipedia: Where (SQL)
- ↑ Wikipedia: Where (SQL)
- ↑ Wikipedia: Where (SQL)
- ↑ Wikipedia: Where (SQL)
- ↑ Ramklass and Watson. OCA Oracle Database 11g SQL Fundamentals I Exam Guide: Exam 1Z0-051. Oracle Press. ISBN 9780071597869
- ↑ Wikipedia: Order of operations
- ↑ Wikipedia: Wildcard character