Sie sind auf Seite 1von 4

Interview Questions: Database

1. Question: What are the Difference between Functions and Procedures?


Answer: 1. Functions are used for computations where as Procedures can be used for
performing business logic

2. Functions MUST return a value, Procedures need not be.

3) You can have DML(insert, update, delete) statements in a Function. But, you cannot call
such a function in a SQL query..

eg: suppose, if u have a function that is updating a table.. you can~t call that function in
any sql query.

- select myFunction(field) from sometable;

will throw error.

4) function parameters are always IN, no OUT is possible

5) function returns 1 value only. procedure can return multiple values(max. 1024)

6) we can select the fields from function. in the case of procdure we cannot select the fields.

7) Function do not return the images,text whereas stored procedures returns all

2. Question: What is the difference between UNION and UNION ALL ?

Answer: UNION ALL selects all records between two queries including duplicates, whereas
UNION takes the time to sort records in order to remove duplicates. In short UNION only
selects distinct values, UNION ALL selects all values.

e.g.
SELECT Date FROM table1 UNION SELECT Date FROM table2
SELECT Date FROM table1 UNION ALL SELECT Date FROM table2

3. Question: What is a join and explain different types of joins.

Answer: Joins are used in queries to explain how different tables are related. Joins also let you
select data from a table depending upon data from another table.

Types of joins: INNER JOIN, OUTER JOIN, CROSS JOIN.


OUTER JOIN is further classified as LEFT OUTER JOINS, RIGHT OUTER JOINS and FULL OUTER
JOINS.

Table details
Student
ID
FirstName
LastName
BirthDate
Attendance
ID
AttendanceDate
AttendanceCode
MinutesAbsent
StudentSchedule
ID
Course

1
Interview Questions: Database

Cross Join - A cross join merges two tables on every record in a geometric fashion, every
record of one table is combined with every record from the other table. Two tables of 10
records each in a cross join will create a table of 100 (10 X 10) records.

Inner Join - An Inner join is used to match two tables based on values of a common field.
An inner join gets data from both tables where the specified data exists in both tables. For E.g.

List of students in the database that were absent on December 4, 2003, you would use an
inner join between the two tables "Student" and "Attendance"

SELECT Student.ID, Student.FirstName, Student.LastName, Attendance.AttendanceCode,


Attendance.MinutesAbsent FROM Student INNER JOIN Attendance ON
Student.ID=Attendance.ID WHERE Attendance.AttendanceDate=’12/4/2003’

Outer Join - An outer join gets data from the source table at all times, and returns data from
the outer joined table ONLY if it matches the criteria. When using outer joins, fields will be set
to NULL if data does not exist in the outer-joined table. For E.g.

SELECT Student.ID, Student.FirstName, Student.LastName, Attendance.AttendanceCode,


Attendance.MinutesAbsent FROM Student INNER JOIN StudentSchedule ON
StudentSchedule.ID=Student.ID< br /> LEFT OUTER JOIN Attendance ON
Student.ID=Attendance.ID AND Attendance.AttendanceDate=’12/4/2003’ WHERE
StudentSchedule.Course=’E NGLISH 9’

a)Left Outer join - All outer joins retrieve records from both tables, just as an inner join
does. However, an outer join retrieves all of the records from one of the tables. A column in
the result is NULL if the corresponding input table did not contain a matching record.
b)Right Outer join - The right outer join is similar to the left outer join in that it retrieves all
the records from one side of the relationship, but this time it’s the right table. Only records
where the condition values match are retrieved from the left.
c)Full Outer Join - The full outer join retrieves all records from both the left and the right
table.

4. Question: For ~n~ number of SQL SELECT statements connected by UNION, how
many times should we specify UNION to eliminate the duplicate rows?

Answer: Only Once

5. Question: In the WHERE clause what is BETWEEN and IN?

Answer: IN keyword helps to limit the selection criteria to one or more discrete values, the
BETWEEN keyword allows for selecting a range. The syntax for the BETWEEN clause is as
follows:

SELECT "column_name" FROM "table_name" WHERE "column_name" BETWEEN ~value1~


AND ~value2~

This will select all rows whose column has a value between ~value1~ and ~value2~.

The syntax for using the IN keyword is as follows:

