From 8279decfc73734af0fd8058f2cc0f6ea971f4a60 Mon Sep 17 00:00:00 2001 From: franc1sc0sv Date: Wed, 27 Nov 2024 09:17:16 -0600 Subject: [PATCH 1/2] feat: adding solutions of the exercises --- README.md | 69 ++++++++++++++++++++++++++++++++++++++++------ docker-compose.yml | 16 +++++++++++ src/answers.sql | 69 +++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 139 insertions(+), 15 deletions(-) create mode 100644 docker-compose.yml diff --git a/README.md b/README.md index d6594fa..7ba4ca8 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,11 @@ 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(s.name) as "count" +FROM states s +INNER JOIN countries c ON s.country_id = c.id +GROUP BY c.name; ```

@@ -84,7 +88,10 @@ Your query here 2. How many employees do not have supervisores. ``` -Your query here +SELECT COUNT(*) + employees_without_bosses +FROM employees +WHERE supervisor_id IS NULL; ```

@@ -94,7 +101,16 @@ 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.uuid) AS "count" +FROM employees e +INNER JOIN offices o ON e.office_id = o.id +INNER JOIN countries c ON o.country_id = c.id +GROUP BY c.name, o.address +ORDER BY "count" DESC +LIMIT 5; ```

@@ -104,7 +120,12 @@ Your query here 4. Three supervisors with the most amount of employees they are in charge. ``` -Your query here +SELECT e.supervisor_id, COUNT(*) AS total_employees +FROM employees e +WHERE e.supervisor_id IS NOT NULL +GROUP BY e.supervisor_id +ORDER BY total_employees DESC +LIMIT 3; ```

@@ -114,7 +135,10 @@ Your query here 5. How many offices are in the state of Colorado (United States). ``` -Your query here +SELECT + COUNT(*) AS list_of_office +FROM offices o +WHERE o.country_id = 1 AND o.state_id = 8; ```

@@ -124,7 +148,13 @@ Your query here 6. The name of the office with its number of employees ordered in a desc. ``` -Your query here +SELECT + o.name AS "name", + COUNT(e.office_id) AS "count" +FROM employees e +INNER JOIN offices o ON e.office_id = o.id +GROUP BY o.name +ORDER BY "count" DESC; ```

@@ -134,7 +164,19 @@ Your query here 7. The office with more and less employees. ``` -Your query here +WITH cte AS ( + SELECT + o.name AS "name", + COUNT(e.office_id) AS "count", + ROW_NUMBER() OVER (ORDER BY COUNT(e.office_id) DESC) AS rn_desc, + ROW_NUMBER() OVER (ORDER BY COUNT(e.office_id) ASC) AS rn_asc + FROM employees e + INNER JOIN offices o ON e.office_id = o.id + GROUP BY o.name +); +SELECT "name", "count" +FROM cte +WHERE rn_asc = 1 OR rn_desc = 1; ```

@@ -144,7 +186,18 @@ 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) AS full_name, + e.email, + e.job_title, + o.name AS office_name, + co.name AS country_name, + s.name AS state_name +FROM employees e +INNER JOIN offices o ON e.office_id = o.id +INNER JOIN states s ON o.state_id = s.id +INNER JOIN countries co ON s.country_id = co.id; ```

diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9dae992 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,16 @@ +version: "3.9" +services: + postgres: + image: postgres:latest + container_name: nerdery-container + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: password123 + ports: + - "5000:5432" + volumes: + - postgres_data:/var/lib/postgresql/data + - ./src/dump.sql:/docker-entrypoint-initdb.d/dump.sql + +volumes: + postgres_data: diff --git a/src/answers.sql b/src/answers.sql index 9ad2edc..4a2730d 100644 --- a/src/answers.sql +++ b/src/answers.sql @@ -1,15 +1,70 @@ -- Your answers here: -- 1 - +SELECT + c.name, COUNT(s.name) as "count" +FROM states s +INNER JOIN countries c ON s.country_id = c.id +GROUP BY c.name; -- 2 - +SELECT COUNT(*) + employees_without_bosses +FROM employees +WHERE supervisor_id IS NULL; -- 3 - +SELECT + c.name, + o.address, + COUNT(e.uuid) AS "count" +FROM employees e +INNER JOIN offices o ON e.office_id = o.id +INNER JOIN countries c ON o.country_id = c.id +GROUP BY c.name, o.address +ORDER BY "count" DESC +LIMIT 5; -- 4 - +SELECT e.supervisor_id, COUNT(*) AS total_employees +FROM employees e +WHERE e.supervisor_id IS NOT NULL +GROUP BY e.supervisor_id +ORDER BY total_employees DESC +LIMIT 3; -- 5 - +SELECT + COUNT(*) AS list_of_office +FROM offices o +WHERE o.country_id = 1 AND o.state_id = 8; -- 6 - +SELECT + o.name AS "name", + COUNT(e.office_id) AS "count" +FROM employees e +INNER JOIN offices o ON e.office_id = o.id +GROUP BY o.name +ORDER BY "count" DESC; -- 7 - +WITH cte AS ( + SELECT + o.name AS "name", + COUNT(e.office_id) AS "count", + ROW_NUMBER() OVER (ORDER BY COUNT(e.office_id) DESC) AS rn_desc, + ROW_NUMBER() OVER (ORDER BY COUNT(e.office_id) ASC) AS rn_asc + FROM employees e + INNER JOIN offices o ON e.office_id = o.id + GROUP BY o.name +); +SELECT "name", "count" +FROM cte +WHERE rn_asc = 1 OR rn_desc = 1; +-- 8 +SELECT + e.uuid, + CONCAT(e.first_name, ' ', e.last_name) AS full_name, + e.email, + e.job_title, + o.name AS office_name, + co.name AS country_name, + s.name AS state_name +FROM employees e +INNER JOIN offices o ON e.office_id = o.id +INNER JOIN states s ON o.state_id = s.id +INNER JOIN countries co ON s.country_id = co.id; \ No newline at end of file From 102bae848580b60c450ba64a7bb960fe04ff6787 Mon Sep 17 00:00:00 2001 From: franc1sc0sv Date: Mon, 2 Dec 2024 07:53:58 -0600 Subject: [PATCH 2/2] chore: fixing solution for exercise 8 --- src/answers.sql | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/answers.sql b/src/answers.sql index 4a2730d..62beb17 100644 --- a/src/answers.sql +++ b/src/answers.sql @@ -63,8 +63,11 @@ SELECT e.job_title, o.name AS office_name, co.name AS country_name, - s.name AS state_name + s.name AS state_name, + supervisors.first_name boss_name FROM employees e INNER JOIN offices o ON e.office_id = o.id INNER JOIN states s ON o.state_id = s.id -INNER JOIN countries co ON s.country_id = co.id; \ No newline at end of file +INNER JOIN countries co ON s.country_id = co.id; +INNER JOIN employees supervisors ON e.supervisor_id = supervisors.id +WHERE supervisors.first_name IS NOT NULL;