Relational schema for SQL queries EMPLOYEE (FNAME, MINIT ...
Relational schema for SQL queries EMPLOYEE (FNAME, MINIT, LNAME, SSN, BDATE, ADDRESS, SEX, SALARY, #SUPERSSN, #DNO) DEPARTMENT (DNAME, DNUMBER, #MGRSSN, MGRSTARTDATE) DEPT_LOCATIONS (#DNUMBER, DLOCATION) PROJECT (PNAME, PNUMBER, PLOCATION, #DNUM) WORKS_ON (#ESSN, #PNO, HOURS) DEPENDENT (#ESSN, DEPENDENT_NAME, SEX, BDATE, RELATIONSHIP)
Jane Reid, BSc/IT DB, QMUL, 28/1/02. Page 1.
SQL queries
Query 0
Retrieve the birthdate and address of the employee(s) whose name is `John B Smith'
SELECT FROM WHERE
BDATE, ADDRESS EMPLOYEE FNAME = `John' AND MINIT = `B' AND LNAME = `Smith';
Query 1
Retrieve the name and address of all employees who work for the `Research' department
SELECT FROM WHERE
FNAME, LNAME, ADDRESS EMPLOYEE, DEPARTMENT DNAME = `Research' AND DNUMBER = DNO;
Query 1A
Ambiguous attribute names
SELECT FROM WHERE
FNAME, EMPLOYEE.NAME, ADDRESS EMPLOYEE, DEPARTMENT DEPARTMENT.NAME = `Research' AND DEPARTMENT.DNUMBER = EMPLOYEE.DNUMBER;
Query 1B
Aliasing
SELECT FROM WHERE
E.FNAME, E.NAME, E.ADDRESS EMPLOYEE E, DEPARTMENT D D.NAME = `Research' AND D.DNUMBER = E.DNUMBER;
Query 1C
Retrieve all the attribute values of EMPLOYEE tuples who work in department number 5
SELECT FROM WHERE
* EMPLOYEE DNO = 5;
Query 1D
Retrieve all the attributes of an EMPLOYEE and the attributes of the DEPARTMENT he or she works in for every employee of the `Research' department
SELECT FROM WHERE
* EMPLOYEE, DEPARTMENT DNAME = `Research' AND DNO = DNUMBER;
Query 2 For every project located in `Stafford', list the project number, the controlling department number and the department manager's last name, address and birthdate
SELECT FROM WHERE
PNUMBER, DNUM, LNAME, ADDRESS, BDATE PROJECT, DEPARTMENT, EMPLOYEE DNUM = DNUMBER AND MGRSSN = SSN AND PLOCATION = `Stafford';
Jane Reid, BSc/IT DB, QMUL, 28/1/02. Page 2.
Query 3
Retrieve the name of each employee who works on all the projects controlled by department number 5
SELECT FROM WHERE
FNAME, LNAME
EMPLOYEE
(
(SELECT
FROM
WHERE
PNO WORKS_ON SSN = ESSN)
CONTAINS
(SELECT FROM WHERE
PNUMBER PROJECT DNUM = 5) );
Query 3A
Reformulation of query 3.
SELECT FROM WHERE
FNAME, LNAME
EMPLOYEE
NOT EXISTS
(
(SELECT
FROM
WHERE
EXCEPT
(SELECT FROM WHERE
PNUMBER PROJECT DNUM = 5)
PNO WORKS_ON SSN = ESSN) );
Query 3B
Reformulation of query 3 using two-level nesting
SELECT FROM WHERE
LNAME, FNAME
EMPLOYEE
NOT EXISTS
(SELECT
*
FROM
WORKS_ON B
WHERE
B.PNO IN
(SELECT
FROM
WHERE
AND
NOT EXISTS (SELECT FROM WHERE
PNUMBER PROJECT DNUM = 5) )
* WORKS_ON C C.ESSN = SSN AND C.PNO = B.PNO));
Jane Reid, BSc/IT DB, QMUL, 28/1/02. Page 3.
Query 4
Make a list of 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 FROM WHERE
UNION
(SELECT FROM WHERE
DISTINCT PNUMBER PROJECT, DEPARTMENT, EMPLOYEE DNUM = DNUMBER AND MGRSSN = SSN AND LNAME = `Smith')
DISTINCT PNUMBER PROJECT, WORKS_ON, EMPLOYEE PNUMBER = PNO AND ESSN = SSN AND LNAME = `Smith');
Query 4A Reformulation of query 4 using nested queries
SELECT FROM WHERE
DISTINCT PNUMBER PROJECT PNUMBER IN (SELECT
FROM WHERE
OR
PNUMBER IN (SELECT FROM WHERE
PNUMBER PROJECT, DEPARTMENT, EMPLOYEE DNUM = DNUMBER AND MGRSSN = SSN AND LNAME = `Smith')
PNO WORKS_ON, EMPLOYEE ESSN = SSN AND LNAME = `Smith');
Query 5
Retrieve the names of all employees who have two or more dependents
SELECT FROM WHERE
LNAME, FNAME
EMPLOYEE
(SELECT
COUNT (*)
FROM
DEPENDENT
WHERE
SSN = ESSN) >= 2;
Query 6
Retrieve the names of employees who have no dependents
SELECT FROM WHERE
FNAME, LNAME EMPLOYEE NOT EXISTS
(SELECT FROM WHERE
* DEPENDENT SSN = ESSN);
Jane Reid, BSc/IT DB, QMUL, 28/1/02. Page 4.
Query 7
List the names of managers who have at least one dependent
SELECT FROM WHERE
FNAME, LNAME
EMPLOYEE
EXISTS
(SELECT
FROM
WHERE
* DEPENDENT SSN = ESSN)
AND
EXISTS
(SELECT FROM WHERE
* DEPARTMENT SSN = MGRSSN);
Query 8
For each employee, retrieve the employee's first and last name, and the first and last name of his or her immediate supervisor.
SELECT FROM WHERE
E.FNAME, E.LNAME, S.FNAME, S.LNAME EMPLOYEE AS E, EMPLOYEE AS S E.SUPERSSN = S.SSN;
Query 8A
Reformulation of query 8 to retrieve the last name of each employee and his or her supervisor, while renaming the resulting attribute names as EMPLOYEE_NAME and SUPERVISOR_NAME
SELECT FROM WHERE
E.LNAME AS EMPLOYEE_NAME, S.LNAME AS SUPERVISOR_NAME EMPLOYEE AS E, EMPLOYEE AS S E.SUPERSSN = S.SSN;
Query 9
Select all EMPLOYEE SSNs in the database
SELECT FROM
SSN EMPLOYEE;
Query 10
Select all combination of EMPLOYEE SSN and DEPARTMENT DNAME in the database
SELECT FROM
SSN, DNAME EMPLOYEE, DEPARTMENT;
Query 10A
Select the CROSS PRODUCT of the EMPLOYEE and DEPARTMENT relations
SELECT FROM
* EMPLOYEE, DEPARTMENT;
Query 11
Retrieve the salary of every employee
SELECT ALL SALARY
FROM
EMPLOYEE;
Jane Reid, BSc/IT DB, QMUL, 28/1/02. Page 5.
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- connection string for sql using encryption
- sql information schema views
- sql information schema table
- information schema sql query
- writing sql queries for dummies
- set default schema sql server
- schema browser in sql developer
- oracle sql schema query
- sql table schema query
- microsoft command line utilities for sql server
- powershell module for sql server
- sql get schema of table