Dbms 6
Dbms 6
2. FROM Employee, Department: Indicates the query will fetch data from two tables:
Employee and Department.
3. WHERE DName='Research': Filters the results to include only rows where the department
name (DName) is "Research".
4. AND DNumber=DNo: Ensures that only rows where the department number (DNumber from
Department) matches the department number (DNo in Employee) are included. This creates a
link between the Employee and Department tables.
In short,
The query retrieves the first name, last name, and address of all employees who work in the
"Research" department by joining the Employee and Department tables based on their
department number.
- Query 2: For every project located in ‘Naivasha', list the project number, the controlling
department number, and the department manager's last name, address, and birthdate..
3. WHERE DNum=DNumber:
Links the Project table to the Department table by matching the department number (DNum in
Project and DNumber in Department).
4. AND MgrSSN=SSN:
Links the Department table to the Employee table by matching the manager's SSN (MgrSSN in
Department and SSN in Employee).
5. AND PLOCATION='Naivasha':
Filters the results to include only projects where the project location (PLocation) is "Naivasha".
In short
The query retrieves the project number, department number, and the last name, birth date, and
address of the manager for all projects located in "Naivasha". It connects the Project,
Department, and Employee tables using the department number and manager's SSN.
```sql
SELECT E.Fname, E.LName, S.FName, S.Lname
FROM Employee AS E, Employee AS S
WHERE E.SuperSSN=S.SSN;
```
- Empty WHERE-clause: Selects all tuples.
```sql
SELECT SSN
FROM EMPLOYEE;
```
- Use of : Retrieve all attribute values.
```sql
SELECT FROM EMPLOYEE WHERE DNO=5;
```
- DISTINCT: Eliminates duplicate tuples.
```sql
SELECT DISTINCT SALARY FROM EMPLOYEE;
```
Set Operations
- Union, Minus, Intersect: Operate on union-compatible relations.
- Example: List project numbers involving 'Omondi'.
```sql
(SELECT PName
FROM Project, Department, Employee
WHERE DNUM=DNumber AND MGRSSN=SSN AND LNAME='Omondi')
UNION
(SELECT PName
FROM Project, Works_On, Employee
WHERE PNumber=PNo AND ESSN=SSN AND LName='Omondi');
```
Nesting of Queries
- Example: Retrieve name and address of employees in 'Research' department using nested
query.
```sql
SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE
WHERE DNO IN
(SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME='Research');
```
Explicit Sets
- Example: Retrieve SSNs of employees working on projects 1, 2, or 3.
```sql
SELECT DISTINCT ESSN FROM WORKS_ON WHERE PNO IN (1, 2, 3);
```
Substring Comparison
- LIKE operator: Used for partial string comparison.
```sql
SELECT FName, LName, Address
FROM Employee
WHERE Address LIKE '%Nairobi%';
```
Arithmetic Operations
- Example: Give 10% raise to employees on 'ProductX' project.
```sql
SELECT FNAME, LNAME, 1.1SALARY
FROM EMPLOYEE, WORKS_ON, PROJECT
WHERE SSN=ESSN AND PNO=PNUMBER AND PNAME='ProductX';
```
Aggregate Functions
- COUNT, SUM, MAX, MIN, AVG: Used for aggregate calculations.
```sql
SELECT SUM(Salary), MAX(Salary), MIN(Salary), AVG(Salary)
FROM EMPLOYEE;
```
Grouping
- GROUP BY clause: Groups tuples for aggregate functions.
```sql
SELECT DNO, COUNT(), AVG(SALARY)
FROM EMPLOYEE
GROUP BY DNO;
```