SELECT "column_name"
FROM "table_name"
WHERE "column_name" IN (~value1~, ~value2~, ...)

The number of values in the parenthesis can be one or more, with each values separated by
comma. Values can be numerical or characters. If there is only one value inside the

2
Interview Questions: Database

parenthesis, this commend is equivalent to

WHERE "column_name" = ~value1~

6. Question: What is ~LIKE~ used for in WHERE clause? What are the wildcard
characters?

Answer: LIKE is another keyword that is used in the WHERE clause. Basically, LIKE allows you
to do a search based on a pattern rather than specifying exactly what is desired (as in IN) or
spell out a range (as in BETWEEN).

The syntax for is as follows:

SELECT "column_name" FROM "table_name" WHERE "column_name" LIKE {PATTERN}

‘%’ ( for a string of any character ) and ‘_’ (for any single character ) are the two wild card
characters.

7. Question: When do you use a LIKE statement?

Answer: To do partial search e.g. to search employee by name, you need not specify
the complete name; using LIKE, you can search for partial string matches.

Example SQL : SELECT EMPNO FROM EMP WHERE EMPNAME LIKE ~RAMESH%~

% is used to represent remaining all characters in the name.


This query fetches all records contains RAMESH in six characters.

8. Question: What is a transaction?

Answer: A transaction is a logicl unit of work where all steps must be commited or rolled back.

9. Question: What is ACID?

Answer: ACID stands for Atomicity, Consistency, Isolation and Duralbility. These are the
properties of a
transaction.

1. Atomicity - states that database modifications must follow an "all or nothing" rule. Each
transaction is said to be "atomic." If one part of the transaction fails, the entire transaction
fails. It is critical that the database management system maintain the atomic nature of
transactions in spite of any DBMS, operating system or hardware failure.

2. Consistency - states that only valid data will be written to the database. If, for some reason,
a transaction is executed that violates the database’s consistency rules, the entire transaction
will be rolled back and the database will be restored to a state consistent with those rules. On
the other hand, if a transaction successfully executes, it will take the database from one state
that is consistent with the rules to another state that is also consistent
with the rules.

3. Isolation - requires that multiple transactions occurring at the same time not impact each
other~s execution.
For example, if Joe issues a transaction against a database at the same time that Mary issues
a different transaction, both transactions should operate on the database in an isolated
manner. The database should either perform Joe~s entire transaction before executing Mary~s
or vice-versa. This prevents Joe~s transaction from reading intermediate data
produced as a side effect of part of Mary’s transaction that will not eventually be committed to

3
Interview Questions: Database

the database. Note that the isolation property does not ensure which transaction will execute
first, merely that they will not interfere with each other.

4. Durability - ensures that any transaction committed to the database will not be lost.
Durability is ensured through the use of database backups and transaction logs that facilitate
the restoration of committed transactions in spite of any subsequent software or hardware
failures.

10. Question: What is GROUP BY?

Answer: The GROUP BY keywords have been added to SQL because aggregate functions (like
SUM) return the aggregate of all column values every time they are called. Without the GROUP
BY functionality, finding the sum for each individual group of column values was not possible.

The corresponding SQL syntax is,

SELECT "column_name1", SUM("column_name2")


FROM "table_name"
GROUP BY "column_name1"

11. Question: What is the difference between dropping, truncating and deleting from
a table.

Answer: 1. Dropping - (Table structure + Data are deleted), Invalidates the dependent objects
,Drops the indexes
2. Truncating - (Data alone deleted), Performs auto commit and is faster than delete
3. Delete - (Data alone deleted), Auto commit is not performed

12. Question: What are triggers? How to invoke a trigger on demand?

Answer: Triggers are special kind of stored procedures that get executed automatically when
an INSERT, UPDATE or DELETE operation takes place on a table.

Triggers can’t be invoked on demand. They get triggered only when an associated action
(INSERT, UPDATE, DELETE) happens on the table on which they are defined.

Triggers are generally used to implement business rules, auditing. Triggers can also be used to
extend the referential integrity checks, but wherever possible, use constraints for this purpose,
instead of triggers, as constraints are much faster.

13. Question: How do you select second maximum marks from a Student table with a
single statement?

Answer: select max(marks) from Student where marks < (select max(marks) from Student);

Das könnte Ihnen auch gefallen