0% found this document useful (0 votes)
2 views

MySQL Interview Questions

The document provides a comprehensive overview of SQL interview questions and answers, covering key concepts such as databases, database management systems (DBMS), types of DBMS, normalization, and SQL commands. It also explains various types of joins, keys, constraints, and the differences between SQL and MySQL. Additionally, it addresses topics like views, indexes, and the differences between drop, delete, and truncate commands.

Uploaded by

sampath kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

MySQL Interview Questions

The document provides a comprehensive overview of SQL interview questions and answers, covering key concepts such as databases, database management systems (DBMS), types of DBMS, normalization, and SQL commands. It also explains various types of joins, keys, constraints, and the differences between SQL and MySQL. Additionally, it addresses topics like views, indexes, and the differences between drop, delete, and truncate commands.

Uploaded by

sampath kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

SQL INTERVIEW QUESTIONS & Answers by Jeevan Raj

1. What is Database [DB]


Ans:- Database is a container where data can be collected systematically Managing and
Manipulation of those data are easy.
Ex:- An online telephone directory uses a database to store their data,
Like :- Name, Address, Phone Number and other contact details
Ex:- An Online Library who has millions of books in order to maintain their data they use
database.

2. What is a Database Management System [DBMS]


Ans:- Database Management System is basically a software, where you can store,
retrieve, define and manage your data in a database.
- Note:- What does data retrieval means?
 In Databases, Data Retrieval is the process of identifying and extracting data
from a database based on a query provided by the user or applicant.

3. Types of Database Management System


There are Mainly 4 types of Database Management System
1. Hierarchical Database
2. Network Database
3. Relational Database
4. Object-oriented Database

4. What is Hierarchical Database


1. Hierarchical Database :- (ex:- XML data storage has root node enclosing one or
more child node)
 In a hierarchical database data is structured in a tree-like manner.
Those data are stored in hierarchically (Top-down) order and
Also they are represented by Parent child relationship

 In hierarchical database Parent can have many children, but in the case of
children, have only one parent
SQL INTERVIEW QUESTIONS & Answers by Jeevan Raj
5. What is Network Database
1. Network Database:- (ex:- Integrated data store)
 The concept of network database model allows all the child to have multiple
parents and also it helps to solve the more complex model
It uses many to many relationship

6. What is Relational Database


1. Relational Database:-
 Relational Database are Mostly used DBMS, because of its flexibility and easy
to implement nature.
 The model is worked on Normalizing data in the database
 Relational model stores the data in fixed structure and manipulated them using
SQL
- Note:- Normalizing data:- Normalizing is the process of organizing the data in
order to reduce data redundancy and improve data integrity.

7. What is Object Oriented Database


1. Object Oriented Database:-
In Object Oriented Database data are stored in the form of objects.
It Maintains a structure which is known as class and display the data within that.

8. Property of RDBMS
Some of the Properties of RDBMS are :-
1. Values are Atomic in RDBMS (Multiple operations can be grouped into a single
entity)
2. Each column can have common name
3. Integrity constraints helps to maintain the data consistency for multiple tables
4. Each row is Different
5. Columns are Different
SQL INTERVIEW QUESTIONS & Answers by Jeevan Raj

9. Difference between DBMS and RDBMS


DBMS RDBMS
1. Data stored in the form of file 1. Data stored in Table format
format
2. Multiple Data Element are accessible
2. Individual Access of Data together
Element
3. No connection between data 3. Data is stored in the form of table are
4. Data stored in small quantity linked together
5. DBMS Supports a single user 4. Data stored in large amount
6. There is low security while
5. RDBMS Supports multiple users
handling data
7. DBMS does not offer any support 6. It features multiple layers security
for Normalization while handling data [ACID]
7. RDBMS do support multiple
normalization operation.

10. Difference between SQL and MySQL

