EXPERIMENT-6

Use of Inbuilt Functions and Relational Algebra Operations

Database Systems | MTech First Semester | Dr. Mohsin Dar

SOCS | UPES

๐Ÿ“š Objective

To understand the use of inbuilt functions and relational algebra operations with SQL queries using the COMPANY database schema.

๐Ÿ—๏ธ COMPANY Database Schema

EMPLOYEE Table

Column Data Type Constraints Description
SSNCHAR(9)PRIMARY KEYSocial Security Number
FNAMEVARCHAR(15)NOT NULLFirst Name
MINITCHAR(1)Middle Initial
LNAMEVARCHAR(15)NOT NULLLast Name
BDATEDATEBirth Date
ADDRESSVARCHAR(30)Employee Address
SEXCHAR(1)Gender
SALARYDECIMAL(10,2)Employee Salary
SUPER_SSNCHAR(9)FOREIGN KEYSupervisor SSN
DNOINTFOREIGN KEYDepartment Number

DEPARTMENT Table

Column Data Type Constraints Description
DNAMEVARCHAR(15)NOT NULLDepartment Name
DNUMBERINTPRIMARY KEYDepartment Number
MGR_SSNCHAR(9)FOREIGN KEYManager SSN
MGR_START_DATEDATEManager Start Date

PROJECT Table

Column Data Type Constraints Description
PNAMEVARCHAR(15)NOT NULLProject Name
PNUMBERINTPRIMARY KEYProject Number
PLOCATIONVARCHAR(15)Project Location
DNUMINTFOREIGN KEYDepartment Number

WORKS_ON Table

Column Data Type Constraints Description
ESSNCHAR(9)PRIMARY KEY, FOREIGN KEYEmployee SSN
PNOINTPRIMARY KEY, FOREIGN KEYProject Number
HOURSDECIMAL(3,1)Hours Worked

DEPENDENT Table

Column Data Type Constraints Description
ESSNCHAR(9)PRIMARY KEY, FOREIGN KEYEmployee SSN
DEPENDENT_NAMEVARCHAR(15)PRIMARY KEYDependent Name
SEXCHAR(1)Gender
BDATEDATEBirth Date
RELATIONSHIPVARCHAR(8)Relationship to Employee

DEPT_LOCATIONS Table

Column Data Type Constraints Description
DNUMBERINTPRIMARY KEY, FOREIGN KEYDepartment Number
DLOCATIONVARCHAR(15)PRIMARY KEYDepartment Location

๐Ÿ”ง Key Concepts

Relational Algebra Operations

ฯƒ Selection: Selects rows that satisfy a condition

ฯ€ Projection: Selects specific columns

โ‹ˆ Join: Combines tables based on related columns

โˆช Union: Combines results from multiple queries

โˆฉ Intersection: Common rows between two relations

โˆ’ Difference: Rows in first relation but not in second

SQL Inbuilt Functions

Aggregate Functions: COUNT(), SUM(), AVG(), MAX(), MIN()

String Functions: UPPER(), LOWER(), SUBSTRING(), LENGTH()

Date Functions: YEAR(), MONTH(), DAY(), CURRENT_DATE()

Conditional Functions: CASE, COALESCE(), NULLIF()

๐ŸŽฏ Query Solutions

Query 1

Retrieve the names of all employees in department 5 who work more than 10 hours per week on the 'ProductX' project.

Relational Algebra:
ฯ€FNAME,LNAME(ฯƒDNO=5 โˆง HOURS>10 โˆง PNAME='ProductX'(EMPLOYEE โ‹ˆ WORKS_ON โ‹ˆ PROJECT))
SELECT E.FNAME, E.LNAME
FROM EMPLOYEE E
JOIN WORKS_ON W ON E.SSN = W.ESSN
JOIN PROJECT P ON W.PNO = P.PNUMBER
WHERE E.DNO = 5 
  AND W.HOURS > 10 
  AND P.PNAME = 'ProductX';
Explanation:

This query uses multiple JOIN operations to combine three tables. We filter employees by department number 5, hours worked greater than 10, and project name 'ProductX'. The projection operation selects only the first and last names.

Query 2

List the names of all employees who have a dependent with the same first name as themselves.

Relational Algebra:
ฯ€FNAME,LNAME(ฯƒFNAME=DEPENDENT_NAME(EMPLOYEE โ‹ˆSSN=ESSN DEPENDENT))
SELECT E.FNAME, E.LNAME
FROM EMPLOYEE E
JOIN DEPENDENT D ON E.SSN = D.ESSN
WHERE E.FNAME = D.DEPENDENT_NAME;
Explanation:

This query joins the EMPLOYEE and DEPENDENT tables on the SSN/ESSN relationship, then filters where the employee's first name matches their dependent's name. This demonstrates self-referential comparison within joined tables.

Query 3

Find the names of employees who are directly supervised by 'Franklin Wong'.

Relational Algebra:
ฯ€E1.FNAME,E1.LNAME(ฯƒE2.FNAME='Franklin' โˆง E2.LNAME='Wong'(EMPLOYEE E1 โ‹ˆE1.SUPER_SSN=E2.SSN EMPLOYEE E2))
SELECT E1.FNAME, E1.LNAME
FROM EMPLOYEE E1
JOIN EMPLOYEE E2 ON E1.SUPER_SSN = E2.SSN
WHERE E2.FNAME = 'Franklin' 
  AND E2.LNAME = 'Wong';
