WHERE Clause Chapter 2. Objectives Limit rows by using a WHERE clause Use the LIKE operator Effect of NULL values Use compound conditions Use the BETWEEN.

Slides:



Advertisements
Similar presentations
2 Copyright © 2007, Oracle. All rights reserved. Restricting and Sorting Data.
Advertisements

Sometimes you need to use data from more than one table. In example1, the report displays data from two separate tables. Employee IDs exist in the EMPLOYEES.
Objectives After completing this lesson, you should be able to do the following: Describe various types of conversion functions that are available in.
Restricting and sorting data 16 May May May Created By Pantharee Sawasdimongkol.
After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort the rows retrieved by a query.
LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
Restricting and Sorting Data. Consider the table employee(employee_id,last_name,job_id, department_id ) assume that you want to display all the employees.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Structured Query Language Part I Chapter Three CIS 218.
Microsoft Access 2010 Chapter 7 Using SQL.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
Displaying Data from Multiple Tables. Obtaining Data from Multiple Tables Sometimes you need to use data from more than one table. In the example, the.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
IFS Intro. to Data Management Chapter 6 Filtering your data.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
Chapter 3 Single-Table Queries
Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Restricting and Sorting Data. ◦ Limiting rows with:  The WHERE clause  The comparison conditions using =,
2 Copyright © Oracle Corporation, All rights reserved. Restricting and Sorting Data.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
4 Copyright © 2006, Oracle. All rights reserved. Restricting and Sorting Data.
LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
2 第二讲 Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
Structured Query Language
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
2-1 Limiting Rows Using a Selection “…retrieve all employees in department 10” EMP EMPNO ENAME JOB... DEPTNO 7839KINGPRESIDENT BLAKEMANAGER CLARKMANAGER.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
9/29/2005From Introduction to Oracle:SQL and PL/SQL, Oracle 1 Restricting and Sorting Data Kroenke, Chapter Two.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Limiting Selected Rows. 2-2 Objectives Sort row output using the ORDER BY clause. Sort row output using the ORDER BY clause. Enter search criteria using.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
Random Functions Selection Structure Comparison Operators Logical Operator
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
ATS Application Programming: Java Programming
Sequence, Selection, Iteration The IF Statement
Comparison Operators Relational Operators.
Using the Set Operators
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
(SQL) Aggregating Data Using Group Functions
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
Restricting and Sorting Data
Reporting Aggregated Data Using the Group Functions
Restricting and Sorting Data
Reporting Aggregated Data Using the Group Functions
Using the Set Operators
Reporting Aggregated Data Using the Group Functions
Shelly Cashman: Microsoft Access 2016
Restricting and Sorting Data
Presentation transcript:

WHERE Clause Chapter 2

Objectives Limit rows by using a WHERE clause Use the LIKE operator Effect of NULL values Use compound conditions Use the BETWEEN and IN operators

WHERE clause Basic SELECT … FROM statement with no WHERE clause returns all rows from table Limit rows by using a WHERE clause WHERE clause may include one or more conditions A condition is made up of: an expression, one or more operators and an expression An expression may involve column(s) and/or literal(s) and arithmetic operators and … Some operators (comparison) include: = >

WHERE clause using Comparison Operators Example 1: SELECT employee_id, department_id FROM employees WHERE department_id = 90; Example 2: SELECT employee_id, department_id FROM employees WHERE (salary+commission_pct) > salary; NOTE: a result of No rows selected means no data satisfied the criteria specified in WHERE clause

Comparison Operators (ctd) Example using != (not equal operator): SELECT employee_number, department_id FROM employees WHERE department_id != 10; Example using <> (also a not equal operator): SELECT employee_number, department_id FROM employees WHERE department_id <> 10;

LIKE operator LIKE operator is used when exact match with equal operator cannot be used; i.e. to to match partial strings of character data LIKE is normally used with wildcard symbols: –An % represents 0, 1 or more characters –An _ represents exactly one character position Example of finding employees whose last name starts with a H: SELECT last_name FROM employees WHERE last_name LIKE H%

LIKE operator (ctd) Example - find employees whose last name starts with Hi: SELECT last_name FROM employees WHERE last_name LIKE Hi% Example - find employees whose last name ends with s: SELECT last_name FROM employees WHERE last_name LIKE %s

LIKE operator (ctd) Example - find employees whose last name contains an a: SELECT last_name FROM employees WHERE last_name LIKE %a% Example - find employees whose last name has an a as its second letter: SELECT last_name FROM employees WHERE last_name LIKE _a%

LIKE operator (ctd) Exercise - find employees whose last name starts with an H and has an i as second-last letter. SELECT last_name FROM employees WHERE last_name LIKE H%i_

NULL value A NULL value is not actually stored as data and the comparison operators of =, >, will not return rows containing null values Example: list employee# and commission rates for all employees whose commission rate is less than 1: SELECT employee_id, commission_pct FROM employees WHERE commission_pct < 1 NOTE many employees are not included in the result because their commission rate is NULL To refer to a NULL value in a comparison condition the IS operator must be used (i.e. IS NULL or IS NOT NULL)

NULL value (ctd) Example - list all employees with no value for commission rate: INCORRECT (syntactically valid but produces incorrect output): SELECT employee_id, commission_pct FROM employees WHERE commission_pct = NULL CORRECT (syntactically valid AND produces correct output): SELECT employee_id, commission_pct FROM employees WHERE commission_pct IS NULL

NULL value (ctd) Example - list all employee with a value for commission rate : INCORRECT (syntactically valid but produces incorrect output): SELECT employee_id, commission_pct FROM employees WHERE commission_pct != NULL CORRECT (syntactically valid AND produces correct output): SELECT employee_id, commission_pct FROM employees WHERE commission_pct IS NOT NULL

NULL value (ctd) Where are NULL values placed with an ORDER BY clause? Example: list all employees and their commission rates with the employees paid the lowest commission rates listed first: SELECT last_name, first_name, commission_pct FROM employees ORDER BY commission_pct; Now list employees with highest comm rate first: SELECT last_name, first_name, commission_pct FROM employees ORDER BY commission_pct DESC; From looking at the outputs, where are NULL values placed with an ORDER BY clause?

Compound Conditions Compound conditions are formed by connecting two or more simple conditions using logical operators of AND, OR, and NOT AND: all conditions must be true in order for compound condition to be true OR: if any one of conditions is true then compound condition is true NOT: reverses the value of the original condition Standard order of precedence is used but it is strongly recommended that parentheses be used when compound condition includes both ANDs and ORs to insure correct understanding of order of evaluation

Compound conditions (ctd) Example: display names of employees whose salary is over $5000 and who are located in 20 or 10: SELECT last_name FROM employees WHERE salary > 5000 AND (department_id = 20 OR department_id = 10) NOTE: the following statement produces different results: SELECT last_name FROM employees WHERE salary > 5000 AND department_id = 20 OR department_id = 10

BETWEEN Operator The BETWEEN operator is used to easily specify a range of values as a condition, i.e. BETWEEN lower_value AND upper_value, where the range includes the lower and upper values Example: SELECT employee_id, salary FROM customer WHERE salary BETWEEN 5000 AND Could also be accomplished by: SELECT employee_id, salary FROM employees WHERE salary >= 5000 AND salary <= 10000

IN Operator IN operator is used to easily specify a list of values as a condition Example: SELECT employee_id, department_id FROM employees WHERE department_id IN (10, 20, 30) Could also be accomplished by: SELECT employee_id, department_id FROM employees WHERE department_id = 10 OR department_id = 20 OR department_id = 30