SQL Interview Questions and Answers for business analyst - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
SQL Interview Questions and Answers for business analyst

SQL Interview Questions and Answers for business analyst

 SQL Interview Questions

There is given SQL interview questions and answers that have been asked in many companies. For PL/SQL interview questions, visit our next page.

Q.1- How we can update gender column data from 'M' to 'F' and 'F' to 'M' in emp table?
Ans:- By using decode or case function
          UPDATE EMP SET gender=DECODE(gender,'M','F','F','M');
                                         OR
          UPDATE EMP SET gender = CASE WHEN gender='M' THEN 'F' ELSE 'F' END;

Q.2-How we can get 2nd highest salary without using Analytical Function ?
Ans:-select max(sal) from emp where sal<>(select max(sal) from emp);

Q.3-How we can get the employee details of 2nd highest salary without  using Analytical Function ?
Ans:-      select * from emp where sal=(select max(sal) from emp
               where sal<>(select max(sal) from emp));

Q.4- Find the employee details whose salary is greater than avg salary of his department ?
Ans:-select * from emp a where a.sal>(select avg(sal) from emp b where a.deptno=b.deptno);

Q.5- Find the employee details whose salary is greater than avg salary of employees with same job ? 
Ans:-select * from emp a where a.sal>(select avg(sal) from emp b where a.job=b.job) ;

Q.6- Write a query for deleting duplicate records on the basis of empno & ename ?
Ans:-delete from emp where rowid not in (select max(rowid) from emp
         group by empno,ename) ;

Q.7- What are the database objects ? 
Ans:-Below are the database objects
         1) Table
         2) View
         3) Sequence
         4) Synonym
         5) Index
         6) Tablespace

Q.8- What is the difference between delete and truncate ?
Ans:-Difference between delete and truncate are below:-
         a) Delete is a DML Command while as Truncate is a DDL Command
         b) Delete locks  table row while as Truncate locks entire table
         c) We can use "where condition" with delete while as we can't user "where
              condition" with Truncate
         d) DML Trigger fired during the delete while as DML Trigger doesn't fire during the Truncate
         e) We can rollback delete operation while as we can't rollback Truncate operation

Q.9- What is the difference between Primary Key and Unique Key ? 
Ans:-Difference between Primary Key and Unique Key are below:-
        a) Only 1 Primary key can be defined in one table while as unique
            key can be defined more than  1 in a table
        b) Primary never accept null value while as Unique key can
             accept multiple null values
        c) On the creation of Primary Key,clustered index created on table
            while as On the creation of Unique Key ,non-clustered index created on table
        d) Primary key is used for identify a record while a Unique key is
            used  to prevent duplicate records in a table

Q.10- What is the difference between case and decode?  
Ans:- a) Case can work with the relational operator while decode can't work
              with relational operator.
          b) Case can work with operator like,between,exists, in while decode
               can't work with these operators
          c) case can be used in PLSQL with same syntax individually while
              decode can't work individually in PLSQL
          d) Both treat NULL differently.

Q.11- What is the difference between replace and translate ?
Ans:- a) Translate works character by character while replace works
              string by string.

Q.12- What is the difference between trunc and round ?
Ans:- Round(float_value,number)  function looks decimal digit on position number+1,if this digit is 5 or greater than 5 then it add 1 on last decimal digit .
             Trunc(float_value,number) function, select value upto number decimal position.

Q.13- What is the difference between rank and dense_rank ?
Ans:-a) rank and dense_rank both are Analytical function and using for
             ranking. Both giving same rank number while getting same value
             more than one times but rank skip next rank number by n-1 while
             dense_rank does not skip the next rank number.

Q.14- What is the difference between lag and lead ?
Ans:- a) lag and lead both are analytical function, Lead is used get the next
             value of same column while Lag is used to get the previous value of same column.

Q.15- What is the difference between view and synonym ?
Ans:- a) synonym can be created for any Oracle objects like
             table,view,sequence,function,procedure etc. while view can be
             created for only table,view or synonym.
          b) synonym can be private or public while view can be read only or
              update-able.
          c) View is logical table while synonym is a alternate name for object

Q.16- What is the difference between varchar and varchar2 ?
Ans:- a) Varchar take space null string while varchar2 is not taking space
          for  null string.
         b) Varchar store maximum 2000 byte while varchar2 can store
             maximum 4000 byte.

Q.17- What are pseudo column available in Oracle ?
Ans:-Below are the pseudo column available in the Oracle
         1) Rownum
         2) Rowid
         3) Level
         4) Currval
         5) Nextval

