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

✨ DB Nerdery Challenges Solutions I #54

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

marvnramos
Copy link

No description provided.

README.md Outdated
Comment on lines 76 to 82
```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
```
Copy link

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

Suggested change
```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)
Copy link

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

Copy link

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
Copy link

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
Copy link

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
Comment on lines 136 to 141
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');
Copy link

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');
Copy link

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?

Copy link
Author

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
Comment on lines 173 to 177
(SELECT *
FROM office_count
WHERE count = (SELECT MAX(count) FROM office_count)
LIMIT 1)

Copy link

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)
Copy link

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

Copy link

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

Copy link
Author

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,
Copy link

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
Copy link

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

@erickvh
Copy link

erickvh commented Dec 5, 2024

Nice job 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants