-###############################################
###############################################
###
- 1.- apellido y el salario de los empleados que ganan ms de $ 12,000
select last_name, salary from employees where salary > 12000;
-- 11.- Create a query that prompts the user for a manager ID and generates
the employee ID, last name, salary, and department for that managers
employees.
-- where e.MANAGER_ID = 103 order by e.LAST_NAME desc;
-- where e.MANAGER_ID = 201 order by e.SALARY desc;
-- where e.MANAGER_ID = 124 order by e.EMPLOYEE_ID desc;
select EMPLOYEE_ID, LAST_NAME, SALARY from employees where MANAGER_ID
= &manager order by &orderColumn desc;
-- 12.- Display all employee last names in which the third letter of the name is a
select LAST_NAME, substr(LAST_NAME, 3,1) 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_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.
-###############################################
###############################################
###
-- ##### LESSON 03 #######
-- 1.- Write a query to display the current date. Label the column Date.
select sysdate as miFecha from dual;
-- 2.- For each employee, display the employee number, last_name, salary, and
salary increased by 15%and expressed as a whole number. Label the column
New Salary. Place your SQL statement in a text file named
-- lab3_2.sql.
select EMPLOYEE_ID, LAST_NAME, SALARY, (SALARY + SALARY*0.15) as
SALARYNEW from employees where COMMISSION_PCT = 0.2;
-- 3.- Modify your query lab3_2.sql to add a column that subtracts the old salary
from the new salary
-- lab3_4.sql.
select EMPLOYEE_ID, LAST_NAME, SALARY, (SALARY + SALARY*0.15) as
SALARYNEW, SALARY*0.15 as INCREASE from employees;
-- 4.- Write a query that displays the employees 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.
select INITCAP(LOWER(LAST_NAME)), LENGTH(LAST_NAME) as
NUMBER_CARACTER, SUBSTR(UPPER(LAST_NAME), 1,1) AS FIRST_LETTER from
employees where SUBSTR(UPPER(LAST_NAME), 1,1) in ('J','A','M') order by
LAST_NAME ASC;
-- 5.- For each employee, display the employees last name, and calculate the
number of months Between today and the date the employee was hired
select INITCAP(LOWER(LAST_NAME)), CEIL(MONTHS_BETWEEN(SYSDATE,
hire_date)) AS MONTHS_WORKED from employees order by CEIL
(MONTHS_BETWEEN(SYSDATE, hire_date)) asc, LAST_NAME asc;
-###############################################
###############################################
###
-- ##### LESSON 04 AND LESSON 05 AND LESSON 06 #######
-- 1.- Write a query to display the last name, department number, and
department name for all employees.
select INITCAP(LOWER(LAST_NAME)), DEPARTMENT_ID, DEPARTMENT_NAME
from employees NATURAL JOIN DEPARTMENTS ;
-- 2.- Create a unique listing of all jobs that are in department 80. Include the
location of the Department in the output.
select j.JOB_TITLE, l.CITY
FROM JOB_HISTORY jh
JOIN EMPLOYEES e ON e.EMPLOYEE_ID = jh.EMPLOYEE_ID
JOIN DEPARTMENTS d ON d.DEPARTMENT_ID = e.DEPARTMENT_ID
JOIN LOCATIONS l ON l.LOCATION_ID = d.LOCATION_ID
JOIN JOBS j ON j.JOB_ID = jh.JOB_ID
WHERE e.DEPARTMENT_ID = 80;
-- 3.- Write a query to display the employee last name, department name,
location ID, and city of all employees who earn a commission;
select e.LAST_NAME, d.DEPARTMENT_NAME, l.LOCATION_ID, l.CITY,
e.COMMISSION_PCT
FROM EMPLOYEES e
-- 4.- Display the employee last name and department name for all employees
who have an a (lowercase) in their last names. Place your SQL statement in a
text file named lab4_4.sql.
-- lab4_4.sql
select e.LAST_NAME, d.DEPARTMENT_NAME, INSTR(e.LAST_NAME, 'a')
FROM EMPLOYEES e
JOIN DEPARTMENTS d ON d.DEPARTMENT_ID = e.DEPARTMENT_ID
WHERE INSTR(e.LAST_NAME, 'a') > 0;
-- 5.- Write a query to display the last name, job, department number, and
department name for all employees who work in Toronto.
select e.LAST_NAME, d.DEPARTMENT_ID, d.DEPARTMENT_NAME
FROM EMPLOYEES e
JOIN DEPARTMENTS d ON d.DEPARTMENT_ID = e.DEPARTMENT_ID
JOIN LOCATIONS l ON l.LOCATION_ID = d.LOCATION_ID
WHERE l.CITY LIKE 'Toronto';
-- 6.- Display the employee last name and employee number along with their
managers last name and manager number. Label the columns Employee,
Emp#, Manager, and Mgr#, respectively.
-- Place your SQL statement in a text file named lab4_6.sql.
select
e.LAST_NAME as Employee,
e.employee_id as Emp#,
e2.LAST_NAME as Manager,
e2.employee_id Mgr#
FROM EMPLOYEES e, EMPLOYEES e2
where e.manager_id = e2.employee_id;
-- 7.- Modify lab4_6.sql to display all employees including King, who has no
manager.
-- Place your SQL statement in a text file named lab4_7.sql. Run the query in
lab4_7.sql.
select
e.LAST_NAME as Employee,
e.employee_id as Emp#,
e2.LAST_NAME as Manager,
e2.employee_id Mgr#
FROM EMPLOYEES e
LEFT JOIN EMPLOYEES e2 ON e.manager_id = e2.employee_id;
-- 8.- Create a query that displays employee last names, department numbers,
-- and all the employees who work in the same department as a given
employee. Give each column an Appropriate label.
select e2.LAST_NAME, d.DEPARTMENT_ID, d.DEPARTMENT_NAME
FROM EMPLOYEES e
JOIN DEPARTMENTS d ON d.DEPARTMENT_ID = e.DEPARTMENT_ID
JOIN EMPLOYEES e2 ON e2.DEPARTMENT_ID = d.DEPARTMENT_ID
WHERE e.EMPLOYEE_ID = &employeeId; -- 20;
-- 10.- Create a query to display the name and hire date of any employee hired
after employee Davies
select e.LAST_NAME, e.HIRE_DATE
FROM EMPLOYEES e
where e.HIRE_DATE > (select e2.HIRE_DATE from EMPLOYEES e2 where
e2.last_name = 'Davies');
-- 11.- Display the names and hire dates for all employees who were hired
before their managers,
-- along with their managers names and hire dates. Label the columns
Employee, Emp Hired, Manager, and Mgr Hired, respectively.
select e.LAST_NAME, e.HIRE_DATE, e2.LAST_NAME, e2.HIRE_DATE
FROM EMPLOYEES e, EMPLOYEES e2
where e2.EMPLOYEE_ID = e.MANAGER_ID
and e.HIRE_DATE < e2.HIRE_DATE ;
-###############################################
###############################################
-- ### LESSON 07 AND LESSON 08 ####### 13JUN2016
#######################################
-- 1.- Group functions work across many rows to produce one result. true
-- 2.- Group functions include nulls in calculations. false
-- 3.- The WHERE clause restricts rows prior to inclusion in a group calculation
FALSE.
-- 4.- Display the highest, lowest, sum, and average salary of all employees.
-###############################################
###############################################
-- ### HELP-ME
###############################################
###################################
select * from employees;
select * from DEPARTMENTS ;
select * from LOCATIONS;
select * from JOBS;
select * from JOB_HISTORY;
select NEXT_DAY(sysdate, 2), last_day(sysdate),
add_months(last_day(sysdate),-2) + 1 from dual;
select sysdate, to_char(sysdate,'D') from dual; -- day of week for today
select to_char(sysdate-1,'D') from dual; -- day of week for yesterday
select to_char(to_date('03/09/1982','dd/mm/yyyy'), 'Dy') from dual;
select * from asd where trunc(updated_date) <= to_date('08-Jun-2010', 'ddMON-yyyy');
-###############################################
###############################################
-###############################################
###############################################
-- #########
-- #########
-- #########
-- #########
-- #########
-- #########
-###############################################
###############################################
###
-- ##### LESSON 09 AND LESSON 10 ####### 19JUL2016 ####
INSERT, UPDATE, DELETTE, CREATE TABLE
-- 1.- 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
select (e.LAST_NAME || ' earns ' || e.SALARY || ' monthly but wants ' || e.SALARY
* 3) AS DreamSalaries from employees e;
-- 2.- Display each employees last name, hire date, and salary review date,
which is the first Monday after six months of service
-- Format the dates to appear in the format similar to Monday, the Thirty-First
of July, 2000.
select e.LAST_NAME, e.HIRE_DATE, e.SALARY,
TO_CHAR(NEXT_DAY(ADD_MONTHS(e.HIRE_DATE, 6), 2), 'fmDay "the" ddspth
"of" Month"," YYYY', 'nls_date_language=english') as REVIEW
from employees e;
-- 3.- 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
withMonday.
select e.LAST_NAME,
e.HIRE_DATE,
NEXT_DAY(e.HIRE_DATE, 2) as NEXT_MONDAY,
to_char(NEXT_DAY(e.HIRE_DATE, 2), 'dd') as DAY
from employees e
ORDER BY
-- NEXT_DAY(ADD_MONTHS(e.HIRE_DATE, 6), 2) asc, -- order for the next
monday
to_char(NEXT_DAY(e.HIRE_DATE, 2), 'dd') asc
-- 4.- 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 e.FIRST_NAME || ' ' || e.LAST_NAME as Employee,
-- e.salary, e.COMMISSION_PCT,
case when e.COMMISSION_PCT is null then 'Not Commission' else
to_char(e.COMMISSION_PCT * e.salary) END AS COMM
from employees e;
-- 5.- Using the DECODE function, write a query that displays the grade of all
employees
-- based on the value of the JOB_ID column, using the following data:
select e.FIRST_NAME || ' ' || e.LAST_NAME as Employee,
e.JOB_ID,
DECODE(e.JOB_ID
,'AD_PRES' , 'A'
,'ST_MAN' , 'B'
,'IT_PROG' , 'C'
,'SA_REP' , 'D'
,'ST_CLERK' , 'E'
,'0') as USING_DECODE
from employees e;
-- 6.- Rewrite the statement in the preceding exercise by using the CASE syntax
select e.FIRST_NAME || ' ' || e.LAST_NAME as Employee,
e.JOB_ID,
case e.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 USING_CASE
from employees e;
-- 7.- 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.
select e.LAST_NAME, d.DEPARTMENT_ID, e.JOB_ID -- , d.DEPARTMENT_NAME
FROM EMPLOYEES e
JOIN DEPARTMENTS d ON d.DEPARTMENT_ID = e.DEPARTMENT_ID
-- JOIN LOCATIONS l ON l.LOCATION_ID = d.LOCATION_ID
-- 8.- Create a report for HR that displays the last name and salary of every
employee who reports to King.
select e.LAST_NAME, e.SALARY
FROM EMPLOYEES e
JOIN EMPLOYEES e2 ON e2.EMPLOYEE_ID = e.MANAGER_ID
WHERE e2.LAST_NAME = 'King';
-- 9.- Create a report for HR that displays the department number, last name,
and job ID for
-- every employee in the Executive department
select e.LAST_NAME, d.DEPARTMENT_ID, e.JOB_ID -- , d.DEPARTMENT_NAME
FROM EMPLOYEES e
JOIN DEPARTMENTS d ON d.DEPARTMENT_ID = e.DEPARTMENT_ID
WHERE d.DEPARTMENT_NAME = 'Executive';
-###############################################
###############################################
###
-- ##### LESSON 10 AND Managing Schema Objects ####### 20JUL2016
####
-- 1.- Create the DEPT2 table based on the following table instance chart. Type
the syntax in the SQL Worksheet.
-- Then, execute the statement to create the table. Confirm that the table is
created.
CREATE TABLE dept2 (
deptno number(2),
dname varchar2(14),
loc varchar2(13),
create_date date default sysdate
);
select * from dept2;
-- 2.- Populate the DEPT2 table with data from the DEPARTMENTS table. Include
only the columns that you need.
insert into dept2 (deptno, dname, loc)
select department_id, department_name, location_id from DEPARTMENTS
where length(department_name) < 14 and department_id < 99;
-- ERROR: Error SQL: ORA-12899: value too large for column
"HR"."DEPT2"."DNAME" (actual: 15, maximum: 14)
select * from dept2;
-- 3.- Create the EMP2 table based on the following table instance chart. Type
the syntax in the SQL Worksheet.
-- Then execute the statement to create the table. Confirm that the table is
created.
DROP table emp2;
CREATE TABLE emp2 (
id number(6,0),
first_name varchar2(15),
last_name varchar2(15),
email varchar2(15),
cellphone varchar2(15),
salary number(8,2),
create_date date default sysdate
);
-- 4.- Modify the EMP2 table to allow for longer employee last names. Confirm
your modification.
alter table emp2 modify last_name varchar(250);
describe emp2;
-- 5.- 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. Name the columns in your new table ID,
-- FIRST_NAME, LAST_NAME, SALARY, and DEPT_ID, respectively.
create table EMPLOYEES2 AS
SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY, DEPARTMENT_ID
FROM EMPLOYEES;
-- CHANGE COLUMNS NAME
ALTER TABLE EMPLOYEES2 RENAME COLUMN DEPARTMENT_ID TO DEP_ID;
-- TEST
describe EMPLOYEES2;
SELECT * FROM EMPLOYEES2;
-- 7.- Query the recycle bin to see whether the table is present.
SELECT original_name, operation, droptime FROM recyclebin;
-- 8.- Restore the EMP2 table to a state before the DROP statement.
flashback table emp2 to before DROP;
describe emp2;
SELECT * FROM emp2;
-- 11.- Drop all the UNUSED columns from the EMPLOYEES2 table.
-- Confirm your modification by checking the description of the table.
alter table EMPLOYEES2 drop unused columns;
SELECT * FROM USER_UNUSED_COL_TABS;
-- 12.- Add a table-level PRIMARY KEY constraint to the EMP2 table on the ID
column.
-- 13.- Create a PRIMARY KEY constraint to the DEPT2 table using the ID
column.
-- The constraint should be named at creation. Name the constraint
my_dept_id_pk.
describe DEPT2;
SELECT * FROM DEPT2;
-- change name the column DEPTNO to ID
alter table dept2 rename column DEPTNO to id;
-- first method
-- 14.- Add a foreign key reference on the EMP2 table that ensures that the
employee is not assigned to a nonexistent department.
-- Name the constraint my_emp_dept_id_fk
describe EMP2;
describe dept2;
SELECT * FROM EMP2;
SELECT * FROM dept2;
-alter table emp2 add dept_id number(6) not null;
alter table emp2 add constraint my_emp_dept_id_fk FOREIGN key(dept_id)
REFERENCES dept2(id);
-- 15.- Modify the EMP2 table. Add a COMMISSION column of the NUMBER data
type, precision 2, scale 2.
-- Add a constraint to the COMMISSION column that ensures that a
commission value is greater than zero
describe EMP2;
-alter table emp2 add COMMISSION number(2,2) CHECK( COMMISSION > 0);
-- 16.- Drop the EMP2 and DEPT2 tables so that they cannot be restored. Verify
the recycle bin.
describe emp2;
SELECT original_name, operation, droptime FROM recyclebin;
-drop table emp2 purge;
flashback table emp2 to before drop;
-###############################################
###############################
-- 2.- Describe the structure of the MY_EMPLOYEE table to identify the column
names
describe my_employees;
drop table my_employees;
describe my_employees;
create table my_employees(
ID NUMBER(6) primary key,
LAST_NAME VARCHAR2(25) not null,
FIRST_NAME VARCHAR2(20),
USERID VARCHAR2(20),
SALARY NUMBER(8,2)
);
-- 3.- Create an INSERT statement to add the first row of data to the
MY_EMPLOYEE table
--from the following sample data. Do not list the columns in the INSERT clause.
Do not enter all rows yet.
insert into my_employees values(1,'Patel', 'Ralph', 'rpatel', 895);
-- 4.- Populate the MY_EMPLOYEE table with the second row of the sample data
from the preceding list.
-- This time, list the columns explicitly in the INSERT clause.
-- 6.- Write an INSERT statement in a dynamic reusable script file to load the
remaining rows into the MY_EMPLOYEE table.
-- The script should prompt for all the columns (ID,LAST_NAME, FIRST_NAME,
USERID, and SALARY).
-- Save this script to a lab_09_06.sql file.
insert into my_employees (ID, LAST_NAME, FIRST_NAME, USERID, SALARY)
values(&ID, '&LAST_NAME', '&FIRST_NAME', '&USERID', &SALARY);
-- 7.- Populate the table with the next two rows of the sample data listed in step
3 by
-- running the INSERT statement in the script that you created.
select 'OK - Si funciono el script' from dual;
--- 11) Change the salary to $1,000 for all employees who have a salary less
than $900.
update my_employees set SALARY = 1000 where SALARY < 900;
-###############################################
###############################
-- 26JUL2016
-- Practice 11-1: Creating Other Schema Objects -- Part 1
-- 1.- The staff in the HR department wants to hide some of the data in the
EMPLOYEES table.
-- Create a view called EMPLOYEES_VU based on the employee numbers,
-- employee last names, and department numbers from the EMPLOYEES table.
-- 2.- Confirm that the view works. Display the contents of the EMPLOYEES_VU
view.
select * from employee;
-- 3.- Using your EMPLOYEES_VU view, write a query for the HR department to
display all employee names and department numbers.
select last_name, DEPARTMENT_ID from employee;
-- 4.- Department 50 needs access to its employee data. Create a view named
DEPT50 that
-- contains the employee numbers, employee last names, and department
numbers for
-- all employees in department 50. You have been asked to label the view
columns
-- EMPNO, EMPLOYEE, and DEPTNO. For security purposes, do not allow an
employee to
-- be reassigned to another department through the view.
create view DEPT50 (EMPNO, EMPLOYEE, DEPTNO) as
select employee_id, last_name, DEPARTMENT_ID FROM employees where
DEPARTMENT_ID = 50;
-- part 2
-- 7.- You need a sequence that can be used with the PRIMARY KEY column of
the DEPT table.
-- The sequence should start at 200 and have a maximum value of 1,000.
-- Have your sequence increment by 10. Name the sequence DEPT_ID_SEQ.
create sequence DEPT_ID_SEQ start with 200 increment by 10 maxvalue 1000;
-- 8.- To test your sequence, write a script to insert two rows in the DEPT table.
-- Name your script lab_11_08.sql. Be sure to use the sequence that you
created for the ID column.
-- Add two departments: Education and Administration. Confirm your
additions.
-- Run the commands in your script.
select * from dept;
describe dept;
insert into dept (id, dname) values (dept_id_seq.nextval, 'Education');
insert into dept (id, dname) values (dept_id_seq.nextval, 'Administration');
-- 9.- Create a nonunique index on the NAME column in the DEPT table.
alter table dept add constraint no_repeat_name unique(dname);
alter table dept drop constraint no_repeat_name;
-###############################################
###############################
-- Practice 11-1: Creating Other Schema Objects -- Part 2 - to do in other place