Joining tables and sum by attribute - sql

I need to know how many employees does each company have for each country they are present in? I have to join two tables (companies and cities) and sum number of employees for each country.
SELECT *, SUM(EMPLOYEES)
FROM COMPANIES WHERE
JOIN CITIES
ON COMPANIES.CITYNAME = CITIES.CITYNAME
doesn´t work...
Tables to join and sum employees for each country

A couple of tips,
Try to alias your joins so your not typing the full table names in your join statements
Add a group by on every field your not aggregating on for your sum to work
SELECT CI.COUNTRYNAME,CO.CITYNAME,CO.COMPANYNAME,CI.POPULATION,CI.COUNTRYNAME,
SUM(EMPLOYEES) AS TOTAL_EMPLOYEES
FROM COMPANIES AS CO
JOIN CITIES AS CI
ON CO.CITYNAME = CI.CITYNAME
GROUP BY CI.COUNTRYNAME,CO.COMPANYNAME,CO.CITYNAME,CI.POPULATION
https://rextester.com/DLZNT87647

Related

Count/sum rows and group them by another column

I am starting with SQL and I have a problem grouping 2 columns. I need the counting to be filtered by another column, but I obtain the count of all the rows as if they weren't grouped.
I have 4 different tables with countries, cities, matches and stadiums. I have to obtain names and codes of countries and cities as well as the number of referees that worked in each city and the total number of goals made in every city. I have done this, but I obtain the total number of referees that have worked in the tournament and the total number of goals made during the tournament:
SELECT ci.city_code,
ci.city_name,
co.country_code,
co.country_name,
COUNT(DISTINCT referee_code),
SUM(home_goals+visitor_goals)
FROM euro2021.tb_city AS ci
INNER JOIN euro2021.tb_country AS co ON ci.country_code=co.country_code
INNER JOIN euro2021.tb_match AS m ON ci.city_name=ci.city_name
INNER JOIN euro2021.tb_stadium AS st on st.stadium_code=m.stadium_code
GROUP BY ci.city_code, ci.city_name, co.country_code, co.country_name
Do you have any ideas or do you know if this was answered previously? Thanks in advance.
I think the problem is all in your JOIN clauses. I believe you want this:
SELECT ci.city_code,
ci.city_name,
co.country_code,
co.country_name,
COUNT(DISTINCT m.referee_code),
SUM(m.home_goals+m.visitor_goals)
FROM euri2021.tb_match AS m
INNER JOIN euro2021.tb_stadium AS st ON st.stadium_code=m.stadium_code
INNER JOIN euro2021.tb_city AS ci ON ci.city_code=st.city_code
INNER JOIN euro2021.tb_country AS co ON ci.country_code=co.country_code
IROUP BY ci.city_code, ci.city_name, co.country_code, co.country_name;

Count the number of employees for every country

I have this task:
Count the number of employees for every country. Show only those countries, when works more than 20 employees
employee_id is dedicated for Employees table
country belongs to different table - Countries table and we need country_name from this table
I have no idea how to solve this task. Below what I was able to create. I think we should use Inner Join.
SELECT a.employee_id
, b.country_name
, COUNT(a.employee_id) AS count
FROM employees a
INNER JOIN countries b ON a.employee_id = b.country_name
GROUP BY b.country_name
WHERE employee_id >20;
I think I need help from the beginning.
Thanks
Your join doesn't seem correct but as I don't know the table structure, I can't say what the right column is (I'm going to assume that it should be country_name. Even so, try this:
SELECT b.country_name
, COUNT(a.employee_id) AS count
FROM employees a
INNER JOIN countries b ON a.country_name = b.country_name
GROUP BY b.country_name
HAVING COUNT(employee_id) >20;
When grouping you need to use the HAVING statement to filter.

Tallying strings in column

I want to create a query with two columns, country and individuals. Country is a list of countries individuals is the total number of employeeid that match each country. I've messed around with join clauses but I'm not sure where to start.
If you want to get the total number of employees per country, then here it is:
SELECT C.COUNTRY,
INDIVIDUALS = SUM(E.EMPLOYEE)
FROM COUNTRIES C
INNER JOIN EMPLOYEES E
ON E.COUNTRY_ID = C.COUNTRY_ID
GROUP BY E.COUNTRY_ID
I assumed that COUNTRIES and EMPLOYEES are your tables.

Combining distinct and count() from two tables

I have two tables:
Customers (name, address, postcode (FK))
Postcodes (postcode (PK), county)
I want to find out how many customers are in each county.
I am assuming I need an inner join on postcode but don't know how to combine this with a count(customer_id) and distinct(county).
Although you can write queries with SELECT DISTINCT country it prevents you from doing aggregates such as COUNT. Instead you can use GROUP BY which broadly has the same effect as DISTINCT but with much more power and flexibility.
These two queries give the same results, but the second lets you then go on to add your JOIN and COUNT statements.
SELECT DISTINCT county FROM postcodes
SELECT county FROM postcodes GROUP BY county
By and large, don't use SELECT DISTINCT, but use this kind of pattern...
SELECT
postcodes.county,
COUNT(customers.customer_id)
FROM
postcodes
INNER JOIN
customers
ON customers.postcode = postcodes.postcode
GROUP BY
postcodes.county
Just join the Customers table to the Postcodes table on the common field 'postcode '. Then you can use Group By to get your counts and return one row per County
SELECT
County,
COUNT(Customer_Id) CustomerCount
FROM
Postcodes pc
JOIN Customers c ON pc.PostalCode = c.PostalCode
GROUP BY
County

sql count with inner join

I have a table for absentees, and that table am storing the studentids of those who have been absent.
From this table I had to find total presentees and total absentees, for this I just joined the Sections table which contains the maximum capacity of particular Section.
For this my query was
select COUNT(Attendance.studentid) as Absentees
,Sections.Max-count(studentid) as Presentees
from Attendance
inner join Students
on students.StudentId=Attendance.StudentId
inner join Sections
on Sections.CourseId=students.CourseId
group by Sections.Max
Its working fine, the same way how can I find the gender wise presentees/absentees......gender column is in Students table, can anyone give me some idea, thanks in advance
Just add the gender column to your select ... columns and the group by, you'll end up with one row for each gender:
select COUNT(Attendance.studentid) as Absentees,
Sections.Max-count(studentid) as Presentees,
Students.Gender as Gender
from Attendance
inner join Students
on Students.StudentId=Attendance.StudentId
inner join Sections
on Sections.CourseId=Students.CourseId
group by Sections.Max, Students.Gender