Q.18- What are constraint available in Oracle ?
Ans:-Below are the constraint available in the Oracle
         1) Primary Key
         2) Unique Key
         3) Not Null
         4) Check Constraint
         5) Foreign Key

Q.19- What is the Simple Sub-Query & correlated Sub-Query ?
Ans:- Simple Sub-Query is Query that can be executed independently and parent query  executed on the  result of Simple Sub-Query.
Example:-select * from emp where deptno in (select deptno from dept);

correlated Sub-Query is a query that is dependent on parent query and executed for every records of parent query . It can't be executed independently.
Example:-select a.* from emp a where a.sal>(select avg(b.sal) from emp b where a.deptno=b.deptno);

Q.20-Write a query that display Odd number of records? 
Ans:-select * from emp where (rowid,1) in (select rowid, mod(rownum,2) from emp);

Q.21-Write a query that display Even number of records? 
Ans:-select * from emp where (rowid,0) in (select rowid, mod(rownum,2) from emp);

Q.22- Which one is faster between IN and EXISTS and why?
Ans:- EXISTS is more faster than IN since EXISTS return Boolean and IN return values.

Q.23-Which Operator is used for dealing with NULL value?
Ans:- IS and IS NOT Operator is used for dealing with NULL values.

Q.24-Find all department which have more than 3 employees 
Ans:-select * from dept where deptno in (select deptno from (
select deptno,count(*) count from emp group by deptno having count(*)>3));

Q.25-Display the name of employee who joined on same date
Ans.-select a.* from emp a,emp b
         where a.hiredate=b.hiredate and a.empno<>b.empno;

Q.26- Display the employee information whose salary is greater that his manager salary
Ans:-select a.* from emp a where a.sal>(select b.sal from emp b where b.empno=a.mgr);

Q27:- Display the employee details who joined company before 10 years from today 
Ans:-select * from emp where months_between(hiredate,sysdate)<(10*12);

Q.28- What is the difference between SUBSTR and INSTR ?
Ans:- SUBSTR is used get the sub-string from main string ,It return string.
         While INSTR return the number and used for getting the position of
          sub-string/character   in main string

Q.29- What is the difference between UNION and UNION ALL ?
Ans:- "Union" return unique records from both queries while as "Union all" show the all records from both queries means "Union all" does not eliminate the duplicate records while as "Union" eliminate the duplicate records.

Q.30- What is the difference between WHERE and HAVING ?
Ans:-HAVING and WHERE , both are used to filter the records .HAVING filter the records of "group by" result while WHERE is used to filter records before group by.

"Where" is records filter while as "Having" is the filter for group by result.
Where can not apply on grouping function while having can apply on grouping function

Example:- select deptno,job,count(*) from emp
                   where job in ('MANAGER','SALESMAN','CLERK')
                   group by deptno,job having count(*)>1;

Here we can use having with any of column deptno, job and count(*) while where condition can not apply with count(*) .

Q.31- What is View ? 
Ans:- View is a logical table created from table,view or synonym . View doesn't take any storage except query stored on data dictionary.

Q.32-What is the fastest way to fetch data from the table ?
Ans:-Access by "ROWID" is the fastest way to fetch data from table.

Q.33-What is the dual table in Oracle?
Ans:-Dual is a table with only 1 column and 1 record . Column name is DUMMY and record value is X . Datatype of column is varchar2(1). This table is used for calculations .

Q.34- How we can fetch common records from two tables ?
Ans:- By INTERSECT set Operator 

Q.35- Which operator is used in query for pattern matching? 
 Ans:- Like operator is used for pattern matching with ( _ and  % ) wildcards.

Q.36- If EMP table has 14 records and DEPT table has 5 records then howmany records will be there in below query ?
select * from emp,dept;

Ans:- There will be 14x5=70 records as no join condition mentioned in query so cross join will be there.

Q.37- One Table "PARTS" has a column part_type_id with following values:-
1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1

Write a query to add 2 where part_type_id is 0 and add 3 where part_type_id is not equal to 0. 
Ans:- update PARTS set part_type_id = case when part_type_id = 0 then part_type_id+2 else part_type_id+3 end;

Q.38- Write a SQL query using UNION ALL that uses the WHERE clause to eliminate duplicates.
Ans:- Below is example query for same
select * from emp where deptno =10
union all
select * from emp where deptno!=10;

Q.39-How can we fetch only common records from two tables ?
Ans:- By using INTERSECT set operator.  
Example:-
Select * from emp
INTERSECT
Select * from emp1;