Parameter
SQL MySQL
SQL is a structured query MySQL is an RDBMS to
Definition
language, It is useful to store, retrieve, modify and
manage relational Database administrate a database using
SQL
It is readily available through
You need to learn the SQL
Complexity download and installation
language to use it effectively
MySQL is database software,
SQL is a Query Language
It uses SQL Language to query
Type
the database
To Query and Operate Allows data handling, storing,
Purpose database system modifying, deleting in a
tabular format.
SQL INTERVIEW QUESTIONS & Answers by Jeevan Raj

11. Different Type of Datatype in MySQL

Data Type

Numeric Float String Data & time

 Tiny Int  Float  Char  Date


 Small Int  Decimal  Varchar  Time
 Int  Double  Binary  Datetime
 Medium Int  Text  Year
 Time Stamp

12. Type of Commands in MySQL in detail


SQL commands are the instructions used to communicate with a database to perform
tasks, functions, and queries with data.
Type of Commands in MySQL are :-
1. DDL [Data Definition Language]
2. DML [Data Manipulation Language]
3. DQL [Data Query Language]
4. DCL [Data Control Language]
5. TCL [Transaction Control Language]
DDL :
- DDL is a SQL Commands to define the database schema.
- It is used to create or modify the structure of database object in the DB
1. Create
2. Drop
3. Alter
4. Truncate
5. Rename
SQL INTERVIEW QUESTIONS & Answers by Jeevan Raj
DML :
- DML used to Manipulate the data present in the database
1. Insert
2. Update
3. Delete
DQL :
- DQL is used to fetch data from database
1. Select
DCL :
- DCL is used to give privileges (permission) to the user to perform any operation in the
database
1. Grant
2. Revoke
TCL :
- If it is a banking sector the money is debited from one account and credited to the other
account
- To manage changes made to data in a table by DML Statement
1. Commit
2. Save point
3. Rollback

13. What is Normalization


Normalization is the process of organizing the data to avoid data duplication and
redundancy by adding more table.

14. What is Denormalization


Denormalization is the opposite of Normalization.
Where as denormalization is a technique used to access the data from higher to lower
form of the database. Where the redundant data is added to speedup complex queries by
joining the multiple table
SQL INTERVIEW QUESTIONS & Answers by Jeevan Raj

15. Type of Normalization in detail


We have 4 types of Normalization
1. First Normal Form [1NF]
2. Second Normal Form [2NF]
3. Third Normal Form [3NF]
4. Boyce Codd Normal Form [BCNF]
First Normal Form [1NF] :-
Each table cell should have a single value. So, Basically all the records must be unique
Second Normal Form [2NF] :-
To use Second Normal Form it should satisfy 1st Normal Form and Should also have
single column Primary Key.
Third Normal Form [3NF] :-
To use Third Normal Form It should satisfy 2nd Normal Form and Must Not have any
transitive functional dependency [Every Attribute should be dependent on Primary key
because there should no partial dependency of the data]
Boyce Codd Normal Form [BCNF] :-
If your Database is in 3rd Normal Form, Then there would be some scenarios where
anomalies would be present. If you have more than one candidate key then BCNF comes
into picture, Where you divide your table further, So that there would be only one
candidate key present.

16. What is Data Model


Data Model is a Blueprint of Business Model, It helps to organize the elements of data &
Systematically defines how they are related to each other according to the business
process.

Data Model

Record - Based Object - Based


Hierarchical Data Models
Object oriented Data Models
Network Data Models
Entity Relationship Data
Relational Data Models Models
SQL INTERVIEW QUESTIONS & Answers by Jeevan Raj

17. What is Entity-Relationship Data Model


Entity Relation data Model is based on the Notion of real-word Entities and relationship
between them.
Note:- Notion:- Notion is an all in one relationship, Where you can write, Plan,
Collaborate and get organized
Entity :- An Entity is an easily recognizable living or non-living real word object
Ex:- Student, Customer, Product.
Relationship :- Relationship is an association between two entities.
Ex:- A Customer Buys a Product

18. What is Schemas and explain type of Schemas


A database Schema is a skeleton structure that represents the logical view of the database.
It defines its entities and relationship between them.
-- The Database Schema is categorized into 2 types
1. Physical Schema
2. Logical Schema

