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

MySQL Interview Questions

The document provides a comprehensive overview of MySQL interview questions and answers, covering key concepts such as primary keys, foreign keys, normalization, and various SQL commands. It includes explanations of differences between SQL operations like drop, delete, and truncate, as well as details on joins and their types. Additionally, it contains sample queries for creating tables, inserting data, and retrieving specific information from databases.

Uploaded by

RAAAVAN RAJ
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)
4 views

MySQL Interview Questions

The document provides a comprehensive overview of MySQL interview questions and answers, covering key concepts such as primary keys, foreign keys, normalization, and various SQL commands. It includes explanations of differences between SQL operations like drop, delete, and truncate, as well as details on joins and their types. Additionally, it contains sample queries for creating tables, inserting data, and retrieving specific information from databases.

Uploaded by

RAAAVAN RAJ
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
You are on page 1/ 20

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.

11. How many foreign keys allowed per table?


we can create any number of foreign keys
per table.
12. what are the diff between drop and
delete?
drop delete

1. drop table will delete 1. a delete statement will


the table (along with data) delete only data, but table
will be retained.
2. drop is a DDL 2. delete is a DML
statement statement

eg: drop table employee; eg: delete from employee


where eno = 1;

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)

2. delete is a DML statement 2. truncate is a DDL statement

3. we can give where clause in 3. we can’t give where clauses in


delete. Delete operation is slower. truncate. Truncate is faster that
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.

15. what are the different types of joins supported in sql


server?
1.inner join
2.outer join
1)Left Outer Join
2)Right Outer Join

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.

17. what is LOJ?


In left outer join data from left table is
completely included into the result-set but only
matched data from right side table is included. In
place of right table data null’s are included
where there is no match.

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.

19. What is self join?


1.Joining a table to itself is called as self join.
2.we must use inner join keyword for self join.

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.

21. what is the purpose of having clause?


using having clause we can filter the records
which are produced by group by clause.

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.

23. what is the purpose of group by clause?


Ans : using group by clause we can group data
based on one or more 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,
);

2. write query to insert below employee details into


employee table?
1, ramesh, 30000
2, mahesh, 40000
Ans : insert into employee
values(1,’ramesh’,30000),(2,’mahesh’,40000);
15
3. write query to select all employees who’s name
starts with a or A?
Ans : select * from employee where ename like
'a%' or 'A%‘;

4. write query to find number of rows in a table?


Ans : select count(*) from employee;

5. write query to select and display first highest


salary from employee table.

Ans : select max(salary) from employee ;


Or
select salary from employee order by salary
desc limit 1
16
6. write query to display employee details of second
highest salary from employee table?
Ans : select salary from employee e1 where
1=(select count(*) from employee e2 where
e2.salary>e1.salary)

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

select c.name, a.aname from country c left join animal a on


c.NAid=a.Aid

select c.name, a.aname from country c right join animal a on


c.NAid=a.Aid
18
Write one example for inner joins
Patient1 p Bg b

select p.name,b.bg,p.age from patient1 p join bg b on


p.bg_id=b.id

19
Write one example for self join
Emp_mgr Emp_mgr e2

select e1.name as 'employee',e2.name as 'manager' from emp_mgr e1


join emp_mgr e2 on e1.mgr_id=e2.eid

20

You might also like