0% found this document useful (0 votes)
17 views20 pages

DP 3 1

This document covers logical comparisons and precedence rules in SQL, focusing on how to restrict query results based on multiple conditions using logical operators like AND, OR, and NOT. It explains the evaluation order of expressions in SQL queries and emphasizes the importance of understanding precedence rules to obtain accurate results. Key terms and examples illustrate how to effectively apply these concepts in database programming.

Uploaded by

wisdomolukoya
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)
17 views20 pages

DP 3 1

This document covers logical comparisons and precedence rules in SQL, focusing on how to restrict query results based on multiple conditions using logical operators like AND, OR, and NOT. It explains the evaluation order of expressions in SQL queries and emphasizes the importance of understanding precedence rules to obtain accurate results. Key terms and examples illustrate how to effectively apply these concepts in database programming.

Uploaded by

wisdomolukoya
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/ 20

Database Programming with

SQL
3-1
Logical Comparisons and Precedence Rules

Copyright © 2020, Oracle and/or its affiliates. All rights reserved.


Objectives
• This lesson covers the following objectives:
−Evaluate logical comparisons to restrict the rows returned
based on two or more conditions
−Apply the rules of precedence to determine the order in
which expressions are evaluated and calculated

DP 3-1
Logical Comparisons and Precedence Rules Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 3
Purpose
• Not too many things in life depend on just one
condition
• For instance, if you want to go to college, you probably
need good grades and the tuition money to pay for it
• If you have extra money, you could either save it or
spend it
• If you want to go to a movie, you may not want to go
this weekend and you may not want to sit in the first
10 rows of the theater

DP 3-1
Logical Comparisons and Precedence Rules Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 4
Purpose
• In SQL, it is often desirable to be able to restrict the
rows returned by a query based on two or more
conditions
• As the manager of a fast food business, you may need
to know the names of your staff who are either cooks
or order takers
• You don't need or want the entire staff list, you just
want a subset of it
• Conditional operators such as AND, NOT, and OR make
these types of requests easy to do

DP 3-1
Logical Comparisons and Precedence Rules Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 5
Logical Conditions
• Logical conditions combine the result of two
component conditions to produce a single result based
on them
• For example, to attend a rock concert, you need to buy
a ticket AND have transportation to get there
• If both conditions are met, you can go to the concert
• What if you can't get transportation, can you go?

DP 3-1
Logical Comparisons and Precedence Rules Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 6
Logical Conditions
• Another logical condition combines two component
conditions with OR
• All employees will receive a raise either by having a
perfect attendance record OR by meeting their
monthly sales quota
• If an employee meets either of these two conditions,
the employee gets a raise

DP 3-1
Logical Comparisons and Precedence Rules Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 7
Logical Operators
• A logical operator combines the results of two or more
conditions to produce a single result
• A result is returned ONLY IF the overall result of the
condition is true
• AND -- Returns TRUE if both conditions are true
• OR -- Returns TRUE if either condition is true
• NOT -- Returns TRUE if the condition is false

DP 3-1
Logical Comparisons and Precedence Rules Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 8
AND Operator
• In the query below, the results returned will be rows
that satisfy BOTH conditions specified in the WHERE
clause
SELECT last_name, department_id, salary
FROM employees
WHERE department_id > 50 AND salary > 12000;

LAST_NAME DEPARTMENT_ID SALARY


King 90 24000
Kochhar 90 17000
De Haan 90 17000

DP 3-1
Logical Comparisons and Precedence Rules Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 9
AND Operator
• Another example of using AND in the where clause
SELECT last_name, hire_date, job_id
FROM employees
WHERE hire_date > '01-Jan-1998' AND job_id LIKE 'SA%';

LAST_NAME HIRE_DATE JOB_ID

Zlotkey 29-Jan-2000 SA_MAN

Taylor 24-Mar-1998 SA_REP

Grant 24-May-1999 SA_REP

DP 3-1
Logical Comparisons and Precedence Rules Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 10
OR Operator
• If the WHERE clause uses the OR condition, the results
returned from a query will be rows that satisfy either
one of the OR conditions
• In other words, all rows returned have a location_id of
2500 OR they have a manager_id equal to 124
SELECT department_name, manager_id, location_id
FROM departments
WHERE location_id = 2500 OR manager_id=124;

