mysql_interview_questions_with_theory_answers
mysql_interview_questions_with_theory_answers
Answers
Palle Technologies
Updated on 22-oct-2022
all queries are based on below tables
table 1 - department table
unique key
dept_id (PK) dept_name (UK) dept_location
primary key
1 HR BTM
2 Development HSR
3 Training HSR
4 Sales Bommanahalli
5 Marketing Bommanahalli
6 MD HSR
7 Recruitment HSR
2
All queries are based on below tables
Primary key table 2 - employee table foreign key
emp_id emp_name city emp_salary manag d_id
(PK) er_id (FK)
4
• sql query for creating department table with
appropriate data types and primary key and
unique constraints
• sql query for creating employee table with
appropriate data types and primary key and
foreign key constraints
5
• question based on update query on the
employee table
• question based on delete query on the
employee table
• question based on insert query on the
employee table
• questions based on select statement with
where clause on employee table
• questions based on select statement with like
clause on employee table
• questions based on group by clause on
employee table
• questions based on having clause on
employee table
• questions based on inner joins on employee
and department table
• questions based on left outer joins on
department and employee table
• questions based on self join on employee
table
• questions based on sub queries
• questions based on co-related sub queries
and nth highest nth lowest values
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 values duplicate values
primary key will not allow null unique constraint will allow any
values number of null values
8
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)
)
9
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.
10
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.
12
13. what is 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)
13
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
14
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.
16
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.
17
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.
18
28. write query for finding employee details
with first highest salary?
Ans : student should write query
29. write query for finding employee details
with second highest salary?
Ans : student should write query
19
Link for tables
• Student can use the following referenced
tables ( usually mock interviews we ask
questions based on the given tables )
• https://palle-sql-tables.s3.ap-south-
1.amazonaws.com/index.html
20