SQL EXCERSISE In the exercises given below the following tables would be used.

Employees Table Name EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID Departments Table Name DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID Countries Table Name COUNTRY_ID COUNTRY_NAME REGION_ID Locations Table Name LOCATION_ID STREET_ADDRESS POSTAL_CODE CITY STATE_PROVINCE COUNTRY_ID Null? NOT NULL Type NUMBER(4) VARCHAR2(40) VARCHAR2(12) VARCHAR2(30) VARCHAR2(25) CHAR(2) Null? NOT NULL Type CHAR(2) VARCHAR2(40) NUMBER Null? NOT NULL NOT NULL Type NUMBER(4) VARCHAR2(30) NUMBER(6) NUMBER(4) Null? NOT NULL NOT NULL NOT NULL NOT NULL NOT NULL Type NUMBER(6) VARCHAR2(20) VARCHAR2(25) VARCHAR2(25) VARCHAR2(20) DATE VARCHAR2(10) NUMBER(8,2) NUMBER(2,2) NUMBER(6) NUMBER(4)

NOT NULL

Regions Table Name REGION_ID REGION_NAME Null? NOT NULL Type NUMBER VARCHAR2(25) Job_history Table Name EMPLOYEE_ID START_DATE END_DATE JOB_ID DEPARTMENT_ID Null? NOT NULL NOT NULL NOT NULL NOT NULL Type NUMBER(6) DATE DATE VARCHAR2(10) NUMBER(4) Jobs Table Name JOB_ID JOB_TITLE MIN_SALARY MAX_SALARY Null? NOT NULL NOT NULL Type VARCHAR2(10) VARCHAR2(35) NUMBER(6) NUMBER(6) Emp_Details_View Table Name EMPLOYEE_ID JOB_ID MANAGER_ID DEPARTMENT_ID LOCATION_ID COUNTRY_ID FIRST_NAME LAST_NAME SALARY COMMISSION_PCT DEPARTMENT_NAME JOB_TITLE CITY STATE_PROVINCE COUNTRY_NAME REGION_NAME Null? NOT NULL NOT NULL Type NUMBER(6) VARCHAR2(10) NUMBER(6) NUMBER(4) NUMBER(4) CHAR(2) VARCHAR2(20) VARCHAR2(25) NUMBER(8.2) VARCHAR2(30) VARCHAR2(35) VARCHAR2(30) VARCHAR2(25) VARCHAR2(40) VARCHAR2(25) NOT NULL NOT NULL NOT NULL NOT NULL .2) NUMBER(2.

Queries: 1.000 and $12. 12. 11. 6. There are four coding errors in this statement.Display the last name and salary of employees who earn between $5. Show the structure of the DEPARTMENTS table and select all data from the table. Can you identify them? SELECT employee_id. last_name sal x 12 ANNUAL SALARY FROM employees 4. Order the query in ascending order by start date. 10.Display the last name and department number of all employees in departments 20 and 50 in alphabetical order by name.Display the employee last name. and are in department 20 or 50. 9. 1998. and start date of employees hired between February 20. Create a query to display unique job codes from the EMPLOYEES table.000. Create a query to display the employee last name and department number for employee number 176. Provide an alias STARTDATE for the HIRE_DATE column. Does the following SELECT statement executes successfully: SELECT * FROM job_grades True/False 3. job ID.000 and $12. and employee number for each employee. Create a query to display the last name.000. 5. 1998. respectively. and May 1. job code.000. with employee number appearing first. Create a query to display the last name and salary for all employees whose salary is not in the range of $5. 8. 7. . job_id. Does the following SELECT statement execute successfully: SELECT last_name. Show the structure of the EMPLOYEES table. hire date. Create a query to display the last name and salary of employees earning more than $12. Label the columns Employee and Monthly Salary. salary AS Sal FROM employees True/False 2.

and commission for all employees whose commission amount is 20%. Label the column Increase.’ 17.Write a query to display the current date. 25. Round the number of months up to the closest whole number. or M. A.Write a query that produces the following for each employee: <employee last name> earns <salary> monthly but wants <3 times salary>. salary. 21. 15. for all employees whose name starts with J. $3. 16.500. and commission for all employees who earn commissions. 23. last_name.Display the last name. 14.13. 22.Write a query that displays the employee’s last names with the first letter capitalized and all other letters lowercase.Display the last name and hire date of every employee who was hired in 1994.Display the last name. 18. Sort the results by the employees’ last names.Write a query to add a column that subtracts the old salary from the new salary.Display the last name of all employees who have an a and an e in their last name. Sort data in descending order of salary and commissions. salary. or $7. 24. . Label the column Date.500.Display the last name. Label the column MONTHS_WORKED. and salary for all employees whose job is sales representative or stock clerk and whose salary is not equal to $2. and the length of the names. and calculate the number of months between today and the date the employee was hired. 19. job id.Display the last name and job title of all employees who do not have a manager. Give each column an appropriate label.000. salary.For each employee.Display the last names of all employees where the third letter of the name is an ‘a. Label the column Dream Salaries. display the employee’s last name. display the employee number. and salary increased by 15% and expressed as a whole number. Label the column New Salary. Order your results by the number of months employed. 20.For each employee.

department name.Create a query to display the last name and salary for all employees. and city of all employees who earn a commission. and department name for all employees. location ID. Each asterisk signifies a thousand dollars. and department name for all employees who work in Toronto.Write a query to display the employee last name. Include the location of the department in the output. 29. department number.26. 35. If an employee does not earn commission. Format the salary to be 15 characters long.Using the DECODE function. put “No Commission. 27. Label the column DAY. Label the column SALARY. 32. and day of the week on which the employee started.Display the last name. 28. write a query that displays the grade of all employees based on the value of the column JOB_ID.Write a query to display the last name. Sort the data n descending order of salary.Write a query to display the last name.Create a query that displays the employees’ last names and commission amounts. department number.Create a query that displays the employees’ last names and indicates the amounts of their annual salaries with asterisks. 34. .” Label the column COMM. Label the column EMPLOYEES_AND_THEIR_SALARIES. hire date.Display the employee last name and department name for all employees who have an a (lowercase) in their last names. 33.Create a unique listing of all jobs that are in department 80. left-padded with $. Order the results by the day of the week starting with Monday. as per the following data: Job Grade AD_PRES A ST_MAN B IT_PROG C SA_REP D ST_CLERK E None of the above 0 31. job. 30.

sum. Manager. and Average. Sum.Create a query that will display the total number of employees and. Emp#. Minimum. and the total salary for that job. Label the columns Name. Number of People. lowest. respectively.Display the minimum. the salary for that job based on department number. and 90. location. Maximum. Location. number of employees. and 1998. 50. Label the columns Employee. for departments 20. 80. 40.Display all employees including King. 44. respectively.Display the highest. Hint: Use the MANAGER_ID column to determine the number of managers. of that total.Display the manager number and the salary of the lowest paid employee for that manager. Round your results to the nearest whole number. 43.36. 46. 42. Order the results by the employee number. Label the column DIFFERENCE.Create a matrix query to display the job. 45. 41. Round the average salary to two decimal places. 1997.Write a query to display each department’s name. Label the column Number of Managers.Write a query to display the number of people with the same job. 47. and average salary of all employees. respectively. 38. 1996.Write a query that displays the difference between the highest and lowest salaries. 48. and average salary for each job type. Exclude Zlotkey. sum. Create appropriate column headings. Exclude anyone whose manager is not known. the number of employees hired in 1995.000 or less. .Write a query to display the number of people with the same job. and Mgr#. who has no manager. Exclude any groups where the minimum salary is $6. 37. maximum. and the average salary for all employees in that department. Sort the output in descending order of salary. Label the columns. giving each column an appropriate heading.Determine the number of managers without listing them. and Salary.Display the employee last name and employee number along with their manager’s last name and manager number.Write a query to display the last name and hire date of any employee in the same department as Zlotkey. 39.

50. 55.Write a script to display the last names. department number. Sort the results in ascending order of salary.Display the department number. Use the format MM/DD/YYYY.Display the last name. Note: The MY_EMPLOYEE table mentioned below is a table similar to the employees table. job. Concatenate the name and jobs together. In a separate SQL script file. and job ID for every employee in the Executive department.49. 53.Create a query to display the employee numbers and last names of all employees who earn more than the average salary.Write a query that displays the employee numbers and last names of all employees who work in a department with any employee whose last name contains a “u”. Save the script file as lab7_4. and hire date for all employees who started between a given ranges. and salaries of all employees who earn more than the average salary and who work in a department with any employee with a u in their name. last names. 56.Write a script to display the employee last name. 52. and department names for every employee in a given location. use the DEFINE command to provide the two ranges. 54. The search condition should allow for case-insensitive searches of the department location. 51.sql. job IDs.2) NUMBER(2. The structure of the table is given below My_Employees Name ID FIRST_NAME LAST_NAME EMAIL USERID HIRE_DATE JOB_ID SALARY COMMISSION_PCT Null? NOT NULL NOT NULL NOT NULL NOT NULL NOT NULL Type NUMBER(6) VARCHAR2(20) VARCHAR2(25) VARCHAR2(25) VARCHAR2(20) DATE VARCHAR2(10) NUMBER(8. and label the column Employees.Display the employee numbers.2) . and job ID of all employees whose department location ID is 1700.Display the last name and salary of every employee who reports to King. separated by a space and comma. last name.

Update and delete data in the MY_EMPLOYEE table.Delete Betty Dancs from the MY_EMPLOYEE table. 60.Populate the table with the last row of sample data by modifying the statements in the script that you created in step 6.Empty the entire table. Run the statements in the script. This time. Do not list the columns in the INSERT clause. . 62.Change the last name of employee 3 to Drexler.MANAGER_ID DEPARTMENT_ID NUMBER(6) NUMBER(4) 57.Write an insert to load rows into the MY_EMPLOYEE table.Add the first row of data to the MY_EMPLOYEE table from the following sample data.Confirm your additions to the table.Change the salary to 1000 for all employees with a salary less than 900. 64. 61. 67. Concatenate the first letter of the first name and the first seven characters of the last name to produce the user ID.Mark an intermediate point in the processing of the transaction. 59.Populate the table with the next two rows of sample data by running the insert statement in the script that you created.Make the data additions permanent.Describe the structure of the MY_EMPLOYEE table to identify the column names. 70.Discard the most recent DELETE operation without discarding the earlier INSERT operation. 68. 66. 69. list the columns explicitly in the INSERT clause. 63.Populate the MY_EMPLOYEE table with the second row of sample data from the preceding list. 58. 65.

then execute the statement in the script to create the table. 83. and then execute the statement in the script to create the table. 74.Populate the DEPT table with data from the DEPARTMENTS table. FIRST_NAME.Modify the EMP table to allow for longer employee last names. LAST_NAME. Confirm your modification by checking the description of the table.In the EMP table. Confirm your modification by checking the description of the table.Create the EMPLOYEES2 table based on the structure of the EMPLOYEES table. 73. Confirm your modification by checking the description of the table. Include only columns that you need. and DEPT_ID.Add a table-level PRIMARY KEY constraint to the EMP table on the ID column.71. Place the syntax in a script called lab9_1. The constraint should be named at creation.sql.Confirm that both the DEPT and EMP tables are stored in the data dictionary. FIRST_NAME. Confirm that the table is created.Add a comment to the DEPT and EMP table definitions describing the tables. mark the DEPT_ID column in the EMP table as UNUSED. Name the columns in your new table ID. 78.Create the DEPT table based on the following table instance chart. 81. LAST_NAME. 82. Name the constraint my_emp_id_pk. and DEPARTMENT_ID columns.sql. 72. SALARY. .Drop the EMP table. Confirm your modification. Confirm your additions in the data dictionary.Rename the EMPLOYEES2 table as EMP. SALARY. 79. 80. Confirm that the table is created.Drop the FIRST_NAME column from the EMP table. (Hint: USER_TABLES) 76. respectively. Place the syntax in a script called lab9_3. 77. Hint: The constraint is enabled as soon as the ALTER TABLE command executes successfully. 75. Include only the EMPLOYEE_ID.Drop all the UNUSED columns from the EMP table.Create the EMP table based on the following table instance chart.

85. employee names. Name the constraint my_emp_dept_id_fk. .Display the object names and types from the USER_OBJECTS data dictionary view for the EMP and DEPT tables. precision 2. and department numbers from the EMPLOYEES table. Hint: The constraint is enabled as soon as the ALTER TABLE command executes successfully. Notice that the new tables and a new index were created. 89. 86. Add a foreign key reference on the EMP table that ensures that the employee is not assigned to a nonexistent department. 91.Add a column DEPT_ID to the EMP table. enter a query to display all employee names and department numbers.84.Create a view called EMPLOYEES_VU based on the employee numbers. 87. Name the constraint my_dept_id_pk.Modify the EMP table.Create a PRIMARY KEY constraint to the DEPT table using the ID column. Change the heading for the employee name to EMPLOYEE.Using your EMPLOYEES_VU view.Select the view name and text from the USER_VIEWS data dictionary view. Add a constraint to the commission column that ensures that a commission value is greater than zero.Confirm that the constraints were added by querying the USER_CONSTRAINTS view. Add a COMMISSION column of NUMBER data type. 90. scale 2. The constraint should be named at creation. 88.