Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 COMPANY schema EMPLOYEE DEPARTMENT DEPT_LOCATIONS PROJECT WORKS_ON DEPENDENT FNAME MINIT LNAME SSN BDATE ADDRESS SEX SALARY SUPERSSN DNO DNAME DNUMBER.

Similar presentations


Presentation on theme: "1 COMPANY schema EMPLOYEE DEPARTMENT DEPT_LOCATIONS PROJECT WORKS_ON DEPENDENT FNAME MINIT LNAME SSN BDATE ADDRESS SEX SALARY SUPERSSN DNO DNAME DNUMBER."— Presentation transcript:

1 1 COMPANY schema EMPLOYEE DEPARTMENT DEPT_LOCATIONS PROJECT WORKS_ON DEPENDENT FNAME MINIT LNAME SSN BDATE ADDRESS SEX SALARY SUPERSSN DNO DNAME DNUMBER MGRSSN MGRSTARTDATE DNUMBER DLOCATION PNAME PNAME PLOCATION DNUM ESSN PNO HOURS ESSN DEPENDENT_NAME SEX BDATE RELATIONSHIP

2 2 Examples Retrieve the birthdate and address of the employee whose name is ‘John B. Smith’.  SELECTBDATE, ADDRESS FROM EMPLOYEE WHEREFNAME=‘John’ AND MINIT=‘B’ AND LNAME=‘Smith’ Retrieve the name and address of all employees who work for the ‘Research’ department.  SELECTFNAME, LNAME, ADDRESS FROMEMPLOYEE, DEPARTMENT WHEREDNAME=‘Research’ AND DNUMBER=DNO For every project located in ‘Stafford’, list the project number, the controlling department number, and the department manager’s last name, address, and birthdate.  SELECTPNUMBER, DNUM, LNAME, ADDRESS, BDATE FROMPROJECT, DEPARTMENT, EMPLOYEE WHEREDNUM=DNUMBER AND MGRSSN=SSN AND PLOCATION=‘Stafford’

3 3 For each employee, retrieve the employee’s first and last name and the first and last name of his or her immediate supervisor.  SELECTE.FNAME, E.LNAME, S.FNAME, S.LNAME FROMEMPLOYEE E, EMPLOYEE S WHEREE.SUPERSSN=S.SSN Select all EMPLOYEE SSNs, and all combinations of EMPLOYEE SSN and DEPARTMENT DNAME in the database  SELECTSSN FROMEMPLOYEE SELECTSSN, DNAME FROMEMPLOYEE, DEPARTMENT Retrieve the salary of every employee  SELECTSALARY(or SELECT DISTINCT SALARY) FROMEMPLOYEE

4 4 Make a list of all 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.  (SELECTPNUMBER FROMPROJECT, DEPARTMENT, EMPLOYEE WHEREDNUM=DNUMBER AND MGRSSN=SSN AND LNAME=‘Smith’) UNION (SELECTPNUMBER FROMPROJECT, WORKS_ON, EMPLOYEE WHEREPNUMBER=PNO AND ESSN=SSN AND LNAME=‘Smith’)  SELECTDISTINCT PNUMBER FROMPROJECT WHEREPNUMBER IN (SELECT PNUMBER FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND MGRSSN=SSN AND LNAME=‘Smith’) OR PNUMBER IN (SELECT PNO FROM WORKS_ON, EMPLOYEE WHERE ESSN=SSN AND LNAME=‘Smith’)

5 5 Find the names of employees whose salary is greater than the salary of all the employees in department 5.  SELECTLNAME, FNAME FROMEMPLOYEE WHERESALARY > ALL (SELECT SALARY FROM EMPLOYEE WHERE DNO=5) Retrieve the name of each employee who has a dependent with the same first name and same sex as the employee.  SELECTE.FNAME, E.LNAME FROMEMPLOYEE E WHEREE.SSN IN (SELECT ESSN FROM DEPENDENT WHERE ESSN=E.SSN AND E.FNAME=DEPENDENT_NAME AND SEX=E.SEX)

6 6 Retrieve the name of each employee who works on all the projects controlled by department number 5.  SELECTFNAME, LNAME FROMEMPLOYEE WHERE((SELECT PNO FROM WORKS_ON WHERESSN=ESSN) CONTAINS (SELECT PNUMBER FROM PROJECT WHERE DNUM=5))  Note:  The original SQL implementation on SYSTEM R had a CONTAINS operator. It was subsequently dropped from the language because of the difficulty in implementing it efficiently.  Most commercial implementations of SQL do not have CONTAINS operator.

