0% found this document useful (0 votes)
15 views11 pages

DBMS Interview Questions

The document discusses the advantages of Database Management Systems (DBMS) over traditional file-based systems, highlighting issues like data redundancy and inconsistency, difficulty in data access, and security problems. It explains key concepts such as superkeys, primary keys, candidate keys, and foreign keys, as well as the differences between primary keys and unique constraints. Additionally, it covers various SQL topics including normalization, DDL, DML, DCL, joins, views, triggers, stored procedures, transactions, indexes, and the differences between CHAR and VARCHAR data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views11 pages

DBMS Interview Questions

The document discusses the advantages of Database Management Systems (DBMS) over traditional file-based systems, highlighting issues like data redundancy and inconsistency, difficulty in data access, and security problems. It explains key concepts such as superkeys, primary keys, candidate keys, and foreign keys, as well as the differences between primary keys and unique constraints. Additionally, it covers various SQL topics including normalization, DDL, DML, DCL, joins, views, triggers, stored procedures, transactions, indexes, and the differences between CHAR and VARCHAR data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

What are advantages of DBMS over traditional file based systems?

Ans: Database management systems were developed to handle the following difficulties of typical
file-processing systems supported by conventional operating systems.
1. Data redundancy and inconsistency
2. Difficulty in accessing data
3. Data isolation – multiple files and formats
4. Integrity problems
5. Atomicity of updates
6. Concurrent access by multiple users
7. Security problems

What are super, primary, candidate and foreign keys?


Ans: A superkey is a set of attributes of a relation schema upon which all attributes of the schema are
functionally dependent. No two rows can have the same value of super key attributes.
A Candidate key is minimal superkey, i.e., no proper subset of Candidate key attributes can be a
superkey.
A Primary Key is one of the candidate keys. One of the candidate keys is selected as most important and
becomes the primary key. There cannot be more that one primary keys in a table.
Foreign key is a field (or collection of fields) in one table that uniquely identifies a row of another table.If
the primary key of a table act as a non key into another table then tat one is considered as Foreign key.

What is the difference between primary key and unique constraints?


Ans: Primary key cannot have NULL value, the unique constraints can have NULL values. There is only
one primary key in a table, but there can be multiple unique constrains.

What is database normalization?

Ans: It is a process of analyzing the given relation schemas based on their functional dependencies and
primary keys to achieve the following desirable properties:

1) Minimizing Redundancy

2) Minimizing the Insertion, Deletion, And Update Anomalies

Relation schemas that do not meet the properties are decomposed into smaller relation schemas that
could meet desirable properties.

What are the differences between DDL, DML and DCL in SQL?

Ans: Following are some details of three.


DDL stands for Data Definition Language. SQL queries like CREATE, ALTER, DROP and RENAME come
under this.

DML stands for Data Manipulation Language. SQL queries like SELECT, INSERT and UPDATE come under
this.

DCL stands for Data Control Language. SQL queries like GRANT and REVOKE come under this.

What is the difference between having and where clause?

Ans: HAVING is used to specify a condition for a group or an aggregate function used in select statement.
The WHERE clause selects before grouping. The HAVING clause selects rows after grouping. Unlike
HAVING clause, the WHERE clause cannot contain aggregate functions.

WhatisJoin?
Ans: An SQL Join is used to combine data from two or more tables, based on a
common field between them. For example, consider the following two tables.
Student Table

ENROLLNO STUDENTNAME ADDRESS

1000 geek1 geeksquiz1

1001 geek2 geeksquiz2

1002 geek3 geeksquiz3

StudentCourse Table

COURSEID ENROLLNO

1 1000

2 1000

3 1000

1 1002
2 1003

Following is join query that shows names of students enrolled in different courseIDs.

SELECT StudentCourse.CourseID, Student.StudentName

FROM StudentCourse

INNER JOIN Customers

ON StudentCourse.EnrollNo = Student.EnrollNo

ORDER BY StudentCourse.CourseID;

The above query would produce following result.

COURSEID STUDENTNAME

1 geek1

1 geek2

2 geek1

2 geek3

3 geek1

What is Identity?

Ans: Identity (or AutoNumber) is a column that automatically generates numeric values. A start and
increment value can be set, but most DBA leave these at 1. A GUID column also generates numbers; the
value of this cannot be controlled. Identity/GUID columns do not need to be indexed.

What is a view in SQL? How to create one

Ans: A view is a virtual table based on the result-set of an SQL statement. We can create using create
view syntax.

CREATE VIEW view_name AS


SELECT column_name(s)

