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(exercises): completed all exercises #44

Open
wants to merge 1 commit 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
104 changes: 104 additions & 0 deletions src/answers.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,119 @@
-- Your answers here:
-- 1
SELECT
c.name AS country_name,
COUNT(s.id) AS total_states
FROM
states s
JOIN
countries c
Comment on lines +7 to +9
Copy link

Choose a reason for hiding this comment

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

When you assign the table's name after FROM or JOIN statement I prefer to use the 3 letters to improve readability in the query. I won't comment the same on the following queries but you should solve it there as well.

ON
s.country_id = c.id
GROUP BY
c.name;

-- 2
SELECT COUNT(*) AS employees_wothout_bosses
Copy link

Choose a reason for hiding this comment

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

Fix the typo in wothout

FROM employees
WHERE supervisor_id IS NULL;

-- 3
SELECT
c.name AS country_name,
o.address AS address,
COUNT(e.id) AS numer_employees
Copy link

Choose a reason for hiding this comment

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

Fix the typo in numer

FROM
offices o
JOIN
employees e ON o.id = e.office_id
JOIN
countries c ON o.country_id = c.id
GROUP BY
c.name, o.address
ORDER BY
numer_employees DESC, c.name ASC
LIMIT 5;

-- 4
SELECT
s.id AS supervisor_id,
COUNT(e.id) AS number_of_employe
Copy link

Choose a reason for hiding this comment

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

Fix the typo in employe and it should be in plural.

FROM
employees e
JOIN
employees s ON e.supervisor_id = s.id
GROUP BY
s.id
ORDER BY
number_of_employe DESC
LIMIT 3;


-- 5
SELECT
s.name AS state_name,
COUNT(o.id) AS colorado_offices
FROM
offices o
JOIN
states s ON o.state_id = s.id
GROUP BY
s.name
HAVING
s.name = 'Colorado';
Comment on lines +62 to +63
Copy link

Choose a reason for hiding this comment

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


-- 6
SELECT
o.name AS office_name,
COUNT(e.id) AS number_of_employees
FROM
offices o
JOIN
employees e ON o.id = e.office_id
GROUP BY
o.name
ORDER BY
number_of_employees DESC;

-- 7
(
SELECT o.address AS office_address, COUNT(*) AS employee_count
FROM employees e
JOIN offices o ON e.office_id = o.id
GROUP BY o.id, o.address
ORDER BY employee_count DESC
LIMIT 1
)
UNION ALL
(
SELECT o.address AS office_address, COUNT(*) AS employee_count
FROM employees e
JOIN offices o ON e.office_id = o.id
GROUP BY o.id, o.address
ORDER BY employee_count ASC
LIMIT 1
);

-- 8

SELECT
e.uuid AS employee_uuid,
CONCAT(e.first_name, ' ', e.last_name) AS full_name,
e.email,
e.job_title,
o.name AS company,
s.name AS state_name,
c.name AS country_name,
CONCAT(b.first_name, ' ', b.last_name) AS 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 c ON s.country_id = c.id
INNER JOIN
employees b ON e.supervisor_id = b.id
ORDER BY
e.id;
Loading