Here are some complex MySQL interview questions & answers that can help experienced candidates prepare:
1. Query with Subqueries:
Write a query to find the employees who earn more than the average salary in their department.
SELECT e.name, e.salary, e.department_id
FROM employees e
WHERE e.salary > (
SELECT AVG(salary)
FROM employees
WHERE department_id = e.department_id
);
5. Window Functions
Write a query to show the running total of sales for each month.
SELECT order_date, SUM(amount) OVER (ORDER BY order_date) AS running_total FROM orders;
6. Conditional Aggregation
Write a query to count the number of employees in each department, distinguishing between full-time and part-time employees.
Answer:
7. Self Join
Question: Write a query to find all employees who work in the same department as 'John Doe'.
Answer:
8. Pivot Table
Question: Write a query to pivot the sales data to show total sales per product per month.
Answer:
9. Finding Duplicates
Question: Write a query to find duplicate emails in the users table.
Answer:
10. Using JSON Functions
Question: Assume you have a table users
with a JSON column attributes
. Write a query to find users whose age
attribute is greater than 30.
Answer:
11. Date Functions
Question: Write a query to find the total number of orders placed in each year.
Answer:
Comments
Post a Comment