Complex Queries
Complex Queries
Company Database
Nested Queries
Retrieve project numbers of projects that have an employee with last name ‘Smith’
involved as manager or involved as a worker.
OR
The first nested query selects the project numbers of projects that have an employee with last
name ‘Smith’ involved as manager, and whereas the second nested query selects the project
numbers of projects that have an employee with last name ‘Smith’ involved as worker.
The OR logical connective is used to retrieve a PROJECT tuple if the PNUMBER value of
that tuple is in the result of either nested query.
Retrieve the name of each employee who has a dependent with the same first name and
is the same sex as the employee.
Retrieve the name of each employee who has a dependent with the same first name and
is the same sex as the employee.
AND
1. COUNT
2. SUM
3. MAX
4. MIN
5. AVG
The 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.
The functions MAX and MIN can also be used with attributes that have nonnumeric
domains if the domain values have a total ordering among one another.
Find the sum of the salaries of all employees, the maximum salary, the minimum salary,
and the average salary.
SELECT SUM (Salary), MAX (Salary), MIN (Salary), AVG (Salary) FROM EMPLOYEE;
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.
SELECT SUM (Salary), MAX (Salary), MIN (Salary), AVG (Salary) FROM (EMPLOYEE
JOIN DEPARTMENT ON Dno = Dnumber) WHERE Dname = ‘Research’;
Retrieve the names of all employees who have two or more dependents.
SELECT Lname, Fname FROM EMPLOYEE WHERE ( SELECT COUNT (*) FROM
DEPENDENT WHERE Ssn = Essn ) > = 2;
SELECT Dno, COUNT (*), AVG (Salary) FROM EMPLOYEE GROUP BY Dno;
For each project, retrieve the project number, the project name, and the number of
employees who work on that project.
SELECT Pnumber, Pname, COUNT (*) FROM PROJECT, WORKS_ON WHERE Pnumber
= Pno GROUP BY Pnumber, Pname;
For each department that has more than five employees, retrieve the department
number and the number of its employees who are making more than 50,000.
SELECT Dno, COUNT (*) FROM EMPLOYEE WHERE Salary>50000 AND Dno IN (
SELECT Dno FROM EMPLOYEE GROUP BY Dno HAVING COUNT (*) > 5) GROUP
BY Dno;
For each project, retrieve the project number, the project name, and the number of
employees from department 5 who work on the project.
For each project on which more than two employees work, retrieve the project number,
the project name, and the number of employees who work on the project.
SELECT Pnumber, Pname, COUNT (*) FROM PROJECT, WORKS_ON WHERE Pnumber
= Pno GROUP BY Pnumber, Pname HAVING COUNT (*) > 2;
Source
Text book