Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
22 views

SQL Queries

Uploaded by

Sowmya Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

SQL Queries

Uploaded by

Sowmya Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 120

DATABASE MANAGEMENT SYSTEM

Query Languages
Allow manipulation and retrieval of data from a database
Outline

Practical Language SQL

Formal Query Languages
– Relational Algebra

– Relational Calculus

Domain Relational calculus

Tuple Relational calculus
Query Languages

SQL is a computer language for storing, manipulating and
retrieving data stored in a relational database

Relational model supports simple, powerful Formal Query
Languages

Formal languages are important for several reasons

1. Provides a formal foundation for relational model


operations.
2. Used as a basis for implementing and optimizing queries
in the query processing and optimization modules
3. Some of formal language concepts are incorporated
into the SQL.
Query Languages

Two mathematical Query Languages form the basis for “real”
languages (e.g. SQL), and for implementation:

Relational Algebra:
– Defines a set of operations for the relational model

– More operational(procedural), very useful for representing


execution plans.

Relational Calculus:
– Provides a higher-level declarative language for specifying
rela-tional queries
– Lets users describe what they want, rather than how to
compute it. (Non-operational, declarative.)
Query Languages

Query
Relational Algebra

Its operations can be divided into two groups
– Set operations


Each relation is defined to be a set of tuples in the
formal relational model

UNION, INTERSECTION, SET DIFFERENCE, and CARTESIAN
PRODUCT (also known as CROSS PRODUCT)
– Operations(specifically for relational databases)

SELECT, PROJECT(Unary),JOIN and other complex binary
operations
– Additional operations

Some common database requests cannot be performed with
the original relational algebra operations

Aggregate functions, additional types of JOIN and UNION
operations
Relational Algebra


Unary Relational Operations:

– SELECT(σ)
– PROJECT(π)
Select Operation

The SELECT operation is used to choose a subset of the tuples
from a relation that satisfies a selection condition.


The general form of the SELECT operation is
σ<selection condition>(R)


<selection condition> is specified on the attributes of relation R

R is generally a relational algebra expression whose result is a
relation

The relation resulting from the SELECT operation has the same
attributes as R
Projection Operation

SELECT operation chooses some of the rows from the table while
discarding other rows.

The PROJECT operation, on the other hand, selects certain
columns from the table and discards the other columns.


The general form of the PROJECT operation is
π<attribute list>(R)


<attribute list> is the desired sublist of attributes from the
attributes of relation R.

R is generally a relational algebra expression whose result is a
relation
Projection Operation

If the attribute list includes only non-key attributes of R, duplicate
tuples are likely to occur.

The PROJECT operation removes any duplicate tuples, so the
result of the PROJECT operation is a set of distinct tuples


In SQL queries duplicate tuples will not be removed

If We want to remove duplicate tuples in SQL we can use keyword
DISTINCT
SQL Select Statement

SQL has one basic statement for retrieving information from a
database; The SELECT statement

Basic form of the SQL SELECT statement is called a mapping
or a SELECT-FROM-WHERE block

– SELECT <attribute list>


– FROM <table list>
– WHERE <condition>

<attribute_list> is a list of attribute names whose values are
to be retrieved by the query.

<table_list> is a list of the relation names required to process
the query.

<tuple_condition> is a conditional (Boolean) expression that
identifies the tuples to be retrieved by the query
SQL Select Statement
SQL Select Statement

πA1, A2, …,An(σTuple_Condition(R1 x R2 x...x Rm) )


COMPANY Database Schema with
Referential Integrity Constraints
Company Database schema
Company Database schema
Simple Retrieval Queries

Q1: Select the EMPLOYEE tuples who works in DEPARTMENT


number 4.

SELECT *
σDno=4(EMPLOYEE)
FROM EMPLOYEE
WHERE Dno = 4;

In SQL, the SELECT condition is typically specified in the


WHERE clause of a query.
Simple Retrieval Queries

Q1: Select the EMPLOYEE tuples who works in DEPARTMENT


number 4.
Simple Retrieval Queries

Q2: Select the EMPLOYEE tuples whose salary is greater than


$30,000

SELECT *
σSalary>30000(EMPLOYEE)
FROM EMPLOYEE
WHERE Salary > 30000;
Simple Retrieval Queries

Q2: Select the EMPLOYEE tuples whose salary is greater than


$30,000
Simple Retrieval Queries

Q3: Select the tuples for all employees who work in department 4
and make over $25,000 per year

σDno=4 AND Salary>25000(EMPLOYEE)

SELECT *
FROM EMPLOYEE
WHERE Dno = 4 and Salary > 25000;
Simple Retrieval Queries

