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
Related
I'm trying to write a query that considers the following tables that will return the number of orders submitted by each customer, grouped by their email.
Ex: john.smith#email.com | 23
Tables
This is what I have so far:
SELECT COUNT(Order ID), Customer.Customer email
FROM Orders
INNER JOIN Customer ON Customer.Customer ID = Orders.Customer ID
GROUP BY Customer.Customer email
ORDER BY COUNT(Order ID)
I'm really struggling with SQL JOINS. Can anyone help me grasp this?
You have a customer table where each row refers to a specific customer.
You have an order table where a customer can have multiple orders.
When you join these two tables, that is, each row of the customer table is joined to each row of the order table that has a single customer ID.
For example, if a customer has two orders, he will be linked to the order table twice. Your table has a one-to-many relationship.
SELECT C.CustomerID, COUNT(*) FROM Customers C, Orders O
WHERE C.CustomerID = O.CustomerID
GROUP BY C.CustomerID
I tried the above query in this website and looks like it worked.
I believe that in your case you'll need to change the C.CustomerID of the SELECT and the GROUP BY to your email like C.email
SELECT C.CustomerEmail, COUNT(*) FROM Customers C, Orders O
WHERE C.CustomerID = O.CustomerID
GROUP BY C.CustomerEmail
If your Customer table allows that the same email can be from multiple IDs, you're going to have some consistency problems in this report.
Find the customer that spend less than 3$ on individual film rentals, but has spent a total higher than 15?
The query that I wrote is given below
SELECT CustomerID,Customer.CustomerFirstName,Customer.CustomerSurname,Total FROM (SELECT DISTINCT Customer.CustomerID,Customer.CustomerFirstName,Customer.CustomerSurname,sum(([Rental].[Quantity])*([Film].[FilmPrice])) AS Total
FROM Film RIGHT JOIN (Customer INNER JOIN Rental ON Customer.CustomerID = Rental.CustomerIDFk) ON Film.FilmID = Rental.FlimIDFk
GROUP BY Customer.CustomerID,Customer.CustomerFirstName,Customer.CustomerSurname) T WHERE Total>15;
Now how can I apply the second condition which is FilmPrice<3
Please help me out.
This is the ERD
Thanks
Simply add another where clause inside the subquery, after your join on but before the group by for WHERE FilmPrice<3
You want to find the customers where the maximum price of a film they have rented is less than 3 and the total price of all the films they have rented is greater than 15.
Something like this:
SELECT CustomerID,
CustomerFirstName,
CustomerSurname
FROM Customer
WHERE CustomerId IN (
SELECT r.CustomerIDFk
FROM Rental r
INNER JOIN Film f
ON ( f.FilmId = e.FilmIdFk )
GROUP BY r.CustomerIdFk
HAVING MAX(f.FilmPrice) < 3
AND SUM(f.FilmPrice * r.Quantity)>15
)
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
I am trying to work out how to go about this one SQL query.
I have two tables Orders and Customers.
Orders has two columns CustomerNumber and Fruit
Customers has two columns as well CustomerNumber and Address
Not all customers have placed an order but I need a query that runs through the list of Customers.CustomerNumber and lists how many times that Customers.CustomerNumber times shows up in the table Orders.
It's a countif query but im not sure how to set it up.
select customer.id, count(order.*)
from Customer inner join Order on Customer.id=Order.ID
group by customer.id
select c.CustomerNumber, count(1)
from Customer as c
left join Order as o on c.CustomerNumber = o.CustomerNumber
group by c.CustomerNumber
This will return a zero for customer's without any orders.
My Question:-
A list of customers who placed more than one order with MIP in the past. Write a SELECT statement that returns one row for each customer that has orders with these columns:
The customer FirstName and EmailAddress columns
A count of the number of orders placed by the customer
The total amount for all orders placed by that customer (from the table OrderItems)
My ERD:-
My problem:- I am completed stuck. No idea. This is an assignment question.
I tried it with count also but just a suggestion as to how to go about it would be appreciated. I need to calculate the total amount spent by these customers which is through a third table
Thanks
Do an inner join on the customer and order table. Then group by on firstname and email address. Do a count on the no of orders and a sum on the amount.
If your tables were named Customer and Order, the sql statement would be like
SELECT
Customer.FirstName
,Customer.EmailAddress
,COUNT(Order.OrderId) AS OrdersPlaced
,SUM(Order.Amount) AS TotalAmount
FROM
Customer
INNER JOIN Order ON Customer.CustomerID = Order.CustomerID
GROUP BY
Customer.FirstName
,Customer.EmailAddress
Hope this helps.