FROM table_name

WHERE condition

What are the uses of view?

1. Views can represent a subset of the data contained in a table; consequently, a view can limit the
degree of exposure of the underlying tables to the outer world: a given user may have permission to
query the view, while denied access to the rest of the base table.

2. Views can join and simplify multiple tables into a single virtual table

3. Views can act as aggregated tables, where the database engine aggregates data (sum, average etc.)
and presents the calculated results as part of the data

4. Views can hide the complexity of data; for example a view could appear as Sales2000 or Sales2001,
transparently partitioning the actual underlying table

5. Views take very little space to store; the database contains only the definition of a view, not a copy of
all the data which it presentsv.

6. Depending on the SQL engine used, views can provide extra security

What is a Trigger?

Ans: A Trigger is a code that associated with insert, update or delete operations. The code is executed
automatically whenever the associated query is executed on a table. Triggers can be useful to maintain
integrity in database.

What is a stored procedure?

Ans: A stored procedure is like a function that contains a set of operations compiled together. It contains
a set of operations that are commonly used in an application to do some common database tasks.

What is the difference between Trigger and Stored Procedure?

Ans: Unlike Stored Procedures, Triggers cannot be called directly. They can only be associated with
queries.
What is a transaction? What are ACID properties?

Ans: A Database Transaction is a set of database operations that must be treated as whole, means either
all operations are executed or none of them.

An example can be bank transaction from one account to another account. Either both debit and credit
operations must be executed or none of them.

ACID (Atomicity, Consistency, Isolation, Durability) is a set of properties that guarantee that database
transactions are processed reliably.

What are indexes?

Ans: A database index is a data structure that improves the speed of data retrieval operations on a
database table at the cost of additional writes and the use of more storage space to maintain the extra
copy of data.

Data can be stored only in one order on disk. To support faster access according to different values,
faster search like binary search for different values is desired, For this purpose, indexes are created on
tables. These indexes need extra space on disk, but they allow faster search according to different
frequently searched values.

What are clustered and non-clustered Indexes?

Ans: Clustered indexes is the index according to which data is physically stored on disk. Therefore, only
one clustered index can be created on a given database table.

Non-clustered indexes don’t define physical ordering of data, but logical ordering. Typically, a
tree is created whose leaf point to disk records. B-Tree or B+ tree are used for this purpose.

Query to find 2nd highest salary of an employee?


SELECT max(salary) FROM EMPLOYEES WHERE salary IN

(SELECT salary FROM EMPLOYEEs MINUS SELECT max(salary)

FROM EMPLOYEES);

OR

SELECT max(salary) FROM EMPLOYEES WHERE

salary <> (SELECT max(salary) FROM EMPLOYEES);


There is a table where only one row is fully repeated. Write a Query to find the
Repeated row
Name Section

abc CS1

bcd CS2

abc CS1

In the above table, we can find duplicate row using below query.

SELECT name, section FROM tbl

GROUP BY name, section

HAVING COUNT(*) > 1


Why we cannot use WHERE clause with aggregate functions like HAVING ?
The difference between the having and where clause in SQL is that the where clause
canNOT be used with aggregates, but the having clause can. Please note : It is not a
predefined rule but by and large you’ll see that in a good number of the SQL queries,
we use WHERE prior to GROUP BY and HAVING after GROUP BY.
The Where clause acts as a pre filter where as Having as a post filter.
The where clause works on row’s data, not on aggregated data.
When we apply having in above query, we get

SELECT Student, sum(score) AS total

FROM Marks having total > 70

Student Total
a 90
e 80

Difference between primary key and unique key and why one should use unique key if
it allows only one null ?
Primary key:
▪ Only one in a row(tuple).
▪ Never allows null value(only key field).
▪ Unique key identifier and can not be null and must be unique.
Unique Key:
▪ Can be more than one unique key in one row.
▪ Unique key can have null values(only single null is allowed).
▪ It can be a candidate key.
▪ Unique key can be null and may not be unique.

What’s the difference between materialized and dynamic view?


Materialized views
▪ Disk based and are updated periodically based upon the query definition.
▪ A materialized table is created or updated infrequently and it must be synchronized with its
associated base tables.
Dynamic views
▪ Virtual only and run the query definition each time they are accessed.
▪ A dynamic view may be created every time that a specific view is requested by the user.

Q. What is embedded and dynamic SQL?