Q3: Select the tuples for all employees who work in department 4
and make over $25,000 per year
Simple Retrieval Queries

Q4: Select the tuples for all employees who either work in
department 4 and make over $25,000 per year, or work in
department 5 and make over $30,000

σ(Dno=4 AND Salary>25000) OR (Dno=5 AND Salary>30000)(EMPLOYEE)

SELECT *
FROM EMPLOYEE
WHERE (Dno = 4 AND Salary > 25000) OR (Dno = 5 AND
Salary > 30000);
Simple Retrieval Queries

Q4: Select the tuples for all employees who either work in
department 4 and make over $25,000 per year, or work in
department 5 and make over $30,000
Simple Retrieval Queries
Q4A. Retrieve all employees in department 5 whose
salary is between $30,000 and $40,000.

SELECT *
FROM EMPLOYEE
WHERE (Salary BETWEEN 30000 AND 40000) AND Dno = 5;
Simple Retrieval Queries

Q5: Retrieve the Gender and salary of every employee.

πSex, Salary(EMPLOYEE) SELECT DISTINCT Sex, Salary


FROM EMPLOYEE

In SQL, the PROJECT attribute list is specified in the SELECT


clause of a query.
Simple Retrieval Queries

Q5: Retrieve the Gender and salary of every employee.


Simple Retrieval Queries

Q6: Retrieve employee’s first and last name and salary

πFname,Lname, Salary(EMPLOYEE)

SELECT DISTINCT Fname,Lname, Salary


FROM EMPLOYEE
Simple Retrieval Queries

Q6: Retrieve employee’s first and last name and salary


Simple Retrieval Queries
Q7: Retrieve the salary of every employee

SELECT ALL Salary


FROM EMPLOYEE;

Q7A: Retrieve the all distinct salary values

SELECT DISTINCT Salary π Salary(EMPLOYEE)


FROM EMPLOYEE;
Simple Retrieval Queries
Q8: Retrieve the birth date and address of the employee(s)
whose name is ‘John B. Smith’.

πBdate,Address(σFname = ‘John’AND Minit = ‘B’AND Lname = ‘Smith’


(EMPLOYEE) )

SELECT Bdate, Address


FROM EMPLOYEE
WHERE Fname = ‘John’AND Mname = ‘B’AND Lname = ‘Smith’;
Sequence of Operations

The results of relations in previous Queries do not have any
names.

In general, for most queries, we need to apply several
relational algebra operations one after the other.

Either we can write the operations as a single relational
algebra expression by nesting the operations, or we can apply
one operation at a time and create intermediate result
relations.

In the latter case, we must give names to the relations
that hold the intermediate results
Sequence of Operations and Rename Operation

We can also define a formal RENAME operation—which can rename
either the relation name or the attribute names, or both
● ρS(B1, B2, ... , Bn)(R) or ρS(R) or ρ(B1, B2, ... , Bn)(R)

ρ (rho) is used to denote the RENAME operator, S is the new rela-tion
name, and B1, B2, ... , Bn are the new attribute name

In SQL, a single query typically represents a complex relational
algebra expression.

Renaming in SQL is accomplished by aliasing using AS
Sequence of Operations and Rename Operation

Q9: Retrieve the first name, last name, and salary of all
employees who work in department number 5

πFname,Lname, Salary(σDno=5(EMPLOYEE) )
Alterna-tively, we can explicitly show the sequence of
operations, giving a name to each intermediate relation, and
using the assignment operation,
denoted by ← (left arrow), as follows:

DEP5_EMPS ←σDno=5(EMPLOYEE)
RESULT ←πFname,Lname, Salary(DEP5_EMPS)
Sequence of Operations and Rename Operation

Q6: Retrieve the first name, last name, and salary of all
employees who work in department number 5
Sequence of Operations and Rename Operation
Q9A: Retrieve the first name, last name, and salary of all
employees who work in department number 5 by using
renaming
ρE(First_name, Last_name, Salary)(πFname,Lname, Salary(σDno=5(EMPLOYEE)))

SELECT E.Fname as First_name, E.Lname as Last_name,


E.Salary as Salary FROM EMPLOYEE E WHERE E.Dno=5;
CARTESIAN PRODUCT (CROSS PRODUCT) Operation

CARTESIAN PRODUCT operation is denoted by ×

It combines two relations

This set operation produces a new element by combining every
member (tuple) from one relation (set) with every member (tuple)
from the other relation (set).

If R(A1, A2, ... , An) and S(B1, B2, ... , Bm) two relations

The result of R×S is a relation Q with degree n + m attributes
Q(A1, A2, ... , An, B1, B2, ... , Bm), in that order

