1
CREATE TABLE    CUSTOMER (
cusID   VARCHAR(10) NOT NULL,
cusNAME     VARCHAR(50),
CONSTRAINT CUSTOMER_PKEY PRIMARY KEY (cusID)
);

CREATE TABLE    ORDERS (
cusID       VARCHAR(10) NOT NULL,
item    VARCHAR(25),
PPRICE  NUMBER(12,2),
CONSTRAINT ORDERS_FKEY FOREIGN KEY (cusID) REFERENCES CUSTOMER(cusID)
);

i have this two tables, and i want a query results of

CUSNAME    ITEM BOUGHT
JOHN          2
MAY           5

is it possible to do this? something like select cusNAME,COUNT(*) FROM CUSTOMER,ORDERS .......and so on... have no idea how to continue this.

3
  • please correct your question format , it isn't readable! Commented May 5, 2013 at 11:25
  • Is "bought" defined as the number of rows in the orders table? Or is there a quantity purchased amount somewhere? Commented May 5, 2013 at 11:25
  • bought is just how many times cusID appear in the orders table because customer can only buy one item at a time. so i need to find out how many item has they bought Commented May 5, 2013 at 11:33

1 Answer 1

3
SELECT  cus.cusName,
        COUNT(*) AS ItemBought
FROM    Customer AS cus
        INNER JOIN Orders AS ord
            ON cus.CusID = ord.cusID
GROUP   BY cus.cusName

To further gain more knowledge about joins, kindly visit the link below:

The query above will only show when a customer has atleast one record on table Order because it uses INNER JOIN. If you want to show all customers whether it has atleast one record on table order or not, use LEFT JOIN instead.

Sign up to request clarification or add additional context in comments.

1 Comment

@downvoter you have so many chances to downvote my answers. clearly :) happy downvoting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.