Skip to content
Ifeanyi Osiaku

Question · answered ✓

Sample: How do I sum sales per customer in SQL?

Deleted member ✎ ·

I have customers and orders tables. How do I get each customer's total, highest first?

This is a sample post for testing.

0 helpful

2 replies

  1. ✓ Accepted answer

    Deleted member ✎ ✓ ·

    Group by the customer and sort by the sum:

    SQL
    sql
    SELECT c.name, SUM(o.amount) AS total
    FROM customers c
    JOIN orders o ON o.customer_id = c.id
    GROUP BY c.name
    ORDER BY total DESC;
    
    1 helpful
  2. Deleted member ✎ ·

    That worked, thank you!

    0 helpful

This site uses a few cookies it needs to work, such as keeping you signed in. With your permission, videos from YouTube and Vimeo can also set their own cookies. You can change your mind at any time from "Cookie settings" at the bottom of every page. Cookie policy

Customise

Choose which optional cookies you allow. You can change this at any time.

Always on

Always on

Cookies in this group (6)
NameSet byPurposeHow long
ifeanyi-osiaku-session Ifeanyi Osiaku Keeps you signed in and protects forms while you browse 120 minutes after your last visit
XSRF-TOKEN Ifeanyi Osiaku Security: proves forms were sent from this site 120 minutes
remember_web_… Ifeanyi Osiaku Only if you tick "Keep me signed in" Until you sign out
io_cart Ifeanyi Osiaku Remembers your cart before you sign in 30 days
io_aid Ifeanyi Osiaku Only if you use the assistant: keeps your conversation 1 year
io_consent Ifeanyi Osiaku Remembers these cookie choices 180 days
Cookies in this group (2)
NameSet byPurposeHow long
YouTube player cookies and storage YouTube (Google) Plays the video and remembers player settings Set by YouTube; see its privacy policy
vuid Vimeo Plays the video and counts views Set by Vimeo; see its privacy policy