The resulting relation Q has one tuple for each combination of tuples
one from R and one from S.

Hence, if R has N tuples and S has M tuples, then R × S will have
N*M tuples.
Simple Retrieval Queries

Q1: Select the EMPLOYEE tuples who works in DEPARTMENT


number 4.

SELECT *
σDno=4(EMPLOYEE)
FROM EMPLOYEE
WHERE Dno = 4;

Q1A: Select the EMPLOYEEs who works for Administration


DEPARTMENT.
Simple Retrieval Queries

Q1: Select the EMPLOYEE tuples who works in DEPARTMENT


number 4.

RESULT ← EMPLOYEE × DEPARTMENT


Simple Retrieval Queries

Q1: Select the EMPLOYEE tuples who works in DEPARTMENT


number 4.
CARTESIAN PRODUCT (CROSS PRODUCT) Operation


The CARTESIAN PRODUCT operation applied by itself is generally
meaningless.

It is mostly useful when followed by a selection that matches
values of attributes coming from the component relations
Select * from (select Fname, Lname, ssn from employee where sex = 'F') EMP_names,
Dependent ;
Simple Retrieval Queries

Q1: Select the EMPLOYEE tuples who works in DEPARTMENT


number 4.

RESULT ← σDno=Dnumber(EMPLOYEE × DEPARTMENT )


RESULT ← σDno=Dnumber and Dname = ‘Administration’(EMPLOYEE × DEPARTMENT )
CARTESIAN PRODUCT (CROSS PRODUCT) Operation
Q10:Retrieve a list of names of each female employee’s dependents

FEMALE_EMPS ←σSex=‘F’(EMPLOYEE)
EMPNAMES ←πFname, Lname, Ssn(FEMALE_EMPS)
EMP_DEPENDENTS ← EMPNAMES × DEPENDENT
ACTUAL_DEPENDENTS ←σSsn=Essn(EMP_DEPENDENTS)
RESULT ←πFname, Lname, Dependent_name(ACTUAL_DEPENDENTS)

Select * from (select Fname, Lname, ssn from employee


where sex = 'F') EMP_names, Dependent ;
CARTESIAN PRODUCT (CROSS PRODUCT) Operation


The CARTESIAN PRODUCT operation applied by itself is generally
meaningless.

It is mostly useful when followed by a selection that matches
values of attributes coming from the component relations
Select * from (select Fname, Lname, ssn from employee where sex = 'F') EMP_names,
Dependent ;
Select * from (select Fname, Lname, ssn from employee where sex = 'F')
EMP_names, Dependent where EMP_names.ssn = Dependent.essn;

Select Fname, Lname, Dependent_name from (select Fname, Lname, ssn


from employee where sex = 'F') EMP_names, Dependent where
EMP_names.ssn = Dependent.essn;
CARTESIAN PRODUCT (CROSS PRODUCT) Operation

Q11. Specifies the CROSS PRODUCT of the EMPLOYEE and


DEPARTMENT relations.

RESULT ← EMPLOYEE × DEPARTMENT

SELECT *
FROM EMPLOYEE, DEPARTMENT
CARTESIAN PRODUCT (CROSS PRODUCT) Operation

Q12. Retrieve the name and address of all employees


who work for the ‘Research’ department.

SELECT Fname, Lname, Address


FROM EMPLOYEE, DEPARTMENT
WHERE Dname = ‘Research’ AND Dnumber = Dno;
CARTESIAN PRODUCT (CROSS PRODUCT) Operation

Q12A. 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.

SELECT *
FROM EMPLOYEE, DEPARTMENT
WHERE Dname = ‘Research’ AND Dnumber = Dno;
CARTESIAN PRODUCT (CROSS PRODUCT) Operation

Q13. For every project located in ‘Stafford’, list the


project number, the controlling department number, and
the department manager’s last name, address, and birth
date.

SELECT Pnumber, Dnum, Lname, Address, Bdate


FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum = Dnumber AND Mgr_ssn = Ssn AND Plocation = ‘Stafford’
Ambiguous Names and Renaming(Aliasing)
Q14. For each employee, retrieve the employee’s first and last
name and the first and last name of his or her immediate
supervisor.

SELECT E.Fname, E.Lname, S.Fname, S.Lname


FROM EMPLOYEE E, EMPLOYEE S
WHERE E.Super_ssn = S.Ssn;
COMPANY Database Schema with
Referential Integrity Constraints
Ambiguous Names and Renaming(Aliasing)
SET Operations

Set operations are used to merge the elements of two sets in
various ways including UNION, INTERSECTION, SET
DIFFERENCE(MINUS or EXCEPT)

