Skip to main content

SQL Interview Questions on Functions

SQL Functions:

Use only functions for data manupulation.

1. List employees whose name having 4 characters
2. List employees whose job is having 7 characters
3. Find out howmany times letter 'S' occurs in 'qspiders'
4. List the employees whose job is having last 3 characters as 'man'
5. List employees whose job is having first 3 characters as 'man'
6. Display all the names whose name is having exactly 1 'L'
7. Display dept names which are having letter 'O'
8. Display the output as shown below,
   Scott working as a clerk earns 3000 in dept 20
9. Display employees who earn odd numbered salaries
10. Display number of employees getting NULL comission.
11. Display total sal and comm drawn by dept 30
12. Count number of clerks in dept 10 and 20
13. List Department wise total salary
14. List department wise total sal only if the total sal is > 3000
15. Display job wise total salary excluding dept 30 only if the total salary is > 5000
16. Display job wise max sal only for managers, clerks, salesman working in dept 10 and 20. Sort the data by des order of the max salary.
17. Display job wise number of employees in all the department with total salary > 9000
18. Display the department number having atleast 4 employees in it
19. Display the department having only saleman in it
20 Calculate number of L in string 'HELLLLL'
21. Display all the employees whose job has a string 'MAN'
22. Display all the employees whose job starts with string 'MAN'
23. Display all the employees whose job ends with string 'MAN'
25. Display first 3 characters of ename in lower case and rest everything in upper case.
If ename is 'MITHUNASHOK' then display  this as 'mitHUNASHOK'

26. Display the result from emp table as below.

SMITH is a CLERK and gets salary 2000 

Here SMITH is ename column, CLERK is JOB and 2000 is SAL column and rest everything is literal strings.


Vijay_twinkle5 said…
can u pls update the solution for query
Jobwise 2nd or 3rd highest sal where i'll pass 2nd dynamically
using subqueries.
Basav said…
hi sir for Question NO. 4
--will select all the employees who are mangers
--says no rows selected
why it is so
Saurabh said…
Answer For Question 4 is
FROM emp
WHERE SUBSTR(job,-3,3)='MAN';
Unknown said…
Answer For 21st:
SQL> select * from emp
where INSTR(job,'MAN') >= 1;
Unknown said…
can u pls post remaining answers for reference
karthik said…
Did u got any reply for the remaining questions ? If so,please share me the answers
Hitesh said…
can you tell the answer for ques 6
chinnu said…
MAN size is 3, u haven't specified any size so it can not compare with the unknown size
Unknown said…
answer of 3:- select LEN('qspiders')-LEN(REPLACE('qspiders','s',''))
Unknown said…

Allow me to show my gratitude bloggers. You guys are like unicorns. Never seen but always spreading magic. Your content is yummy. So satisfied.

I come up with a problem. In the pivot I want to summarize amount based on Hub and I want to always select the latest data of a country to add
Date Hub Country Amount
2017.01.01 Asia China 80
2017.01.02 Asia Japan 75
2017.01.03 Asia China 85
2017.01.01 Europe France 50
2017.01.05 Europe France 55
2017.01.02 Europe Turkey 20
I want to pivot to always select the latest data for each country and summarize based on Hub . Eg, for Asia that is 75+85=160, and for Europe, that is 55+20=75

It was cool to see your article pop up in my google search for the process yesterday. Great Guide.
Keep up the good work!

Irene Hynes
Naveen S said…
Pretty blog, so many ideas in a single site, thanks for the informative article, keep updating more article.
PHP course in chennai
w3webschool said…
Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!
Web Design Training
Unknown said…
Answers are in seqquence(Except 8th and 26th those are simple concatenations) SQLs are based on HR schema
--select First_name from employees where length(FIRST_NAME)=4;
--select job_id, employee_id ID from employees where length(JOB_ID)=7
--select length('qspiders')-length(replace('qspiders','s','')) from dual;
--select employee_id,Job_id from employees where substr(job_id,-3,3) like'%MAN%'
--select employee_id,Job_id from employees where substr(job_id,1,3) like'%MAN%'
/*select first_name from employees
where (length(upper(first_name)))-length(replace(upper(first_name),'L',''))=1*/
--select department_name from DEPARTMENTS where DEPARTMENT_NAME like '%o%'
--select first_name,salary,mod(salary,2) from employees where mod(salary,2)!=0
--select first_name,salary,commission_pct from employees where nvl(commission_pct,'0')=0
--select sum(salary),sum(nvl(commission_pct,'0')),department_id from employees where department_id=80 group by department_id
--select job_id,count(*) from employees where department_id in (10,20,30) and job_id like '%CLERK%' group by job_id
--select (sum(salary)+sum(nvl(commission_pct,'0'))) Tsal,Department_id from employees group by DEPARTMENT_ID order by DEPARTMENT_ID asc
/*select (sum(salary)+sum(nvl(commission_pct,'0'))) Tsal,Department_id from employees
having (sum(salary)+sum(nvl(commission_pct,'0')))>=3000
group by DEPARTMENT_ID order by DEPARTMENT_ID asc*/

/*select Job_id,sum(salary)+sum(nvl(commission_pct,'0')) from employees where department_id not in(30)
having sum(salary)+sum(nvl(commission_pct,'0'))<5000
group by Job_id*/

/*select max(salary),job_id from EMPLOYEES where job_id like '%CLERK%' or Job_id like '%SA_MAN%' or Job_id like '%MGR%'
and department_id in (10,20)
group by job_id
order by Max(salary) desc*/

/*select job_id ,count(employee_id),sum(salary) from employees group by job_id
having sum(salary)>9000*/

--select department_id,count(employee_id) from employees group by DEPARTMENT_ID having count(employee_id)>=4
/*select department_id from employees where job_id like 'SA_MAN'
select department_id from employees where job_id not like 'SA_MAN'*/
--select length('HELLL')-length(replace('HELLL','L','')) from dual;

--select first_name,job_id from employees where job_id like '%MAN%'
--select first_name,job_id from employees where job_id like 'MAN%'
--select first_name,job_id from employees where job_id like '%MAN'

--select 'harishKommuri',lower(substr('harishkommuri',1,3))||upper(substr('harishkommuri',4) )from dual
divya said…
Action Items: Develop ways to improve student and the community involvement in the governance and decision making process of educational institutions. Identify and promote ways for institutions to improve involvement with the public and the practice of democracy within their own institution share some more ino
Ai & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai

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

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