You are on page 1of 7

ORDER BY EXAMPLES:

*****************
--get ename, sal based on salary order?
select ename,sal from emp_info
order by sal;

--Get emp data based on desg order?

select * from emp_info


order by desg;

--Order the data based on desg and under each desg order data based on sal?

select * from emp_info


order by desg,sal;

--Get emp names and gender values based on gender order?


select gender,ename from emp_info
order by gender;

--Get emp names and gender values based on gender order and under each gender
--order the emp names?

select gender,ename
from emp_info
order by gender,ename;

--Get emp data like old emp to recent emp?

select * from emp_info


order by 4;

--get eid,esal and jdate like old emp to recent emp?

select eid,sal,jdate
from emp_info
order by 3;

Aggregate Functions examples:

--FIND THE TOTAL SAL PAYING TO EMPS IN DEPT NO 10


SELECT SUM(SAL) FROM EMP
WHERE DEPTNO=10;

--FIND THE TOTAL SAL PAYING TO RESEARCH DEPT?


SELECT SUM(SAL) FROM EMP
WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE DNAME='RESEARCH');

--FIND AVERAGE SALARY OF MANAGER?


select trunc(avg(sal),2) from emp
where job='MANAGER';

--display emp info who is getting more salary than


--average salary of manager?
select * from emp
where sal>any(select trunc(avg(sal),2) from emp
where job='MANAGER');

--Find the highest account balance from the account type "SB"?
select max(act_bal)
from cust_act_dtls
where act_type='SB';

--Display prod details with min price under laptop category?


select * from prod_dtls
where prod_name='LAPTOP' and
cost IN(select min(cost) from prod_dtls
where prod_name='LAPTOP');

--Find the number of emps working under dept no 30?


select count(empno) from emp
where deptno=30;

--Find the number of products from SAMSUNG?


select count(*)
from prod_dtls
where comp_code=(select comp_code from comp_dtls
where comp_name='SAMSUNG');

GROUP BY and HAVING examples:

--Find out number of emps from each job category?


SELECT JOB,COUNT(*) "Number of emps"
FROM EMP
GROUP BY JOB;

--Find out number of emps from each job category


--and also display average sal for each job category?
SELECT JOB,COUNT(*) "Number of emps",trunc(avg(sal),2) "Avg. Salary"
FROM EMP
GROUP BY JOB;

--Find out number of products from each company name?


select c.comp_name,count(p.prod_code) "Number of prod."
from comp_dtls c, prod_dtls p
where c.comp_code=p.comp_code
group by c.comp_name;

--Get above output if there exists minimum 3 prods from a company?


select c.comp_name,count(p.prod_code) "Number of prod."
from comp_dtls c, prod_dtls p
where c.comp_code=p.comp_code
group by c.comp_name
having count(p.prod_code)>=3;

--Find the number of customers from each city?


select city,count(*) "Number of customers"
from cust_dtls
group by city;

--Find the number of customers from each city and


--display the result if there exists min 3 customers from a city?
select city,count(*) "Number of customers"
from cust_dtls
group by city
having count(*)>=3;

--get deptno and number of emps from each dept


select deptno,count(empno) "cnt"
from emp
group by deptno
order by deptno;

--Get job name and min sal and max sal from each job category?

select job, min(sal), max(sal)


from emp
group by job
order by job;

--Get dept names and average salary under each dept?

select d.dname,Trunc(avg(e.sal)) "AvgSal"


from emp e,dept d
where e.deptno=d.deptno
group by d.dname;

--Display deptno and number of emps from each job category?

select deptno, job, count(*) "count"


from emp
group by deptno,job
order by deptno;

--Display deptno,count of emps from each job category under each dept
--and also we need at least 2 emps from a job category?
select deptno, job, count(*) "count"
from emp
group by deptno,job
having count(*)>=2
order by deptno;

AGGREGATE EXAMPLES:-

Ex: display addition of all salaries?


select sum(sal) from emp;

Ex: Find the sum of salaries for managers?

Ex: Find the sum of salaries of emps working under RESEARCH department?

Ex: Find the total investment amount for the products from sony?