The two relations must have the same type of tuples;

this condition has been called union compatibility or
type compatibility

Two relations R and S are said to be union compatible
– The two relations have the same number of attributes
– and each corresponding pair of attributes has the same
domain (data type)
SET UNION

We can define the UNION on two union compatible relations R and S
as follows:

UNION: The result of this operation, denoted by R∪S, is a
relation that includes all tuples that are either in R or in S or in both
R and S. Duplicate tuples are eliminated.

R RUS
UNION
Q15. Retrieve the Social Security numbers of all employees
who either work in department 5 or directly supervise an
employee who works in department 5.

DEP5_EMPS ←σDno=5(EMPLOYEE)
RESULT1←πSsn(DEP5_EMPS)
RESULT2(Ssn)←πSuper_ssn(DEP5_EMPS)
RESULT ← RESULT1 ∪ RESULT2
or
Result ←πSsn (σDno=5 (EMPLOYEE) ) ∪πSuper_ssn (σDno=5 (EMPLOYEE)).
UNION
SELECT DISTINCT ssn FROM EMPLOYEE WHERE Dno = 5
UNION
SELECT DISTINCT Super_ssn FROM EMPLOYEE WHERE Dno = 5;
UNION
SELECT DISTINCT ssn FROM EMPLOYEE WHERE Dno = 5
UNION ALL
SELECT DISTINCT Super_ssn FROM EMPLOYEE WHERE Dno = 5;
UNION
Q16. 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.

(SELECT DISTINCT Pnumber FROM PROJECT, DEPARTMENT, EMPLOYEE


WHERE Dnum = Dnumber AND Mgr_ssn = Ssn AND Lname = ‘Smith’ )
UNION
( SELECT DISTINCT Pnumber FROM PROJECT, WORKS_ON, EMPLOYEE WHERE
Pnumber = Pno AND Essn = Ssn AND Lname = ‘Smith’ );
SET Operations

We can define the SET DIFFERENCE on two union compatible
relations R and S as follows:

SET DIFFERENCE (or MINUS): The result of this operation,
denoted by R – S, is a relation that includes all tuples that are in R
but not in S.
SET DIFFERENCE
Q17. Retrieve the names of all employees who work on project
2 but not work on project 1.

P2_EMPS ←σPno=2(WORKS_ON)
RESULT1←πSsn(P2_EMPS)
P1_EMPS ←σPno=1(WORKS_ON)
RESULT2←πSsn(P1_EMPS)
RESULT ← RESULT1 - RESULT2
SET DIFFERENCE
Q17. Retrieve the Social Security numbers of all employees
who work on project 2 but not work on project 1.

SELECT Essn from Works_on where pno = 2


MINUS
SELECT Essn from Works_on where pno = 1;
SET INTERSECTION

We can define the INTERSECTION on two union compatible relations
R and S as follows:

INTERSECTION: The result of this operation, denoted by R∩S, is a
relation that includes all tuples that are in both R and S.
INTERSECTION
Q18. Retrieve the Social Security numbers of all employees
who work on project 2 and 1.

P2_EMPS ←σPno=2(EMPLOYEE)
RESULT1←πSsn(P2_EMPS)
P1_EMPS ←σPno=1(EMPLOYEE)
RESULT2←πSsn(P1_EMPS)
RESULT ← RESULT1 ∩ RESULT2
INTERSECTION
Q18. Retrieve the Social Security numbers of all employees
who work on project 2 and 1.

SELECT Essn FROM Works_on where pno = 2


INTERSECT
SELECT Essn FROM Works_on where pno = 1;
DIVISION Operation

The DIVISION operation, denoted by ÷

DIVISION operation is applied to two relations R(Z) ÷ S(X)

where the attributes of S are a subset of the attributes of R

for a tuple t to appear in the result T of the DIVISION, the values in t
must appear in R in combination with every tuple in S

the tuples in the denominator relation S restrict the numerator rela-
tion R by selecting those tuples in the result that match all values
present in the denominator.
DIVISION Operation
Q22: Retrieve the names of employees who work on all the
projects that ‘John Smith’ works on.

First, retrieve the list of project numbers that ‘John Smith’ works on
● SMITH←σFname=‘John’AND Lname=‘Smith’(EMPLOYEE)
SMITH_PNOS←πPno(WORKS_ON ⋈ Essn=Ssn
SMITH)

