SQL Queuries
SQL Queuries
sql 1
--A1
--Display all the contents of the departments, employees and jobs tables
select * from departments
select * from employees
select * from jobs
--A2
--Display the name and commission of all the employees
select first_name,last_name,commission_pct from employees
--A3 Display the name and commission of all the employees together with
--another column that shows their commission increased by 10%
select first_name,last_name,commission_pct as 'old_commission',(commission_pct+10) as
'increased_commission' from employees
--A5 Display all the different job titles which currently exist in the company
select DISTINCT job_id from employees
--A6 Display the employee number, name and current job of all those who work in
Department 30
select employee_id,first_name,last_name, job_id from employees
where department_id=30
--A7 Display the names of all the IT Programmer, showing their employee number and
that of their manager id
select first_name,last_name, employee_id, manager_id from employees
where job_id = 'IT_PROG'
--A9 Display details of employees who are not st_clerks, ad_vp or salesmen
select * from employees
where job_id NOT IN ('ST_CLERK' , 'AD_VP' , 'SA_MAN')
--A10 Display details of all employees whose commission is greater than salary
select * from employees
where commission_pct>salary
--A11 Display employee name, job and department number for employees whose names begin
with ‘N’
select first_name,last_name,job_id,department_id
from employees
where first_name LIKE 'N%'
--A12 Display details of employees whose salaries are not between 12,000 and 14,000
select * from employees
where salary NOT between 12000 and 14000
...1,2,3,4\semester 4 data\Ayesha\Database LAB\SQLQuery1.sql 2
--A13 Display details of sales representative (sa_rep) and managers in department 80,
whose salary is
--greater than or equal to 7,000 (Note: with logical operators - AND precedes OR)
select * from employees
where job_id IN ('SA_REP','SA_MAN') or department_id=80 and salary>=7000
--A14 Display the employee number, current job and salary of all those
--who work in Department 50, with the output in ascending salary order
select employee_id, job_id, salary from employees
where department_id=50
order by salary asc
--A15 Display the employee name and current job of all those who work
--in Department 50, with the output in descending salary order
select first_name,last_name, job_id, salary from employees
where department_id=50
order by salary desc
--A16 Display the employee name and current job of all those who work in Department
50,
--with the output in descending salary order within each job
select first_name,last_name, job_id, salary from employees
where department_id=50
order by job_id asc,salary desc
--A17 Display employee details for departments 90 and 30, in name order, within each
department
select * from employees
where department_id IN (90,30)
order by first_name , last_name
--A18 What are the lowest and highest basic salaries within the company?
select min(salary) as 'Min_Salary' , max(salary) as 'Max_Salary' from employees
--A20 What are the highest and lowest incomes (i.e. to include commission) in the
Department 80?
select min(salary+commission_pct) as 'Lowest income' , max(salary+commission_pct) as
'highest income'
from employees
where department_id=80
--A22 How many employees are there in each type of job in each department?
...1,2,3,4\semester 4 data\Ayesha\Database LAB\SQLQuery1.sql 3
select department_id,job_id, count(*) as 'Total_employees'
from employees
group by department_id, job_id
--A23 For each department, find the average salary and the total salary bill excluding
commission
select department_id, AVG(salary) as 'Average_salary', SUM(salary) as 'Total Salary
Bill'
from employees
group by department_id
--A24 Find the maximum commission earned, and the number of people in each department
select department_id, max(commission_pct) as 'Max Commission earned', count
(employee_id) as 'Total no of people'
from employees
group by department_id
--A25 Display the department number and number of employees in departments with fewer
than 6 employees
select department_id, count(employee_id) as 'no. of employess'
from employees
group by department_id
having count(employee_id)<6
--A26
--a) Write statement to create table EMPLOYEE_TEST with these fields.
create Table EMPLOYEE_TEST
(
EMPID INT not Null,
EMPNAME varchar(50) not Null,
JOB varchar(20) not Null,
HIREDATE date not Null,
SAL INT not Null,
COMM INT,
DEPTNO INT not Null,
PRIMARY KEY (EMPID)
)
--b) write statements to insert the following data into the table just created
insert into EMPLOYEE_TEST (EMPID,EMPNAME,JOB,HIREDATE,SAL,COMM,DEPTNO)
values('7369', 'Abid', 'CLERK', '17-DEC-2000', '16000', NULL,
'20')
insert into EMPLOYEE_TEST (EMPID,EMPNAME,JOB,HIREDATE,SAL,COMM,DEPTNO)
values('7499', 'Hafeez', 'SALESPERSON', '20-FEB-2001', '38000',
'2000', '30')
insert into EMPLOYEE_TEST (EMPID,EMPNAME,JOB,HIREDATE,SAL,COMM,DEPTNO)
values('7521', 'Namrah', 'SALESPERSON', '22-FEB-2001', '38000',
'3500', '30')
insert into EMPLOYEE_TEST (EMPID,EMPNAME,JOB,HIREDATE,SAL,COMM,DEPTNO)
values('7566', 'Nadeem', 'MANAGER', '02-APR-2001', '75000', NULL,
'20')
insert into EMPLOYEE_TEST (EMPID,EMPNAME,JOB,HIREDATE,SAL,COMM,DEPTNO)
values('7654', 'Fatima', 'SALESPERSON', '28-SEP-2001', '18700',
...1,2,3,4\semester 4 data\Ayesha\Database LAB\SQLQuery1.sql 4
'2500', '30')
--A30
--a) Delete all records of Employee
Drop TABLE EMPLOYEE_TEST
--b) Destroy table EMPLOYEE
DELETE *FROM EMPLOYEE_TEST
--A31 Display the names, designations and department names of all the employees
select first_name,last_name,job_id,department_name
From employees INNER JOIN departments
ON employees.department_id=departments.department_id
--A32 Display all the employees along-with department names in which employees work
select first_name,department_name
From employees INNER JOIN departments
ON employees.department_id=departments.department_id
--A33 Display All the departments and only those employees who work in any department
select first_name,last_name,department_name
From departments LEFT OUTER JOIN employees
ON employees.department_id=departments.department_id
--A34 Display all the employees and departments along-with those employees who work in
any department
select first_name,last_name,department_name
From employees FULL OUTER JOIN departments
ON employees.department_id=departments.department_id
--A37 Display all the employees along-with department names in which employees work
create view EMPLOYEE_2
as
select first_name,department_name
From employees INNER JOIN departments
ON employees.department_id=departments.department_id
--A38 Display All the departments and only those employees who work in any department
create view EMPLOYEE_3
as
select first_name,last_name,department_name
From departments LEFT OUTER JOIN employees
ON employees.department_id=departments.department_id
--A39 Create a procedure that displays the names, designations and department names of
all the employees
create procedure E_Details_1
AS
SELECT first_name,last_name,job_id, department_id
from employees
exec E_Details_1
--A40 Create a procedure that displays all the employees along-with department names
in which employees work
create procedure E_Details_2
AS
select first_name,department_name
From employees INNER JOIN departments
ON employees.department_id=departments.department_id
exec E_Details_2
--A41 Create a procedure that displays All the departments and only those employees
who work in any department
create procedure E_Details_3
AS
select first_name,last_name,department_name
From departments LEFT OUTER JOIN employees
...1,2,3,4\semester 4 data\Ayesha\Database LAB\SQLQuery1.sql 7
ON employees.department_id=departments.department_id
exec E_Details_3