Vous êtes sur la page 1sur 16

DBS201 SQL Practice Problems

Sample questions and SQL answers:


Click here for answers Answers

Use the following sample tables, data, and constraints to answers the questions that follow:

EMP

EMP_NUM 101 102 103 104 105 106 114 118

EMP_FNAME John David June Anne Alice William Annelise default value

EMP_LNAME News Senior Arbough Ramoras Johnson Smithfield Jones Frommer

JOB_CLASS DBD SYA EEG SYA DBD PRG

HIREDATE 1-NOV-78 3-OCT-88 23-JUN-99 30-FEB-92 15-AUG-95 31-OCT-02

JOB

JOB_CODE APD DBD EEG GSP PRG SYA

JOB_CLASS Application Designer Database Designer Elec. Engineer General Support Programmer Systems Analyst

CHG_HOUR $48.10 $105.00 $84.50 $18.36 $37.75 $96.75

PROJECT

PROJ PROJ

_NUM _NAME 15 18 Evergreen Amber Wave

PROJ_EMP

PROJ

EMP

_NUM _NUM HOUR 15 15 15 18 18 103 101 105 114 118 23.8 19.4 35.7 24.6 25.3

A) Create/Alter/Drop Table Commands:


-1) All employees may or may not be given a job_class. Job_class can be repeated in the employee table. If no first name is specified when inserting an employee record, then a default of "unknown" will be entered. All employees must have a last name, and the last name can not be repeated by other employees. All employees must have a hiredate, and the hiredate must be >= 1-Jan-90.

Complete the data dictionary below: Table: EMPLOYEE


Column Type Len gth Emp_Num PK FK Required Not Null Unique Validation /Check Default

emp_fname

emp_lname

Job_Class

hiredate

All chg_hour will default to a value of $15/hour if no value is specified during the insert. If a value is specified, then it must be >= $15.

0.

Complete the data dictionary below:

Table: JOB
Column Type Len gth job_code PK FK Required Not Null Unique Validation /Check Default

job_class

chg_hour

1. Create the EMP table from the sample data on the previous page. Also implement the following constraints.
Column Type Len gth Emp_Num int PK PK FK Required Not Null Unique Validation /Check Default

emp_fname

char

20

unknown

emp_lname

char

20

NN

unique

Job_Class

char

job (job_code)

hiredate

date

NN

>=1/1/1990

2. Create the JOB table from the sample data on the previous page. Also implement the following constraints.
Column Type Len gth job_code char 3 PK PK FK Required Not Null Unique Validation /Check Default

job_class

char

30

chg_hour

decimal

5,2

>=15

15

3. Add a new attribute to the JOB table named OvertimeCharge that must always have a value.

4.

Add a constraint to the JOB table that will ensure all CHG_HOUR data is >= 25.

5. Add a constraint that will add the value NA to all JOB_CLASS columns if no value is specified when the record is inserted into the table.

6.

Add a constraint that always requires a value entered into the CHG_HOUR column.

7. Change the CHG_HOUR column to store numbers up to 12 digits in length (including decimal points).

8.

Drop the constraint CHARGE_CK from the CHG_HOUR column.

9.

Drop the column named OvertimeCharge from the JOB table.

10.

Drop the chg_hour column and all of it's data from the job table.

11.

Remove the job table and all of it's data.

12.

Drop the table emp and all rows of it's data.

B) Data Manipulation Commands:


1. Insert the last 3 rows shown into the EMP table.

2.

Update the employee fname to Jim instead of James for employee 118

3.

Update the Database Designer chg_hour value by 10%

4.

Remove the row from the job table for job code PRG

5.

Remove the JOB table and all of it's data

6.

Remove the employees from the employee table where the hiredate is before 1-Jan-90

7.

Remove the emp table and all of it's data

C) Select Commands:
1. Show the employee first and last names and their job class description and hourly charge.

2.

Same question as above, but only show the records where the chg_hour is <100

3.

Select the hours worked and lastname and hiredate for all employees.

4. Create a view called: WORK_INFO which will select the hours worked and lastname and hiredate for all employees hired before 1-jan-02.

5. Select employee fname and lname and all projects that they are working on. Save this select statement as a view called: EMP_PROJECTS

6. 18.

Select employee fname and lname and all projects that they are working on for project

7. Select all project names and job class descriptions that the project requires. Save this select statement as a view called: EMP_JOB_SKILLS

8. Select all project names and job class descriptions that the project requires for only the job code of DBD. Save this statement as a view called: DATABASE_DESIGNERS

9. Select customer first and last name, credit limit, and salesrep first and last name where the credit limit is >= 1500.