Next, create a relation that includes a tuple <Pno, Essn> whenever
the employee whose Ssn is Essn works on the project whose
number is Pno in the intermediate relation SSN_PNOS
SSN_PNOS←πEssn, Pno(WORKS_ON)
SSNS(Ssn) ←SSN_PNOS ÷ SMITH_PNOS
RESULT←πFname, Lname(SSNS * EMPLOYEE)
DIVISION Operation

a tuple t to appear in the result T of the


DIVISION, the values in t must appear in R in
combination with every tuple in S
JOIN Operation

The JOIN operation, denoted by ⋈, is used to combine related tuples
from two relations into single “longer” tuples
● The general form of a JOIN operation on two relations R(A1 , A2,
... , An) and S(B1, B2, ... , Bm)
R ⋈<join condition>S

The JOIN operation can be specified as a CARTESIAN PRODUCT
operation followed by a SELECT operation

In JOIN, only combinations of tuples satisfying the join condition
appear in the result

In the CARTESIAN PRODUCT all combinations of tuples are included
in the result.
JOIN Operation

Suppose that we want to retrieve the name of the manager of each
department.

To get the manager’s name, we need to combine each department
tuple with the employee tuple whose Ssn value matches the Mgr_ssn
value in the department tuple
DEPT_MGR←DEPARTMENT ⋈Mgr_ssn=SsnEMPLOYEE

RESULT←πDname, Lname, Fname(DEPT_MGR)



Mgr_ssn is a foreign key of the DEPARTMENT relation that references
Ssn, the primary key of the EMPLOYEE relation.

This referential integrity constraint plays a role in having matching
tuples in the referenced relation EMPLOYEE.
JOIN Operation
Q19: Retrieve the name of the manager of each department.

DEPT_MGR←DEPARTMENT ⋈Mgr_ssn=SsnEMPLOYEE

RESULT←πDname, Lname, Fname(DEPT_MGR)

SELECT Fname, Lname, Dname


FROM employee join Department ON ssn = mgr_ssn;
Select Dname, Lname,Fname From employee join department on ssn = mgr_ssn;
JOIN Operation
Q20:Retrieve a list of names of each employee’s dependents
EMP_DEPENDENTS ← EMPLOYEE × DEPENDENT
ACTUAL_DEPENDENTS ←σSsn=Essn(EMP_DEPENDENTS)

These two operations can be replaced with a single JOIN operation as
follows:
● ACTUAL_DEPENDENTS←EMPLOYEE ⋈ Ssn=Essn
DEPENDENT

SELECT * FROM employee, Dependent where ssn = Essn;


SELECT * FROM employee join Dependent ON ssn = Essn;
JOIN Operation
THETA JOIN

R ⋈<join condition>S
● each <join condition> is of the form Ai θ Bj, Ai is an attribute of R, Bj
is an attribute of S

θ (theta) is one of the comparison operators {=, <, ≤, >, ≥, ≠}.

A JOIN operation with such a general join condition is called a THETA
JOIN

Tuples whose join attributes are NULL or for which the join
condition is FALSE do not appear in the result
Variations of JOIN : EQUIJION

The most common use of JOIN involves join conditions with
equality comparisons only.

Such a JOIN, where the only comparison operator used is
=, is called an EQUIJOIN

ACTUAL_DEPENDENTS←EMPLOYEE ⋈ Ssn=Essn
DEPENDENT

SELECT * FROM employee join Dependent ON ssn = Essn;


Variations of JOIN: Natural JOIN

Equality join condition specified on these two attributes
requires the values to be identical in every tuple in the result.

one of each pair of attributes with identical values is
superfluous

a new operation called NATURAL JOIN denoted by * was
created to get rid of the second (superfluous) attribute in an
EQUIJOINcondition

The general form of a JOIN operation on two relations R and S
R*S

The standard definition of NATURAL JOIN requires that the
two join attributes have the same name in both relations.

If this is not the case, a renaming operation is applied first.
Variations of JOIN: Natural JOIN
Q21. Join Department table and Dept_locations using Natural Join
RESULT←DEPARTMENT * DEPT_LOCATIONS
SELECT * FROM Department NATURAL JOIN Dept_locations

SELECT * FROM Department d JOIN Dept_locations dl on d.Dnumber = dl.Dnumber


Variations of JOIN: Natural JOIN

Suppose we want to combine each PROJECT tuple with the
DEPARTMENT tuple that controls the project.

In the following example, first we rename the Dnumber attribute
of PROJECT to Dnumber so that it has the same name as
the Dnumber attribute in DEPARTMENT and then we apply
NATURAL JOIN
DEPT←ρ(Dname, Dnumber, Mgr_ssn, Mgr_start_date)(DEPARTMENT)
PROJ_DEPT←PROJECT*DEPT
The attribute Dnumber is called the join attribute for the
NATURAL JOIN operation
Variations of JOIN: Natural JOIN
Q22. List the project number, the controlling department number,
and the department manager’s ssn, and Mgr_start_date.

