Skip to main content

Answers for SQL Functions



1. SQL> SELECT empno, ename FROM emp WHERE Length(ename) = 4;

2. SQL> SELECT empno, ename, job FROM emp where Length(job)=7;

3. SQL> SELECT Length('qspiders') - Length(replace('qspiders','s','')) FROM dual;

4. SQL>  SELECT empno, ename, job FROM emp WHERE Instr(job,'MAN') >0;

5. SQL> SELECT empno, ename, job FROM emp WHERE Instr(job, 'MAN') =1;

6. SQL> SELECT empno, ename, job FROM emp WHERE (Length(ename) - Length(Replace(ename, 'L',''))) = 1;

7. SQL> SELECT * FROM dept WHERE Instr(dname,'O') > 0;

8. SQL> SELECT Concat(ename,' working as a ') || Concat(job, ' earns ') || Concat(sal, '  in ') || Conc
at('dept ',deptno) AS text from emp;


OR

SQL> SELECT Concat(Concat(Concat(Concat(Concat(Concat(Concat(ename,' working as a '), job),' earns '), sal),'  in '),'dept '), deptno) AS text FROM emp;

9. SQL> SELECT empno, ename, sal FROM emp WHERE mod(sal,2) > 0;


Comments

Unknown said…
sir can u pls add all the answers for functions (only 9 ans are there)
and also answers for qspider questions,,,,,
Shailaja Yadav said…
Sir, can you plz add all the answers,there are only 9 answers...
R.N SOUMYA RANJAN SAMAL said…
I AM NOT GETTING THE EXACT O/P FROM THE ANSWER NO-4.ACTUALLY I AM GETTING ALL THE MANAGER AND SALESMAN ON MY SCREEN.SO KINDLY IF YOU MODIFY I'LL BE THANKFUL.
Rekhamopuru said…
select * from employees where instr(job_id,'MAN')=(length(job_id)-length('MAN')+1);
i hope this will work for the 4th question
Rekhamopuru said…
i hope this will give better output for 4th question
Soumya said…
Dear rekhamopuru
for the 4th question how could you find the job_id from emp table
there is no job_id in EMP table.hope for the next answer
Rekhamopuru said…
Soumya...in my table i named column as job_id..u can give u r own name like job..it dont nt matter ....
u try job insted of job_id...
jeremia said…
sir,i think your answer to Q-4 is wrong because it will display all the employees whose job are having 'MAN' as the last three character or as the first three character or any other position in the string...so sir, the answer should be as follow:-
select * from empwhere substr(job,-3,3)='MAN';
jeremia said…
answer to Q-10
select count(empno) from emp where comm is null;
jeremia said…
answer to Q-11
select sum(sal),sum(comm) from emp where deptno=30;
jeremia said…
answer to Q-12
select deptno,count(empno) no_of_clerks from emp where job='CLERK' and deptno in(20,30) group by deptno;
jeremia said…
answer to Q-13
select deptno,sum(sal) total_salary from emp group by deptno;
jeremia said…
answer to Q-14
select deptno,sum(sal) total_salary from emp
group by deptno
having sum(sal)>3000;
jeremia said…
answer to Q-15
select job,sum(sal) total_sal from emp where deptno not in 30 group by job having sum(sal)>5000;
jeremia said…
answer to Q-16
select job,max(sal) from emp where deptno in(10,20) and job in('MANAGER','CLERK','SALESMAN') group by job order by max(sal) desc;
jeremia said…
answer to Q-17
select job,sum(sal) total_salary,count(empno) no_of_employees from emp group by job having sum(sal)>5000;
jeremia said…
correction in answer of Q-17.sum(sal)>9000;replace 5000 with 9000.
jeremia said…
answer to Q-18
select deptno,count(empno) no_of_employees from emp group by deptno having count(empno)>=4;
jeremia said…
answer to Q-19
select distinct deptno from emp where deptno in (select deptno from emp group by deptno having count(distinct job)=1) and job='SALESMAN';
jeremia said…
answer to Q-20
select length('HELLLLL')-length(replace('HELLLLL','L')) no_of_L from dual;
jeremia said…
answer to Q-21
select * from emp where instr(job,'MAN',1)>0;
jeremia said…
answer to Q-22
select * from emp where substr(job,1,3)='MAN';
jeremia said…
answer to Q-23
select * from emp where substr(job,-3,3)='MAN';
jeremia said…
answer to Q-25
select ename,lower(substr(ename,1,3))||upper(substr(ename,4)) from emp;
jeremia said…
answer to Q-26
select ename || 'is a' || job || 'and gets salary' || sal from emp;

