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

Rakesh 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
This comment has been removed by the author.
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
sir please provide all answers

Popular posts from this blog

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. Write a query to get 2nd…

'Linux-x86_64 Error: 28: No space left on device' While trying to start the database -- Error

SQL> startup mount pfile='/tmp/initdlfasp12.ora'
ORA-27102: out of memory
Linux-x86_64 Error: 28: No space left on device


This as you can see is on Linux x86 with 64 bit processor. We got this error after we changed SGA on 10gR2 database. So was sure that this is something to do with the OS.

Parameters to check for this are shmall.

shmall is the total amount of shared memory, in pages, that the system can use at one time.

$cat /proc/sys/kernel/shmmax
53687091200

$ getconf PAGE_SIZE
4096

As per Oracle SHMALL should be set to the total amount of physical RAM divided by page size.

Our system has 64GB memory, so change kernel.shmall = 1024 * 1024 * 1024 * 64 / 4096 = 16777216


Once this value is calculated you can modify Linux system configuration file directly.

$ su - root
vi /etc/sysctl.conf file:
kernel.shmall=16777216

and

# sysctl -p

Once this is done the database was started without any problem.

Answers for SUB Queries

1. SQL> select empno, ename from emp where deptno=(select deptno from dept where dname='RESEARCH');


2. SQL> select empno, ename from emp where deptno in (select deptno from dept where loc in ('NEW YORK','CHICAGO'));

3. SQL> select dname from dept where deptno in ( select deptno from emp where job ='ANALYST');

4. SQL> select empno, ename, mgr from emp where mgr = (select empno from emp where ename='JONES');

5. SQL> select empno, ename, mgr from emp where mgr = (select mgr from emp where ename='JONES')

6. SQL> select empno, ename, job from emp where deptno in ( select deptno from dept where dname in ('SALES','ACCOUNTING'))

7. SQL> select empno, ename, job from emp where deptno in ( select deptno from dept where dname in ('SALES','RESEARCH')) and empno in (select mgr from emp)

8. SQL> select empno, ename from emp where empno not in ( select mgr from emp where mgr is not null)

9. select…