You are on page 1of 5

COMP 401: Instructions 1. Create a database named sokomoko.

Use this database and create the tables below, assign appropriate data types and enter the sample data presented below. 2. Answer the questions beneath. 3. Submit your TYPED and PRINTED solutions in SQL statements to the work no later than Friday 9th November at 3 p.m. The SQL statements should have been tested and debugged for all errors. Hints: 1. This work requires querying from two tables and therefore involves JOINs. 2. It also requires the use of SQL in-built functions such as SUM, MAX, MIN, AVG, COUNT. EmpData (Employee Data) empID Sa001 Sa002 Sa003 Sa004 Sa005 Sa006 Sa007 Sa008 Sa009 Sa010 Sa011 Sa012 Sa013 Sa014 Sa015 Fname Sammy Jane Jimmy Michael Sammy Jackson Winnie Nicholas Dolphin Faustino Augustine Wycliffe John Alfred Evans Lname Nyokabi Nyongesa Omoro Kamau Koech Njeri Mbaka Kiptum Nthenge Cherono Mayende Omondi Okello Karuiki Kabira Title CEO Senior Programmer Senior Programmer Senior Programmer Web Designer Programmer Programmer Programmer Programmer Multimedia Programmer Multimedia Programmer Multimedia Programmer Multimedia Programmer Senior Web Designer System Administrator DoEmp 2002 1999 2005 1987 2004 2002 1995 1988 2006 2010 1970 1975 2000 2004 1990 Salary 22450 48321 73664 56265 63362 72636 76266 46362 23658 28466 54846 75284 32548 78951 23546 Allowance 251265 45269 256584 25297 12556 23544 335556 323453 324233 254233 224155 523211 225413 212421 232414 Email sn@mail.com jnyos@mail.com omo@mail.com kama@mail.com koech@mail.com njeri@mail.com mbaka@mail.com kiptum@mail.com nthenge@mail.com cherono@mail.com aug@mail.com joni@mail.com joke@mail.com karis@mail.com kabi@mail.com

Sa016 Sa017 Sa018 Sa019 Sa020 Sa021 Sa022

Maxwell Martha Rhoda Tom Nancy Esther Sharon

Omwenga Gitonga Apondi Lewis Auma Riungu Kosgei

System Administrator Senior Marketing Executive Marketing Executive Marketing Executive Marketing Executive Customer Service Manager Finance Manager

1999 2007 2003 2000 1995 1997 1989

85123 55456 51533 84656 52456 75256 55668

242533 322428 125442 212445 212354 212445 232443

maxom@mail.com magit@mail.com apo@mail.com lewi@mail.com nauma@mail.com riungu@mail.com kosgei@mail.com

Legend: empID employees ID, Fname employees First Name, lname employees last name, DoEmp employees Date of Employment/Appointment,

empID Sa001 Sa002 Sa003 Sa004 Sa005 Sa006 Sa007 Sa008 Sa009 Sa010 Sa011 Sa012 Sa013 Sa014

Address 845 655 5615 253 5236 2652 3236 2562 6536 266 566 259 365 668

City Nakuru Mombasa Nakuru Nairobi Mombasa Kisii Nairobi Mombasa Pokot Bahati Nairobi Isiolo Nyeri Nairobi

Phone 0751752525 0722524655 0723265852 0787225522 0725125455 0714225725 0713255425 0745225225 0758422552 0785555865 0713225525 0789556763 0735256857 0733525653

AltEmail sn@gmail.com jnyos@yahoo.com omo@gmail.com kama@hotmail.com koech@hotmail.com njeri@gmail.com mbaka@hotmail.com kiptum@yahoo.com nthenge@gmail.com cherono@gmail.com aug@gmail.com joni@gmail.com joke@yahoo.com karis@rockmail.com

YoB 1986 1984 1991 1973 1984 1990 1969 1971 1996 1999 1957 1944 1996 1998

