0% found this document useful (0 votes)
20 views38 pages

W4 DBMS Chapter07

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

W4 DBMS Chapter07

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

CHAPTER 7

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe


More Complex SQL Retrieval
Queries
 Additional features allow users to specify more
complex retrievals from database:
 NULL value

 Nested queries,
 Joined tables

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 4


SELECT Statement

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe


SQL Alias: AS Keyword
 SQL AS command is used to rename a column or table with an alias.
 SQL alias allows you to assign a table or a column a temporary name
during the execution of a query. SQL has two types of aliases: table and
column aliases.
 An alias only exists for the duration of the query
SELECT CustomerName AS Customer, ContactName AS [Contact
Person]
FROM Customers
SELECT CustomerName, Address + ', ' + PostalCode + ' ' + City + ', ' +
Country AS Address
FROM Customers;
 AS keyword is optional. So you can omit it

SELECT o.OrderID, o.OrderDate, c.CustomerName


FROM Customers c, Orders o
WHERE c.CustomerName="Around the
Horn" AND c.CustomerID=o.CustomerID;
Slide 7- 4
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe
NULL value
 A field with a NULL value is a field with no
value.
 If a field in a table is optional, it is possible to
insert a new record or update a record without
adding a value to this field. Then, the field will
be saved with a NULL value.
 A NULL value is different from a zero value or
a field that contains spaces. A field with a
NULL value is one that has been left blank
during record creation!
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 5
NULL value
 The following examples to illustrate the meanings of NULL:
 Unknown value. A person’s date of birth is not known, so it

is represented by NULL in the database. An example of the


other case of unknown would be NULL for a person’s home
phone because it is not known whether or not the person
has a home phone.
 Unavailable or withheld value. A person has a home

phone but does not want it to be listed, so it is withheld and


represented as NULL in the database.
 Not applicable attribute. An attribute LastCollegeDegree

would be NULL for a person who has no college degrees


because it does not apply to that person.

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 6


Comparisons Involving NULL Value
 It is not possible to test for NULL values with
comparison operators, such as =, <, or <>.
 SQL allows queries that check whether an
attribute value is NULL by using:
 IS or IS NOT NULL

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 7


Example: Comparisons Involving NULL Value
Custom CustomerName ContactName Address City PostalCode Country
erID
1 Alfreds Maria Anders Obere Str. 57 NULL 12209 Germany
Futterkiste

2 Ana Trujillo Ana Trujillo México D.F. 05021 Mexico


Emparedados y
helados
3 Antonio Moreno Antonio Mataderos 2312 México D.F. 05023 Mexico
Taquería Moreno

4 Around the Thomas 0 London WA1 1DP UK


Horn Hardy

5 Berglunds Christina Berguvsvägen 8 Luleå S-958 22 Sweden


snabbköp Berglund

SELECT CustomerName, SELECT CustomerName,


ContactName, Address ContactName, Address
FROM Customers FROM Customers
WHERE Address IS NULL;WHERE City IS NOT NULL;
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 8
Nested Query in SQL
 A nested query in SQL contains a query inside
another query. The result of the inner query will be
used by the outer query.
 Nested query can have two SELECT statements,
one on the inner query and the other on the outer
query.
 Nested queries in SQL can be classified into two
different types:
 Independent Nested Queries
 Co-related Nested Queries

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 9


Independent Nested Queries
 In independent nested queries, the execution order is from the inner most
query to the outer query. An outer query won't be executed until its inner query
completes its execution. The result of the inner query is used by the outer
query. Operators such as IN, NOT IN, ALL, and ANY are used to write
independent nested queries.
 The IN operator checks if a column value in the outer query's result

is present in the inner query's result. The final result will have rows that
satisfy the IN condition.
 The NOT IN operator checks if a column value in the outer query's result

is not present in the inner query's result. The final result will have rows that
satisfy the NOT IN condition.
 The ALL operator compares a value of the outer query's result with all the

values of the inner query's result and returns the row if it matches all the
values.
 The ANY operator compares a value of the outer query's result with all the

inner query's result values and returns the row if there is a match with any
value.
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 10
Co-related Nested Queries
 In co-related nested queries, the inner query uses
the values from the outer query so that the inner
query is executed for every row processed by the
outer query.
 The co-related nested queries run slowly because
the inner query is executed for every row of the outer
query's result.
 We can write a nested query in SQL by nesting
SELECT statement within another SELECT
statement.
 The outer SELECT statement uses the result of the
inner SELECT statement for processing. Slide 7- 11
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe
Nested Queries Examples (Independent)

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 12


Independent Nested Queries

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe


Example: Database Tables
Employees

id name salary role


Augustine
1 10000 Developer
Hammond
2 Perice Mundford 10000 Manager
3 Cassy Delafoy 30000 Developer
4 Garwood Saffen 40000 Manager
5 Faydra Beaves 50000 Developer

Awards

id employee_id award_date
1 1 2022-04-01
2 3 2022-05-01

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 14


Independent Nested Queries

id name
2 Perice Mundford
4 Garwood Saffen
5 Faydra Beaves

