SQL Queries
SQL Queries
SQL Queries
SQL Queries
Prepared By Manjunath BT
Prepared By Manjunath BT
SQL Queries
Prepared By Manjunath BT
SQL Queries
SQL QUERIES
1) Display the details of all employees
SQL>Select * from emp;
2) Display the depart information from department table
SQL>select * from dept;
3) Display the name and job for all the employees
SQL>select ename,job from emp;
4) Display the name and salary for all the employees
SQL>select ename,sal from emp;
5) Display the employee no and totalsalary for all the employees
SQL>select empno,ename,sal,comm, sal+nvl(comm,0) as"total salary" from
emp
6) Display the employee name and annual salary for all employees.
SQL>select ename, 12*(sal+nvl(comm,0)) as "annual Sal" from emp
7) Display the names of all the employees who are working in depart
number 10.
SQL>select emame from emp where deptno=10;
8) Display the names of all the employees who are working as clerks and
drawing a salary more than 3000.
SQL>select ename from emp where job='CLERK' and sal>3000;
9) Display the employee number and name who are earning comm.
SQL>select empno,ename from emp where comm is not null;
10) Display the employee number and name who do not earn any comm.
SQL>select empno,ename from emp where comm is null;
Prepared By Manjunath BT
SQL Queries
Prepared By Manjunath BT
SQL Queries
Prepared By Manjunath BT
SQL Queries
Prepared By Manjunath BT
SQL Queries
39) Display empno,ename,deptno,sal sort the output first base on name and
within name by deptno
and with in deptno by sal.
SQL>select empno,ename,deptno,sal from emp order by
40) Display the name of the employee along with their annual
salary(sal*12).The name of the
employee earning highest annual salary should appear first.
SQL>select ename,sal*12 from emp order by sal desc;
41) Display name,salary,hra,pf,da,total salary for each employee. The
output should be in the order
of total salary,hra 15% of salary,da 10% of salary,pf 5% salary,total salary
will
be(salary+hra+da)-pf.
SQL>select ename,sal,sal/100*15 as hra,sal/100*5 as pf,sal/100*10 as
da, sal+sal/100*15+sal/100*10-sal/100*5 as total from emp;
42) Display depart numbers and total number of employees working in each
department.
SQL>select deptno,count(deptno)from emp group by deptno;
43) Display the various jobs and total number of employees within each job
group.
SQL>select job,count(job)from emp group by job;
44) Display the depart numbers and total salary for each department.
SQL>select deptno,sum(sal) from emp group by deptno;
45) Display the depart numbers and max salary for each department.
SQL>select deptno,max(sal) from emp group by deptno;
Prepared By Manjunath BT
SQL Queries
46) Display the various jobs and total salary for each job
SQL>select job,sum(sal) from emp group by job;
47) Display the various jobs and total salary for each job
SQL>select job,min(sal) from emp group by job;
48) Display the depart numbers with more than three employees in each
dept.
SQL>select deptno,count(deptno) from emp group by deptno having
count(*)>3;
49) Display the various jobs along with total salary for each of the jobs
where total salary is greater
than 40000.
SQL>select job,sum(sal) from emp group by job having sum(sal)>40000;
50) Display the various jobs along with total number of employees in each
job. The output should
contain only those jobs with more than three employees.
SQL>select job,count(empno) from emp group by job having count(job)>3
51) Display the name of the empployee who earns highest salary.
SQL>select ename from emp where sal=(select max(sal) from emp);
52) Display the employee number and name for employee working as clerk
and earning highest
salary among clerks.
SQL>select empno,ename from emp where where job='CLERK'and
sal=(select max(sal) from
emp where job='CLERK');
Prepared By Manjunath BT
SQL Queries
53) Display the names of salesman who earns a salary more than the
highest salary of any clerk.
SQL>select ename,sal from emp where job='SALESMAN' and sal>(select
max(sal) from emp
where job='CLERK');
54) Display the names of clerks who earn a salary more than the lowest
salary of any salesman.
SQL>select ename from emp where job='CLERK' and sal>(select
min(sal)from emp
where job='SALESMAN');
**) Display the names of employees who earn a salary more than that of
Jones or that of salary
greater than that of scott.
SQL>select ename,sal from emp where sal>(select sal from emp where
ename='JONES')and
sal> (select sal from emp where ename='SCOTT');
55) Display the names of the employees who earn highest salary in their
respective departments.
SQL>select ename,sal,deptno from emp where sal in(select max(sal) from
emp group by
deptno);
56) Display the names of the employees who earn highest salaries in their
respective job groups.
SQL>select ename,sal,job from emp where sal in(select max(sal) from emp
group by job)
Prepared By Manjunath BT
SQL Queries
10
Prepared By Manjunath BT
SQL Queries
11
Prepared By Manjunath BT
SQL Queries
12
Prepared By Manjunath BT
SQL Queries
13
80) Display the common jobs from department number 10 and 20.
SQL>select job from emp where deptno=10 and job in(select job from emp
where deptno=20);
81) Display the jobs found in department 10 and 20 Eliminate duplicate
jobs.
SQL>select distinct(job) from emp where deptno=10 or deptno=20
(or)
SQL>select distinct(job) from emp where deptno in(10,20);
82) Display the jobs which are unique to department 10.
SQL>select distinct(job) from emp where deptno=10
join
83) Display the details of those who do not have any person working under
them.
SQL>select e.ename from emp,emp e where emp.mgr=e.empno group by
e.ename having
count(*)=1;
84) Display the details of those employees who are in sales department and
grade is 3.
SQL>select * from emp where deptno=(select deptno from dept where
dname='SALES')and sal between(select losal from salgrade where
grade=3)and
(select hisal from salgrade where grade=3);
85) Display those who are not managers and who are managers any one.
i)display the managers names
SQL>select distinct(m.ename) from emp e,emp m where m.empno=e.mgr;
Prepared By Manjunath BT
SQL Queries
14
Prepared By Manjunath BT
SQL Queries
15
93)Display those employees whose salary greater than his manager salary.
SQL>select p.ename from emp e,emp p where e.empno=p.mgr and
p.sal>e.sal
94) Display those employees who are working in the same dept where his
manager is work.
SQL>select p.ename from emp e,emp p where e.empno=p.mgr and
p.deptno=e.deptno;
95) Display those employees who are not working under any manager.
SQL>select ename from emp where mgr is null
96) Display grade and employees name for the dept no 10 or 30 but grade is
not 4 while joined the
company before 31-dec-82.
SQL>select ename,grade from emp,salgrade where sal between losal and
hisal and deptno
in(10,30) and grade<>4 and hiredate<'31-DEC-82';
97) Update the salary of each employee by 10% increment who are not
eligiblw for commission.
SQL>update emp set sal=sal+sal*10/100 where comm is null;
98) SELECT those employee who joined the company before 31-dec-82
while their dept location is
newyork or Chicago.
SQL>SELECT EMPNO,ENAME,HIREDATE,DNAME,LOC FROM EMP,DEPT WHERE
(EMP.DEPTNO=DEPT.DEPTNO)AND HIREDATE <'31-DEC-82' AND DEPT.LOC
IN('CHICAGO','NEW
YORK');
Prepared By Manjunath BT
SQL Queries
16
Prepared By Manjunath BT
SQL Queries
17
Prepared By Manjunath BT
SQL Queries
18
Prepared By Manjunath BT
SQL Queries
19
Prepared By Manjunath BT
SQL Queries
20
Prepared By Manjunath BT
SQL Queries
21
Prepared By Manjunath BT
SQL Queries
22
SQL>SELECT
SUBSTR(LOWER(ENAME),1,3)||SUBSTR(UPPER(ENAME),3,LENGTH(ENAME))
FROM EMP;
136) Display the 10th record of emp table without using group by and
rowid?
SQL>SELECT * FROM EMP WHERE ROWNUM<11 MINUS SELECT * FROM EMP
WHERE
ROWNUM<10
**)Delete the 10th record of emp table.
SQL>DELETE FROM EMP WHERE EMPNO=(SELECT EMPNO FROM EMP WHERE
ROWNUM<11
MINUS SELECT EMPNO FROM EMP WHERE ROWNUM<10)
137) Create a copy of emp table;
SQL>create table new_table as select * from emp where 1=2;
138) Select ename if ename exists more than once.
SQL>select ename from emp e group by ename having count(*)>1;
139) Display all enames in reverse order?(SMITH:HTIMS).
SQL>SELECT REVERSE(ENAME)FROM EMP;
140) Display those employee whose joining of month and grade is equal.
SQL>SELECT ENAME FROM EMP WHERE SAL BETWEEN (SELECT LOSAL FROM
SALGRADE
WHERE GRADE=TO_CHAR(HIREDATE,'MM')) AND (SELECT HISAL FROM
SALGRADE
WHERE GRADE=TO_CHAR(HIREDATE,'MM'));
141) Display those employee whose joining DATE is available in deptno.
SQL>SELECT ENAME FROM EMP WHERE TO_CHAR(HIREDATE,'DD')=DEPTNO
Prepared By Manjunath BT
SQL Queries
23
Prepared By Manjunath BT
SQL Queries
24
(or)
Disable the constraint by using alter table emp modify constraint chk_001
disable;
151) My boss has changed his mind. Now he doesn't want to pay more than
10,000.so revoke that
salary constraint.
SQL>alter table emp modify constraint chk_001 enable;
152) Add column called as mgr to your emp table;
SQL>alter table emp add(mgr number(5));
153) Oh! This column should be related to empno. Give a command to add
this constraint.
SQL>ALTER TABLE EMP ADD CONSTRAINT MGR_DEPT FOREIGN KEY(MGR)
REFERENCES
EMP(EMPNO)
154) Add deptno column to your emp table;
SQL>alter table emp add(deptno number(5));
155) This deptno column should be related to deptno column of dept table;
SQL>alter table emp add constraint dept_001 foreign key(deptno)
reference dept(deptno)
[deptno should be primary key]
156) Give the command to add the constraint.
SQL>alter table <table_name) add constraint <constraint_name>
<constraint type>
157) Create table called as newemp. Using single command create this table
as well as get data
into this table(use create table as);
Prepared By Manjunath BT
SQL Queries
25
Prepared By Manjunath BT
SQL Queries
26
Prepared By Manjunath BT
SQL Queries
27
------******------
Prepared By Manjunath BT