DBT Interview Prep
DBT Interview Prep
RDBMS
A relational database management system (RDBMS or just RDB) is a common type of
database that stores data in tables, so it can be used in relation to other stored datasets.
DBMS RDBMS
DBMS applications store data as file. RDBMS applications store data in a
tabular form.
Normalization is not present in DBMS Normalization is present in RDBMS.
DBMS uses file system to store data, so in RDBMS, data values are stored in the
there will be no relation between the form of tables, so a relationship between
tables. these data values will be stored in the form
of a table as well.
Features:
- Provides data to be stored in tables.
- Persists data in the form of rows and columns.
- Provides facility primary key, to uniquely identify the rows.
- Creates indexes for quicker data retrieval.
- Provides a virtual table creation in which sensitive data can be stored and simplified
query can be applied.(views)
- Sharing a common column in two or more tables(primary key and foreign key).
- Provides multi user accessibility that can be controlled by individual users.
1. What is mysql?
MySQL is an open-source relational database management system (RDBMS)
2. What is SQL?
SQL stands for Structured Query Language, and it is used to communicate with the
Database.
This is a standard language used to perform tasks such as retrieval, updation, insertion
and deletion of data from a database.
Candidate key is a single key or a group of multiple keys that uniquely identify rows in a
table.
For example, in the example that we took earlier, both Id and Email can act as a
Candidate for the table as they contain unique and non-null values.
A CHECK constraint is used to limit the values that can be placed in a column. CHECK
constraints are most often used to enforce domain integrity.
A not null constraint enforces that the column will not accept null values. Not null
constraints are used to enforce domain integrity.
A DEFAULT definition is used to add values into a column when values were omitted.
The default value must be compatible with the data type of the column to which the
DEFAULT definition applies.
Where Having
1 WHERE Clause can be used without HAVING Clause cannot be used without
GROUP BY Clause GROUP BY Clause
4 WHERE Clause can be used with HAVING Clause can only be used with
SELECT, UPDATE, DELETE statement. SELECT statement.
5 WHERE Clause is used before GROUP HAVING Clause is used after GROUP BY
BY Clause Clause
COMMIT: Ends the current transaction by making all pending data changes permanent.
ROLLBACK: Ends the current transaction by discarding all pending data changes.
SAVEPOINT: Divides a transaction into smaller parts. You can rollback the transaction till a
particular named savepoint.
1. INNER JOIN / Equijoin: Returns records that have matching values in both tables
2. Cross join / Cartesian Join - The absence of join condition it takes Cartesian product of
table.
That is no of rows in table A & no of rows in table B.
3. Natural join: Returns records that have matching values in both tables.
4. Non-Equi join: Returns records base on non-matching values in both tables.
5. Self-join: Returns records that have matching values in same (self) table.
6. LEFT OUTER JOIN: Return all records from the left table, and the matched records from
the right table
7. RIGHT OUTER JOIN: Return all records from the right table, and the matched records
from the left table
8. FULL OUTER JOIN: Return all records when there is a match in either left or right table.
Normalization is used to remove redundant data from the database and to store non-redundant
data into it.
Normalization rules divides larger tables into smaller tables and links them using relationships.
Without Normalization in SQL, we may face many issues such as
1. Insertion anomaly: It occurs when we cannot insert data to the table without the
presence of another attribute
2. Update anomaly: It is a data inconsistency that results from data redundancy and a
partial update of data.
3. Deletion Anomaly: It occurs when certain attributes are lost because of the deletion of
other attributes.
1NF :
Each table cell should contain a single value.
Each record needs to be unique.
2NF :
Be in 1NF
Single Column Primary Key
3NF :
Be in 2NF
Has no transitive functional dependencies
BCNF:
Even when a database is in 3rd Normal Form, still there would be anomalies resulted if it has
more than one Candidate Key.
4NF
5NF
6NF
1. Clustered Index: A clustered index determines the physical order of data in a table. For
this reason a table can have only one clustered index.
Primary key constraint create clustered indexes automatically if no index already exists
on the table.
Eg. Clustered index is as same as dictionary where the data is arranged by alphabetical
order.
In clustered index, index contains pointer to block but not direct data.
2. Non- Clustered Index: Non-Clustered Index is similar to the index of a book. The
index of a book consists of a chapter name and page number, if you want to read any
topic or chapter then you can directly go to that page by using index of that book. No
need to go through each and every page of a book.
The data is stored in one place, and index is stored in another place. Since, the data
and non-clustered index is stored separately, then you can have multiple non-
clustered index in a table.
Cursors are used to store Database Tables. There are 2 types of Cursors: Implicit Cursors, and
Explicit Cursors.
1. Implicit Cursors:
Implicit Cursors are also known as Default Cursors of SQL SERVER. These Cursors are allocated
by SQL SERVER when the user performs DML operations.
2. Explicit Cursors :
Explicit Cursors are Created by Users whenever the user requires them. Explicit Cursors are
used for Fetching data from Table in Row-By-Row Manner.
A trigger uses the special table to keep a copy of the row which we have just inserted, deleted or
modified.
1. Row level triggers (6 types): Row-level triggers execute once for each row in a transaction.
2. Statement level triggers (6 types): A statement level trigger is also called as table level
trigger. A table level trigger is a trigger that doesn't fire for each row to be changed.
21. Subquery
In SQL a Subquery can be simply defined as a query within another query. In other words we
can say that a Subquery is a query that is embedded in WHERE clause of another SQL query.
You can place the Subquery in a number of SQL
clauses: WHERE clause, HAVING clause, FROM clause.
Subqueries can be used with SELECT, UPDATE, INSERT, DELETE statements along
with expression operator
A subquery is a query within another query. The outer query is called as main
query and inner query is called as subquery.
Subquery must be enclosed in parentheses.
ORDER BY command cannot be used in a Subquery. GROUPBY command can be used
to perform same function as ORDER BY command.
3. You cannot call stored procedures from a You can call a function from a stored
function procedure.
4. A function does not allow output parameters A procedure allows both input and output
parameters.
5. You cannot manage transactions inside a You can manage transactions inside a function.
function.
A transaction can be defined as a group of tasks. A single task is the minimum processing unit
which cannot be divided further.
In order to maintain consistency in a database, before and after the transaction, certain
properties are followed. These are called ACID properties.
Isolation keeps transactions separated from each other until they are finished.
Durability guarantees that the database will keep track of pending changes in such a way that
the server can recover from an abnormal termination and committed transactions will not be
lost.
24. View
Views in SQL are kind of virtual table that is used to view & manipulate parts of the table.
A view also has rows and columns as they are in a real table in the database.
It's depends on your requirement. You have to write an optimized query. In the table you can do
indexing for better performance. Or you can divide the single table into multiple tables on the
basis of different criteria like date created, alphabetical, etc.
Queries:
EMPNO SAL
or……………………………………………………………..
//odd no records
select * from
(select empno, ename, sal, rownum rn
from emp
order by rn)
where mod (rn, 2) != 0;
//even no records
select * from
(select empno, ename, sal, rownum rn
from emp
order by rn)
where mod (rn, 2) = 0;
10. Display names of all emplyoees whose name contains exactly 4 letters
Select ename from emp
Where ename like ‘____’;
11. Display names of all emplyoees whose name contains the second letter as ‘L’ &
forth letter as ‘ M’
12. Display the employee names and hire dates for the employees joined at the month
of December
13. Display names of all emplyoees whose name contains exactly 2 ‘ L’s
14. Display names of employee whose name starts with ‘J’ and ends with ‘S’
Or……………………………………….
select * from
(select rownum r, ename, sal from emp)
where r=4;
…………………………………………………………….
16. Display employees who are working in location Chicago from emp and dept table
17. Display the department name and total salaries from each department
Or…………………………………………………………………………