Assignment1 - 94089
Assignment1 - 94089
1
7. You need to determine the structure of the EMPLOYEES table.
DESCRIBE EMPLOYEES;
2
Chapter two:
The HR department needs your assistance with creating some
queries.
1. Due to budget issues, the HR department needs a report that
displays the last name and salary of employees who earn more
than $12,000. Place your SQL statement in a text file named
lab_02_01.sql. Run your query.
SELECT last_name, salary
FROM EMPLOYEES
WHERE salary > 12000;
3
4. Create a report to display the last name, job ID, and start date
for the employees with the last names of Matos and Taylor.
Order the query in ascending order by the start date.
SELECT last_name, job_id, start_date
FROM EMPLOYEES
WHERE last_name IN ('Matos', 'Taylor')
ORDER BY start_date ASC;
4
6. The HR department needs a report that displays the last name
and hire date for all employees who were hired in 1994.
7. Create a report to display the last name and job title of all
employees who do not have a manager.
SELECT e.last_name, j.job_title
FROM EMPLOYEES e
JOIN JOBS j ON e.job_id = j.job_id
WHERE e.manager_id IS NULL;
6
12. Display all employee last names in which the third letter of the
name is a.
SELECT last_name
FROM EMPLOYEES
WHERE SUBSTR(last_name, 3, 1) = 'a';
13. Display the last name of all employees who have both an a and
an e in their last name.
SELECT last_name
FROM EMPLOYEES
WHERE last_name LIKE '%a%' AND last_name LIKE '%e%';
14. Display the last name, job, and salary for all employees whose
job is sales representative or stock clerk and whose salary is not
equal to $2,500, $3,500, or $7,000.
SELECT last_name, job, salary
FROM EMPLOYEES
WHERE (job = 'sales representative' OR job = 'stock clerk')
AND salary NOT IN (2500, 3500, 7000);
7
Chapter three: Part 1
1. Write a query to display the current date. Label the column
Date.
SELECT SYSDATE AS "Date" FROM DUAL;
4. Write a query that displays the last name (with the first letter
uppercase and all other letters lowercase) and the length of the last
name for all employees whose name starts with the letters J, A, or M.
Give each column an appropriate label. Sort the results by the
employees’ last names.
SELECT INITCAP(last_name) AS "Last Name", LENGTH(last_name) AS "Last Name
Length"
FROM EMPLOYEES
WHERE last_name LIKE 'J%' OR last_name LIKE 'A%' OR last_name LIKE 'M%'
ORDER BY last_name;
8
5. Rewrite the query so that the user is prompted to enter a letter
that starts the last name. For example, if the user enters H when
prompted for a letter, then the output should show all employees
whose last name starts with the letter H.
ACCEPT start_letter CHAR PROMPT 'Enter the starting letter of
the last name: '
SELECT INITCAP(last_name) AS "Last Name",
LENGTH(last_name) AS "Last Name Length"
FROM EMPLOYEES
WHERE last_name LIKE UPPER('&start_letter%')
ORDER BY last_name;
9
9. Create a query to display the last name and salary for all
employees. Format the salary to be 15 characters long, left-padded
with the $ symbol. Label the column SALARY.
SELECT last_name, LPAD('$' || TO_CHAR(salary), 15, '$') AS
"SALARY"
FROM employees;
10. Display each employee’s last name, hire date, and salary review
date, which is the first Monday after six months of service. Label the
column REVIEW. Format the dates to appear in the format similar to
“Monday, the Thirty-First of July, 2000.”
SELECT last_name, hire_date,
NEXT_DAY(ADD_MONTHS(hire_date, 6), 'MONDAY') AS
"REVIEW"
FROM employees;
11. Display the last name, hire date, and day of the week on which the
employee started. Label the column DAY. Order the results by the day of the
week, starting with Monday.
SELECT last_name, hire_date, TO_CHAR(hire_date, 'Day') AS
"DAY"
FROM employees
ORDER BY TO_CHAR(hire_date, 'D');
12. Create a query that displays the employees’ last names and commission
amounts. If an employee does not earn commission, show “No
Commission.” Label the column COMM.
SELECT last_name,
CASE
WHEN commission_pct IS NOT NULL THEN commission_pct
ELSE 'No Commission'
END AS "COMM"
FROM employees;
10
13. Create a query that displays the first eight characters of the
employees’ last names and indicates the amounts of their salaries
with asterisks. Each asterisk signifies a thousand dollars. Sort the
data in descending order of salary. Label the column
EMPLOYEES_AND_THEIR_SALARIES.
SELECT
SUBSTR(last_name, 1, 8) || RPAD('*', ROUND(salary / 1000),
'*') AS "EMPLOYEES_AND_THEIR_SALARIES"
FROM employees
ORDER BY salary DESC;
14. Using the DECODE function, write a query that displays the
grade of all employees based on the value of the column JOB_ID,
using the following data:
a. Job Grade
b. AD_PRES A
c. ST_MAN B
d. IT_PROG C
e. SA_REP D
f. ST_CLERK E
g. None of the above 0
11
15. Rewrite the statement in the preceding exercise using the CASE
syntax.
SELECT
last_name,
job_id,
CASE job_id
WHEN 'AD_PRES' THEN 'A'
WHEN 'ST_MAN' THEN 'B'
WHEN 'IT_PROG' THEN 'C'
WHEN 'SA_REP' THEN 'D'
WHEN 'ST_CLERK' THEN 'E'
ELSE '0'
END AS "Grade"
FROM
employees;
Good Luck
Dr. Esmat Mohamamed
12