Ex: Find the total balance from the account types SB and SAL?

Ex: Find the average sales amount in the last year?


Ex: Find the average account balance from "delhi"
customers?

Ex: Find the average salary of managers?

Ex: Find the average salary of given job category of emp?

Ex: Find the highest account balance from "female"


customers?

Ex: Find the least salary of all emps?

Ex: Find the least salaried employee details?

Ex: Find the salesman who is getting more salary


than all salesman?

Ex: Find the emp details who is getting below average


salary?

Ex: Find out number of products having warrenty?

Ex: Find out number of customers for the "SAL"


account type?

Ex: Find out number of people from the city "hyd"

Ex: Find out number of female customers having


"sal" account type?

Ex: Find out number of books published by a particular


publisher?

Ex: Find number of books written by any particular


Author?

-----------------------------------------------------------------------------------
--------
INTERVIEW QUESTIONS

1) What is a correlated subquery?

2) How to delete NOT NULL constraint with default name?

A:
ALTER TABLE <name>
MODIFY col datatype(size) NULL;

3) How to find out length of user name from any mail id?

4) What is the meaning of WINDOW clause?


A:
On which basis , the analytical function is executing , that is known as
WINDOW clause.

5) How to delete duplicate records from any table?

6) What is the difference between TAB and TABLE?


7) What is use of TRUNCATE? Where did you used this in your project?

8) How to enable primary key?

9) What is the difference between DROP and DELETE?

10) How to get back deleted table?

11) What is recycle bin?

12) What is a primary key?

13) Difference between UNIQUE and PRIMARY KEY?

14) What is a join?

15) what is NATURAL join?

16) what is the syntax to add FOREIGN key ON any column?

17) what is INDEX and why is INDEX?

18) What are types of Primary keys?

19) How many primary keys, we can define on a table?

20) What is bitmap index? Where did you used?

21) What is B tree index?

22) How to delete values from a column?

23) How to display unique records from a table?

24) Why are you creating views?

25) What is inline view?

26) What is a temporary view?

27) what is a materialized view?

28) what are REFRESH methods?

29) what are the types of MATERIALIZED VIEWS?

30) How to disable a foreign key?

31) what is a cross join?

32) Difference between cross join and non equi join?

33) Why we need Inner Join?

34) HOw to display ordered data from a table?

35) What is the meaning of numbers after ORDER BY clause?

36) How to display department wise total salary?


37) Can we use HAVING clause without GROUP BY clause?

38) what is the result of following query?

SELECT TRUNC(sysdate,'WW') from dual;

39) How to remove a character from string?

40) How to replace a character with corresponding character?

41) what is the result from substr() function?

42) what is the difference between ROUND(), TRUNC() functions?

43) How to define range of values on any column? Like,


Min salary 5000 and Max salary 100000?

44) What is pre requisite for writing a join query?

45) What is the difference between Inner join and Left outer Join?

46) Assume , that there are no constraints on tables, but there is a common
column. Then are you able to write JOIN condition?

47) Consider , multiple branches existed for a business. I want to display


all customers information from all branches?

48) What are limitations of SET OPERATORS?

49) What are PSEUDO COLUMNS of table?

50) What is a sequence, use of it in project or real time?

51) How to delete a table from RECYCLE BIN?

52) How do i get constraints information from a table?

53) what is the meaning of "SYS_Cn"?

54) what is the meaing of a table name like "BIN$XXazXXsa"?

55) How to print char date value in oracle date format?

56) How to find out experience of employee in terms of years?

57) Display 10 days back date from today?

58) How to display day name?

59) How to convert gender column values like "Males into Females"
and "Females into Males"?

60) How do i search for the values having a character either "_"(under score)
or "%"?

61) Can we enable a constraint , even the column having invalid data?
If Yes, How?
If no , Why?
62) What is the use of alias names of tables?

63) How to define a value in to a column ?

64) What is multitenancy?

65) What is CDB and PDB?

66) How to synchronize data based on recent transactional data?

A: MERGE

67) What is DECODE function and give any one example?

68) What is the difference between NVL() and NVL2()?

-----------------------------------------------------------------------------------
-------

You might also like