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’)
Continue………

Solutions 6: Oracle Database Fundamental I


Oracle Database Fundamental I
 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—————–

Solutions 2: Oracle Database Fundamental I


Oracle Database Fundamental I
 Practice 2 Solutions
                  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.

Solution: select last_name, salary
from employees
where salary > 12000;
                 2.      Create a report that displays the last name and department number for employee number 176.
Solution: select last_name, department_id
from employees
where employee_id = 176;
                  3.      The HR departments needs to find high-salary and low-salary employees. Modify lab_02_01.sql to display the last name and salary for any employee whose salary is not in the range of $5,000 to $12,000. Place your SQL statement in a text file named lab_02_03.sql.
Solution: select last_name, salary
from employees
where salary>12000 or salary<5000;
                  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 start date.
Solution: select last_name, job_id, hire_date
from employees
where last_name=’Matos’ or last_name=’Taylor’
order by hire_date;
                  5.      Display the last name and department number of all employees in departments 20 or 50 in ascending alphabetical order by name.
Solution: select last_name, department_id
from employees
where department_id in (20,50)
order by last_name;
                  6.      Modify lab_02_03.sql to display the last name and salary of employees who earn between $5,000 and $12,000 and are in department 20 or 50. Label the columns Employee and Monthly Salary, respectively. Resave lab_02_03.sql as lab_02_06.sql. Run the statement in lab_02_06.sql.
Solution: select last_name “Employee”, salary “Monthly Salary”
from employees
where (salary between 5000 and 12000) and department_id in (20,50);
                              7.      The HR department needs a report that displays the last name and hire date for all employees who were hired in 1994.
Solution: select last_name, hire_date
from employees
where hire_date like ‘%94’;
                              8.      Create a report to display the last name and job title of all employees who do not have a manager.
Solution: select last_name, job_id
from employees
where manager_id is null;
                                9.      Create a report to display the last name, salary, and commission of all employees who earn commissions. Sort data in descending order of salary and commissions.
Solution: select last_name, salary, commission_pct
 from employees
 where commission_pct is not null
 order by salary, commission_pct
                            10.  Members of the HR department want to have more flexibility with the queries that you are writing. They would like a report that displays the last name and salary of employees who earn more than an amount that the user specifies after a prompt. (You can use the query that you created in practice exercise 1 and modify it.) Save this query to a file named lab_02_10.sql. If you enter 12000 when prompted, the report displays the following results:
Solution: select last_name, salary
from employees
where salary> &Salary;
                            11.  The HR department wants to run reports based on a manager. Create a query that prompts the user for a manager ID and generates the employee ID, last name, salary, and department for that manager’s employees. The HR department wants the ability to sort the report on a selected column. You can test the data with the following values:
manager ID = 103, sorted by employee last name*
manager ID = 201, sorted by salary**
manager ID = 124, sorted by employee ID***
Solution: select employee_id, last_name, salary, department_id
from employees
where manager_id= &manager
*order by last_name;
**order by salary;
***order by employee_id;
                            12.  Display all employee last names in which the third letter of the name is a.
Solution: select last_name
 from employees
 where last_name like ‘__a%’;
                            13.  Display the last name of all employees who have both an a and an e in their last name.
Solution: 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.
Solution: select last_name, job_id, salary
from employees
where (job_id=’SA_REP’ or job_id=’ST_CLERK’) and salary not in(2500,3500,7000);
                            15.  Modify lab_02_06.sql to display the last name, salary, and commission for all employees whose commission amount is 20%. Resave lab_02_06.sql as lab_02_15.sql. Rerun the statement in lab_02_15.sql.
Solution: select last_name “Employee”, salary “Monthly Salary”, commission_pct
 from employees
 where commission_pct=.2;
———The End———–

Solutions 1: Oracle Database Fundamental I


Oracle Database Fundamental I
 Practice 1 Solutions
          (Questions 1 to 5 just true and false, So try itself)
                6.Your first task is to determine the structure of the DEPARTMENTS table and its contents.
            Solutions: Structure- desc departments;
     Contents– select * from departments;
         7.      You need to determine the structure of the EMPLOYEES table.
            Solutions: desc employees;
         8.      The HR department wants a query to display the last name, job code, hire date, and employee number for each employee, with employee number appearing first. Provide an alias STARTDATE for the HIRE_DATE column. Save your SQL statement to a file named lab_01_07.sql so that you can disperse this file to the HR department.
Solutions: select employee_id, last_name, job_id, hire_date startdate
from employees;
         9.      The HR department needs a query to display all unique job codes from the EMPLOYEES table.
Solutions: select distinct job_id
from employees;
        10.  The HR department wants more descriptive column headings for its report on employees. Copy the statement from lab_01_07.sql to the iSQL*Plus text box. Name the column headings Emp #, Employee, Job, and Hire Date, respectively. Then run your query again.
Solutions: select employee_id “EmP#”, last_name “Employee”, job_id “Job”, hire_date “Hire Date”
from employees;
        11.  The HR department has requested a report of all employees and their job IDs. Display the last name concatenated with the job ID (separated by a comma and space) and name the column Employee and Title.
Solutions: select last_name||’, ‘||job_id “Employee and Title” from employees;
        12.  To familiarize yourself with the data in the EMPLOYEES table, create a query to display all the data from that table. Separate each column output by a comma. Name the column title THE_OUTPUT.
Solutions: select employee_id||’,’||first_name||’,’|| last_name||’,’||email||’,’||phone_number||’,’||hire_date||’,’||job_id||’,’||salary||’,’||commission_pct the_output from employees;
———-The End———-