if I have a two tables (one of customers, with their information including address, name, emails etc) and another of orders (with order number, shipping date, customer name who ordered that item), how could I show the email of the customers who have less than 3 orders?
I know I have to use an inner join and some alias's, but i'm not sure how to proceed.
Thanks!
what I have so far:
SELECT customer.email
FROM customer as cust
INNER JOIN (select customer_id, sum(line_qty) AS total
from orders as o ON cust.customer_id = o.customer_id
where total = (SELECT total < 3
FROM (select customer_id, sum(line_qty) AS total
from orders as o ON cust.customer_id = o.customer_id
) as sub);
I have created the full example with SQL. Just run the query to create the database, tables, and the Stored Procedure "Get Customer Orders".
There are sample data in the two table of "Customers" and the table "Orders" the relation is "1 Customer to MANY Orders" so there is a Foreign key for the Customer inside table Orders, to identify which customer had did the order. So.
First Create the Data base, Run this query.
Refresh the Server explorer, you will find a database with that name has created. Then Run the Query to create Stored Procedure and Tables.
Then to access your Stored procedure that you want to get the Customer Email if he/she did orders less than 3 Orders.
Try this: