20 SQL Exercises For Practice: Table Structure and Schema
20 SQL Exercises For Practice: Table Structure and Schema
20 SQL Exercises For Practice: Table Structure and Schema
, department_name VARCHAR(30)
, location_id INTEGER
) ;
( employee_id INTEGER
, first_name VARCHAR(20)
, last_name VARCHAR(25)
, email VARCHAR(25)
, phone_number VARCHAR(20)
, hire_date DATE
, job_id VARCHAR(10)
, salary INTEGER
, commission_pct INTEGER
, manager_id INTEGER
, department_id INTEGER
) ;
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
first_name,
last_name,
job_id,
salary
from employees
where salary = (select max(salary) from employees);
first_name,
last_name,
job_id,
salary
from employees
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.
set @input:=3;
select employee_id,
first_name,
last_name,
job_id,
salary
from employees e
from employees p
where e.Salary<=p.Salary);
emp.salary emp_sal,
mgr.salary mgr_sal
sup.employee_id employee_id,
order by 3 desc;
count(emp.employee_id) emp_count
group by dept.department_name
order by 2 desc;
from employees
group by year(hire_date)
order by 2 desc;
max(salary) max_sal,
round(avg(salary)) avg_sal
from employees;
10. Write a query to divide people into three groups based on their
salaries
select concat(first_name,' ',last_name) employee,
salary,
case
else
"high"
end as salary_level
from employees
order by 1;
from employees
replace(phone_number,'.','-') phone_number
from employees;
hire_date
from employees
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
order by dept.department_id;
dept.department_id,
dept.department_name department,
max(emp.salary)maximum_salary
dept.department_id
order by dept.department_id ;
first_name, last_name,
employee_id,
salary
from employees
order by salary
limit 5;
18. Display the employees first name and the name in reverse order
select lower(first_name) name,
lower(reverse(first_name)) name_in_reverse
from employees;
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
20. Display the managers and the reporting employees who work in
different departments
select
mgr.department_id mgr_dept,
emp.department_id emp_dept
order by 1;
Source: https://oindrilasen.com/2021/04/sql-interview-practice/