From bc0de03b81f0bf3f8d1b037b8bc06e8100cfcbae Mon Sep 17 00:00:00 2001 From: DavidH29 <54407010+DavidH29@users.noreply.github.com> Date: Fri, 29 Nov 2024 15:06:07 -0600 Subject: [PATCH] Adding solutions to challenge 1 --- README.md | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d6594fa..0338da4 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,9 @@ Now it's your turn to write SQL querys to achieve the following results: 1. Count the total number of states in each country. ``` -Your query here +select c.name, count(c.id) from countries c +inner join states s on c.id = s.country_id +group by c.id; ```

@@ -84,7 +86,7 @@ Your query here 2. How many employees do not have supervisores. ``` -Your query here +select count(*) employees_without_bosses from employees e where supervisor_id is null; ```

@@ -94,7 +96,11 @@ Your query here 3. List the top five offices address with the most amount of employees, order the result by country and display a column with a counter. ``` -Your query here +select c.name, o.address, count(e.office_id) from offices o +inner join countries c on o.country_id = c.id +inner join employees e on o.id = e.office_id +group by o.id, c.name +order by count(e.office_id) desc limit 5; ```

@@ -104,7 +110,9 @@ Your query here 4. Three supervisors with the most amount of employees they are in charge. ``` -Your query here +select supervisor_id, count(*) from employees e +where supervisor_id is not null +group by supervisor_id order by count(*) desc limit 3; ```

@@ -114,7 +122,8 @@ Your query here 5. How many offices are in the state of Colorado (United States). ``` -Your query here +select count(*) list_of_office from offices o +where state_id = (select id from states s where name ilike 'colorado'); ```

@@ -124,7 +133,10 @@ Your query here 6. The name of the office with its number of employees ordered in a desc. ``` -Your query here +select o.name, count(e.office_id) from offices o +inner join employees e on o.id = e.office_id +group by o.id +order by count(e.office_id) desc; ```

@@ -134,7 +146,14 @@ Your query here 7. The office with more and less employees. ``` -Your query here +(select o.address , count(e.office_id) from offices o +inner join employees e on o.id = e.office_id +group by o.id +order by count(e.office_id) desc limit 1) +union all (select o.address, count(e.office_id) from offices o +inner join employees e on o.id = e.office_id +group by o.id +order by count(e.office_id) limit 1); ```

@@ -144,9 +163,16 @@ Your query here 8. Show the uuid of the employee, first_name and lastname combined, email, job_title, the name of the office they belong to, the name of the country, the name of the state and the name of the boss (boss_name) ``` -Your query here +select e.uuid, concat(e.first_name, ' ' ,e.last_name) full_name, e.email, e.job_title, o.name company, c.name country, s.name state, m.first_name boss_name +from employees e +inner join offices o on e.office_id = o.id +inner join countries c on o.country_id = c.id +inner join states s on o.state_id = s.id +inner join employees m on e.supervisor_id = m.id; ```

result_8

+ +Challenge 3 link: https://dbdiagram.io/d/EDR-Challenge-3-674a25dbe9daa85aca2d1246