id name
1 Augustine Hammond
3 Cassy Delafoy
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 15
id name salary role Independent
5 Faydra Beaves 50000 Developer Nested
Queries

id name salary role


5 Faydra Beaves 50000 Developer
3 Cassy Delafoy 30000 Developer
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 16
Co-related Nested Queries

id name salary role

4 Garwood Saffen 40000 Manager

5 Faydra Beaves 50000 Developer

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 17


The EXISTS and UNIQUE Functions
in SQL for correlating queries
 EXISTS function
 Check whether the result of a correlated nested
query is empty or not. They are Boolean functions
that return a TRUE or FALSE result.
 EXISTS and NOT EXISTS
 Typically used in conjunction with a correlated
nested query
 SQL function UNIQUE(Q)
 Returns TRUE if there are no duplicate tuples in
the result of query Q

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 14


The SQL EXISTS Operator

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 19


Example:
EXISTS

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 20


Example:
NOT EXISTS

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 21


Unique constraint
 Unique constraint in SQL is used to check whether the sub
query has duplicate tuples in it’s result.
 It returns a Boolean value indicating the presence/absence
of duplicate tuples.
 Unique construct returns true only if the sub query has no
duplicate tuples, else it return false.
 Important Points:
 Evaluates to true on an empty sub query.
 Returns true only if there are unique tuples present as the
output of the sub query (two tuples are unique if the value of
any attribute of the two tuples differ).
 Returns true if the sub query has two duplicate rows with at
least one attribute as NULL.
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 22
Find all the instructors that taught at most one course in the year 2017 .

Unique
constraint
EmployeeID Name CourseID Year
77505 Alan SC110 2017

77815 Will CSE774 2017

85019 Smith EE457 2017

92701 Sam PYS504 2017

60215 Harold HSS103 2016

77505 Alan BIO775 2017

92701 Sam ECO980 2017

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 23


Specifying Joined Tables in the
FROM Clause of SQL
 As the name shows, JOIN means to combine something. In
case of SQL, JOIN means "to combine two or more
tables".
 The SQL JOIN clause takes records from two or more tables
in a database and combines them together.
 Joined table: Permits users to specify a table resulting from
a join operation in the FROM clause of a query
 Contains a single joined table. JOIN may also be called

INNER JOIN

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 19


Join Operations
 Typically, we want only combinations of the Cartesian product
that satisfy certain conditions and so we would normally use a
Join operation instead of the Cartesian product operation.
 Equivalent to perform a Selection, using join predicate as
selection formula, over Cartesian product of the two operand
relations.
 A NATURAL JOIN is a JOIN operation that creates an implicit
join clause for you based on the common columns in the two
tables being joined. Common columns are columns that have
the same name in both tables.
 A NATURAL JOIN can be an INNER join, a LEFT OUTER
join, or a RIGHT OUTER join.

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe


Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe
INNER and OUTER Joins
 INNER JOIN
 Default type of join in a joined table

 Tuple is included in the result only if a matching tuple exists

in the other relation


 LEFT OUTER JOIN
 Every tuple in left table must appear in result

 If no matching tuple


Fill in with NULL values for attributes of right table
 RIGHT OUTER JOIN
 Every tuple in right table must appear in result

 If no matching tuple


Fill in with NULL values for attributes of left table

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 22


SQL
JOIN

 The SQL JOIN joins two


tables based on a
common column, and
selects records that have
matching values in these
columns.
 INNER JOIN is same as
JOIN

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 29


SQL LEFT JOIN

 The SQL LEFT JOIN


joins two tables based
on a common column,
and selects records
that have matching
values in these
columns and remaining
rows from the left
table.

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 30


SQL RIGHT JOIN

 The SQL RIGHT


JOIN joins two
tables based on a
common column,
and selects records
that have matching
values in these
columns and
remaining rows
from the right table.

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 31


SQL FULL OUTER JOIN

 The SQL FULL


OUTER JOIN joins
two tables based on
a common column,
and selects records
that have matching
values in these
columns and
remaining rows
from both of the
tables.

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 32


SQL Cross Join
 If we use the cross join to combine two different tables,
then we will get the Cartesian product of the sets of rows
from the joined table.
 When each row of the first table is combined with each
row from the second table, it is known as Cartesian join or
cross join.
 After performing the cross join operation, the total number
of rows present in the final table will be equal to the
product of the number of rows present in table 1 and the
number of rows present in table 2.

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 33


SQL Cross Join
SELECT * FROM Meals
CROSS JOIN Drinks

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 34


Assignment 5-1
 Show the result of each query if it is applied to
the COMPANY database:

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 35


Assignment 5-2

Use COMPANY database to write SQL
queries statements using the concept of nested
queries and other concepts described in this
chapter:
a) Retrieve the names of all employees who work in the
department that has the employee with the highest
salary among all employees.
b) Retrieve the names of all employees whose
supervisor’s supervisor has ‘888665555’ for Ssn.
c) Retrieve the names of employees who make at least
$10,000 more than the employee who is paid the
least in the company.
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 36
COMPANY database

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 37


COMPANY database

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 38

You might also like