Static or Embedded SQL
▪ SQL statements in an application that do not change at runtime and, therefore, can be hard-coded
into the application.
Dynamic SQL
▪ SQL statements that are constructed at runtime; for example, the application may allow users to
enter their own queries.
▪ Dynamic SQL is a programming technique that enables you to buildSQL statements dynamically at
runtime. You can create more general purpose, flexible applications by using dynamic SQL
because the full text of a SQL statement may be unknown at compilation.
S.No. Static (embedded) SQL Dynamic (interactive) SQL

In static SQL how database will be accessed is In dynamic SQL, how database will be accessed

1. predetermined in the embedded SQL statement. is determined at run time.

2. It is more swift and efficient. It is less swift and efficient.

3. SQL statements are compiled at compile time. SQL statements are compiled at run time.

Parsing, validation, optimization, and generation of Parsing, validation, optimization, and generation

4. application plan are done at compile time. of application plan are done at run time.

It is generally used for situations where data is It is generally used for situations where data is

5. distributed uniformly. distributed non-uniformly.

EXECUTE IMMEDIATE, EXECUTE and PREPARE EXECUTE IMMEDIATE, EXECUTE and PREPARE

6. statements are not used. statements are used.

7. It is less flexible. It is more flexible.

What is the difference between CHAR and VARCHAR?


▪ CHAR and VARCHAR are differ in storage and retrieval.
▪ CHAR column length is fixed while VARCHAR length is variable.
▪ The maximum no. of character CHAR data type can hold is 255 character while VARCHAR can
hold up to 4000 character.
▪ CHAR is 50% faster than VARCHAR.
▪ CHAR uses static memory allocation while VARCHAR uses dynamic memory allocation.

Normal Forms
▪ First Normal Form: A relation is in first normal form if it does not contain any
multi-valued or composite attribute.
▪ Second Normal Form: A relation is in second normal form if it does not contain
any partial dependency. A dependency is called partial dependency if any proper
subset of candidate key determines non-prime (which are not part of candidate
key) attribute.
▪ Third Normal Form: A relation is in third normal form if it does not contain any
transitive dependency. For a relation to be in Third Normal Form, either LHS of
FD should be super key or RHS should be prime attribute.
▪ Boyce-Codd Normal Form: A relation is in Boyce-Codd Normal Form if LHS of
every FD is super key. The relationship between Normal Forms can be
represented as:1NF⊃2NF⊃3NF⊃BCNF

Cardinality Minimum No. of tables

1:1 cardinality with partial participation of

both entities 2

1:1 cardinality with total participation of

atleast 1 entity 1

1:n cardinality 2

m:n cardinality 3

Checking for Conflict Serializability

To check whether a schedule is conflict serializable or not, find all conflicting


operations pairs of a schedule and draw precedence graph ( For all conflicting
operation pair, an edge from Ti to Tj if one operation of conflicting pair is from Ti and
other from Tj and operation of Ti occurs before Tj in schedule). If graph does not
contain cycle, the schedule is conflict serializable else it is not conflict serializable.
Schedules are said to be conflict equivalent if 1 schedule can be converted into
another by swapping non conflicting operations.
Note: Two phase locking protocol produce conflict serializable schedule but may
suffer from deadlock. On the other hand, Time-Stamp based protocols are free from
deadlock yet produce conflict serializable schedule.

View Serializable and View Equivalence: Two schedules S1 and S2 are said to be
view-equivalent if all conditions are satisfied for all objects:
▪ If the transaction Ti in S1 reads an initial value for object X, in S2 also, Ti must
read the initial value of X.
▪ If the transaction Ti in S1 reads the value written by transaction Tj in S1 for
object X, same should be done in S2.
▪ If the transaction Ti in S1 is the final transaction to write the value for an object
X, in S2 also, Timust write the final value of X.
A schedule is view serializable if it is view equivalent to any serial schedule.

Irrecoverable Schedules: For a transaction pair < Ti, Tj >, if Tj is reading the value
updated by Ti and Tj is committed before commit of Ti, the schedule will be
irrecoverable.

Recoverable Schedules: For a transaction pair < Ti, Tj >, if Tj is reading the value
updated by Ti and Tj is committed after commit of Ti, the schedule will be
recoverable.

Cascadeless Recoverable Schedules: For a transaction pair < Ti, Tj >, if value updated
by Ti is read by Tj only after commit of Ti, the schedule will be cascadeless
recoverable.

Strict Recoverable: For a transaction pair < Ti, Tj >, if value updated by Ti is read or
written by Tj only after commit of Ti, the schedule will be strict recoverable. The
relationship between them can be represented as:

Strict ⊂ Cascadeless Recoverable ⊂ recoverable ⊂ all schedules

You might also like