Physical Schema :- Physical Schema represents actual storage of the data


Logical Schema :- Logical Schema represents the logical constraints, That need to be
applied to the data stored
It may define Integrity constraints, Views and Table.

19. What is the difference Between Where Clause and Having Clause
Where Clause Having Clause
 Where clause is used to filter the Having clause used to filter the records
records based on specified from group based on specified condition
condition
SQL INTERVIEW QUESTIONS & Answers by Jeevan Raj
 Where clause cannot have Having clause can operate on aggregate
aggregate function function ex:-[sum, count, min, max, avg]
 Where clause is implemented on
Having clause is implemented on
rows
columns
 Where clause are executed before
group by and after from clause Having clause are executed after group
by clause

20. What is Difference Between Drop, Delete and Truncate


The difference between drop, delete, and Truncate are:-
Delete :-
 Delete is a DML command
 It is used to delete one or more records from a table.
 It is comparatively slower than the truncate command.
 We can use Rollback Command to restore the data Because it does not Auto
commit.
Drop :-
 Drop is a DDL Command
 It is used to remove entire data, The view of the table does not exist, Integrity
constraints will be removed.
 We cannot use Rollback command to restore the data Because it is Auto commit
Truncate :-
 Truncate Removes all the records without removing structure of the table.
 The view of the table exist, Integrity constraints will not be removed
 We cannot use rollback command to restore the data because it is Auto Commit
 Truncate command works faster than Delete Command

21. What are Constraints and Explain the Type of Constraints


 Constraints are Basically a set of Rules or Regulation which we must fallow when
we are entering the data in a table
 Every table has Attributes (columns) and constraints.
 There are Specified while creating the table are can be specified by using alter
command
SQL INTERVIEW QUESTIONS & Answers by Jeevan Raj
Type of Constraints used in SQL:-
 Not Null ----- Ensures that a column cannot have a Null Values
 Unique ---- Ensure all the values in the columns should be unique (different)
 Primary Key – Unique + Not Null [The table should have one primary key
but . may have many not null or unique
constraints]
 Foreign Key – To Maintain integrity of the data and to make the relationship
 Check – Enter the data with some condition ex:- [Age => check (Age>=18)]
 Default - If you have not entered Anything it will Default take the value Provided
 Auto increment – Automatically it increases the values by 1
 Index – Used to create and retrieve data from the database very quickly

22. What are Joins and Explain different type of Joins


Joins are used to combine rows from two or more tables, based on a related column
between them.
Type of Joins in MySQL are:-
 Inner Join : Returns records that have matching values in both tables
 Left Join : Returns all records from the left table, and the matching records from
the right table
 Right Join : Returns all records from the right table, and the matching records
from the left table
 Cross Join : Returns all records from both table

23. What are Keys and Explain the type of DBMS Keys in detail

Keys:- DBMS Keys are one of the most important concept in RDBMS, It is used to
uniquely identify the records in a table. And It is used to make the relationship between
two table.
Why we need DBMS Keys :-
 For Identifying any row of data in a table uniquely
 We can ensure integrity of data is maintained.
 Establish relationship between tables and identifying relationship between table.
Types of DBMS Keys are:-
SQL INTERVIEW QUESTIONS & Answers by Jeevan Raj
1. Super Key
2. Candidate Key
3. Primary Key
4. Foreign Key
5. Composite and Compound Key
6. Alternate Key
7. Surrogate Key
Super Key:-
An Attribute or a set of Attribute that can be used to identify row of data in a table
Ex:- S.ID, Reg ID, Emial ID, S.ID+Reg ID, S.ID+Email ID, Reg ID + Email ID,
S.ID+Reg ID+ Email ID

Candidate Key:-
Candidate key is nothing but minimal subset of super key.
In a table we can have multiple candidate key, An candidate key can have null value
Ex:- S.ID, Reg ID, Email ID

