DBMS M3
DBMS M3
PREPARED BY SHARIKA T R,
SNGCE
Ambiguous Attribute Names,
Aliasing, Renaming, and Tuple
Variables
• In SQL, we can use the same name for two (or more)
attributes as long as the attributes are in different relations
• a multitable query refers to two or more attributes with the
same name, we must qualify the attribute name with the
relation name to prevent ambiguity
• This is done by prefixing the relation name to the attribute
name and separating the two by a period(.).
Suppose
Dno and Lname attributes of
EMPLOYEE relation were
called Dnumber and Name and
the Dname attribute was called
Name. To prevent ambiguity Q1
should be rephrased as Q1A
Q8. For each employee, retrieve the
eemployee’s
first and last name and the first and last name
of his or her immediate supervisor.
• Alternative relation names E and S are called aliases or
tuple variables, for the EMPLOYEE relation.
• An alias follow the keyword AS
• It is also possible to rename the relation attributes within
the query in SQL by giving them aliases.
Unspecified WHERE Clause and Use of the
Asterisk
• missing WHERE clause indicates no condition on tuple
selection;
▫ hence, all tuples of the relation specified in the FROM
sclause qualify and are selected for the query result
• If more than one relation is specified in the FROM clause
and there is no WHERE clause, then the CROSS
PRODUCT all possible tuple combinations of these
relations is selected
Q 9 and 10. Select all EMPLOYEE Ssns(Q 9) and all
combinations of EMPLOYEE Ssn and DEPARTMENT
Dname (Q10) in the database.
• To retrieve all the attribute values of the selected
tuples, we do not have to list the attribute names
explicitly in SQL;
• we just specify an asterisk (*), which stands for
all the attributes
Q1C: retrieves all the attribute values of any
EMPLOYEE who works in DEPARTMENT number 5
Q1D: Retrieves all the attributes of an EMPLOYEE and the
attributes of the DEPARTMENT in which he or she works
for every employee of the ‘Research’ department
Q10A: specifies the CROSS PRODUCT of
the EMPLOYEE and DEPARTMENT
relations
Tables as Sets in SQL
• SQL usually treats a table not as a set but rather as a multiset;
▫ duplicate tuples can appear more than once in a table, and in the result
of a
query.
• SQL does not automatically eliminate duplicate tuples in the results of
queries, for the following reasons
▫ Duplicate elimination is an expensive operation.
One way to implement it is to sort the tuples first and then eliminate duplicates.
▫ The user may want to see duplicate tuples in the result of a query.
▫ When an aggregate function is applied to tuples, in most cases we do
not want to eliminate duplicates
DISTINCT Keyword
• to eliminate duplicate tuples from the result of an SQL
querys we use the keyword DISTINCT in the SELECT
clause
• only distinct tuples should remain in the result
• a query with SELECT DISTINCT eliminates duplicates,
whereas a query with SELECT ALL does not.
• SELECT with neither ALL nor DISTINCT is equivalent to
SELECT ALL
Q11 retrieves the salary of every
employee without distinct
Q11A :retrieves the salary of every employee
using keyword DISTINCT
EXCEPT and INTERSECT
• set union (UNION), set difference (EXCEPT), and set
intersection (INTERSECT) operations.
• The relations resulting from these set operations are sets of tuples;
that is, duplicate tuples are eliminated from the result.
• These set operations apply only to union-compatible relations, so
we must make sure that the two relations on which we apply the
operation have the same attributes and that the attributes appear
in the same order in both relations
Q4. Make a list of all project numbers for projects that
involve an employee whose last name is ‘Smith’, either as a
worker or as a manager of the department that controls the
project
The first SELECT query retrieves the projects that involve a ‘Smith’ as manager of
the department that controls the project, and the second retrieves the projects that
involve a ‘Smith’ as a worker on the project. Notice that if several employees have the
last name ‘Smith’, the project names involving any of them will be retrieved.
Applying the UNION operation to the two SELECT queries gives the desired result.
UNION ALL
• The UNION ALL command combines the result set of two
or more SELECT statements (allows duplicate values).
• The following SQL statement returns the cities (duplicate
values also) from both the "Customers" and the "Suppliers"
table:
This SQL UNION ALL example would return the supplier_id multiple times in
the result set if that same value appeared in both the suppliers and orders table.
The SQL UNION ALL operator does not remove duplicates. If you wish to remove
duplicates, try using the UNION operator.
SNGCE
INTERSECT Operator
• INTERSECT operator is used to
return the records that are in
common between two SELECT
statements or data sets.
• If a record exists in one query
and not in the other, it will be
omitted from the INTERSECT
results.
• It is the intersection of the two
SELECT statements.
EXCEPT
• The SQL EXCEPT clause/operator is used to combine two
SELECT statements and returns rows from the first
SELECT statement that are not returned by the second
SELECT statement.
▫ This means EXCEPT returns only rows, which are
not available in the second SELECT statement.
• Just as with the UNION operator, the same rules apply
when using the EXCEPT operator.
Substring Pattern Matching and
Arithmetic Operators
• LIKE comparison operator
▫ This can be used for string pattern matching
▫ Partial strings are specified using two reserved characters
% replaces an arbitrary number of zero or more characters, and
the underscore (_) replaces a single character
Q 12. Retrieve all employees whose address
is in Houston, Texas.
Q 12A. Find all employees who were born
during the 1950s.
suppose that we want to see the effect of giving all employees who work on the
‘ProductX’ project a 10 percent raise; we can issue Query 13 to see what their
salaries would become. This example also shows how we can rename an attribute in
the query result using AS in the SELECT clause.
• For string data types, the concatenate operator ||
can be used in a query to append two string
values.
• For date, time, timestamp, and interval data
types, operators include incrementing (+) or
decrementing (–) a date, time, or timestamp by
an interval.
• In addition, an interval value is the result of the
difference between two date, time, or timestamp
values.
• Another comparison operator, which can be
used for convenience, is BETWEEN
Q 14. Retrieve all employees in department 5
whose salary is between $30,000 and $40,000.
Student Table
SNGCE
Sort according to multiple columns
Q. Fetch all data from the table Student and then sort the
result in ascending order first according to the column Age.
and then in descending order according to the column
ROLL_NO.
Q15. Retrieve a list of employee and the projects
they are working on,ordered by department and,
within each department, ordered alphabetically by
last name, then first name.
The keyword ALL can be combined with each of >, >=, <, <=, and <>,
these operators. For example, the comparison condition (v > ALL V)
returns TRUE if the value v is greater than all the values in the set (or
multiset) V.
Q . Select the Essns of all employees who work
the same (project, hours)combination on some
project that employee ‘John Smith’ (whose
Ssn =‘123456789’) works on.
Here the nested query has a different result for each tuple
in the outer query.
A query written with nested SELECT... FROM... WHERE...
blocks and using the = or IN comparison operators can
alwaysbe expressed as a single block query.
A Simple Retrieval Query in SQL
• A simple retrieval query in SQL can consist of up to four
clauses,
▫ but only the first two SELECT and FROM are
mandatory
▫ the clauses between square brackets [ ... ] being
optional:
• The SELECT-clause lists the attributes or functions to be
retrieved
• The FROM-clause specifies all relations (or aliases) needed in
the query but not those needed in nested queries
• The WHERE-clause specifies the conditions for selection and
join of tuples from the relations specified in the FROM-clause
• GROUP BY specifies grouping attributes
• HAVING specifies a condition for selection of groups
• ORDER BY specifies an order for displaying the result of a
query
• A query is evaluated by first applying the WHERE-clause,
then GROUP BY and HAVING, and finally the SELECT-clause
• There are three SQL commands to modify the database;
INSERT, DELETE, and UPDATE
The EXISTS and UNIQUE Functions in SQL
• EXISTS function in SQL is used to check whether the result
of a correlated nested query is empty (contains no tuples)
or not
• The result of EXISTS is a Boolean value
▫TRUE if the nested query result contains at least one tuple, or
▫FALSE if the nested query result contains no tuples.
SELECT E.Fname, E.Lname
FROM EMPLOYEE AS E
WHERE EXISTS
( SELECT *
FROM DEPENDENT AS D
WHERE E.Ssn=D.Essn AND E.Sex=D.Sex
);
OUTPUT
• EXISTS(Q) returns TRUE if there is at least one tuple in
the result of the nested query Q, and it returns FALSE
otherwise.
• On the other hand, NOT EXISTS(Q) returns TRUE if there
are no tuples in the result of nested query Q, and it returns
FALSE otherwise
Q. Retrieve the names of employees who
have no dependents.
OUTPUT
Aggregate Functions in SQL
• Aggregate functions are used to summarize information
from multiple tuples into a single-tuple summary.
• Grouping is used to create sub-groups of tuples before
summarization.
• A number of built-in aggregate functions exist:
1. COUNT,
2. SUM,
3. MAX,
4. MIN, and
5. AVG
• COUNT function returns the number of tuples or values as
specified in a query.
• The functions SUM, MAX, MIN, and AVG can be applied to
a set or multiset of numeric values and return, respectively,
the sum, maximum value, minimum value, and average
(mean) of those values
• These functions can be used in the SELECT clause or in a
HAVING clause
Q19. Find the sum of the salaries of all
employees, the maximum salary,
the minimum salary, and the average
salary.
Q 20. Find the sum of the salaries of all
employees of the ‘Research’department, as
well as the maximum salary, the minimum
salary, and the average salary in this
department.
Q21. Retrieve the total number of
employees in the company
SELECT COUNT(*)
FROM EMPLOYEE;
Q22. The number of employees in the
‘Research’ department
V1
V2
• We can specify SQL queries on a view in the same way
we specify queries involving base tables.
• For example,
▫ to retrieve the last name and first name of all
employees who work on the ‘ProductX’ project, we can
utilize the WORKS_ON1 view and specify the query as
in QV1:
• A view is supposed to be always up-to-date;
▫ if we modify the tuples in the base tables on which the view
is defined, the view must automatically reflect these changes.
• Hence, the view is not realized or materialized at the time of view
definition but rather at the time when we specify a query on the
view.
• It is the responsibility of the DBMS and not the user to make
sure that the view is kept up-to-date
DROP VIEW
• If we do not need a view any more, we can use the DROP
VIEW command to dispose of it.
• For example, to get rid of the view V1, we can use the SQL
statement in V1A:
View Implementation, View Update, and Inline
Views
• query modification
▫ involves modifying or transforming the view query
(submitted by the user) into a query on the underlying
base tables.
▫ For example, the query QV1 would be
automatically modified to the following query by
the DBMS
Algebra Operations
PREPARED BY SHARIKA T R,
SNGCE
PREPARED BY SHARIKA T R,
SNGCE
Outline of a Heuristic Algebraic Optimization
Algorithm:
• Using rule 1, break up any select operations with conjunctive conditions into a
cascade of select operations.
• Using rules 2, 4, 6, and 10 concerning the commutativity of select with other
operations, move each select operation as far down the query tree as is permitted
by the attributes involved in the select condition.
• Using rule 9 concerning associativity of binary operations, rearrange the leaf nodes
of the tree so that the leaf node relations with the most restrictive select operations
are executed first in the query tree representation.
• Using Rule 12, combine a Cartesian product operation with a subsequent select
operation in the tree into a join operation.
• Using rules 3, 4, 7, and 11 concerning the cascading of project and the commuting
of project with other operations, break down and move lists of projection attributes
down the tree as far as possible by creating new project operations as needed.
• Identify subtrees that represent groups of operations that can be executed by a
single algorithm.
Summary of Heuristics for
Algebraic Optimization
• The main heuristic is to apply first the operations that reduce the
size of intermediate results.
• Perform select operations as early as possible to reduce the
number of tuples and perform project operations as early
as possible to reduce the number of attributes.
▫ This is done by moving select and project operations as far down
the
tree as possible.
• The select and join operations that are most restrictive should be
executed before other similar operations.
▫ This is done by reordering the leaf nodes of the tree
Query Execution Plans
• An execution plan for a relational algebra query consists of
a combination of the relational algebra query tree and
information about the access methods to be used for each
relation as well as the methods to be used in computing the
relational operators stored in the tree.
• Materialized evaluation: the result of an operation is stored
as a temporary relation.
• Pipelined evaluation:
▫ as the result of an operator is produced, it is forwarded to
the next operator in sequence.
END