Assignment # 2 Database Systems Due Date: Tuesday, 11 November 2008 Roll No Name Signature
PART 1 THE SAMPLE TABLES EMP EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO 7369 SALMAN CLERK 7902 17 DEC 2000 8000 20 7499 ALIA SALESMAN 7698 20 FEB 2001 16000 3000 30 7521 WAHEED SALESMAN 7698 22 FEB 2001 12500 5000 30 7566 JAMIL MANAGER 7839 02 APR 2001 29750 20 7654 MEHVISH SALESMAN 7698 28 SEP 2001 12000 12500 30 7698 BILAL MANAGER 7839 01 MAY 2001 28500 30 7782 QASIM MANAGER 7839 09 JUN 2001 24500 10 7788 SHAMS ANALYST 7566 09 DEC 2002 30000 20 7839 KAMRAN PRESIDENT 17 NOV 2001 50000 7844 TANVEER SALESMAN 7698 08 SEP 2001 15000 0 30 7876 AFTAB CLERK 7788 12 JAN 2003 11000 20 7900 JAVERIA CLERK 7698 03 DEC 2001 9500 30 7902 FOZIA ANALYST 7566 03 DEC 2001 30000 20 7934 MURAD CLERK 7782 23 JAN 2002 13000 10 DEPT DEPTNO DNAME LOC 10 ACCOUNTING ISLAMABAD 20 RESEARCH KARACHI 30 SALES LAHORE 40 OPERATIONS QUETTA SALGRADE GRADE LOSAL HISAL 1 7000 12000 2 12010 14000 3 14010 20000 4 20010 30000 5 30010 99990
EXERCISES 1 Predict what results will be obtained by joining the EMP and DEPT tables without specifying any selection criteria, then try it out. How many rows does the result produce? 2 Repeat B1 but this time joining EMP to itself. Look carefully at the output to make sure you understand it. (Ensure that you do have results table and not an error message!! 3 Find the name and salary of employees in Karachi. 4 Produce a list that will show the salary grade each employee is on. Display the name, job, salary and grade with the output in ascending order of salary, with those on the same salary ordered alphabetically. 5 Display employee name, job, salary and department name for those on grade 3. 6 Display details of employees in ACCOUNTING. (Don t answer this by finding who is in department number 10.) 7 Show the name, employee number and manager's name of those who are managed by either Bilal or Jamil. 8 Display the employee name, location and department name of those whose salary is greater than 15000. 9 What are the lowest and highest basic salaries within the company? 10 How many people have a salary greater than 20000? 11 How many people are there in ACCOUNTING? Don t work out how many people there are in department 10! NULL Values If a column has no value for any particular row, it is referred to as a NULL value. This is not the same as a zero in a numeric column, or a space in a character column. In MS SQL Server, the IsNull function can be used to convert a null into a specified value e.g. IsNull(COMM, 0) will return the actual value of COMM if it has a value, or a zero if COMM is null. This is often necessary when performing calculations, or formatting for output since null values will be totally ignored by many functions and operations. A column can be tested using IS NULL or IS NOT NULL e.g. SELECT...FROM... WHERE column_name IS NOT NULL; 12 What are the highest and lowest incomes (i.e. to include commission) in the Sales Department?
GROUPING DATA The GROUP BY clause splits the table into specified groups, returning one summary row for each group which is then used in the SELECT clause. In each of the following queries, you may treat the President as not an employee (as he isn t given a deptno). However, here s a question for the sharper ones: how your queries change if you did need to include him as an employee? 13 How many people are there in each department? 14 How many people are there in each type of job in each department? 15 For each department, find the average salary and the total salary bill excluding commission. 16 Find the maximum commission earned, and the number of people in each department. THE HAVING CLAUSE. The HAVING clause is used to specify which GROUPS are to be displayed. WHERE restricts which rows a SELECT works on and HAVING restricts which groups. 17 Display the department name and number of employees in departments with fewer than 6 employees. 18 Display the names of all the people in a management position, their department name and the number of staff for whom they have direct responsibility. (This is not simply asking for departmental managers.)
PART 2 1. Draw an E R diagram for each of the following situations (if you believe that you need to make additional assumptions, clearly state them for each situation): a. A company has a number of employees. The attributes of EMPLOYEE include Employee_ID (identifier), Name, Address, and Birthdate. The company also has several projects. Attributes of PROJECT include Project_ID (identifier), Project_Name, and Start_Date. Each employee may be assigned to one or more projects, or may not be assigned to a project. A project must have at least one employee assigned, and may have any number of employees assigned. An employee's billing rate may vary by project and the company wishes to record the applicable billing rate for each employee when assigned to a particular project. Do the attribute names in this description follow the guidelines for naming attributes? If not, suggest better names. b. A university has a large number of courses in its catalog. Attributes of COURSE include Course_Number (identifier), Course_Name, and Units. Each course may have one or more different courses as prerequisites, or may have no prerequisites. Similarly, a particular course may be a prerequisite for any number of courses, or may not be prerequisite for any other course. Provide a good definition of COURSE, Why is your definition a good one? c. A laboratory has several chemists who work on one or more projects. Chemists also may use certain kinds of equipment on each project. Attributes of CHEMIST include EmployeeJD (identifier), Name, and Phone_No. Attributes of PROJECT include ProjectJD (identifier) and Start_Date. Attributes of EQUIPMENT include Serial_No and Cost. The organization wishes to record Assign_Date that is, the date when a given equipment item was assigned to a particular chemist working on a specified project. A chemist must be assigned to at least one project and one equipment item. A given equipment item need not be assigned, and a given project need not be assigned either a chemist or an equipment item. Provide good definitions for all of the relationships in this situation. d. A college course may have one or more scheduled sections, or may not have a scheduled section. Attributes of COURSE include Course ID, Course_Name, and Units. Attributes of SECTION include Section_Number and Semester_ID. Sernester_ID is composed of two parts: Semester and Year. Section_Number is an integer (such as "1" or "2") that distinguishes one section from another for the same course but does not uniquely identify a section. How did you model SECTION? Why did you choose this way versus alternative ways to model SECTION? e. A hospital has a large number of registered physicians. Attributes of PHYSICIAN include PhysicianJlD (the identifier) and Specialty. Patients are admitted to the hospital by physicians. Attributes of PATIENT include Patient_ID (the identifier) and Patient_Name. Any patient who is admitted must have exactly one admitting physician. A physician may optionally admit any number of patients. Once admitted, a given patient must be treated by at least one physician. A particular physician may treat any number of patients, or may not treat any patients. Whenever a patient is treated by a physician, the hospital wishes to record the details of the treatment (Treatment_Detail). Components of Treatment_Detail include Date, Time, and
Results. Did you draw more than one relationship between physician and patient? Why or why not? f. An organization makes many models of car, where a model is characterized by a name and suffix (such as GL or XL, which indicates the degree of luxury) and an engine size. Each model is made from many parts and each part may be used in the manufacturing of more than one model. Each part has a description and an ID code. Each model of car is produced at just one of the firm s Factories, which are located in Islamabad, Lahore, Faisalabad, Sahiwal and Karachi, one in each city. A factory produces many models of car and many types of parts, although each type of part is produced at one factory only.