Rdbms and SQL: Name:Ibrahim Sameer V S REG NO:17BCA0062
Rdbms and SQL: Name:Ibrahim Sameer V S REG NO:17BCA0062
Rdbms and SQL: Name:Ibrahim Sameer V S REG NO:17BCA0062
NAME:IBRAHIM SAMEER V S
REG NO:17BCA0062
*INTRODUCTION TO RDBMS
1. select*FROM HR.EMPLOYEES
*RETRIVING THE DATA USING SELECT COMMAND
SELECT*FROM HR.DEPARTMENTS
2. Create a query to display the last name, job ID, hire date, and employee ID for
each employee, with the employee ID appearing first. Provide an alias
STARTDATE for the HIRE_DATE column.
4. Create a query to display employee id, last name, job id and hiredate from
employee table with more describing column names. Name the column headings
Emp # , Employee , Job and Hire Date respectively.
1.Create a report that displays the last name and salary of employees who earn
more than $12,000.
3.To find high-salary and low-salary employees. Create a query to display the last
name and salary for any employee whose salary is not in the range of $5,000 to
$12,000
select last_name, salary from hr.employees where salary not between '5000' and
'12000' order by salary;
4. Create a report to display the last name, job ID, and hire date for employees with
the last names of Matos and Taylor. Order the query in ascending order by the hire
date.
select last_name,job_ID,HIRE_DATE from hr.employees where last_name=
'Matos' OR last_name='Taylor' order by HIRE_DATE;
7. Create a report that displays the last name and hire date for all employees who
were hired in 1994.
select last_name,HIRE_DATE from hr.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.
select last_name,Job_ID from hr.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 based on salary and
commissions. Use the column’s numeric position in the ORDER BY clause.
select last_name, salary, commission_pct from hr.employees where
commission_pct IS NOT NULL order by 2,3 desc;
10. Create a report that displays the last name and salary of employees who earn
more than an amount that the user specifies after a prompt. If you enter 12000, it
should display all employees earning more than 12000. Eg: Salary_value: 12000
select last_name, salary from hr.employees where salary > &salary_value;
11. 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 and
prompts a column name by which result should be sorted. Eg: manager_id :103
sorted_by : last_name
select employee_id,last_name,salary,department_id from hr.employees where
manager_id =&manager_id order by &sorted_by;
12. Display all employee last names in which the third letter of the name is “a”.
select last_name from hr.employees where last_name like '__a%';
13. Display the last names of all employees who have both an “a” and an “e” in
their last name.
select last_name from hr.employees where last_name like '%a%'AND last_name
like '%e%' ;
14. Display the last name, job, and salary for all employees whose jobs are either
those of a sales representative or of a stock clerk, and whose salaries are not equal
to $2,500, $3,500, or $7,000.
select last_name, job_id, salary from hr.employees where salary != 2500 AND
salary != 3500 AND salary != 7000 AND job_id IN (select job_id from hr.employees
where job_id = 'SA_REP' OR job_id ='ST_CLERK';
*USING SINGLE ROW FUNCTION TO CUSTOMIZE THE OUTPUT
1.Display first names(First letter in caps) and their jobs(in Uppercase) of all
employees. Solution:
select INITCAP(First_name) as "First Name", UPPER(last_name) as Job from
hr.employees;
1.Find the highest, lowest, sum, and average salary of all employees. Label the
columns as Maximum, Minimum, Sum, and Average, respectively. Run the query.
select max(salary) as Maximum, min(salary) as Minimum, sum(salary) as Sum,
avg(salary) as Average from hr.employees;
2.Modify the above query to display the minimum, maximum, sum, and average
salary for each job type.
select job_id as "Job Type", max(salary) as Maximum, min(salary) as Minimum,
sum(salary) as Sum,avg(salary) as Average from hr.employees group by job_id;
3.Determine the number of managers without listing them. Label the column as
Number of Managers.
select count(manager_id) as "Number of Managers" from hr.employees;
4.Find the difference between the highest and lowest salaries. Label the column
DIFFERENCE.
select max(salary)-min(salary) as DIFFERENCE from hr.employees;
5.Create a report to display the manager number and the salary of the lowest-paid
employee for that manager. Exclude anyone whose manager is not known. Exclude
any groups where the minimum salary is $6,000 or less. Sort the output in
descending order of salary.
select manager_id, min(salary) from hr.employees where manager_id IS NOT
NULL GROUP BY manager_id HAVING min(salary) > 6000 order by min(salary)
desc;
1.Write a query to produce the addresses of all the departments. Use the
LOCATIONS and COUNTRIES tables. Show the location ID, street address, city,
state or province, and country in the output. Use a NATURAL JOIN to produce the
results.
select distinct department_id, location_id, street_address, city, state_province,
country_id from hr.departments NATURAL JOIN hr.locations NATURAL JOIN
hr.countries order by department_id;
2.Create a report of only those employees with corresponding departments. Write a
query to display the last name, department number and department name for these
employees.
select last_name, department_id, department_name from hr.employees
NATURAL JOIN hr.departments;
3.Create a report of employees in Toronto. Display the last name, job, department
number and the department name for all employees who work in Toronto.
select last_name, job_id, department_id, department_name from hr.employees
NATURAL JOIN hr.departments NATURAL JOIN hr.locations where city = 'Toronto';
4.Create a report to display employee's last name and employee number along with
their manager’s last name and manager number. Label the columns Employee ,
Emp# , Manager , and Mgr# , respectively.
select e.employee_id as "Emp#", e.last_name as Employee, m.employee_id as
"Mgr#", m.last_name as Manager from hr.employees e join hr.employees m on
e.manager_id = m.employee_id;
5.The HR department wants to determine the names of all the employees who were
hired after Davies. Create a query to display the name and hire date of any
employee hired after employee Davies.
select CONCAT(CONCAT(first_name,' '), last_name) as Name, hire_date as "Hire
Date" from hr.employees where hire_date > (select hire_date from hr.employees
where last_name = 'Davies') order by hire_date;
6.The HR department needs to find the names and hire dates for all employees who
were hired before their managers, along with their managers’ names and hire dates.
select e.employee_id as "Emp#",e.hire_date, m.first_name as Manager,
m.hire_date from hr.employees e join hr.employees m on e.manager_id =
m.employee_id where e.hire_date < m.hire_date;
*USING SUB QUERIES TO SOLVE QUERIES
1.Display the first name & salary of all the employees who are getting salary
greater than "Bruce".
select first_name, salary from hr.employees where salary > (select salary from
hr.employees where first_name = 'Bruce');
2.Display the last name,department ID,job & salary of those employees who are
working in the same department of "Neena" and having the same job.
select last_name, department_id, job_id, salary from hr.employees where
department_id = (select department_id from hr.employees where first_name =
'Neena') AND job_id = (select job_id from hr.employees where first_name =
'Neena');
3.Display the first name & salary of all the employees who are getting salary less
than the average salary of the company.
select first_name, salary from hr.employees where salary < (select avg(salary)
from hr.employees);
4.Display the first Name & salary of the employees who are getting salary greater
than that of both "Bruce" and "Alexander"
select first_name, salary from hr.employees where salary > (select salary from
hr.employees where first_name = 'Bruce') AND salary IN (select salary from
hr.employees where first_name = 'Alexander');
5.Display the first name & salary of the employees who are getting salary equal to
that of either "Neena" or "John".
select first_name,salary from hr.employees where salary = (select salary from
hr.employees where first_name = 'Neena') OR salary IN (select salary from
hr.employees where first_name = 'John');
6.Display the deptno and average salary of the dept which is less than the average
salary of the organization.
select department_id, avg(salary) from hr.employees group by department_id
having avg(salary) < (select avg(salary) from hr.employees);
7.Display the details of departments which do not have any employees.
select * from hr.departments where manager_id is null;
1.Run the below script replacing "UserID" with ResumeID for PJPians or ADID for PRPians.
e.g. If your ResmeID or ADID is "AB121212" then the table name will be
MY_EMPLOYEE_AB121212
4.Insert one record without listing the column names in the insert statement where
salary value remain undetermined. Check whether data is inserted
Eg: employee_id first_name last_name department_id salary 201 Michael
Hartstein 20 13000 202 Pat Fay 20 (null)
insert into my_employee_9651043values(202,'Pat','Fay',20,null);
SELECT * FROM my_employee_9651043;
5.Insert one record with listing the column names avoiding salary column in the
insert statement where salary value remain undetermined. Check whether data is
inserted employee_id first_name last_name department_id salary 201 Michael
Hartstein 20 13000 202 Pat Fay 20 (null) 203 Susan Mavris 40 (null)
insert into
my_employee_9651043(employee_id,first_name,last_name,department_id,salar
y) values(203,'Susan','Marvis',40,null);
select * from my_employee_9651043;
6.Use the above Script to insert the below given records employee_id first_name
last_name department_id salary
insert into my_employees_9651043 values(205,'Shelley','Higgins',110,12000);
insert into my_employees_9651043 values(100,'Steven','King',90,24000);
insert into my_employees_9651043 values(101,'Neena','Kochhar',90,17000);
insert into my_employees_9651043 values(102,'Lex De','Haan',90,17000);
insert into my_employees_9651043 values(111,'Ismael','Sciarra',100,7700);
insert into my_employees_9651043 values(112,'Jose Manuel','Urman',100,7800);
insert into my_employees_9651043 values(204,'Hermann','Baser',70,10000)
SELECT * FROM my_employees_9651043;
7.Create a query to increase salary by 10% for all employees in dept 90. Solution:
update my_employee_9651043 set salary=(salary/100)*10+salary where
department_id = 90;
select * FROM my_employee_9651043;
9.Delete employees whose name either first or last name has char seq of ‘man’
delete from my_employee_9652149 where first_name like '%man%' OR
last_name like '%man%';
select * from my_employee_9652149;
*DDL STATEMENTS
1.Create the DEPT_UserID table based on the following table instance chart. Save
the statement in a script called lab_10_01.sql , and then execute the statement in
the script to create the table. Confirm that the table is created. Specification
Values: Column named Dept_ID of Numeric 7 size and would be a primary key.
Column named Dept_Name of varchar2 size 20.
create table DEPT_9651043(
Dept_ID number(7),
Dept_Name varchar2(20),
constraint department_id primary key(Dept_ID));
2.Populate the DEPT_UserID table with data from the DEPARTMENTS table.
Include only columns that you need. Insert dept Id 10 and Name Accounts Insert
dept Id as null and Name as TT Correct by giving 20 and TT Insert A1 as Id and
Accounts Correct by giving 30 and Accounts
insert into dept_9652149 values(10,'Accounts');
insert into dept_9652149 values(null,'TT');
insert into dept_9652149 values(20,'TT');
3.Create the EMP_UserID table based on the following table instance chart. Save
the statement in a script called lab_10_03.sql , and then execute the statement in
the script to create the table. Confirm that the table is created. Specification-
Values Column Name: ID, LAST_NAME, FIRST_NAME, DEPT_ID Key Type:
PK, -, -, FK Nulls /Unique: -, Not null, -, -, FK Table: -, -, -, Dept FK Column: -, -,
-, ID Data type: NUMBER, VARCHAR2, VARCHAR2, NUMBER Length: 7, 25,
25, 7 Insert 101,Sam,Sundar,10 Insert 101,Ram,Krishna,20 Insert 102,Gopi,null,40
Insert 103,null,ram,20
*SCHEMA OBJECT
1.The staff in the HR department wants to hide some of the data in the
EMPLOYEES table. Create a view called EMPLOYEES_VU_UserID based on
the employee numbers, employee last names, and department numbers from the
EMPLOYEES table. The heading for the employee name should be EMPLOYEE.
Confirm that the view works. Display the contents of the
EMPLOYEES_VU_UserID view.
create or replace view EMPLOYEES_VU_9651043
(EMPLOYEE_NUMBER,EMPLOYEE,DEPARTMENT_NUMBER) as select
employee_id,last_name,department_id from hr.employees;
2.Populate the DEPT_UserID table with data from the DEPARTMENTS table.
Include only columns that you need.
4.You need a sequence that can be used with the PRIMARY KEY column of the
DEPT_UserID 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_UserID