MySQL Interview Questions
MySQL Interview Questions
Answers
Palle Technologies
1. what is primary key?
▪ primary key gives uniqueness to the tables rows
▪ only one primary key is allowed per table
▪ primary key will not allow null values and
duplicate values
2. What is the difference between primary key and
unique key?
Primary key Unique key
Only one primary key is allowed any number of unique keys are
per table. allowed per table
Primary key will not allow unique constraint will not allow
duplicates and null values duplicate values and allows
one null value
2
3. what is foreign key?
• using foreign key constraint we can link 2 or
more tables.
• using foreign key we can achieve referential
integrity
Syntax
Create table table_name
(
Col1 datatype ,
Col2 datatype,
foreign key(col1) references table_name(column_name)
)
3
4. show one example for using foreign key?
Independent table
dependent table
4
5. what is the diff b/w char and varchar?
char is a fixed size data type & varchar is varying
size data type. wastage of memory will be more
in char type.
6. what is normalization? and what are the
benefits?
normalization is used for eliminating the
data redundancy. normalization will
eliminate the insert, update anomalies.
7. is it possible to store null in a foreign key
column?
Yes.
5
8. Is null allowed in primary key column?
no.
9. how many primary keys we can give for a
table?
only one.
10. how many unique keys allowed for a table?
any number.
7
13. what are the diff between delete
and truncate?
delete truncate
1. a delete statement will delete 1. a truncate statement will delete
only data, but table will be only data, but table will be
retained. retained. (just like delete)
4. we can roll back the delete 4. We can’t roll back the truncate
operation operation.
8
14. what is the purpose of joins in sql?
• joins are useful for getting data from 2 or more tables
into a result set.
3.Self Join
4.Cross Join
9
16. what is inner join?
Inner join is used for getting matched data from
2 or more tables based on the condition.
9
18. What is ROJ?
In right outer join data from right table is completely
included into the result-set but only matched data
from left side table is included. In place of left table
data null’s are included where there is no match.
11
20. what is cross join?
cross join will join each row present in a table
with all rows present in other table. in simple
words it will produce Cartesian product of all
rows present in both the tables.
12
22. what is the purpose of order by clause?
Ans : order by clause specifies that a SQL SELECT
statement returns rows with sorted order, by
the values of given column.
13
Queries samples
14
1. write query to create employee table with eno,
ename, esal?
Ans :
Create table employee
(
eno int primary key,
ename varchar(50),
salary int,
);
17
Write one example for outer joins
Take one master table and child table.
a. perform left outer join
b. perform right outer join
country c animal a
19
Write one example for self join
Emp_mgr Emp_mgr e2
20