DEPT←ρ(Pnumber, Dnum, Mgr_ssn, Mgr_start_date)(DEPARTMENT)


PROJ_DEPT←PROJECT*DEPT

ALTER table Project rename column Dnum to Dnumber


SELECT Pnumber, Dnumber, Mgr_ssn, Mgr_start_date
FROM DEPARTMENT Natural Join PROJECT

SELECT Pnumber, Dnumber, Mgr_ssn, Mgr_start_date


FROM DEPARTMENT Join PROJECT using(Dnumber)
Additional Relational Operations

Some common database requests which are needed in
commercial applications for RDBMSs cannot be performed with
the original relational algebra operations.

Additional operations can serve these requests.

These operations enhance the expressive power of the original
relational algebra.
– Generalized Projection
– Aggregate Functions and Grouping
– OUTER JOIN Operations
– Recursive Closure Operations
Generalized Projection

The generalized projection operation extends the projection
operation by allowing functions of attributes to be included in the
projection list.

The generalized form can be expressed as:
● πF1, F2, ..., Fn (R)

where F1, F2, ... , Fn are functions over the attributes in relation R
and may involve arithmetic operations and constant values
Generalized Projection

As an example, consider the relation
EMPLOYEE (Ssn, Salary, Deduction, Years_service)

A report may be required to show
Net Salary = Salary – Deduction,
Bonus = 2000 *Years_service,
Tax = 0.25 *Salary

Then a generalized projection combined with renaming may be
used
● REPORT←ρ(Ssn, Net_salary, Bonus, Tax)(πSsn, Salary – Deduction, 2000 *Years_service, 0.25

*Salary
(EMPLOYEE)
Generalized Projection
REPORT←ρ(Ssn, Net_salary, Bonus, Tax)(πSsn, Salary – Deduction, 2000 *Years_service, 0.25

*Salary
(EMPLOYEE))

Select ssn, salary- Deduction Net_salary, 2000* Years_service


Bonus, 0.25* Salary Tax From EMPLOYEE

Select ssn, 0.25* Salary Tax


From EMPLOYEE
Generalized Projection
Q23: Show the resulting Salaries if every employee working on the
‘ProductX’ project is given 10% raise

Select Fname,Lname,1.1 * Salary new_sal


From EMPLOYEE, works_on, Project
Where ssn =Essn AND Pno = Pnumber AND Pname = 'ProductX'
Aggregate Functions and Grouping

Another type of request that cannot be expressed in the basic
relational algebra is to specify mathematical aggregate functions
on collections of values from the database

Examples of such functions include retrieving the average or total
salary of all employees or the total number of employee tuples

Common functions applied to collections of numeric values include
SUM, AVERAGE, MAXIMUM, and MINIMUM.

The COUNT function is used for counting tuples or values

Another common type of request involves grouping the tuples in a
relation by the value of some of their attributes and then applying
an aggregate function independently to each group.
Aggregate Functions and Grouping

Aggregate functions work on sets and will produce one value as
output on set of values

Aggrigate Function R
Aggregate Functions
Q24: Find the sum of the salaries of all employees, the maximum
salary, the minimum salary, and the average salary.

ℑSUM salary, MAX salary, MIN salary, AVERAGE Salary(EMPLOYEE)

SELECT SUM(Salary), MAX(Salary), MIN(Salary), AVG(Salary)


FROM EMPLOYEE;
Aggregate Functions
Q24: Find the sum of the salaries of all employees, the maximum
salary, the minimum salary, and the average salary.

ρ(Total_sal,Max_sal, Min_sal, Avg_sal)(ℑSUM salary, MAX salary, MIN salary, AVERAGE Salary(EMPLOYEE))

SELECT SUM(Salary) Total_Sal, MAX(Salary) Max_Sal, MIN(Salary)


Min_Sal, AVG(Salary) Avg_Sal FROM EMPLOYEE
Aggregate Functions and Grouping

An example would be to group EMPLOYEE tuples by Dno, so that
each group includes the tuples for employees working in the same
department.

We can then list each Dno value along with, say, the average
salary of employees within the department, or the number of
employees who work in the department.

We can define an AGGREGATE FUNCTION operation, using the
symbol ℑ (pro-nounced script F), to specify these types of
requests
<grouping attributes>
ℑ<function list> (R)

where <grouping attributes> is a list of attributes of the relation
specified in R

<function list> is a list of (<function> <attribute>) pairs
Aggregate Functions and Grouping
Q25: Find the number of employees in the department, and their
average salary

ℑCOUNT ssn, AVERAGE Salary(EMPLOYEE)

SELECT COUNT(ssn), AVG(Salary) FROM EMPLOYEE;


Aggregate Functions and Grouping
Q25A: For each department, retrieve the number of employees in
the department, and their average salary

Dno
ℑCOUNT ssn, AVERAGE Salary(EMPLOYEE)

SELECT Dno, COUNT(ssn), AVG(Salary)


FROM EMPLOYEE GROUP BY Dno;

Grouping attribute(Dno) should be


added in result
Aggregate Functions and Grouping
Q26: Retrieve each department number, the number of employees
in the department, and their average salary, while renaming the
resulting attributes
ρR(Dno, No_of_employees, Average_sal) (DnoℑCOUNT Ssn, AVERAGE Salary (EMPLOYEE))

SELECT Dno, COUNT(ssn) No_of_employees, AVG(Salary) Average_sal


FROM EMPLOYEE
GROUP BY Dno;
Aggregate Functions and Grouping
Q25B: For each department, retrieve the number of employees in
the department, and their average salary gender wise
(Dno,Sex ℑCOUNT Ssn, AVERAGE Salary (EMPLOYEE))

SELECT Dno,Sex, COUNT(ssn) No_of_employees, AVG(Salary) Average_sal


FROM EMPLOYEE
GROUP BY Dno, Sex;
Aggregate Functions and Grouping
Q25B: For each department, retrieve the number of employees in
the department, and their average salary gender wise
(Dno,Sex ℑCOUNT Ssn, AVERAGE Salary (EMPLOYEE))

SELECT Dno,Sex, COUNT(ssn) No_of_employees, AVG(Salary) Average_sal


FROM EMPLOYEE
GROUP BY Dno, Sex;
Aggregate Functions and Grouping
Q27: For each project, retrieve the project number, the project
name, and the number of employees who work on that project

Pnumber, Pname
ℑPnumber, Pname, COUNT Ssn (PROJECT ⋈ Pnumber=Pno
WORKS_ON))

