Download Database Interview Questions - Nutrition Foundation of India

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Concurrency control wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Database wikipedia , lookup

Functional Database Model wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Clusterpoint wikipedia , lookup

Ingres (database) wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

PL/SQL wikipedia , lookup

SQL wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Relational algebra wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Join (SQL) wikipedia , lookup

Transcript
Database Interview Questions
Q: Emp table had an employee with salary 2000. I issued an update statement to set the salary to 3000.
Then I issued a create table statement. However the create table command errored out. I want to rollback
the earlier update statement. Can I do that?
ALL DDL statements are auto-commit. That means whenever you execute a DDL statement, all prior transactions get
commited. Please note that the commit is issued before excuting the DDL. So even if the DDL statement errors out, commit
would have happened.
Q: Please explain the concepts of transaction, commit and rollback
A sequence of database modifications, i.e., a sequence of insert, update, and delete statements,is called a
transaction.
These modifications are temporarily stored in the database system. They become permanent only after the
statement commit; has been issued.
As long as the user has not issued the commit statement, it is possible to undo all modifications since the last
commit. To undo modifications, one has to issue the statement rollback;.
All statements between two commits or a commit and a rollback form one transaction. Please note that all ddl
statements are autocommit.
Q: Explain the concept of NULL
In SQL NULL means the value is unknown. This is not same as 0 or the empty string ''. To check if the value in a
column is NULL we use the clause "IS NULL"
Select * from emp where sal IS NULL; This statement will return all records with salary as null. However the
statement Select * from emp where sal = NULL will not return any records.
Function nvl is used to replace the null value with some other value.
Q: Explain Normal Join
A normal join will look like the following
SELECT e.ename NAME, d.deptname DEPARTMENT
FROM emp e, dept d
WHERE e.deptno = d.deptno;
Here the tables emp and dept have been joined by the column deptno. Please note the use of table alias (emp e)
and column alias (e.ename NAME) in this statement.
Q: Explain Concept of Self Join
When a table is joined to itself in a query then that is called a self-join.
The structure of emp table is
eno
Employee No
ename
Employee Name
mgrno
Manager Number
Data in the table is
eno
ename
mgrno
1
suku
2
2
pari
3
Now we want to find the name of the manager of the employee named 'suku'. This can be done by the following
query
Select mgr.ename "Manager Name"
From emp e, emp mgr
Where e.mgrno=mgr.eno
Here the emp table is joined with itself. So this is a self join.
Q: explain Outer Join in SQL
This is one of the areas that many students make a mistake and it is perhaps one of the most frequently asked
question in interviews. So study it carefully.
In this type of join the query returns all rows from one table and selected number of rows from the second table.
For example say we want to find all employees from emp table and their deprtname from dept table. However the
problem is some of the employees have a department id that does not exist in dept table. In such a situation we will
use outer join in the SQL query.
The structure of emp table is
eno
ename
Employee
No
Employee
Name
mgrno
Manager
Number
dept_id
Department
id
Data in the table is
eno
ename
mgrno
dept_id
1
suku
2
10
2
pari
3
30
The structure of dept table is
dept_id
Department id
dname
Departtment Name
Data in the table is
dept_id
dname
10
Physics
20
Chemistry
Now run the following query
Select e.ename, d.dname
from emp e, dept d
where e.dept_id=d.dept_id(+)
This will return
ename
dname
suku
Physics
pari
Please see that even though pari does not have a matching record in dept the record from emp table is returned.
how to know the days or months or years between two employees in emp table?
Let us say you want to find the days between the hiredates of two employees with employee no 99 and 345. in that
case you can use the following query
select emp1.hiredate - emp2.hiredate
from emp emp1, emp emp2
where emp1.eno = 99
and emp2.eno= 345
if you do not know the employee no but rather the name then
select emp1.hiredate - emp2.hiredate
from emp emp1, emp emp2
where emp1.ename = 'ram'
and emp2.eno= 'shyam'.
What are the properties of the Relational tables?
Relational tables have six properties:
columns is insignificant.
Q: What is normalization?
Database normalization is a data design and organization process applied to data structures based on rules that
help build relational databases. In relational database design, the process of organizing data to minimize
redundancy. Normalization usually involves dividing a database into two or more tables and defining relationships
between the tables. The objective is to isolate data so that additions, deletions, and modifications of a field can be
made in just one table and then propagated through the rest of the database via the defined relationships.
What are different normalization forms?
1NF: Eliminate Repeating Groups
Make a separate table for each set of related attributes, and give each table a primary key. Each field contains at
most one value from its attribute domain.
2NF: Eliminate Redundant Data
If an attribute depends on only part of a multi-valued key, remove it to a separate table.
3NF: Eliminate Columns Not Dependent On Key
If attributes do not contribute to a description of the key, remove them to a separate table. All attributes must be
directly dependent on the primary key
BCNF: Boyce-Codd Normal Form
If there are non-trivial dependencies between candidate key attributes, separate them out into distinct tables.
4NF: Isolate Independent Multiple Relationships
No table may contain two or more 1:n or n:m relationships that are not directly related.
5NF: Isolate Semantically Related Multiple
Relationships
There may be practical constrains on information that justify separating logically related many-to-many
relationships.
ONF: Optimal Normal Form
A model limited to only simple (elemental) facts, as expressed in Object Role Model notation.
DKNF: Domain-Key Normal Form
A model free from all modification anomalies.
Remember, these normalization guidelines are cumulative. For a database to be in 3NF, it must first fulfill all the
criteria of a 2NF and 1NF database.
What is an execution plan? When would you use it? How would you view the execution plan?
An execution plan is basically a road map that graphically or textually shows the data retrieval methods chosen by
the SQL Server query optimizer for a stored procedure or ad-hoc query and is a very useful tool for a developer to
understand the performance characteristics of a query or stored procedure since the plan is the one that SQL
Server will place in its cache and use to execute the stored procedure or query. From within Query Analyzer is an
option called "Show Execution Plan" (located on the Query drop-down menu). If this option is turned on it will
display query execution plan in separate window when query is ran again.
Q: List few advantages of Stored Procedure.
overhead.
res help promote code reuse.
Q: What are the advantages of VIEW ?
To protect some of the columns of a table from other users.
To hide complexity of a query.
To hide complexity of calculations.
Q: What is difference between UNIQUE and PRIMARY KEY constraints?
A table can have only one PRIMARY KEY whereas there can be any number of UNIQUE keys. The columns that
compose PK are automatically define NOT NULL, whereas a column that compose a UNIQUE is not automatically
defined to be mandatory must also specify the column is NOT NULL.
Q: What is ON DELETE CASCADE ?
When ON DELETE CASCADE is specified ORACLE maintains referential integrity by automatically removing
dependent foreign key values if a referenced primary or unique key value is removed
What is a join ? Explain the different types of joins ?
Join is a query which retrieves related columns or rows from multiple tables.
Self Join - Joining the table with itself.
Equi Join - Joining two tables by equating two common columns.
Non-Equi Join - Joining two tables by equating two common columns.
Outer Join - Joining two tables in such a way that query can also retrieve rows that do not have corresponding join
value in the other table. . Outer Join is used where you can query all the rows of one of the tables in the join
condition even though they don’t satisfy the join condition.
When do you use WHERE clause and when do you use HAVING clause?
HAVING clause is used when you want to specify a condition for a group function and it is written after GROUP BY
clause. The WHERE clause is used when you want to specify a condition for columns, single row functions except
group functions and it is written before GROUP BY clause if it is used.
Q: Which is more faster - IN or EXISTS?
EXISTS is more faster than IN because EXISTS returns a Boolean value whereas IN returns a value.
What is difference between TRUNCATE & DELETE ?
TRUNCATE commits after deleting entire table i.e., can not be rolled back. Database triggers do not fire on
TRUNCATEDELETE allows the filtered deletion. Deleted records can be rolled back or committed.Database
triggers fire on DELETE. synchronized.
Q: Explain UNION,MINUS,UNION ALL, INTERSECT ?
INTERSECT returns all distinct rows selected by both queries.MINUS - returns all distinct rows selected by the first
query but not by the second.UNION - returns all distinct rows selected by either queryUNION ALL - returns all rows
selected by either query, including all duplicates.
Q: What is CYCLE/NO CYCLE in a Sequence ?
CYCLE specifies that the sequence continues to generate values after reaching either maximum or minimum value.
After pan ascending sequence reaches its maximum value, it generates its minimum value. After a descending
sequence reaches its minimum, it generates its maximum.NO CYCLE specifies that the sequence cannot generate
more values after reaching its maximum or minimum value.
Can a view be updated/inserted/deleted? If Yes under what conditions ?
A View can be updated/deleted/inserted if it has only one base table if the view is based on columns from one or
more tables then insert, update and delete is not possible.
What are the Various Master and Detail Relation ships.
The various Master and Detail Relationship are
child is exisiting
What are various constraints used in SQL?
.