SQL statement to find The number of customers who sign up and did not order

48 Views Asked by At

I am looking for a query than can list the customers:

  1. customers who sign up but not orders.
  2. customers who order more than 1 time.

I have 2 Table Users and Orders:

  • Users table: user_id, created_at

  • Orders table: orders_id, order_no, customer_id, created_at.

customer_id in the orders table is user_id

I wrote this SQL query statement but I couldn't find a result

SELECT Users.user_id,
FROM Users
JOIN Orders
ON customer.id=Orders.customer_id 

Could someone please help me out with this? Thanks!

1

There are 1 best solutions below

2
Sergey On
SELECT U.user_id 
FROM Users AS U
LEFT JOIN Orders AS O ON U.customer.id=O.customer_id
WHERE O.customer_id IS NULL

It is for "customers who sign up but not orders."

SELECT U.user_id 
FROM Users AS U
JOIN Orders AS O ON U.customer.id=O.customer_id
GROUP BY U.user_id 
HAVING COUNT(O.orders_id) >1

It is for "customers who order more than 1 time"