SELECT Pnumber, Pname,COUNT(*)


FROM PROJECT, WORKS_ON
WHERE Pnumber = Pno
GROUP BY Pnumber, Pname;
OUTER JOIN Operations

The JOIN operations described earlier match tuples that satisfy the join
condition.

Tuples without a matching (or related) tuple are eliminated from the JOIN result

Tuples with NULL values in the join attri-butes are also eliminated.

This type of join, where tuples with no match are eliminated, is known as an
inner join

Outer joins, were developed for the case where the user wants to keep all the
tuples in R, or all those in S, or all those in both relations in the result of the
JOIN, regardless of whether or not they have matching tuples in the other
relation.

This satisfies the need of queries in which tuples from two tables are to
be combined by matching corresponding rows, but without losing any tuples
for lack of matching values.
OUTER JOIN: LEFT OUTER JOIN

For example, suppose that we want a list of all employee names as well as the
name of the departments they manage if they happen to manage a
department;

if they do not manage one, we can indicate it with a NULL value.

We can apply an operation LEFT OUTER JOIN, denoted by ⟕ , to retrieve the
result

TEMP← (EMPLOYEE ⟕ Ssn=Mgr_ssn


DEPARTMENT)

RESULT←πFname, Minit, Lname, Dname(TEMP)



The LEFT OUTER JOIN operation keeps every tuple in the first, or left, relation R
in R⟕ S;

if no matching tuple is found in S, then the attributes of S in the join result are
filled or padded with NULL values.
OUTER JOIN: LEFT OUTER JOIN
Q30: Retrive the list of all employee names as well as the name of the
departments they manage if they happen to manage a department;

TEMP← (EMPLOYEE ⟕ Ssn=Mgr_ssn


DEPARTMENT)

RESULT←πFname, Minit, Lname, Dname(TEMP)

SELECT Fname, Mname, Lname, Dname


FROM EMPLOYEE left outer join DEPARTMENT on Ssn=Mgr_ssn
OUTER JOIN: LEFT OUTER JOIN
OUTER JOIN: RIGHT OUTER JOIN

RIGHT OUTER JOIN denoted by ⟖, keeps every tuple in the
second, or right, relation S in the result of R ⟖ S

When no matching tuples are found, padding them with NULL
values as needed
OUTER JOIN: RIGHT OUTER JOIN
Q31: Retrive the list of all Pojects handled by department 5;

σDnum=5( DEPARTMENT ⟖ Dnumber=Dnum


PROJECT)

