0% found this document useful (0 votes)
23 views

SQL Queuries

Uploaded by

ay34345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

SQL Queuries

Uploaded by

ay34345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

...1,2,3,4\semester 4 data\Ayesha\Database LAB\SQLQuery1.

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

--A4 Display the job id of all the employees


select job_id 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'

--A8 Display details of all st_clerks, ad_vp and salesmen (sa_man)


select * from employees
where job_id IN ('ST_CLERK' , 'AD_VP' , 'SA_MAN')

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

--A19 How many people have a salary greater than 20,000?


select count(employee_id) as 'Total employees having salary>20000' from employees
where salary>20000

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

--A21 How many employees are there in each department?


select department_id, count(*) as 'Total_employees'
from employees
group by department_id

--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')

--A27 Write statements to


--a) Show all the records from table EMPLOYEE_TEST
select * from EMPLOYEE_TEST
--b) Show all the employees who’s job is ‘CLERK’
select * from EMPLOYEE_TEST
where JOB='CLERK'
--c) Display the no of employees who are earning commission more then 1000
select count(*) as 'No. of Employees'
from EMPLOYEE_TEST
where COMM>1000
--d) Display name and HIREDATE of the employees who’s name starts with ‘n’ and has ‘e’
as second last character
select EMPNAME, HIREDATE from EMPLOYEE_TEST
where EMPNAME Like 'N_e%'
--e) Display Gross Salary of each employee along with employee’s name (hint: gross
salary = sal + comm)
select EMPNAME, SAL+ isnull(COMM,0) as 'gross salary' from EMPLOYEE_TEST
--f) Display all the different job titles, which currently exist in the company
select Distinct JOB
from EMPLOYEE_TEST
--g) Display the department and number of employees in departments with fewer than 6
employees
select DEPTNO, Count(EMPID) as 'No of employees'
from EMPLOYEE_TEST
group by DEPTNO
having Count(EMPID)<6
--h) How many people are there in each type of job in each department?
select DEPTNO, JOB, count(*) as 'Total_employees'
from EMPLOYEE_TEST
group by DEPTNO, JOB
--i) Find the employee name and its salary who is earning maximum salary in department
20
select EMPNAME,SAL from EMPLOYEE_TEST
where DEPTNO=20 and SAL =(select max(SAL) from EMPLOYEE_TEST)
--j) Display the no of employees in each department
select DEPTNO,count(*) as 'No of employees'
from EMPLOYEE_TEST
group by DEPTNO

--A28 Write statements to


--a) Delete all employees having NULL commission.
Delete from EMPLOYEE_TEST
where COMM=NULL
--b) Delete all employees whose salary is not between 9000 and 20000
Delete from EMPLOYEE_TEST
where SAL NOT BETWEEN 9000 and 20000
--c) Delete the employees those were employed before 01-jan-2001
Delete from EMPLOYEE_TEST
...1,2,3,4\semester 4 data\Ayesha\Database LAB\SQLQuery1.sql 5
where HIREDATE < '01-JAN-2001'
--d) Delete employees working in department 20
Delete from EMPLOYEE_TEST
where DEPTNO=20

--A29 Write statements to


--a) Change the department no. to 30 where job is not ‘CLERK’ or ‘SALESMAN’
Update EMPLOYEE_TEST
set DEPTNO=30
where JOB NOT IN('CLERK', 'SAL')
--b) Add 1000 to the salary of those employees earning commission greater than 2500
Update EMPLOYEE_TEST
set SAL=1000+SAL
where COMM>2500
--c) Change all salesmen to Manager, those getting commission greater than salary
Update EMPLOYEE_TEST
set JOB='MANAGER'
where COMM>SAL and JOB=SAL

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

--A35 Display the names of employees and their managers


select first_name,departments.manager_id
From employees LEFT OUTER JOIN departments
ON employees.department_id=departments.department_id
...1,2,3,4\semester 4 data\Ayesha\Database LAB\SQLQuery1.sql 6
--A36 Display the names, designations and department names of all the employees
CREATE VIEW EMPLOYEE_1
AS
SELECT first_name,last_name,job_id, department_id
from employees

SELECT first_name,last_name,job_id as designations, department_id as department_name


from EMPLOYEE_1

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

SELECT first_name,department_name from EMPLOYEE_2

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

select first_name,department_name from EMPLOYEE_3

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

You might also like