Primary Key:-
The Most Appropriate Candidate key is Primary Key, Will Pick any one of the Attribute
from the candidate key as a Primary Key.

Alternate Key:-
Alternate key is nothing but in a candidate key that is not considered as Primary key

Foreign Key:-
 It is an Attribute in a table which is used to define its relationship with another
table
 Foreign Key is nothing but you are taking reference from other table, where in the
reference table, reference attribute should be unique.
 The table containing a foreign key is a child table, The table that has reference to
is a Parent table
SQL INTERVIEW QUESTIONS & Answers by Jeevan Raj
Composite Key:-
 Any key with more than one attribute is called composite key
Ex:- (SID, Reg ID), (Reg ID, Email ID), (Email ID, SID),(SID, Reg ID, Email Id)

Compound Key:-
If a composite key has at-least one attribute which is a foreign key then it is called as
compound key
Ex:- In the above example if we have composite key (Reg ID, Branch code) then it will
be known as compound key because Branch code Attribute is a Foreign key.

Surrogate Key:-
Sometimes a table has no attribute that can uniquely identify the row in the table
In this situation when there is no Natural Primary Key than will create an attribute that
act as an primary key that is known as Surrogate key
This key do not adds any meaning to the data but serves the sole purpose of identifying
rows uniquely in the table.

24. What are Views in MySQL


View is a virtual table that do not store any data of their own but display the data stored
in other tables.
You will not have to create complex queries involving joins repeatedly
Views helps in data security. You can use view to show only authorized information to
the user and hide the sensitive information

25. What is Index in MySQL and type of Index


1. Index is just like indexes in a book
2. Index will tell exact page no (location) of that page
3. Index is used to optimize the performance of Database by minimizing the number
of disk access required when a query is processed.
SQL INTERVIEW QUESTIONS & Answers by Jeevan Raj
Mainly we have 2 types of index.
1. Clustered Index
2. Non Clustered Index

Clustered Index Non-Clustered Index

1. Clustered Index is used for easy 1. Non-Clustered Index is used for easy
retrieval of the data from the retrieval of data from the database and is
database and is faster slower
2. One Table can only have one
2. One table can have many non-
clustered index
clustered index
3. Clustered index alters the way,
The records are stored in database, 3. Non-clustered index does not alter the
As it sorts the rows by columns way records are stored, but it create
separate object with in a table.

26. What do you mean by Data Integrity


Data Integrity basically defines the Accuracy and Consistency of the data

27. What is ACID Property


ACID Property Ensures the Data Integrity is Maintained
A – Atomicity
C – Consistency
I – Isolation
D – Durability

Atomicity :- If one Part of any Transaction fails, Then Entire Transaction Fails and the
Data Base is left unchanged.
Consistency :- Data must meet all the validation rules [consistency should be maintained
before & after Transaction]
SQL INTERVIEW QUESTIONS & Answers by Jeevan Raj
Isolation :- Concurrency Control [Multiple Transaction occur independently without
Interference]
Durability:- If the Transaction has been committed it will occur what ever may come
between like crash or system failure

ACID Properties
Transactions system in the database has 4 basic properties which are called as
ACID properties. These properties play crucial understanding with the transactions
happening in the database. They are used to maintain the consistency in the
database during transactions.
Properties of transactions are:

1. Atomicity: The entire transactions takes place at once or doesn’t happen at


all. The transactions cannot perform partially. For other users, the transaction
is only visible when it is completely executed.
E.g. A wants to transfer money (Rs. 500) to B. Call it a Transaction.
Atomicity guarantees that A’s balance will get deducted by Rs. 500, and B’s
balance will get credited by Rs. 500. So both this credit and deduction
happen to be a single step.
SQL INTERVIEW QUESTIONS & Answers by Jeevan Raj
Just imagine the case if it was not atomic e.g. A’s money got deducted and
then Transaction failed, and B was never credited.

2. Consistency: The database must be consistent before & after the