Sex MStatus SName M Yes Stella Jemurgor Kipkosgei F Yes Lewis Tom Gitonga M Yes Esther Omoro Omondi M Yes Celestine Nyongesa M Yes Janet Kabui Gathere M Yes Chira Ann Nyokabi F No Not Married M No Not Married F Yes Glen Nguyo F Yes Jason Romney M No Not Married M No Not Married M Yes Juliet Ng'etich M Yes Mornica Akinyi Opeyo

children 11 8 1 3 4 5 0 1 0 4 0 2 4

Sa015 Sa016 Sa017 Sa018 Sa019 Sa020 Sa021 Sa022

9845 26635 6336 523 66526 663 5566 236

Nyeri Embu Nairobi Nairobi Machakos Kisii Kericho Nairobi

0751556523 0798252565 0745522552 0722525252 0721225633 0720236685 0711745235 0713333563

kabi@rockmail.com maxom@gmail.com magit@rockmail.com apo@mymail.com lewi@domail.com nauma@rockmail.com riungu@gmail.com kosgei@yahoo.com

1979 1981 1993 1991 1988 1980 1977 1982

M M F F M F F F

Yes No No Yes No Yes Yes No

Jesse Njogu Gichuki Not Married Not Married Gachanja Peter Not Married Kimani David Ngure Samuel Ogach Not Married

3 0 0 4 0 2 0 0

empid: are employee ids, same as that in table employee_data YoB employees Year of Birth, address: Addresses of employees phone: Phone numbers AltEmail: Personal email addresses birth_date: Birth dates sex: The sex of the employee, Male or Female m_status: Marital status,Yes or No. s_name: Name of Spouse (NULL if employee is unmarried) children: Number of children (NULL if employee is unmarried) Create the tables above, assign appropriate data types and enter the data; Write SQL statements to perform the following; 1. How can you write an SQL statement that will query the database and tell you how many employees were appointed to office before 1999? How many after 1999? (Fields Displayed: fname, lname, Title, empID, DoEmp, gender/sex,) (4 marks)

2. How can you write an SQL statement that will query the database and tell you how many employees are married? (Fields Displayed: fname, lname, Sex, sname) How many are not? (Fields Displayed: fname, lname, Sex) (4 marks)

3. Query the database so that you produce a list of employees and their spouses, for only those that have one.

(5 marks)

4. How can you write an SQL statement that will query the database and tell you how many employee have children? How many do not? Which employee has the highest number of children? (Fields Displayed: fname, lname, Title, empID, YoB, gender/sex, children) In total, how many children do employees of the company have? (Returns No Fields, just an Integer) (8 marks)

5. Assuming that the city field refers to the employee home city, how many employees originate from a) Nairobi? b) Mombasa? (Fields Displayed: fname, lname, Title, empID, City) (4 marks)

6. The employer wants to prepare a special retirement package for employees who are retiring at the age of 50. You have therefore been asked to prepare a report showing how many employees are over 48 years old as of the present date. (Fields Displayed: fname, lname, Title, DoEmpl, empID, YoB, gender/sex) (5 marks)

7. How would you write an SQL statement that would return a single integer that represents the number of employees of the company? Of those that are married? Of those that are not married? (Returns No Fields, just an Integer) (9 marks)

8. The employer would like to know how much in total, he spends on salaries every month. Write an SQL statement that can enable you give you total expenditure per month in a) salaries b) in allowances. (Returns No Fields, just an Integer) (6 marks) 9. Write an SQL statement that will query the database and tell you the name of the employee who earns the highest a) salary? b) Allowance? (Fields Displayed: fname, lname, Title, empID, salary, Allowance) (6 marks)

10. Write an SQL statement that will query the database and tell you the name of the employee who earns the least a) salary? b) Allowance? (Fields Displayed: fname, lname, Title, empID, salary, Allowance) (6 marks)

11. Assume that it makes sense to calculate on average how much an employee earns. What is the average salary earned in the company? Average allowance? (Returns No Fields, just an Number) (4 marks)

12. How would you write an SQL statement that would return a single integer that represents the number Male employees in employment? Female? (Returns No Fields, just an Integer) (4 marks)