Вы находитесь на странице: 1из 6

EMPLOYEES

EMPLOYEE_ID
FIRST_NAME
LAST_NAME
EMAIL
PHONE_NUMBER
HIRE_DATE
JOB_ID
SALARY LOCATIONS
COMMISSION_PCT LOCATION_ID
MANAGER_ID DEPARTMENTS STREET_ADDRESS
DEPARTMENT_ID DEPARTMENT_ID POSTAL_CODE
DEPARTMENT_NAM
E CITY
MANAGER_ID STATE_PROVINCE
LOCATION_ID COUNTRY_ID

COUNTRIES
COUNTRY_ID
COUNTRY_NAME
REGION_ID

1. For each employee, display the employee number, last_name, salary, and salary
increased by 15%

2. Write a query that displays the employee’s last names with the first letter
capitalized and all other letters lowercase and the length of the name for all
employees whose name starts with J, A, or M.Give each column an appropriate
label.

3. Write a query to display the number of people with the same department

4. Write a query for the HR department to produce the addresses of all the
departments.Use the LOCATIONS and COUNTRIES tables. Show the department
id, department name, location ID, street address,city, state or province, and
country in the output

5. The HR department needs a report that display last name ,first name ,
department_id ,salary,department_name and job id of all employees whose salary
is more than or equal average salary of each department .
6. Write a query to display the last name, salary, department number, and department
name for all employees.

7. Write a query to display the last name, salary,department number, and department
name for all employees whose salary is more than 1000.

8. Write a query to display each department’s name, City, number of employees, and
the average salary for all employees in that department. Label the columns Name,
Location, Number of People, and Salary, respectively.

9. The HR department needs a report of employees in Toronto. Display the last


name,first_name, jobid,salary ,department number,city, and department name for
all employees who work in Toronto

10. Create a query to display the employee numbers, department_id, salary ,last names and
department_name of all employees who earn more than the average salary. Sort the results in
descending order of salary.

11. Display all employees including King, who has no manager. Order the results by
the employee number. (last_name, first_name, salary, manager_id)

12. The HR department wants to determine the names of all employees who were
hired after Davies. Create a query to display the name and hire date of any
employee hired after employee Davies.

13. Display the department number, last name, and job ID for every employee in the Executive
department.

14. Write a query that displays the employee number and last name of all employees
who work in a department with any employee whose last name contains a u.

15. The HR department needs a report that displays the last name, department number,
department name and job ID of all employees whose department location ID is
1700.

16. Create the DEPT table based on the HR.DEPARTMENTS


(DEPT_ID,DEPT_NAME) with dept_id the PRIMARY KEY.

17. Create the EMPLOYEES2 table based on the structure of the EMPLOYEES table.
Include only the EMPLOYEE_ID, FIRST_NAME, LAST_NAME,SALARY, and
DEPARTMENT_ID columns with EMPLOYEE_ID the PRIMARY KEY and
DEPARTMENT_ID the FOREIGN KEY.

Reponse Solutions 3: Oracle Database Fundamental I

Oracle Database Fundamental I


 Practice 3 Solutions
                   1.      Write a query to display the current date. Label the column Date.
Solution: select sysdate from dual;

                  2.      The HR department needs a report to display the employee number, last
name, salary, and salary increased by 15.5% (expressed as a whole number) for each
employee. Label the column New Salary. Place your SQL statement in a text file named
lab_03_02.sql.
                  3.      Run your query in the file lab_03_02.sql.
Solution: select employee_id, last_name, salary, salary+
(salary*15.5/100) "New Salary"
from employees;

                  4.      Modify your query lab_03_02.sql to add a column that subtracts the old
salary from the new salary. Label the column Increase. Save the contents of the file as
lab_03_04.sql. Run the revised query.
Solution: select employee_id, last_name, salary, salary+
(salary*15.5/100) "New Salary", (salary+(salary*15.5/100))-salary
"Increses"
from employees;

                 5.      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.
Solution: select initcap(last_name) "Name", length(last_name)
"Length of Name"
from employees
where last_name like 'J%' or last_name like 'A%' or last_name
like 'M%'
order by last_name;

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.
Solution: select initcap(last_name) "Name", length(last_name)
"Length of Name"
from employees
where last_name like '&name%'
order by last_name;

                 6.      The HR department wants to find the length of employment for each
employee. For each employee, display the last name and calculate the number of
months between today and the date on which the employee was hired. Label the
column MONTHS_WORKED. Order your results by the number of months employed.
Round the number of months up to the closest whole number.
Solution: select last_name,
round(months_between(sysdate,hire_date),0) Months_worked from
employees
order by 2;

                 7.      Create a report that produces the following for each employee: <employee
last name> earns <salary> monthly but wants <3 times salary>. Label the column
Dream Salaries.
Solution: select last_name||' earns $'||salary||' monthly but wants
$'||salary*3 "Dream Salary"
from employees

                8.      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.
Solution: select last_name, lpad(salary,15,'$') Salary
 from employees;

               9.      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.”
Solution: select last_name, hire_date,
to_char((next_day(hire_date,'Monday')),'fmday," the "ddspth "of"
month,yyyy')
     from employees;

            10.  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.
Solution: select Last_name, hire_date, to_char(hire_date,'Day')
"Day"
from employees
order by to_char(hire_date-1,'d')
Practice 6 Solutions
                  1.      The HR department needs a query that prompts the user for an employee
last name. The query then displays the last name and hire date of any employee in the
same department as the
employee whose name they supply (excluding that employee). For example, if the user
enters
Zlotkey, find all employees who work with Zlotkey (excluding Zlotkey).
Solution: select last_name, hire_date
from employees
where department_id =
(select department_id from employees where last_name like
'&name')and last_name<> '&name';

                  2.      Create a  report that displays the employee number,  last name, and salary
of all employees who earn more than the average salary. Sort the results in order of
ascending salary.
Solution: select employee_id, last_name, salary
from employees
where salary > (select avg(salary) from employees)
order by salary;

                 3.      Write a query that displays the employee number and last name of all
employees who work in a department with any employee whose last name contains a u.
Place your SQL statement in a text file named lab_06_03.sql. Run your query.
Solution: select employee_id, last_name
from employees
where department_id in (select department_id from employees where
last_name like '%u%');

                  4.      The HR department needs a report that displays the last name, department
number, and job ID of all employees whose department location ID is 1700.
Solution: select last_name, department_id, job_id
from employees
where department_id in (select department_id from departments
where location_id =1700);

Modify the query so that the user is prompted for a location ID.
Solution: select last_name, department_id, job_id
from employees
where department_id in (select department_id from departments
where location_id =&Location);

                5.      Create a report for HR that displays the last name and salary of every
employee who reports to King.
Solution: select last_name, salary
from employees
where manager_id in (select employee_id from employees where
last_name='King');

                6.      Create a report for HR that displays the department number, last name, and
job ID for every employee in the Executive department.
Solution: select department_id, last_name, job_id
from employees
where department_id in (select department_id from departments
where department_name = 'Executive');

                7.      Modify the query in lab_06_03.sql to display the employee number, last
name, and salary of all employees who earn more than the average salary and who
work in a department with any employee whose last name contains a u. Resave
lab_06_03.sql as lab_06_07.sql. Run the statement in lab_06_07.sql.
Solution: select employee_id, last_name, salary
from employees
where salary > (select avg(salary) from employees) and
department_id in (select department_id from employees where
last_name like '%u%');

          ---------------The End-----------------

Вам также может понравиться