Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases SQL Reporting by Example Day 2: Advanced Selecting Day 2: Review

olu adesina
olu adesina
23,007 Points

sql help do i need to use the group by key word

Write a query to figure out how many employees are in each job role.

SELECT JOBS.ROLE, COUNT(1) FROM EMPLOYEES JOIN JOBS ON EMPLOYEES.JOB_ID = JOBS.ID ;

1 Answer

Billy Bellchambers
Billy Bellchambers
21,689 Points

Group by is used as the name suggests to group values by a specific column value.

In this example it wants to do a count of the number of records against each value of Jobs.Role

To achieve this you can do the following.

SELECT JOBS.ROLE, COUNT(1)
FROM EMPLOYEES
JOIN JOBS ON EMPLOYEES.JOB_ID = JOBS.ID
GROUP BY JOBS.ROLE;

Should you want some further detail on the GROUP BY statement. https://www.w3schools.com/sql/sql_groupby.asp