Explanation:

This is a self-join query where we alias the EMPLOYEE table twice. E1 represents subordinates and E2 represents supervisors. We join on the supervisor relationship and filter for Franklin Wong as the supervisor.

Query 4

Retrieve the names of employees who work on every project.

Relational Algebra:
ฯ€FNAME,LNAME(EMPLOYEE โ‹ˆ (ฯ€ESSN(WORKS_ON) รท ฯ€PNUMBER(PROJECT)))
SELECT E.FNAME, E.LNAME
FROM EMPLOYEE E
WHERE NOT EXISTS (
    SELECT P.PNUMBER
    FROM PROJECT P
    WHERE NOT EXISTS (
        SELECT W.PNO
        FROM WORKS_ON W
        WHERE W.ESSN = E.SSN 
          AND W.PNO = P.PNUMBER
    )
);
Explanation:

This query uses the division operation concept implemented with double negation (NOT EXISTS). It finds employees for whom there doesn't exist any project that they don't work on - meaning they work on all projects.

Query 5

Retrieve the names of employees who do not work on any project.

Relational Algebra:
ฯ€FNAME,LNAME(EMPLOYEE โˆ’ ฯ€FNAME,LNAME(EMPLOYEE โ‹ˆSSN=ESSN WORKS_ON))
SELECT E.FNAME, E.LNAME
FROM EMPLOYEE E
WHERE E.SSN NOT IN (
    SELECT DISTINCT W.ESSN
    FROM WORKS_ON W
    WHERE W.ESSN IS NOT NULL
);
Explanation:

This query uses the difference operation to find employees who are not in the WORKS_ON table. The NOT IN clause excludes employees who have any project assignments.

Query 6

Retrieve the names and addresses of all employees who work on at least one project located in Houston but whose department has no location in Houston.

Relational Algebra:
ฯ€FNAME,LNAME,ADDRESS((EMPLOYEE โ‹ˆ WORKS_ON โ‹ˆ ฯƒPLOCATION='Houston'(PROJECT)) โˆ’ (EMPLOYEE โ‹ˆ ฯƒDLOCATION='Houston'(DEPT_LOCATIONS)))
SELECT DISTINCT E.FNAME, E.LNAME, E.ADDRESS
FROM EMPLOYEE E
JOIN WORKS_ON W ON E.SSN = W.ESSN
JOIN PROJECT P ON W.PNO = P.PNUMBER
WHERE P.PLOCATION = 'Houston'
  AND E.DNO NOT IN (
      SELECT DL.DNUMBER
      FROM DEPT_LOCATIONS DL
      WHERE DL.DLOCATION = 'Houston'
  );
Explanation:

This complex query finds employees who work on Houston projects but their department isn't located in Houston. It combines positive selection (works on Houston projects) with negative selection (department not in Houston).

Query 7

Retrieve the last names of all department managers who have no dependents.

Relational Algebra:
ฯ€LNAME(ฯƒSSN=MGR_SSN(EMPLOYEE โ‹ˆ DEPARTMENT) โˆ’ ฯ€LNAME(ฯƒSSN=ESSN(EMPLOYEE โ‹ˆ DEPENDENT)))
SELECT E.LNAME
FROM EMPLOYEE E
JOIN DEPARTMENT D ON E.SSN = D.MGR_SSN
WHERE E.SSN NOT IN (
    SELECT DISTINCT DEP.ESSN
    FROM DEPENDENT DEP
    WHERE DEP.ESSN IS NOT NULL
);
Explanation:

This query identifies department managers (by joining EMPLOYEE and DEPARTMENT on manager SSN) who are not in the DEPENDENT table as employees with dependents. It combines role-based selection with exclusion logic.

๐Ÿ“Š Sample Data and Expected Results

Note: The following sample data demonstrates the structure and relationships in the COMPANY database. Actual results will depend on the specific data in your database instance.

Sample EMPLOYEE Data

SSN FNAME LNAME ADDRESS DNO SUPER_SSN
123456789JohnSmith731 Fondren, Houston TX5333445555
333445555FranklinWong638 Voss, Houston TX5888665555
999887777AliciaZelaya3321 Castle, Spring TX4987654321
987654321JenniferWallace291 Berry, Bellaire TX4888665555

๐Ÿ’ก Advanced Topics

Query Optimization Tips:
  • Use appropriate indexes on frequently joined columns
  • Consider query execution plans for complex operations
  • Use EXIST instead of IN for better performance with NULLs
  • Avoid unnecessary DISTINCT operations
Common Pitfalls:
  • NULL values in NOT IN clauses can produce unexpected results
  • Self-joins require careful aliasing to avoid confusion
  • Division queries (universal quantification) are complex and require double negation
  • Always consider cardinality when using JOINs

๐Ÿ“ Practice Exercises

Additional Practice Problems:

  1. Find employees who work on more than 3 projects
  2. List departments with more than 5 employees
  3. Retrieve projects with no assigned employees
  4. Find the highest paid employee in each department
  5. List employees who have the same supervisor as 'John Smith'

๐ŸŽฏ Learning Outcomes

After completing this lab, students should be able to:

  • Understand and apply fundamental relational algebra operations
  • Write complex SQL queries involving multiple tables and conditions
  • Use various types of JOIN operations effectively
  • Implement universal quantification using double negation
  • Handle NULL values appropriately in queries
  • Optimize query performance through proper indexing and query structure