Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding solutions of the exercises - challenge 1 #48

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 61 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
```

<p align="center">
Expand All @@ -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;
```

<p align="center">
Expand All @@ -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;
```

<p align="center">
Expand All @@ -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;
```

<p align="center">
Expand All @@ -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;
```

<p align="center">
Expand All @@ -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;
```

<p align="center">
Expand All @@ -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;
```

<p align="center">
Expand All @@ -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;
Comment on lines +189 to +200

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the query doesn't return the boss name of an employee

```

<p align="center">
Expand Down
16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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:
72 changes: 65 additions & 7 deletions src/answers.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,73 @@
-- 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,
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;
INNER JOIN employees supervisors ON e.supervisor_id = supervisors.id
WHERE supervisors.first_name IS NOT NULL;