Top MySQL Interview Questions And Answers

Mosharrf Hossain
Mh Mohon
Published in
1 min readMar 22, 2019

Q) Consider below simple table:

Name     Salary
---------------
abc 100000
bcd 1000000
efg 40000
ghi 500000

Now find second highest salary in this table?

Query:

SELECT name, MAX(salary) AS Salary
FROM employee
where Salary < (SELECT MAX(salary)
from employee);

OR

SELECT name, salary 
FROM (SELECT salary FROM employee ORDER BY salary DESC LIMIT 2)
ORDER BY salary
limit 1;

OR

SELECT DISTINCT Salary 
FROM employee
ORDER BY Salary DESC
LIMIT 1 OFFSET 1

OFFSET starts from 0th position, and hence use N-1 rule here

Q) Consider below simple table:

Now find avg mark in this table?

Sql Query:

SELECT AVG(marks)
FROM results

Eloquent:

$avgMarks = Result::avg('marks');
or
DB::table('Results')
->avg(marks);

For try online.

--

--