Mysql Practice
Mysql Practice
Mysql Practice
Problem 1
Find the number of Male (M) and Female (F) employees in the database and order the counts in
descending order.
Problem 2
Find the average salary by employee title, round to 2 decimal places and order by descending
order.
Problem 3
Find all the employees that have worked in at least 2 departments. Show their first name,
last_name and the number of departments they work in. Display all results in ascending order.
Problem 4
Display the first name, last name, and salary of the highest payed employee.
Problem 5
Display the first name, last name, and salary of the second highest payed employee.
SELECT CONCAT(employees.first_name, ' ', employees.last_name) AS
employee_name, salaries.salary
FROM employees JOIN salaries ON employees.emp_no = salaries.emp_no
WHERE salaries.salary < (SELECT MAX(salaries.salary) FROM salaries)
ORDER BY salaries.salary DESC
LIMIT 1;
Problem 6
Display the month and total hires for the month with the most hires.
Problem 7
Display each department and the age of the youngest employee at hire date.
SELECT dept.dept_name,
MIN(TIMESTAMPDIFF(YEAR, e.birth_date, e.hire_date)) AS age_hire_date
FROM employees e
JOIN dept_emp d_emp ON e.emp_no = d_emp.emp_no
JOIN departments dept ON d_emp.dept_no = dept.dept_no
GROUP BY dept.dept_name
Problem 8
Find all the employees that do not contain vowels in their first name and display the department
they work in.
So, now we have 2 tables and some data ready to run our sql. It’s time for some exercises.
1. Select employees first name, last name, job_id and salary whose first name starts with
alphabet S
select first_name,
last_name,
job_id,
salary
from employees
where upper(first_name) like 'S%';
select employee_id,
first_name,
last_name,
job_id,
salary
from employees
where salary = (select max(salary) from employees);
select employee_id,
first_name,
last_name,
job_id,
salary
from employees
where salary != (select max(salary) from employees)
order by salary desc
limit 1;
The above query selects only one person with the second-highest salary. But what if there are
more than 1 person with the same salary? Or, what if we want to select the 3rd or 4th highest
salary? So, let’s try a generic approach.
5. Write a query to select employees and their corresponding managers and their salaries
Now, this is a classic example of SELF JOIN in SQL exercises. Also, I am using the CONCAT
function to concatenate the first name and last name of each employee and manager.
6. Write a query to show count of employees under each manager in descending order
select
sup.employee_id employee_id,
concat(sup.first_name,' ', sup.last_name)manager_name,
COUNT (sub.employee_id) AS number_of_reportees
from employees sub
join employees sup
on sub.manager_id = sup.employee_id
group by sup.employee_id, sup.first_name, sup.last_name
order by 3 desc;
select dept.department_name,
count(emp.employee_id) emp_count
from employees emp
join departments dept on emp.department_id = dept.department_id
group by dept.department_name
order by 2 desc;
8. Get the count of employees hired year wise
10. Write a query to divide people into three groups based on their salaries
select (first_name)
from employees
where lower(first_name) like '%an%';
12. Select employee first name and the corresponding phone number in the format (_ _ _)-
(_ _ _)-(_ _ _ _)
14. Write an SQL query to display employees who earn more than the average salary in
that company
select
concat(emp.first_name,last_name) name,
emp.employee_id,
dept.department_name department,
dept.department_id,
emp.salary
from departments dept
JOIN employees emp on dept.department_id = emp.department_id
where emp.salary > (select avg(salary) from employees)
order by dept.department_id;
select
dept.department_id,
dept.department_name department,
max(emp.salary)maximum_salary
from departments dept
JOIN employees emp on dept.department_id = emp.department_id
group by dept.department_name,
dept.department_id
order by dept.department_id ;
select
first_name, last_name,
employee_id,
salary
from employees
order by salary
limit 5;
select employee_id,
concat(first_name,' ' , last_name) employee,
hire_date
from employees
where year(hire_date) between 1980 and 1989;
18. Display the employees first name and the name in reverse order
19. Find the employees who joined the company after 15th of the month
select employee_id,
concat(first_name, ' ' , last_name) employee,
hire_date
from employees
where day(hire_date)> 15;
20. Display the managers and the reporting employees who work in different departments
select
concat(mgr.first_name,' ',mgr.last_name) manager,
concat(emp.first_name,' ',emp.last_name) employee,
mgr.department_id mgr_dept,
emp.department_id emp_dept
from employees emp
join employees mgr on emp.manager_id = mgr.employee_id
where emp.department_id != mgr.department_id
order by 1;