Commonly Asked DBMS Interview Questions
Commonly Asked DBMS Interview Questions
6. What are the differences between DDL, DML, and DCL in SQL?
Following are some details of three :
DDL stands for Data Definition Language. SQL queries like CREATE, ALTER,
DROP, TRUNCATE and RENAME come under this.
DML stands for Data Manipulation Language. SQL queries like SELECT, INSERT,
DELETE and UPDATE come under this.
DCL stands for Data Control Language. SQL queries like GRANT and REVOKE
come under this.
7. What is the difference between having and where clause?
HAVING is used to specify a condition for a group or an aggregate function
used in a select statement. The WHERE clause selects before grouping. The
HAVING clause selects rows after grouping. Unlike the HAVING clause, the
WHERE clause cannot contain aggregate functions. (See this for
examples). See Having vs Where Clause? for more details
8.How to print duplicate rows in a table?
See https://www.geeksforgeeks.org/how-to-print-duplicate-rows-in-a-table/
9. What is Join?
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.
Table – Student Table
EnrollNo StudentName Address
1 1000
2 1000
3 1000
1 1002
2 1003
Following is a join query that shows the names of students enrolled in
different courseIDs.
SELECT StudentCourse.CourseID, Student.StudentName
FROM StudentCourse
INNER JOIN Student
ON StudentCourse.EnrollNo = Student.EnrollNo
ORDER BY StudentCourse.CourseID;
The above query would produce the following result.
CourseID StudentName
1 geek1
1 geek3
2 geek1
3 geek1
9. What is Identity?
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.
10.What is a view in SQL? How to create a view?
A view is a virtual table based on the result-set of an SQL statement. We can
create it using create view syntax.
Here, the inner query is not executed for each of the rows of the outer query.
32. Explain Entity, Entity Type, and Entity Set in DBMS?
The entity is an object, place, or thing which has its independent existence in
the real world and about which data can be stored in a database. For
Example, any person, book, etc.
Entity Type is a collection of entities that have the same attributes. For
Example, the STUDENT table contains rows in which each row is an entity
holding the attributes like name, age, and id of the students, hence STUDENT is
an Entity Type that holds the entities having the same attributes.
Entity Set is a collection of entities of the same type. For Example, A collection
of the employees of a firm.
33. What are the different levels of abstraction in the DBMS?
There are 3 levels of data abstraction in the DBMS.
They include:
Physical Level: This is the lowest level of the data abstraction which states how
the data is stored in the database.
Logical Level: This is the next level of the data abstraction which states the type
of the data and the relationship among the data that is stored in the database.
View Level: This is the highest level in the data abstraction which shows/states
only a part of the database.
34 . What integrity rules exist in the DBMS?
There are two major integrity rules that exist in the DBMS.
Entity Integrity: This states a very important rule that the value of a Primary
key can never have a NULL value.
Referential Integrity: This rule is related to the Foreign key which states that
either the value of a Foreign key is a NULL value or it should be the primary key
of any other relation.
35. What is E-R model in the DBMS?
E-R model is known as an Entity-Relationship model in the DBMS which is
based on the concept of the Entities and the relationship that exists among
these entities.
36. What is a functional dependency in the DBMS?
This is basically a constraint that is useful in describing the relationship among
the different attributes in a relation.
Example: If there is some relation ‘R1’ which has 2 attributes as Y and Z then
the functional dependency among these 2 attributes can be shown as Y-
>Z which states that Z is functionally dependent on Y.
37. What is 1NF in the DBMS?
1NF is known as the First Normal Form.
This is the easiest form of the normalization process which states that
the domain of an attribute should have only atomic values. The objective of this
is to remove the duplicate columns that are present in the table.
38. What is 2NF in the DBMS?
2NF is the Second Normal Form.
Any table is said to have in the 2NF if it satisfies the following 2 conditions:
A table is in the 1NF.
Each non-prime attribute of a table is said to be functionally dependent in
totality on the primary key.
39. What is 3NF in the DBMS?
3NF is the Third Normal Form.
Any table is said to have in the 3NF if it satisfies the following 2 conditions:
A table is in the 2NF.
Each non-prime attribute of a table is said to be non-transitively dependent on
every key of the table.
40. What is BCNF in the DBMS?
BCNF is the Boyce Codd Normal Form which is stricter than the 3NF.
Any table is said to have in the BCNF if it satisfies the following 2 conditions:
A table is in the 3NF.
For each of the functional dependencies X->Y that exists, X is the super key of a
table.
41. What is a CLAUSE in terms of SQL?
This is used with the SQL queries to fetch specific data as per the requirements
on the basis of the conditions that are put in the SQL. This is very helpful in
picking the selective records from the complete set of records.
For Example, There is a query that has a WHERE condition or the query with
the HAVING clause.
42.How can you get the alternate records from the table in the SQL?
If you want to fetch the odd numbers then the following query can be used:
SELECT EmpId from (SELECT rowno,EmpId from Emp) WHERE mod(rowno,2)=1;
If you want to fetch the even numbers, then the following query can be used:
The mapping can proceed in The following criteria are often used
two stages: to guide the choice of physical
Choice of database design options:
criteria • System-
independent • Response Time
mapping but • Space Utilization
Parameters Logical Database Design Physical Database Design
The database design is divided into several phases. The logical database design
and physical database design are two of them. This separation is generally
based on the concept of the three-level architecture of DBMS, which provides
data independence. Therefore, we can say that this separation leads to data
independence because the output of the logical database design is the
conceptual and external level schemas of the database system which is
independent of the output of the physical database design that is an internal
schema.
52. What are temporary tables? When are they useful?
Temporary tables exist solely for a particular session, or whose data persists
for the duration of the transaction. The temporary tables are generally used to
support specialized rollups or specific application processing requirements.
Unlike a permanent table, space is not allocated to a temporary table when it is
created. Space will be dynamically allocated for the table as rows are inserted.
The CREATE GLOBAL TEMPORARY TABLE command is used to create a
temporary table in Oracle.
53. Explain different types of failures that occur in the Oracle database.
Types of Failures – In the Oracle database following types of failures can
occur:
• Statement Failure·
• Bad data type
o Insufficient space
• Insufficient Privileges (e.g., object privileges to a role)
• User Process Failure
o The user performed an abnormal disconnect
o The user’s session was abnormally terminated
o The user’s program raised an address exception
• User Error
o The user drops a table
o User damages data by modification
• Instance Failure
• Media Failure
o The user drops a table
o User damages data by modification
• Alert Logs
o Records informational and error messages
o All Instance startups and shutdowns are recorded in
the log
54. What is the main goal of RAID technology?
RAID stands for Redundant Array of Inexpensive (or sometimes
“Independent”)Disks.
RAID is a method of combining several hard disk drives into one logical unit
(two or more disks grouped together to appear as a single device to the host
system). RAID technology was developed to address the fault-tolerance and
performance limitations of conventional disk storage. It can offer fault
tolerance and higher throughput levels than a single hard drive or group of
independent hard drives. While arrays were once considered complex and
relatively specialized storage solutions, today they are easy to use and essential
for a broad spectrum of client/server applications.
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
OR
SELECT max(salary) FROM EMPLOYEES WHERE
salary <> (SELECT max(salary) FROM EMPLOYEES);
Q.Consider the following Employee table. How many rows are there in
the result of the following query?
ID salary DeptName 1 10000 EC 2 40000 EC
3 30000 CS 4 40000 ME 5 50000 ME 6 60000 ME
7 70000 CS
How many rows are there in the result of the following query?
SELECT E.ID
FROM Employee E
WHERE EXISTS (SELECT E2.salary
FROM Employee E2
WHERE E2.DeptName = 'CS'
AND E.salary > E2.salary)
Following 5 rows will be the result of the query as 3000 is the minimum
salary of CS Employees and all these rows are greater than 30000. 2 4 5
67
Q. Write a trigger to update Emp table such that, If an updation is
done in Dep table then salary of all employees of that department
should be incremented by some amount (updation) Assuming Table
name are Dept and Emp, trigger can be written as follows:
CREATE OR REPLACE TRIGGER update_trig
AFTER UPDATE ON Dept
FOR EACH ROW
DECLARE
CURSOR emp_cur IS SELECT * FROM Emp;
BEGIN
FOR i IN emp_cur LOOP
IF i.dept_no = :NEW.dept_no THEN
DBMS_OUTPUT.PUT_LINE(i.emp_no); -- for printing those
UPDATE Emp -- emp number which are
SET sal = i.sal + 100 -- updated
WHERE emp_no = i.emp_no;
END IF;
END LOOP;
END;
Q.Name the Employee who has the third-highest salary using sub
queries.
SELECT Emp1.Name
FROM Employee Emp1
WHERE 2 = (SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary
)
Logic: Number of people with a salary higher than this person will be 2.
Q. 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.
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. Let us consider below table ‘Marks’.
Student Course Score a c1 40
a c2 50 b c3 60 d c1 70
e c2 80 Consider the query
SELECT Student, sum(Score) AS total
FROM Marks
This would select data row by row basis. The having clause works on
aggregated data. For example, the output of the below query
SELECT Student, sum(score) AS total FROM Marks
Student Total a 90 e 80
Q. Difference between primary key and unique key and why one
should use a 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 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.
Q. 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.
SQL statements are compiled at compile SQL statements are compiled at run
time. time.
It is generally used for situations where It is generally used for situations where
data is distributed uniformly. data is distributed non-uniformly.
• Candidate Key: The candidate key represents a set of properties that can
uniquely identify a table. Each table may have multiple candidate keys. One
key amongst all candidate keys can be chosen as a primary key. In the below
example since studentId and firstName can be considered as a Candidate Key
since they can uniquely identify every tuple.
• Super Key: The super key defines a set of attributes that can uniquely identify
a tuple. Candidate key and primary key are subsets of the super key, in other
words, the super key is their superset.
• Primary Key: The primary key defines a set of attributes that are used to
uniquely identify every tuple. In the below example studentId and firstName
are candidate keys and any one of them can be chosen as a Primary Key. In the
given example studentId is chosen as the primary key for the student table.
• Unique Key: The unique key is very similar to the primary key except that
primary keys don’t allow NULL values in the column but unique keys allow
them. So essentially unique keys are primary keys with NULL values.
• Alternate Key: All the candidate keys which are not chosen as primary keys
are considered as alternate Keys. In the below example, firstname and
lastname are alternate keys in the database.
• Foreign Key: The foreign key defines an attribute that can only take the
values present in one table common to the attribute present in another table.
In the below example courseId from the Student table is a foreign key to the
Course table, as both, the tables contain courseId as one of their attributes.
• Composite Key: A composite key refers to a combination of two or more
columns that can uniquely identify each tuple in a table. In the below example
the studentId and firstname can be grouped to uniquely identify every tuple in
the table.
• 1NF: It is known as the first normal form and is the simplest type of
normalization that you can implement in a database. A table to be in its first
normal form should satisfy the following conditions:
o Every column must have a single value and should be atomic.
o Duplicate columns from the same table should be removed.
o Separate tables should be created for each group of related data and
each row should be identified with a unique column.
Table-1 converted to 1NF form
• 2NF: It is known as the second normal form. A table to be in its second normal
form should satisfy the following conditions:
o The table should be in its 1NF i.e. satisfy all the conditions of 1NF.
o Every non-prime attribute of the table should be fully functionally
dependent on the primary key i.e. every non-key attribute should be
dependent on the primary key in such a way that if any key element is
deleted then even the non_key element will be saved in the database.
• BCNF: BCNF stands for Boyce-Codd Normal Form and is an advanced form of
3NF. It is also referred to as 3.5NF for the same reason. A table to be in its
BCNF normal form should satisfy the following conditions:
o The table should be in its 3NF i.e. satisfy all the conditions of 3NF.
o For every functional dependency of any attribute A on B
(A->B), A should be the super key of the table. It simply implies that A
can’t be a non-prime attribute if B is a prime attribute.