Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Rdbms and SQL: Name:Ibrahim Sameer V S REG NO:17BCA0062

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 33

RDBMS AND SQL

NAME:IBRAHIM SAMEER V S

REG NO:17BCA0062
*INTRODUCTION TO RDBMS

1. select*FROM HR.EMPLOYEES
*RETRIVING THE DATA USING SELECT COMMAND

1. Determine the structure of the DEPARTMENTS table and its content

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.

SELECT employee_ID,last_name,job_ID,HIRE_DATE as FIRSTNAME from hr.employees;


3 . Create a query to display all unique job IDs from the EMPLOYEES
table.

SELECT distinct job_ID from hr.employees;

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.

Select employee_ID as EMP_#,last_name as Employee,Job_ID as Job,hire_date as


Hire_Date from hr.employees;
5. Create 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 as "Employee and Title"

Select last_name||','||job_ID"Employee and Title" from hr.employees;

*RESTRICTING AND SORTING DATA

1.Create a report that displays the last name and salary of employees who earn
more than $12,000.

Select last_name, salary from hr.employees where salary> 12000 ;


2.Create a report that displays the last name and department number for employee
number 176.
Select last_name, department_ID from hr.employees where employee_ID=176 ;

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;

5. Display the last name and department ID of all employees in departments 20 or


50 in ascending alphabetical order by name.
select last_name,Department_ID from hr.employees where Department_ID = '20'
OR Department_ID ='50' order by Last_Name asc;
6. List employees who earn between $5,000 and $12,000, and are in department 20
or 50. Label the columns as Employee and Monthly Salary, respectively.
select employee_id as Employee, CONCAT(CONCAT(first_name,' '), last_name) as
Name, salary as"Monthly Salary" from hr.employees where salary BETWEEN
'5000' AND '12000' AND department_id= 20 OR department_id = 50;

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;

2.Display the employee names(First Name) and their year of joining


select first_name as Name, EXTRACT(YEAR from hire_date) as "Year of Joining"
from hr.employees;
3.Display the character position of "O" in the string WIPRO.
select INSTR('WIPRO','O') from dual;

*REPORTIN AGGREGATED DATA USING GROUP FUCTION

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;

*DSPLAYING THE DATA USING MULTIPLE TIMES USING JOINS

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;

*MANIPULAYING THE DATA

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

Create table MY_EMPLOYEE_9651043 as Select

employee_id,first_name,last_name,department_id,salary from hr.EMPLOYEES


where 1=2;
2.Test the table creation by viewing the structure using describe command
3.Insert one record without listing the column names in the insert statement. Check
whether data is inserted
INSERT INTO MY_EMPLOYEE_9651043 values(201, 'Michael', 'Hartstein',
20,13000);
SELECT * FROM MY_EMPLOYEE_9651043

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;

8.Create a query to update Last_name of emp 202 to Higgins


update my_employee_9651043 set last_name = 'Higgins' where employee_id =
202;
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

create table Dept(


DEPT_ID number(7),
constraint dep_id primary key(DEPT_ID));
create table EMP_9652149(
ID number(7),
Last_Name varchar2(25),
First_Name varchar2(25),
DEPT_ID number(7),
constraint id primary key(ID),
constraint d_id foreign key(DEPT_ID)
references Dept(DEPT_ID));
insert into dept values(10);
insert into dept values(20);
insert into dept values(40);
select * from dept;
insert into emp_9651043 values(101,'Sam','Sundar',10);
insert into emp_9651043 values(102,'Gopi',null,40);
insert into emp_9651043 values(103,null,'ram',20);
select * FROM emp_9651043;

*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.

select * from EMPLOYEES_VU_9651043;

3.Department 50 needs access to its employee data. Create a view named


DEPT50_UserID 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. Test your view. Attempt to reassign Matos to department 80.
create or replace view DEPT50_9652149(EMPNO,EMPLOYEE,DEPTNO) as select
employee_id,last_name,department_id from hr.employees where department_id
= 50 with check option constraint dept50_ok;
SELECT * from DEPT50_9651043;
update DEPT50_9651043 set deptno = 80 where employee = 'Matos';

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

You might also like