DB Queries
DB Queries
DB Queries
41) Display
name,salary,Hra,pf,da,TotalSalary for select ename,sal,(sal*15/100) as
each employee,The out put should be in HRA, (sal*10/100) as DA,
the order of total salary ,hra 15% of (sal*5/100) as PF, (sal+
salary ,DA 10% of salary .pf 5% salary (sal*15/100)+(sal*10/100)-
Total Salary will be (salary+hra+da)-pf? (sal*5/100)) as totsal from emp
select COUNT(empno) as
empcount,empno ,ename,job,mg
r from emp where mgr is NOT
null group by mgr
select mgr,count(empno) from
emp WHERE mgr IN (SELECT
empno FROM emp WHERE
job="MANAGER" )GROUP BY
count of employees under manager mgr
61) Display the names of employees from select ename from emp where
department number 10 with salary deptno=10 and sal>any(select
greater than that of ANY employee max(sal) from emp where deptno
working in other departments? not in (10))
62) Display the names of employees from select ename from emp where
department number 10 with salary deptno=10 and sal>(select
greater than that of ALL employee max(sal) from emp where deptno
working in other departments? not in (10))
63) Display the names of mployees in select upper(ename) from emp
Upper Case?
64) Display the names of employees in
Lower Case? select lower(ename) from emp
select
CONCAT(UCASE(SUBSTRING(`
ename`, 1,
65) Display the names of employees in 1)),LOWER(SUBSTRING(`enam
Proper case? e`, 2))) as ename from emp
66) Find the length of your name using select length('pavan kumar') from
Appropriate Function? DUAL
67) Display the length of all the employee select length(ename)
names? enamelength from emp
68) Display the name of employee select
Concatinate with Employee Number? concat(ename,empno)from emp
69) Use appropriate function and extract
3 characters starting from 2 characters
from the following string 'Oracle' i.e., the select substring('oracle',3,2) from
out put should be ac? DUAL
70) Find the first occurance of character select locate('a','Computer
a from the following string Computer Maintenance Corporation') from
Maintenance Corporation? DUAL
71) Replace every occurance of alphabet
A with B in the string .Alliens (Use select REPLACE('Alliens ','A','B')
Translate function)? from DUAL
72) Display the information from the select ename,
employee table . where ever job Manager REPLACE(job,'MANAGER','Boss
is found it should be displayed as Boss? ') as job from emp
SELECT
SELECT ename,empno,encod
73) Display empno,ename,deptno from ename,empno,decode(deptno,de e(deptno,dept.dnam
tvsemp table. Instead of display pt.dname) from emp join dept e) from emp join
department numbers USING(deptno) dept USING(deptno)
display the related department name(Use
decode function)?
To Display age in
days and age like
(28 yr 9 mth 28 dy)
select
DATE_FORMAT(FR
OM_DAYS(DATEDI
FF(CURRENT_DAT
E,'1993-05-02')),'%y
yr %c mth %e dy')
AS age,
select DATEDIFF(CURRE
DATEDIFF(CURRENT_DATE,'1 NT_DATE,'1993-05-
74) Display your Age in Days? 993-05-02') days from DUAL 02') days from DUAL
75) Display your Age in Months? SELECT
FLOOR((DATEDIFF(CURRENT_
DATE,'1993-05-02')/365)*12)+
FLOOR((DATEDIFF(CURDATE()
,'1993-05-02')/365 -
FLOOR(DATEDIFF(CURDATE(),
'1993-05-02')/365))* 12)
TotMonths FROM dual
select
Floor(DATEDIFF(CURRENT_DA
TE,'1993-05-02')/(365) )days
75)Display age in years from DUAL
day name
SELECT
dayname(CURRENT
_DATE) from DUAL
SELECT
monthname(CURRE
NT_DATE) from
DUAL
Sunday 29th
January 2017-
SELECT
date_format(CURRE
NT_DATE,'%W %D
%M %Y') from DUAL
SELECT
SELECT STR_TO_DATE('18,
76) Display current date as 15th August date_format(CURRENT_DATE,' 05,2009','%d,%m,
Friday Nineteen Ninety Seven? %W %D %M %Y') from DUAL %Y');
77) Display the following output for each
row from tvs emp table?
select concat(ename," HAS
JOINED THE COMPANY ON ")
as
title ,date_format(HIREDATE,'%
78) Scott has joined the company on 13th W %D %M %Y') d from EMP
August nineteen ninety? where hiredate='1996-03-05'
SELECT
DATE_ADD(CURDA
TE(), INTERVAL (9 -
IF(DAYOFWEEK(CU
SELECT RDATE())=1, 3,
DATE_ADD(CURDATE(), DAYOFWEEK(CUR
79) Find the nearest Saturday after INTERVAL (6) DAY) as DATE()))) DAY) as
Current date? NEXTSAT NEXTSAT
SELECT CURRENT_TIME from
80) Display the current time? DUAL
select
81) Display the date three months before date_add(CURRENT_DATE,INT
the Current date? ERVAL(-3)month) from dual
select job from emp where
82) Display the common jobs from deptno=10 and job in(select job
department number 10 and 20? from emp where deptno=20)
83) Display the jobs found in department SELECT DISTINCT(job) from
10 and 20 Eliminate duplicate jobs? emp where deptno in (10,20)
84) Display the jobs which are unique to SELECT DISTINCT(job) from
department 10? emp where deptno=10
select
e.ename,emp.mgr
from emp,emp e
where
85) Display the details of those select empno,ename,mgr from emp.mgr=e.empno
employees who do not have any person emp where empno not in (select group by e.ename
working under him? ifnull(mgr,0) from emp) having count(*)=1
select * from emp where
deptno=(select deptno from dept
where dname="Sales") And sal
BETWEEN (SELECT losal from
86) Display the details of those salgrade where grade=3)and
employees who are in sales department (SELECT hisal from salgrade
and grade is 3? where grade=3)
select ename from emp where
87) display those who are not managers job!="MANAGER"
88)select emp whose name is not less select ename from emp where
than 4 characters length(ename)>4
89) Display those department whose
name start with"S" while location name select * from dept where dname
ends with "K"? like'S%' and location like'%K' No data
select ename,mgr from emp
90) Display those employees whose where mgr in (select empno from
manager name is Jones? emp where ename="JONES")
91) Display those employees whose
salary is more than 3000 after giving select ename from emp where
20% increment? (sal+sal*(20/100))>3000
92) Display all employees with their select e.ename,d.dname from
department names? emp e join dept d using (deptno)
select ename from emp where
93)display employees who are in sales deptno in (select deptno from
dept dept where dname="Sales" )
select e.ename,
94) Display employee name,dept d.dname,sal,comm from emp e
name,salary,and commission for those join dept d on e.deptno=d.deptno
sal in between 2000 to 5000 while where sal between 2000 And
location is Chicago? 5000 and d.location="Chicago"
select p.ename from
95) Display those employees whose emp e,emp p where
salary is greater than his managers e.empno=p.mgr and
salary? p.deptno=e.deptno;
select * from emp e1
96) Display those employees who are select p.ename from emp e,emp join emp e2 on
working in the same dept where his p where e.empno=p.mgr and e1.mgr = e2.empno
manager is work? p.sal>e.sal and e1.sal > e2.sal
select ename,mgr from emp
97) Display those employees who are not where mgr in (select empno from
working under any Manager? emp where job!="MANAGER")
98) Display the grade and employees select ename,grade from
name for the deptno 10 or 30 but grade emp,salgrade where deptno
is not 4 while joined the company before in(10,30) and grade<>4 and
31-DEC-82? hiredate<'31-DEC-82'
99) Update the salary of each employee update emp set
by 10% increment who are not eligible for sal=(sal+sal*10/100) where
commission? comm is null
100) Delete those employees who joined delete from emp where
the company before 31-Dec-82 while hiredate<'31-dec-82' and deptno
their department Location is New York or in(select deptno from dept where
Chicago? location='New York' or 'Chicago')
select e.ename,e.job,
101) Display employee d.dname,d.location from emp e
name ,job,deptname,loc for all who are join dept d using (deptno) where
working as manager? e.job="MANAGER"
select m.ename as
ename ,e.ename as mgr from
102) Display those employees whose emp e join emp m on
manager name is jones and also display m.mgr=e.empno where
their manager name? e.ename='JONES'
select ename,grade,hisal from
103) Display name and salary of ford if emp,salgrade where
his salary is equal to hisal of his grade? ename='FORD' and hisal=sal
SELECT
E.ENAME,E.JOB,DN
AME,EMP.ENAME,
GRADE FROM
EMP,EMP
E,SALGRADE,DEPT
Database testing
https://
www.youtube.com/watch?
v=W_fH6CqiTDU
https://docs.google.com/
spreadsheets/d/
1WBljrmVijkT3da6QbqDp
qA2Ty9RgosqcvFwDnX4
Queries 5tGc/edit
Tables CREATE TABLE salgrade(
grade int(4) not null primary key,
losal decimal(10,2),
hisal decimal(10,2));
joining 3 tables:
group by:
#ERROR!
#ERROR!
SELECT * FROM `emp1` group by emp_id
having emp_id=1
distinct:
select DISTINCT(pro_id) from prod
displays single field with distinct values
Order by:
SELECT * FROM `emp1` order BY
emp_name ASC
like:
SELECT * FROM `emp1` order BY
emp_name ASC
title count
Buy a green widget 2
Buy a blue widget 3
About blue widgets 2
SELECT DISTINCT(designation_id) as
desig, count(designation_id) AS count
FROM emp_base_table GROUP BY
designation_id HAVING count > 1