Answers to SQL Practice Questions

advertisement
Answers to SQL Practice Questions
ANSWERS: (A) Create/Alter/Drop Table Commands:
CREATE TABLE EMP
(emp_num
int ,
emp_fname char(20)
WITH DEFAULT 'unknown' ,
emp_lname char(20)
NOT NULL ,
job_class
char(3) ,
hiredate
date
NOT NULL ,
CONSTRAINT emp_pk PRIMARY KEY (emp_num) ,
CONSTRAINT job_fk FOREIGN KEY (job_class)
REFERENCES job (job_code)
CONSTRAINT emp_lname_uk UNIQUE ( emp_lname ) ,
CONSTRAINT hiredate_ck CHECK ( hiredate >= '1/1/1990' )
)
2. CREATE TABLE JOB (
job_code
char( 3 ) ,
job_class
char( 30 ) ,
chg_hour
decimal (5,2) WITH DEFAULT 15 ,
CONSTRAINT job_pk PRIMARY KEY (job_code) ,
CONSTRAINT job_class_ck CHECK ( chg_hour >= 15 )
)
3.
ALTER TABLE job
ADD COLUMN
overtimeCharge dec (6,2) NOT NULL
4.
ALTER TABLE job
ADD CONSTRAINT
charge_ck CHECK( chg_hour >= 25 )
5.
ALTER TABLE job
ALTER column
Job_Class set default 'NA'
6.
ALTER TABLE job
ALTER column
Chg_Hour set NOT NULL
7.
ALTER TABLE job
ALTER column
Chg_Hour set data type decimal (12,2)
8.
ALTER TABLE job
DROP constraint
charge_ck
9.
ALTER TABLE job
DROP COLUMN overtimeCharge
10.
ALTER TABLE job
DROP COLUMN chg_hour
11.
DROP TABLE job
12.
DROP TABLE emp
ANSWERS: (B) Data Manipulation Commands:
1. Insert the last 3 rows shown into the EMP table.
INSERT into emp ( emp_num, emp_fname, emp_lname, job_class, hiredate )
VALUES
( 106, 'William', 'Smithfield', 'PRG', '10/31/2002' ),
INSERT into emp ( emp_num, emp_fname, emp_lname )
VALUES
( 114, 'Annelise', 'Jones' )
INSERT into emp ( emp_num, emp_lname )
VALUES ( 118, 'Frommer' )
2. Update the employee fname to Jim instead of James for employee 118
(Note: if no WHERE clause is used, then all records are updated)
UPDATE emp
SET emp_fname = 'Jim'
WHERE emp_num = 118
3. Update the Database Designer chg_hour value by 10%
UPDATE job
SET chg_hour = chg_hour * 1.1
WHERE job_class = 'Database Designer'
4. Remove the row from the job table for job code PRG
( Note: if no WHERE clause is used, then all records are deleted )
DELETE
FROM job
WHERE job_code = 'PRG'
5. Remove the JOB table and all of it's data
DROP TABLE job
6. Remove the employees from the employee table WHERE the hiredate is before 1-Jan-90
DELETE
FROM emp
WHERE hiredate < '1-Jan-90'
7. Remove the emp table and all of it's data
DROP TABLE emp
ANSWER: (C) Select Commands:
1. Show the employee first and last names and their job class description and hourly charge.
SELECT
FROM
WHERE
emp.emp_fname ,
emp.emp_lname ,
job.job_class ,
job.chg_hour
emp, job
emp.job_class = job.job_code
2. Same question as above, but only show the records WHERE the chg_hour is <100
SELECT
FROM
WHERE
AND
emp.emp_fname ,
emp.emp_lname ,
job.job_class ,
job.chg_hour
emp, job
emp.job_class = job.job_code
job.chg_hour = < 100
3. Select the hours worked and lastname and hiredate for all employees.
(Note: the default for all selects is to show all records that are joined. The above statement has no
further conditions for the WHERE clause)
SELECT
FROM
WHERE
emp.emp_lname ,
emp.hiredate ,
proj_emp.hour
emp, proj_emp
emp.emp_num = proj_emp.emp_num
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.
(Note: the default for all selects is to show all records that are joined. The above statement has no
further conditions for the WHERE clause)
CREATE VIEW WORK_INFO as
SELECT
emp.emp_lname ,
emp.hiredate ,
proj_emp.hour
FROM
emp, proj_emp
WHERE
emp.emp_num = proj_emp.emp_num
AND
emp.hiredate < '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
(Note: the default for all selects is to show all records that are joined. The above statement has no
further conditions for the WHERE clause)
CREATE VIEW EMP_PROJECTS as
SELECT
emp.emp_fname ,
emp.emp_lname ,
project.proj_name
FROM
emp, project , proj_emp
WHERE
proj_emp.proj_num = project.proj_num
AND
proj_emp.emp_num = emp.emp_num
6. Select employee fname and lname and all projects that they are working on for
project 18.
SELECT
FROM
WHERE
AND
AND
emp.emp_fname ,
emp.emp_lname ,
project.proj_name
emp, project , proj_emp
proj_emp.proj_num = project.proj_num
proj_emp.emp_num = emp.emp_num
project.proj_num = 18
7. Select all project names and job class descriptions that the project requires.
Save this select statement as a view called: EMP_JOB_SKILLS
CREATE VIEW EMP_JOB_SKILLS as
SELECT
project.proj_name , job.job_class
FROM
project , job , emp, proj_emp
WHERE
proj_emp.proj_num = project.proj_num
AND
proj_emp.emp_num = emp.emp_num
AND
emp.job_class = job.job_code
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
CREATE VIEW DATABASE_DESIGNERS as
SELECT
project.proj_name ,
job.job_class
FROM
project , job , emp, proj_emp
WHERE
proj_emp.proj_num = project.proj_num
AND
proj_emp.emp_num = emp.emp_num
AND
emp.job_class = job.job_code
AND
emp.job_class = 'DBD'
9. Select customer first and last name, credit limit, and salesrep first and last name WHERE the credit
limit is >= 1500.
SELECT
customer.first_name, customer.last_name,
customer.credit_limit,
salesrep.last_name as salesLastName,
salesrep.first_name as salesFirstName
FROM premiere/customer, premiere/salesrep
WHERE customer.credit_limit >= 1500
Download