Docsity

Relational Calculus and SQL Query Examples for Employee Data - Prof. Yicheng Tu, Study notes of Deductive Database Systems

Toggle sidebar
bg1
Pure language
Define a new relation whose features are defined over some variables.
Variables are tuples in this format:
{ t | COND(t) }
Any tuple that satisfy this condition will be a member of the result.
{t | Employee(t) AND t.salary > 50K}
Question: Find employees whose salaries are more than 50K.
{ t.Fname, t.SSN | Employee(t) AND t.salary > 50K}
{t.Fname, t.address | Employee(t) AND Эd, Department(d) AND d.Dname = 'Research' AND
t.Dno = d.Dnumber}
Question 2: Find the Fnames and address of all employees who work for the Research
department.
SQL = a real-world t.r.c
○
Tuple R.C
1.
Variables are defined as columns
{x1x2 ...xn | COND (x1, … xn, xn+1, …xm)}
Generic Format:
{Z5, Z6| Э(Z1), Э(Z2), Э(Z3), Э(Z4), Э( …..ЭZ10), Employee (Z1, Z2, Z3, Z4 …. Z10) AND Z1= 'John'}
Question 3: Find the b_date and address of employees whom Fname is 'John'
Query -By- Example is an implementation of d.r.c.
○
Domain r.c.
2.
SQL is an implementation of the tuple relation calculus.
Employees(Eid, ename, age, salary)
Works (eid, did, pct_time)
Departments (did, location, manager_id)
∏E.name [σE.age < 45 ^ E.salary >= 80K ^ E.salary >= 100K (E)]
Find the names of all employees who are younger than 45 and with salaries in the range [80000,
100000].
a.
Find the 10s and the pct_time of the department John Doe works.
b.
∏e.did, pct_time [σename = 'John Doe' E W]
∏mdid, pct_time [σename = 'John Doe' E W D]
Find manager_id and the pct_time of the department John Doe works.
c.
∏ename, age{ [σpct_time = 100(W)] E}
d.
Find the names of employees who works for a Brooklyn dept. and a Manhattan dept.
e.
Relational Algebra Examples:
Existential mark
Relational Calculus
Tuesday, September 16, 2008
5:00 PM
Databas e Systems Page 1
pf2

Partial preview of the text

Download Relational Calculus and SQL Query Examples for Employee Data - Prof. Yicheng Tu and more Study notes Deductive Database Systems in PDF only on Docsity!

Pure language Define a new relation whose features are defined over some variables. Variables are tuples in this format: { t | COND(t) } Any tuple that satisfy this condition will be a member of the result. {t | Employee(t) AND t.salary > 50K} Question: Find employees whose salaries are more than 50K. { t.Fname, t.SSN | Employee(t) AND t.salary > 50K} Question 1: Find the names and SSN of the employees whose salaries are more than 50K. {t.Fname, t.address | Employee(t) AND Э d, Department(d) AND d.Dname = 'Research' AND t.Dno = d.Dnumber} Question 2: Find the Fnames and address of all employees who work for the Research department. ○ SQL = a real-world t.r.c

  1. Tuple R.C Variables are defined as columns {x 1 x2 ...xn | COND (x 1 , (^) … xn, xn+1, …xm)} Generic Format: {Z5, Z 6 | Э (Z 1 ), Э (Z 2 ), Э (Z 3 ), Э (Z 4 ), Э ( …..ЭZ 10 ), Employee(Z1, Z2, Z3, Z4 …. Z 10 ) AND Z 1 = 'John'} Question 3: Find the b_date and address of employees whom Fname is 'John' ○ Query - By- Example is an implementation of d.r.c.
  2. Domain r.c. SQL is an implementation of the tuple relation calculus. Employees(Eid, ename, age, salary) Works (eid, did, pct_time) Departments (did, location, manager_id)

∏E.name[σE.age < 45 ^ E.salary >= 80K ^ E.salary >= 100K (E)]

Find the names of all employees who are younger than 45 and with salaries in the range [80000, 100000]. a. b. Find the 10s and the pct_time of the department John Doe works.

∏e.did, pct_time [σename = 'John Doe' E W]

∏mdid, pct_time [σename = 'John Doe' E W D]

c. Find manager_id and the pct_time of the department John Doe works.

∏ename, age{ [σpct_time = 100 (W)] E}

d. Find the names and age of all employees who work full time in one position(One department). e. Find the names of employees who works for a Brooklyn dept. and a Manhattan dept. Relational Algebra Examples: Existential mark

Relational Calculus

Tuesday, September 16, 2008 5:00 PM Database Systems Page 1

∏ename [σD.location = 'Brooklin'] (E W D)] П

∏ename [σD.location = 'Manhattan'] (E W D)]

Note: When there is an AND in relational algebra we can use an intersection e. Find the names of employees who works for a Brooklyn dept. and a Manhattan dept.

age 9 avg(salary)(E)

f. Find the average salaries of employees with the same age, print out both age and average salary.

∏ename , did (E W) ÷ ∏did [σlocation = 'Brooklin' (D)]

r s r = s x y g. Find the names of all employees who work for all depts. Located in Brooklyn. X = {enames} Y = {did} Hint: Manager is also a person who shows up in the Employee table. h. Find the names of all employees who earn more than his/her manager. For natural joins just draw the symbol with no condition y x Database Systems Page 2