Tallying strings in column - sql

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.

Related

Joining tables and sum by attribute

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

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.

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

oracle sql query to count based on another query

I want to know which customer from which country did the most orders. So I have a sales_table with the customer_ids. But the country_id is in the customer_table. So I need somehow to count the customers based on the country count... But I have no idea how to do it. I
I know how to count the customers.
select count(cust_id)
from sh_sales
and how to count the countries
select count(country_id)
from sh_customers
but i want to count the country based on the customer_id which is most often used in the sh_sales table
so it should somehow be
select count(country_id)
from sh_customers
where sh_sales.customer ????
I really need some help here :)
This will count the records in the sh_sales table and group out by each country_id from the customers table
SELECT country_id, count(s.cust_ID)
FROM sh_customers c
INNER JOIN sh_sales s ON c.cust_id = s.cust_id
GROUP BY country_id
If, for some reason, you could have a customer record, but no sales then you can use LEFT OUTER JOIN to return a NULL for countries without any sales

How do I join 3 tables using mysql

I have the following tables:
TABLE: companies c
FIELDS: id, name, description
TABLE: departments d
FIELDS: id, name, description
TABLE employees e
FIELDS: id, company_id, department_id, name
I'm trying to get a list of all employees for company 1 and also get the department name (d.name) into the results of the row.
Here's my original query:
SELECT e.*, c.name as company
FROM employees e, companies c
WHERE e.company_id=c.id AND e.company_id=1;
What should I add to this query to get the department name (d.name) to appear in each row of the query? Also... e.department_id may be equal to 0 in some cases since there are no departments for a specific company.
Thanks for the help everyone!
SELECT e.id, e.name, e.department_id, c.name, d.name
FROM employees AS e
LEFT JOIN departments AS d ON e.department_id = d.id
LEFT JOIN companies AS c ON e.company_id = c.id
WHERE e.company_id = 1;
Generally you can mix-match joins in any order, as long as any columns/tables you require in a join have already been joined previously. In this case, you're slurping up data from two separate tables that are related via the employees data, and neither of them are co-dependent, so you can order the two LEFT JOIN lines in any order.
As well, with a LEFT join, you get all rows from the table on the "left" side of the join (in this case, the employees table) and matching rows (if any) from the right table (companies/departments). If there's no matching rows in either companies or departments, then any columns coming from those tables will be NULL in the result set.