(or)
you can also use the word "concat"
MAYANK said…
SIR IF WE ARE SPECIFYING INSTR(JOB,'MAN',1)>0; THEN WE ARE GETTING THE RECORDS HAVING CHARACTERS MAN EITHER IN STARTING OR END POSITION OF THE JOB BUT ACCORDING TO THE QUESTION IT SHOULD DISPLAY THE EMPLOYEES HAVING JOB WITH THE STARTING STRING 'MAN' TO GET THE RECORDS ACCORDINGLY WE NEED TO WRITE INSTR(JOB,'MAN',1)=1 WHICH LEADS TO THE EXACT SOLUTION OF THE GIVEN QUESTION...
THANKS
Unknown said…
This comment has been removed by the author.
Unknown said…
hi sir for Question NO. 4
SELECT * FROM EMP
WHERE SUBSTR(JOB,1,3)='MAN';
--will select all the employees who are mangers
SELECT * FROM EMP
WHERE SUBSTR(JOB,1)='MAN';
--says no rows selected
why is it so
Unknown said…
sir please provide all answers
Hani said…
This comment has been removed by the author.
Hani said…
31. select * from (select lag() over (order by hiredate asc) dt from emp where mgrid='*****' ) where hiredate<dt ;
rj_srtk said…
for 4th this is the correct query

select * from emp where substr(job,-3,3)='MAN';
rj_srtk said…
for 5th correct query is (works on oracle sql plus)
select * from emp where substr(job,1,3)='MAN';
rj_srtk said…
for 6th correct query is (works on oracle sql plus)
select * from emp where length(ename)-length(replace(ename,'L'))=1;
rj_srtk said…
for 7th correct query is (works on oracle sql plus)

select *from dept where instr(dname,'O')>0;
rj_srtk said…
This comment has been removed by the author.
rj_srtk said…
for 8th correct query is (works on oracle sql plus)

set pages 234 lines 239
select concat(ename,concat(' working as a ',concat(job,concat(' earns ',concat(sal,concat(' in dept ',deptno)))))) from emp;
rj_srtk said…
for 25th correct query is (works on oracle sql plus)
select lower(substr(ename,1,3))||substr(ename,4) from emp;
rj_srtk said…

for 26th correct query is (works on oracle sql plus)

select concat(ename,concat(' is a ',concat(job,concat(' and get salary ',sal)))) from emp;
Unknown said…
CAN ANYONE GIVE ME THE EXPLANATION OF ANSWER NO. 6

Popular posts from this blog

Basics of RDBMS

Data Small set of information becomes data, this set of information helps make decision. Data is always some useful information. Database Place where you store the data. Database represents some aspect of the real world called "miniworld". A database is designed, built and populated with data for a specific purpose. It has intended group of users and some preconceived applications in which these users are interested. In other words, a database has some source from which data is derived, some degree of interaction with events in the real world and an audience that is actively interested in the contents of the database. Database can also be defined as collection of one or more tables. Ex: Mobile, human brain etc DBMS (Database Management System ) Is a program that stores retrieves and modifies data in the database on request. Study of different techniques of design, development and maintenance of the database Types of DBMS These types are based upon their m

SQL Interview Questions on Subqueries

SUB Queries: 1. List the employees working in research department 2. List employees who are located in New York and Chicago 3. Display the department name in which ANALYSTS are working 4. Display employees who are reporting to JONES 5. Display all the employees who are reporting to Jones Manager 6. Display all the managers in SALES and ACCOUNTING department 7. Display all the employee names in Research and Sales Department who are having at least 1 person reporting to them 8. Display all employees who do not have any reportees 9. List employees who are having at least 2 reporting 10. List the department names which are having more than 5 employees 11. List department name having at-least 3 salesman 12. List employees from research and accounting having at-least 2 reporting 13. Display second max salary 14. Display 4th max salary 15. Display 5th max salary  -- Answer for nth Max Salary Co-Related Subqueries: 16. Write a query to get 4th max salary from EMP table 17. Wri