USE company;

SELECT * FROM country;

SELECT first_name "Firstname", last_name "Lastname" from employee;

select distinct department_ID from employee;

select count(job_id) from employee;

select count(distinct job_id) from employee;

-- liste des employés embauchés avant le 01.01.1991
select * from employee where hire_date < '1991-01-01';

-- FOnctions
-- -- mettre en majuscule
select upper(first_name) "Firstname" from employee;
-- -- mettre les 3 premières lettres des nom et prénom des employés
select substring(upper(first_name), 1, 3), substring(upper(last_name), 1, 3)
from employee;
-- -- prénom + nom + longueur totale du prénom+nom
select first_name, last_name,
char_length(first_name) + char_length(last_name) "Longueur totale"
from employee;
-- -- Qui gagne le plus dans l'entreprise
select first_name, last_name, salary
from employee order by salary desc limit 1;
select first_name, last_name,
salary >= (select max(salary) from employee)
from employee order by salary desc;
-- -- salaire min et max
select min(salary), max(salary), avg(salary), sum(salary) from employee;
-- -- les gens de l'IT qui gagnent moins de 5000
select first_name, last_name, department_id, salary
from employee where department_id = 60 and salary > 5000;
-- -- les gens dans la vente (80) et le mercatique (20)
select first_name, last_name, salary
from employee where department_id in (20, 80);
-- -- salaire entre 5000 et 10000
select first_name, last_name, salary
from employee where salary between 5000 and 10000;
-- -- avec une commission
select first_name, last_name, commission_pct
from employee where commission_pct != 0;
-- -- employés dont le nom de famille commance par "K"
select first_name, last_name
from employee where last_name like "K%";
-- -- employés dont le prénom contient "an"
select first_name, last_name
from employee where first_name like "%an%";
-- -- employés dont le prénom fait 4 lettres
select first_name, last_name
from employee where first_name like "____";
-- -- REGEX : nom de famille commence par "S" ou "T"
select first_name, last_name
from employee where last_name regexp "^[ST]";
-- -- le nombre de jour depuis l'embauche
select first_name, last_name, hire_date,
datediff(curdate(), hire_date) as nb_jours
from employee order by nb_jours;

-- Exercice
-- -- les commerciaux gagnant plus de 9000 triés par commission décroissante
select first_name, last_name, salary, commission_pct
from employee
where department_id = 80 and salary > 9000 order by commission_pct desc;
-- -- les postes dont le salaire est au moins le double du salaire minimum
select first_name, last_name, salary
from employee where salary > (select min(salary) from employee) * 2
order by salary asc;
-- -- les employés dont le prénom commence et fini par la même lettre
select first_name, last_name 
from employee 
where left(first_name, 1) = right(first_name, 1);
-- -- le top 5 des employés les mieux payés n'étant pas chefs
select first_name, last_name, salary, job_id
from employee
where employee_id not in (select manager_id from employee)
order by salary desc limit 10;

-- COURS
-- -- Jointures
-- -- -- https://cartman34.fr/informatique/sgbd/differences-entre-inner-left-right-et-outer-join-en-sql.html
select first_name, last_name, department_name
from employee e
inner join department d
on e.department_id = d.department_id;
-- -- -- LEFT avec condition : les employés qui ne sont affectés à aucun départment
select e.first_name, e.last_name, d.department_name
from employee e
left join department d
on e.department_id = d.department_id
where d.department_id is null;
-- -- -- RIGHT avec condition : la liste des départments ne contenant aucun employé
select e.first_name, e.last_name, d.department_name
from employee e
right join department d
on e.department_id = d.department_id
where e.department_id is null;
-- -- -- FULL OUTER JOIN
select e.first_name, e.last_name, d.department_name
from employee e
left join department d
on e.department_id = d.department_id
union
select e.first_name, e.last_name, d.department_name
from employee e
right join department d
on e.department_id = d.department_id;
-- -- -- NATURAL JOIN -> INNER JOIN
select location_id, street_address, city, country_name
from location
natural join country;
-- -- -- EXEMPLE : nous voulons la liste des employés travaillant à Seattle
-- -- -- On doit faire EMPLOYEE -> DEPARTMENT -> LOCATION
select e.first_name, e.last_name, l.city
from employee e
join department d
on e.department_id = d.department_id
join location l
on d.location_id = l.location_id
where lower(l.city) = "seattle";

-- -- -- EXEMPLE : nous voulons la liste des employés travaillant aux USA
-- -- -- On doit faire EMPLOYEE -> DEPARTMENT -> LOCATION -> COUNTRY
select e.first_name, e.last_name, l.city
from employee e
join department d
on e.department_id = d.department_id
join location l
on d.location_id = l.location_id
join country c
on l.country_id = c.country_id
where lower(c.country_name) like "%united%states%";
-- -- -- Prénom et nom de l'employé suivi du prénom de son chef
select e.first_name, e.last_name, m.first_name "Prénom chef", m.last_name "Nom chef"
from employee e
left join employee m
on m.employee_id = e.manager_id;
-- -- -- Nombre de gens dans chaque poste
select j.job_title, count(*)
from employee e
join job j
on e.job_id = j.job_id
group by j.job_id;
-- -- -- la liste des chefs et le nombre d'employés sous leurs ordres
-- -- -- et dont le nombre d'employés est d'au moins 5
select m.first_name "Prénom chef",
m.last_name "Nom chef",
count(e.employee_id) as count
from employee e
join employee m
on m.employee_id = e.manager_id
group by e.manager_id
having count >= 5
order by count desc;

-- -- Sous-requêtes
-- -- -- Tous les employés gagnant plus que l'employé nommé "Bull"
select first_name, last_name, salary
from employee
where salary >
(select salary from employee where lower(last_name) = "Bull");
-- -- -- Tous les employés ayant pour chef un prénommé "Payam"
-- -- -- JOINTURE
select e.first_name, e.last_name, m.first_name "Prénom chef", m.last_name "Nom chef"
from employee e
left join employee m
on m.employee_id = e.manager_id
where lower(m.first_name) = "payam";
-- -- -- Tous les employés ayant pour chef un prénommé "Payam"
-- -- -- SOUS-REQUETE
select first_name, last_name
from employee
where manager_id =
(select employee_id from employee where lower(first_name) = "payam");
-- -- -- Les employés dont le salaire est compris entre le plus petit et 3000
select first_name, last_name, salary
from employee
where salary
between (select min(salary) from employee) and 3000
order by salary asc;
-- -- -- les employés dont le chef bosse aux USA
select first_name, last_name
from employee
where manager_id in
(
select employee_id from employee where department_id in
	(
	select department_id from department where location_id in
		(
		select location_id from location where lower(country_id) = "us"
		)
	)
);
-- -- Création de "Vues"
-- -- Grosso modo, c'est une requête pré-enregistrée
create view `seattle_employees` as
select e.first_name, e.last_name, e.salary, d.department_name, l.city
from employee e
join department d
on e.department_id = d.department_id
join location l
on d.location_id = l.location_id
where lower(l.city) = "seattle";

select * from seattle_employees;