I'm struggling with a question that said Which customer has rented the
most films?
I am doing this using the Seikila sample database in MySQL. I have something that joins my tables together and attempts to give me a count but I know its wrong just looking at the actual data in the rental table.
my code is as below
SELECT r.rental_id, cust.customer_id, count(*) as Total_Rentals
FROM rental as r
INNER JOIN customer AS cust on r.customer_id = cust.customer_id
GROUP BY cust.customer_id;
but it tells me for example customer 1 has rented 32 movies, which I know is wrong. what am I doing wrong?
since I was asked for clarification, the database I am using is:
https://dev.mysql.com/doc/sakila/en/
And I am trying to find which customer has rented the most films, I am not entirely sure what my script is actually returning.
Remove the column rental_id from the select list and sort the result by count(*) descending to return the top 1 row:
SELECT cust.customer_id, cust.name, count(*) as Total_Rentals
FROM rental as r
INNER JOIN customer AS cust on r.customer_id = cust.customer_id
GROUP BY cust.customer_id, cust.name
ORDER BY Total_Rentals DESC LIMIT 1
But if you only need the customer's id then there is no need for a join:
SELECT customer_id, count(*) as Total_Rentals
FROM rental
GROUP BY customer_id
ORDER BY Total_Rentals DESC LIMIT 1
You need to join customer and rental, group by customer id (without rental id) and count it:
SELECT cust.customer_id, count(*) as Total_Rentals
FROM rental as r
INNER JOIN customer AS cust on r.customer_id = cust.customer_id
GROUP BY cust.customer_id;
So this code should work. If it doesn't work, that probably means that you have duplicates or other nonconventional issues.
Related
first sorry, i don't have fluid english.
I want select 3 rows in 3 different tables, two of them without Foreing Key/relation.
Need to select amount of customers in each store, and total amount of payments in these stores in one query.
Here are the tables:
Customers
Stores
Payments
I have tried these querys to get payments for each store and customers for each store, but don't know how can unify in one query:
Payments/store
SELECT count(a.payment_id) as alquileres, b.store_id
FROM customer b, payment a
WHERE a.customer_id = b.customer_id
GROUP BY b.store_id;
customers/store
SELECT count(customer_id), store_id
FROM customer
GROUP BY store_id;
But when I add count(customer_id) in unique query don't have same results.
You can use one query:
select c.store_id,
count(distinct c.customer_id) as num_customers,
count(p.payment_id) as num_payments
from customers c left join
payments p
on p.customer_id = c.customer_id
group by c.store_id;
I have 5 tables connected with each other in the image
[![Image1][1]][1]
I have tried writing query
SELECT lastname,firstname
FROM customer,purchase_order
where customer.customer_id=purchase_order.customer_id
What parameter should I consider for most purchase?
You can solve that by adjusting the select clause (remove order_id).
Then you will need a HAVING clause to implement the condition to only show the customers with the most purchase orders.
SELECT lastname,firstname
FROM customer
INNER JOIN purchase_order
ON customer.customer_id=purchase_order.customer_id
GROUP BY customer_id
ORDER BY COUNT(order_id) DESC LIMIT 1;
select lastname, firstname from customer c
inner join (
select customer_id, count(1) from purchase_order
group by 1
order by 2 desc limit 1) most
/* the above sub-query named as 'most' fetched the customer id with the maximum
number of orders */
on c.customer_id = most.customer_id; -- this is a simple join of customers table with the table generated from sub-query above
SELECT c.firstname,c.lastname
FROM customer c
JOIN purchase_order po
ON c.customer_id = po.customer_id
GROUP BY c.firstname,c.lastname
ORDER BY COUNT(po.order_id) DESC LIMIT 1;
I'm messing around with the Sakila sample database in MySQL and I would like to get the top two people who rented the most movies. I've tried a few things and my most recent attempt is:
SELECT c.last_name, Count(r.rental_id)as NumberOfRentals FROM customer c
INNER JOIN rental r ON c.customer_id = r.customer_id
ORDER BY NumberOfRentals DESC
LIMIT 2
It only returns the first name in the database though...
You need a GROUP BY clause. Without having one defined, MySQL will aggregate all the rows which match the given parameters into a single row, instead of aggregating them based on a defined criteria (the last_name in this case).
SELECT c.last_name, Count(r.rental_id)as NumberOfRentals FROM customer c
INNER JOIN rental r ON c.customer_id = r.customer_id
GROUP BY c.last_name
ORDER BY NumberOfRentals DESC
LIMIT 2
I want to know how many customers placed more than 1 order, grouped by day.
But I want to exclude orders that have been cancelled.
I have a customer table with customers
I have a customer_order table with orders (when an order is cancelled
it stays in the customer_order table)
I have an order_item table (where original orders are, and also
cancelled orders, cancelled orders get a new credit order, and the
original order id appears in the id_credit_order row of the credit
order)
I want something like this:
date | no of customers with 1 order | no of customers with 2 orders | no of customers with 3 order | etc.
But I want no count if the original order has been cancelled!
I have now this query, but its definitely not enough, does someone know how to get my result? thanks!
SELECT DATE(co.date_order), COUNT(co.id_customer)
FROM customer_order co
GROUP BY DATE(co.date_order)
ORDER BY DATE(co.date_order) DESC;
The question is slightly misleading, as you first ask for the number of customers with more than one order, then you ask for the number of customer with each of 1, 2, 3... orders.
Here's something that will give you the numbers, but unpivoted. You'll need to put the right column name in for o.id
Select -- outer query takes each order count and counts how many customers have that many
co.date_order,
co.customer_order_count,
count(*) as customer_count
From (
Select -- inner query counts how many valid orders each customer has
o.date_order,
o.id_customer,
count(*) as customer_order_count
From
customer_order o
Where
o.id_credit_order is null and -- rule out credit orders
not exists ( -- rule out orders with related credit orders
select
'x'
from
customer_order c
where
c.id_credit_order = o.id -- column name isn't mentioned in the question
)
Group By
o.date_order,
o.id_customer
) co
Group By
co.date_order,
co.customer_order_count
Order By
co.date_order desc,
co.customer_order_count
Try using a HAVING clause.
SELECT DATE(co.date_order), COUNT(co.id_customer)
FROM customer_order co
WHERE co.Cancelled = 0
GROUP BY DATE(co.date_order)
HAVING COUNT(co.id_customer) >= 1
ORDER BY DATE(co.date_order) DESC;
Give this a try. Change the WHERE to reflect how you cancel orders.
SELECT DATE(co.date_order), COUNT(co.id_customer)
FROM customer_order co
WHERE co.cancelled = 0
GROUP BY DATE(co.date_order)
HAVING COUNT(co.id_customer) > 0
ORDER BY DATE(co.date_order) DESC;
This is a part of SQL coding project of database.
I need to design a single table to hold orders made by customers.
Assume that customers' details (e.g. names) are stored in another table.
Using the table that I designed for orders, and this assumed other table, write
a single SQL query that can give the number of orders for every customer, one line per customer.
The “Orders” table
Order_ID Order_NO Customer_ID
1 8088 3
2 9632 1
3 1272 4
4 6037 1
Assume that the customer names and other details (address, phone numbers, emails) are stored in the “Customers” table.
My SQL:
SELECT Customers.FirstName, Customers.FirstName, Orders.OrderNo
FROM Customers
FULL JOIN Orders
ON Customers.Customer_ID = Orders.Customer_ID
ORDER BY Customers.LastName
Are there something wring with it ?
Change the query to inner join
SELECT
Customers.FirstName
, Customers.FirstName
, Orders.OrderNo
FROM
Customers
INNER JOIN Orders ON Customers.Customer_ID = Orders.Customer_ID
ORDER BY
Customers.LastName
Query for Ordercount for customer
SELECT
Customers.LastName
, COUNT(Orders.Order_Id)
FROM
Customers
INNER JOIN Orders ON Customers.Customer_ID = Orders.Customer_ID
GROUP BY
Customers.LastName
ORDER BY
Customers.LastName
Full join
http://www.w3schools.com/sql/sql_join_full.asp
Inner join
http://www.w3schools.com/sql/sql_join_inner.asp
If you just want to COUNT the orders you can do this:
SELECT
Customers.FirstName,
(
SELECT
COUNT(*)
FROM
Orders
WHERE
Orders.Customer_ID=Customers.Customer_ID
) AS NbrOfOrders
FROM
Customers
References:
12.15.1. GROUP BY (Aggregate) Functions
Don't forget COUNT and GROUP BY.