Interview Questions: - (SQL)
SQL: -
Q) How you take back up/copy of a table?
Ans – create table <backup_table_name> as select * from <table_which_to_be_back_up>;
Q) How to copy structure of a table?
Ans – create table <copied_table_name> as select * from <table_from_which_structure_copied> where 1=2;
Q) What is self join with example?
Ans – Joining of a table happens with itself is called self join.
In this case, foreign key of a table refers to primary key of same table.
Example –
Select e.ename Employee_Name, m.ename Manager_Name from emp e, emp m
Where e.mgr = m.empno;
Q) What is equi join with example?
Ans – Equi join between two tables return matching records from both table based upon common columns of both table.
Example –
Select e.ename, d.dname from emp e, dept d where e.deptno=d.deptno;
Q) Difference between clustered index and non-clustered index?
Ans –
Clustered Index –
· Clustered Index is a type of index that sort the data rows in a table on their key values.
· Cluster Index doesn’t require additional disk space.
· Cluster Index offers faster data accessing.
· Clustered Index creates on Primary key column.
Non – Clustered Index –
· Non - clustered Index is a type of index that stores the data rows at one location and indices at another location.
· Non - cluster Index requires additional disk space.
· Non - cluster Index offers lesser data accessing speed than clustered index.
· Non - clustered Index creates on unique column.
Q) What is difference between char and varchar data types?
Ans –
Char – (Characters)
· It stores maximum 2000 bytes of characters data.
· Character data type is used to store strings of fixed length.
· Char data type uses static memory location.
· Char data type can be used when we expect the data values in a column to be of same length.
Varchar – (Variable Characters)
· It also stores maximum 2000 bytes of characters data.
· Varchar data type is used to store strings of variable length.
· Varchar data type uses dynamic memory location.
· Varchar data type can be used when we expect the data values in a column to be of variable length.
Q) What is difference between varchar and varchar2?
Ans –
Varchar –
· It stores maximum 2000 bytes of characters data.
· Varchar can identify NULL and empty string separately.
· Varchar is ANSI SQL Standard.
Varchar2 –
· It stores maximum 4000 bytes of characters data.
· Varchar2 can identify NULL and empty string same.
· Varchar2 is Oracle Standard.
Q) Write a SQL query to find sum of positive and negative values from a column in separate columns for a table?
Ans –
select accountid,
sum(case when amount>0 then amount else 0 end) as Positive_Value,
sum(case when amount<0 then amount else 0 end) as Negative_Value
from account_master
group by accountid;
Here accountid and amount are two columns of account_master table.
amount column has some positive and negative values.
Q) What is version of Oracle you are using?
Ans – Oracle 10g
Q) How to find version of Oracle database you are using?
Ans – select * from v$version;
Q) Could you please tell me which is better sub query or join?
Ans – Join is better than sub query as join helps query optimization and faster data retrieval time than subquery.
Q) What s NVL and NVL2 function and difference?
Ans – NVL converts null value to actual value where as NVL2 converts both null and not null values to actual Values.
NVL(COMM,0) -> If comm is null, then it converts that null value to 0.
NVL2(COMM,COMM+100,100) -> if comm is null, then NVL2 converts that null value to 100, and if it is not null, then comm is comm.+100.
Q) If a job is running long or any database query is long running, then what action you are taking?
Ans –
If a job is running long or any database query is long running, then we have to escalate the issue with database team.
DBA has to fix the issue by adding hints in view or rebuilding indexes or optimize the query.
Q) What is table space and how to find out?
Ans –
The table space is where tables get stored.
It links the physical storage layer (files on disks) and the logical storage layer (tables, indexes).
Query – (to get table space name)
select tablespace_name from all_tables where owner = 'SCOTT' and table_name = 'EMP';
Q) What is lock and deadlock?
Ans –
Lock occurs when one process accesses a piece of data where there is a chance that another concurrent process will need this piece of data as well at the same time. By locking the piece of data we ensure that we are able to action on that data the way we expect.
Deadlock occurs when one process is blocked and waiting for a second process to complete its work and release locks, while the second process at the same time is blocked and waiting for the first process to release the lock.
Q) What is the use of where clause and having clause?
Ans –
· WHERE is used to filter rows before grouping, and HAVING is used to exclude records after grouping.
· Where clause is used before group by clause and action on non aggregate columns.
· Having clause is used after group by clause and actions on aggregate function.
Q) One SQL Query runs usually 5 mins, but today it ran more than 15 mins why?
Ans –
Might be we have received more records in the file from source than normal days.
If it’s again long running, then we have to escalate the issue with database team.
Q) A code is developed by developer in DEV environment and its working fine in DEV environment.
If you will push the code in Production environment, then there might be failure which impact users as well as production. What action you will take?
Ans – Before moving a code into production, it should be test in similar PROD environment like UAT environment and if successful, then move it to production and if its fail in UAT environment, then Dev team will fix it.
Q) What is DR activity?
Ans - Disaster recovery is an organization's method of regaining access and functionality to its IT infrastructure after events like a natural disaster, cyber attack, or even business disruptions related to the COVID-19 pandemic.
Q) What is incident and Problem?
Ans –
Any interruption to normal process is called incident.
If the incident happens frequently, then it called problem and then we have to involve different teams to fix the issue permanently.