-
Notifications
You must be signed in to change notification settings - Fork 33
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
✨ DB Nerdery Challenges Solutions I #54
base: main
Are you sure you want to change the base?
Conversation
README.md
Outdated
```sql | ||
SELECT c.name, count(s.name) | ||
FROM states AS s | ||
INNER JOIN countries AS c | ||
ON s.country_id = c.id | ||
group by c.name | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Use uppercase for keywords and consist identention
```sql | |
SELECT c.name, count(s.name) | |
FROM states AS s | |
INNER JOIN countries AS c | |
ON s.country_id = c.id | |
group by c.name | |
``` | |
```sql | |
SELECT | |
c.name, | |
COUNT(s.name) | |
FROM | |
states AS s | |
INNER JOIN countries AS c ON s.country_id = c.id | |
GROUP BY | |
c.name``` |
README.md
Outdated
``` | ||
Your query here | ||
```sql | ||
SELECT c.name, count(s.name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: on aggregated functions tend to use a alias that it actual represents the column
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: what is going to happened if state name is null?
``` | ||
Your query here | ||
```sql | ||
SELECT COUNT(*) AS employees_without_supervisors |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one using a meaning name for the alias
README.md
Outdated
```SQL | ||
SELECT c.name, | ||
o.address, | ||
COUNT(e.id) AS count |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: it is practically the same as not adding a alias, choose a better name
README.md
Outdated
SELECT count(*) AS list_of_office | ||
FROM offices AS o | ||
INNER JOIN states AS s | ||
ON o.state_id = s.id | ||
WHERE s.name = 'Colorado' | ||
AND s.country_id = (SELECT id FROM countries AS c WHERE name = 'United States'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: improve indentation on this query
README.md
Outdated
INNER JOIN states AS s | ||
ON o.state_id = s.id | ||
WHERE s.name = 'Colorado' | ||
AND s.country_id = (SELECT id FROM countries AS c WHERE name = 'United States'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you use a sub query here instead of using a inner join?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was not completely sure how to solve it, so I tried doing it in the first way that I realized worked. Now, I have a better understanding of how to use JOINS
in queries, and it would be more readable to use an INNER JOIN
instead of a subquery
README.md
Outdated
(SELECT * | ||
FROM office_count | ||
WHERE count = (SELECT MAX(count) FROM office_count) | ||
LIMIT 1) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also create more CTEs to improve this
README.md
Outdated
|
||
(SELECT * | ||
FROM office_count | ||
WHERE count = (SELECT MAX(count) FROM office_count) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're already operating a result to match with another result, it works but it can be hard to read, you can use an approach of sorting the office count table instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the call is redundant because you're using two query calls against the same table without being fully necessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You’re alright. It could have been better to use ORDER BY
instead of using this subquery selection
README.md
Outdated
) | ||
|
||
SELECT e.uuid, | ||
e.first_name || ' ' || e.last_name AS full_name, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: IMO concat can be more readable instead of using this one
README.md
Outdated
e2.first_name AS boss_name | ||
FROM employees AS e | ||
INNER JOIN offices_by_countries_with_states AS o ON e.office_id = o.office_id | ||
LEFT JOIN employees AS e2 ON e.supervisor_id = e2.id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: e2 is not a descriptive alias you can use a descriptive one
Nice job 👍🏻 |
No description provided.