7 7 Retrieve the names of employees who have no dependents.  SELECTFNAME, LNAME FROMEMPLOYEE WHERENOT EXISTS (SELECT * FROM DEPENDENT WHERE SSN=ESSN) List the names of managers who have at least one dependent.  SELECTFNAME, LNAME FROMEMPLOYEE WHEREEXISTS (SELECT * FROM DEPENDENT WHERE SSN=ESSN) AND EXISTS (SELECT * FROM DEPARTMENT WHERE SSN=MGRSSN)

8 8 Retrieve the social security numbers of all employees who work on project number 1, 2, or 3.  SELECTDISTINCT ESSN FROMWORKS_ON WHEREPNO IN (1,2,3) Retrieve the names of all employees who do not have supervisors.  SELECT FNAME, LNAME FROMEMPLOYEE WHERESUPERSSN IS NULL Find the sum of the salaries of all employees, the maximum salary, the minimum salary, and the average salary.  SELECTSUM(SALARY), MAX(SALARY), MIN(SALARY), AVG(SALARY) FROMEMPLOYEE

9 9 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) FROMEMPLOYEE, DEPARTMENT WHEREDNO=DNUMBER AND DNAME=‘Research’ Retrieve the total number of employees in the company and the number of employees in the ‘Research’ department.  SELECT COUNT(*) FROMEMPLOYEE SELECTCOUNT(*) FROMEMPLOYEE, DEPARTMENT WHEREDNO=DNUMBER AND DNAME=‘Research’ Count the number of distinct salary values in the database.  SELECTCOUNT(DISTINCT SALARY) FROMEMPLOYEE

10 10 For each department, retrieve the department number, the number of employees in the department, and their average salary.  SELECTDNO, COUNT(*), AVG(SALARY) FROMEMPLOYEE GROUP BY DNO For each project, retrieve the project number, the project name, and the number of employees who work on that project.  SELECTPNUMBER, PNAME, COUNT(*) FROMPROJECT, WORKS_ON WHEREPNUMBER=PNO GROUP BY PNUMBER, PNAME 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.  SELECTPNUMBER, PNAME, COUNT(*) FROMPROJECT, WORKS_ON WHEREPNUMBER=PNO GROUP BY PNUMBER, PNAME HAVINGCOUNT(*) > 2

11 11 For each project, retrieve the project number, the project name, and the number of employees from department 5 who work on the project.  SELECTPNUMBER, PNAME, COUNT(*) FROM PROJECT, WORKS_ON, EMPLOYEE WHEREPNUMBER=PNO AND SSN=ESSN AND DNO=5 GROUP BY PNUMBER, PNAME For each department having more than five employees, retrieve the department number and the number of employees making more than $40,000.  SELECTDNAME, COUNT(*) FROMDEPARTMENT, EMPLOYEE WHEREDNUMBER=DNO AND SALARY>40000 AND DNO IN (SELECT DNO FROMEMPLOYEE GROUP BY DNO HAVINGCOUNT(*) > 5) GROUP BY DNAME

12 12 Show the resulting salaries if every employee working on the ‘ProductX’ project is given a 10% raise.  SELECTFNAME, LNAME, 1.1*SALARY FROMEMPLOYEE, WORKS_ON, PROJECT WHERESSN=ESSN AND PNO=PNUMBER AND PNAME=‘ProductX’ Retrieve a list of employees and the projects they are working on, ordered by department and, within each department, alphabetically by last name, first name.  SELECTDNAME, LNAME, FNAME, PNAME FROMDEPARTMENT, EMPLOYEE, WORKS_ON, PROJECT WHEREDNUMBER=DNO AND SSN=ESSN AND PNO=PNUMBER ORDERED BY DNAME, LNAME, FNAME


Download ppt "1 COMPANY schema EMPLOYEE DEPARTMENT DEPT_LOCATIONS PROJECT WORKS_ON DEPENDENT FNAME MINIT LNAME SSN BDATE ADDRESS SEX SALARY SUPERSSN DNO DNAME DNUMBER."

Similar presentations


Ads by Google