Search code examples
mysqlsqlreactjsdatabasereact-fullstack

Using JOIN and COUNT to find totals SQL


I am super new to SQL and have been working on this query and can not seem to get it to work. I need to sum the categories (how many clients are in each country and how many clients each employee has). This is what I tried to do to get the total clients in each country:

COUNT(*) TotalCount,
country.id, country.country
FROM client
INNER JOIN country
ON client.country_id = country.id
GROUP BY country.id, country.country

These are my tables:

CREATE TABLE employee(
    id INT AUTO_INCREMENT PRIMARY KEY,
    employee VARCHAR(40)
);

CREATE TABLE country (
    id INT AUTO_INCREMENT PRIMARY KEY,
    country VARCHAR(40)
);

CREATE TABLE client(
    id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
    name VARCHAR(40),
    email VARCHAR(40),
    sold BOOLEAN,
    date VARCHAR(40),
    email_type_id INT,
    employee_id INT,
    country_id INT,

    FOREIGN KEY(email_type_id) REFERENCES email_type(id),
    FOREIGN KEY(employee_id) REFERENCES employee(id),
    FOREIGN KEY(country_id) REFERENCES country(id)
);

Thank you so much!


Solution

  • how many clients are in each country

    select country , count(*) from country  inner join client on country.id=client.country_id 
    group by country 
    
    

    how many clients each employee has

    Select employee , count(*) from employee inner join client on client.employee_id =employee.id group by employee