SELECT * FROM Department RIGHT OUTER JOIN Project


ON Dnumber = Dnum AND Dnum = 5
OUTER JOIN: RIGHT OUTER JOIN
OUTER JOIN: FULL OUTER JOIN

FULL OUTER JOIN, denoted by , keeps all tuples in both the left
and the right relations

When no matching tuples are found, padding them with NULL
values as needed
OUTER JOIN: FULL OUTER JOIN
Q31: Retrive the list of Pojects handled by department 5;

σDnum=5( DEPARTMENT Dnumber=Dnum


PROJECT)

SELECT * FROM Department FULL OUTER JOIN Project


ON Dnumber = Dnum AND Dnum = 5
OUTER JOIN: FULL OUTER JOIN
Recursive Closure Operations

This operation is applied to a recursive relationship between tuples
of the same type, such as the relationship between an employee and
a supervisor.

It relates each employee tuple (in the role of supervisee) to another
employee tuple (in the role of supervisor)

An example of a recursive operation is to retrieve all supervisees of an
employee e at all levels

that is, all employees e′ directly supervised by e, all employees e’’
directly supervised by each employee e′, all employees e′′′ directly
supervised by each employee e′′, and so on.

It is relatively straightforward in the relational algebra to specify all
employees supervised by e at a specific level by joining the
table with itself one or more times. However, it is difficult to specify
all supervisees at all levels
Recursive Closure Operations
Q28: Retive the Ssns of all employees e′ directly supervised at level
one by the employee e whose name is ‘James Borg’
BORG_SSN←πSsn(σFname=‘James’AND Lname=‘Borg’(EMPLOYEE))
SUPERVISION(Ssn1, Ssn2) ←πSsn,Super_ssn(EMPLOYEE)
RESULT1(Ssn) ←πSsn1(SUPERVISION ⋈Ssn2=Ssn BORG_SSN)

To retrieve all employees supervised by Borg at level 2

that is, all employees e′′supervised by some employee e′ who is
directly supervised by Borg we can apply another JOIN to the result
of the first query
RESULT2(Ssn) ←πSsn1(SUPERVISION ⋈Ssn2=Ssn RESULT1)
RESULT←RESULT2∪RESULT (Level1 and Level2)
Recursive Closure Operations
Q28: Retive the Ssns of all employees e′ directly supervised at level
one by the employee e whose name is ‘James Borg’

SELECT S.ssn
FROM (select ssn, super_ssn from employee ) S, (select ssn from
employee where Fname='James' AND Lname='Borg') B
WHERE S.super_ssn = B.ssn
Recursive Closure Operations
Q29: Retrieve all supervisees of a supervisory employee e at all levels

WITH SUP_EMP(SupSsn, EmpSsn) AS


(SELECT Super_Ssn, Ssn FROM EMPLOYEE
UNION ALL
SELECT E.Ssn, S.SupSsn FROM EMPLOYEE E,SUP_EMP S WHERE E.Super_Ssn = S.EmpSsn)
SELECT * FROM SUP_EMP;
OUTER UNION Operation

The OUTER UNION operation was developed to take the union of
tuples from two relations that have some common attributes, but
are not union (type) compatible.

This operation will take the UNION of tuples in two relations R(X,
Y) and S(X, Z) that are partially compatible, meaning that only
some of their attributes, say X, are union compatible.

The attributes that are union compatible are represented only
once in the result, and those attributes that are not union
compatible from either relation are also kept in the result relation
T(X, Y, Z).

It is therefore the same as a FULL OUTER JOIN on the common
attribute
Substring Pattern Matching
Q31. Retrieve all employees whose address is in Houston,
Texas.

SELECT Fname, Lname


FROM EMPLOYEE
WHERE Address LIKE‘%Houston,TX%’;
Complex Queries

Nested Queries
– Non Correlated Subqueries
– Correlated Nested Queries
Substring Pattern Matching
Q32. Find all employees who were born during the 1950s.

SELECT Fname, Lname


FROM EMPLOYEE
WHERE Bdate LIKE ‘_ _ 5 _ _ _ _ _ _ _’;
Simple Retrieval Queries
Q33. Retrieve a list of employees and the projects they are
working on, ordered by department and, within each
department, ordered alphabetically by last name, then first
name.

SELECT D.Dname, E.Lname, E.Fname, P.Pname


FROM DEPARTMENT AS D, EMPLOYEE AS E, WORKS_ON AS W, PROJECT AS P
WHERE D.Dnumber = E.Dno AND E.Ssn = W.Essn AND W.Pno = P.Pnumber
ORDERBY D.Dname, E.Lname, E.Fname

You might also like