transactions. This property is satisfied when each data transaction moves the
database from a consistent state to a consistent state.
Using the same example as above, say before Transaction, A’s Balance was
Rs. 1000 and B’s Balance was Rs. 2000.
After Transaction (of Rs. 500 from A → B), we expect A’s balance=500Rs
and B’s balance=Rs.2500
If we think about it, both these states are valid. It must not happen that A’s
balance got updated to Rs. 500 but B’s balance is other than Rs. 2500.
So here, the sum of their balance can be thought of as a state, and it should
remain consistent, before and after Transaction.

3. Isolation: Multiple transactions occur independently without interference.


To take an example, say there are 2 concurrent transactions going on:
A → B and A → C transfer of Rs. 500 in each transaction.
There can be multiple scenarios where concurrency might cause problems
like T1 to read A’s balance as 1000, and also meanwhile T2 read A’s balance
as 1000. Both deducted 500 to send to B and C respectively. This causes the
problem as now both will update A’s balance as 500.
Since A’s balance is now Rs. 500, it contradicts Consistency, as A’s expected
balance should be 0 after these 2 transactions.

4. Durability: After a transaction completes successfully, the changes it has


made to the database persist, even if there are system failures.
E.g. A had Rs. 1000, and it transferred Rs. 500 to B. Now every next time we
query A’s balance, we should get the latest value, and must not lose these
details.
SQL INTERVIEW QUESTIONS & Answers by Jeevan Raj
28. What is Subquery in SQL and type of Subquery
A Subquery is basically a query inside another query, where a query is defined to retrieve
data or information from the database
Subquery are always executed first and the result of the subquery is passed on to the main
query
The Different type of Subquery are:-
1. Correlated Subquery
2. Non-Correlated Subquery
Correlated Subquery :- Relationship between the same table.
This query is not considered as an dependent query as it refers to another table & refers
the column in a table.
Non Correlated Subquery:- Relationship between the different table, (Where outer
query is of different table and inner query is of different table)
This query is considered as an independent query, where the output of subquery is
substituted in the main query.

29. What is the Main difference between ‘BETWEEN’ and ‘IN’ Condition operator

Between:- Used to display rows based on a range of values in a row.


Ex:- Select * from students where roll_no between 10 and 50;
IN :- Used to check for values considered in a specific set of values.
Ex:- Select * from students where roll_no IN (8, 15, 25);

30. What is the need of Group function in SQL


Group function work on the set of rows & returns one result per group,
Some of the commonly used group function are :-
1. Avg
SQL INTERVIEW QUESTIONS & Answers by Jeevan Raj
2. Count
3. Max
4. Min
5. Sum

31. What is the Need of Merge Statement


 Merge Statement Allows conditional updates or insertion of data into a table
 It performs an update if a row exists or an Insert if the row does not exist.

32. What is Trigger explain with real world Example


Trigger are the SQL Codes that are Automatically Executed in response to the certain
events on a particular table.
Trigger are used to maintain the integrity of the data.
Problem without Trigger.
As I am a Marketing Executive in a company, When a new customer data enter into a
company database, I have to send the welcome Email to the customer, If it is one or two
mail I would have sent manually but what if the count is 1000 or millions, It will be a
repetitive job & the efficiency may drop, So I am going to create a trigger where trigger
will automatically send a welcome email to the new customer once a data enter into the
database. This will increase the Efficiency.

33. What is Windows Function


Windows Function or Analytic Function which uses values from one or multiple rows to
return a value for each row. Windows functions have an OVER Clause
SQL INTERVIEW QUESTIONS & Answers by Jeevan Raj

34. What is CTE [Common Table Expression]


CTE is a Temporary named result set, that can reference within a select, Insert, Update or
Delete Statement.
The With Clause can include one or more CTE’s Separated by commas.
CTE Enables user to more easily write & maintain complex queries via increased
readability & simplification

35. What Stored Procedure


A Stored Procedure is a prepared SQL code that you can save, So the code can be reused
over and over again.
Ex:- So if you have an SQL Query that you write over & over again, Save it as a stored
procedure & then just call it to execute it.

You might also like