Q.40-How can we retrieve all records of tab1 those are not present in tab2?
Ans:- By using MINUS set operator.  
Example:-
Select * from emp
MINUS
Select * from emp1;

Q.41-How can we fetch all the records from two tables but common records only once?
Ans:- By using UNION set operator.  
Example:-
Select * from emp
UNION
Select * from emp1;

Q.42- What is the difference between NVL and NVL2 ?
Ans:- NVL function is used to place the value for NULL.
            NVL2 function is used to place the values for NULL and NOT NULL .
Example:- select empno,ename,job,sal,comm,nvl(comm,10) from emp;
Above query will display the value of comm is 10 where comm is null.
  
             
           select empno,ename,job,sal,comm,nvl2(comm,10,20) from emp;
 Above query will display the value of comm is 20 where comm is null and 10 where comm is not null.                


 Q.43- How can I create an empty table emp1 with same structure as emp?
Ans:-
 Create table emp1 as select * from emp where 1=2;

Q.44- Find the 3rd MAX salary in the emp table ?
Ans:- select distinct sal from emp e1 where 3 = (select count(distinct sal) from emp e2 where e1.sal <= e2.sal);

Q.45- Find the 3rd MIN salary in the emp table ?
Ans:-
 select distinct sal from emp e1 where 3 = (select count(distinct sal) from emp e2 where e1.sal >= e2.sal);

 Q.46- How can we select first n records from emp table?
Ans:-
 select * from emp where rownum <= &n;

Q.47- How can we select last n records from emp table?
Ans:-
 select * from emp
            minus
            select * from emp where rownum <= (select count(*) - &n from emp);

Q.48- Howmany LONG datatypes columns are allowed in a table ? Can we use LONG datatypes column in "WHERE" or in "ORDER BY"?
Ans:-
 Only 1 LONG datatype column allowed in a Table.
           No. We can't use LONG datatype column in "Where" clause or in "Order by" clause.

Q.49-How can we find duplicate records in a table ?
Ans:-select empno,count(*) from emp group by empno having count(*)>1;

Note:- Firstly you have to decide on which column/columns basis duplicate you want.
       If more than one columns you have decided then you have put all decided column in
       "select" and in "group by".

 Q.50-Can we create Primary Key and Foreign Key in a same table ?
Ans:-
Yes

Q.51-Find the all employees who have at least one person reporting to them ?
Ans:-select ename from emp where empno in (select mgr from emp);

Q.51-What will be result of below query ?
select 10 from dual minus select 3 from dual;
 Ans:- Result will be
            10   

Q52. How can we convert Julian Date to date ?
Ans :- Using ‘JSP’ format string
SQL > select to_char(to_date(2456317,’JSP’),’dd-Mon-yyyy’) as day  from dual;
DAY
————
24-Jan-2013

Q53. How can we convert date to Julian Date format ?
Ans :- Using ‘J’ format string
SQL > select to_char(to_date(’24-Jan-2013′,’dd-mon-yyyy’),’J’) as julian from dual;
JULIAN
——-
2456317

Q54. msissdn_master table has msisdn column with some data start with 0 that data need to update with 91 in place of 0 , How can we do that ?
Ans :- By below update query we can do that
update master_msisdn
set msisdn = case when substr(msisdn,1,1)=0 then '91'||substr(msisdn,2);
commit;

Q55.How we can pass more than 1000 values in "IN" Operator?
Ans:-For doing this , Firstly values need to insert into temporary table then select values from temporary table in "IN" operator

Example :-
select * from tab1 where col1 in (select col from temp_tab);


Q56. EMP and SALGRADE tables are there with below structure

EMP



SALGRADE

 

How we can get the data for employee with empno, ename and grade ?

Ans:- Below is the query for same
 select a.empno,a.ename,b.grade from emp a , salgrade b
where a.sal between b.minsal and b.maxsal;

Q57. What is the difference between clustered and a non-clustered index? 
Ans:-A clustered index is a type of index where record reordered on same way records present in thetable are physically stored. Therefore table can have only one clustered index. The leaf nodes of a clustered index contain the data pages.
Nonclustered index is a type of index in which the logical order of the index does not match the physical stored order of the rows on disk.

Q58. How can we view last record added to a table?
Ans:- Select * from (select * from employees order by rownum desc) where rownum<2 b=""> 


Q59. How can we display row numbers with the records?

Ans:- select rownum, a.* from emp a; 


About Mariano

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.