DEPARTMENT_NAME MANAGER_ID LOCATION_ID


Shipping 124 1500
Sales 149 2500

DP 3-1
Logical Comparisons and Precedence Rules Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 11
NOT Operator
• The NOT operator will return rows that do NOT satisfy
the condition in the WHERE clause
SELECT department_name, location_id
FROM departments
WHERE location_id NOT IN (1700,1800);

DEPARTMENT_NAME LOCATION_ID
Shipping 1500
IT 1400
Sales 2500

DP 3-1
Logical Comparisons and Precedence Rules Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 12
Rules of Precedence or What Happens First?
• Consider the following SELECT statement
• In what order are the expressions evaluated and
calculated?
SELECT last_name||' '||salary*1.05 As "Employee Raise"
FROM employees
WHERE department_id IN(50,80) AND first_name LIKE 'C%'
OR last_name LIKE '%s%';

• Luckily, when things get this complicated, SQL has a


few basic rules that are easy to follow

DP 3-1
Logical Comparisons and Precedence Rules Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 13
Rules of Precedence or What Happens First?
• Notice that the AND operator is ORDER OPERATOR
evaluated before the OR operator 1 Aritmatika + - * /

• This means that for the example 2 Rangkaian ||

on the previous slide, if either of 3 Perbandingan <, <=,


>, >=, <>
the conditions in the AND 4 IS (NOT) NULL, LIKE,
statement are not met, then the (NOT) IN
OR operator is used to select the 5 (NOT) BETWEEN

rows 6 NOT
7 AND
• This is an important concept to
8 OR
remember

DP 3-1
Logical Comparisons and Precedence Rules Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 14
Rules of Precedence or What Happens First?
• First, the AND condition is evaluated, so all employees
working in dept 80 or 50, AND who have a first name
starting with "C" are returned
• The OR clause is then evaluated Employee Raise DEPARTMENT FIRST_
and returns employees whose Higgins 12600
_ID
110
NAME
Shelley
last name contains "s" Mourgos 6090 50 Kevin
Rajs 3675 50 Trenna
SELECT last_name||' '||salary*1.05
As "Employee Raise", department_id, Davies 3255 50 Curtis
first_name Matos 2730 50 Randall
FROM employees Vargas 2625 50 Peter
WHERE department_id IN(50,80)
Ernst 6300 60 Bruce
AND first_name LIKE 'C%'
OR last_name LIKE '%s%'; Hartstein 13650 20 Michael

DP 3-1
Logical Comparisons and Precedence Rules Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 15
Rules of Precedence or What Happens First?
• In this example, the order of the OR and AND have
been reversed from the previous slide
SELECT last_name||' '||salary*1.05 As "Employee Raise",
department_id,
first_name
FROM employees
WHERE department_id IN(50,80)
OR first_name LIKE 'C%' The order of operations is:
AND last_name LIKE '%s%'; first_name starts with a "C" AND
last_name contains an "s". Both
these conditions must be met to be
returned.
Any instance of employees in
department 50 and 80 will be
returned.

DP 3-1
Logical Comparisons and Precedence Rules Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 16
Rules of Precedence or What Happens First?
• Adding parenthesis changes the way the Where clause
is evaluated, and the rows returned
SELECT last_name||' '||salary*1.05 As "Employee Raise",
department_id,
first_name
FROM employees
WHERE (department_id IN(50,80) OR first_name LIKE 'C%')
AND last_name LIKE '%s%';
The order of operations is:
The values in the parentheses are
selected.
All instances of the values in the
parentheses that also contain the
letter "s" in their last_name will be
returned.

DP 3-1
Logical Comparisons and Precedence Rules Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 17
Terminology
• Key terms used in this lesson included:
−AND
−OR
−NOT
−Precedence Rules

DP 3-1
Logical Comparisons and Precedence Rules Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 18
Summary
• In this lesson, you should have learned how to:
−Evaluate logical comparisons to restrict the rows returned
based on two or more conditions
−Apply the rules of precedence to determine the order in
which expressions are evaluated and calculated

DP 3-1
Logical Comparisons and Precedence Rules Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 19

You might also like