-1

enter image description here

Hi, I'm starting to learn mysql and am confronted with the commands. I think I understand the basics, but with this example I'm failing to set up the commands correctly. The underlined keys are primary keys. Can you help me a bit ? Determine the sum of the points of each account without it's own entries. This is what I have, and I believe it's wrong..

SELECT SUM points FOR * accID
       FROM (SELECT accID, SUM points FROM entry
              WHERE entry.accID HAS entry.entryID = null
New contributor
green tea is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4
  • 1
    Please consider following these suggestions. Why do you believe what you have written is wrong? Did you try it? What was the result? What did you expect instead?
    – mustaccio
    Nov 26 at 1:42
  • What database system, version, and edition are you using? (Please add it to your post's tags.)
    – J.D.
    Nov 26 at 2:35
  • Determine the sum of the points of each account without it's own entries. points is an attribute of the account, not of the entry. The task is not clear. Provide some sample data (as textual CREATE TABLE + INSERT INTO codes) and desired output for this data with detailed explanations. Also terll us what DBMS you're using, uncluding its precise version.
    – Akina
    Nov 26 at 6:09
  • Use a JOIN instead of FROM ( SELECT ... )
    – Rick James
    Nov 26 at 18:56
1

This is what I have, and I believe it's wrong

You are correct, and that's because you're still getting to grips with SQL syntax (I've assumed we're talking MySQL here; you didn't actually say). You can't just "invent" it - you have to take the time to familiarise yourself with it.

SUM points should be SUM( points ) - the braces identify the argument(s) to the SUM function.

SELECT ... FOR * accID isn't valid SQL syntax.
"select .. for" is used for something completely different (namely locking rows as you read them, ready for updating later).

WHERE entry.accID HAS entry.entryID = null isn't valid SQL syntax either.
WHERE entry.entryID IS NULL is what you'd need.

Now, to the question you've [been] posed:

Determine the sum of the points of each account without it's own entries

Unless I'm missing something here (which is quite possible) there's no need for any table other than Account!

The points field is in that table and you're told not to include "its own entries" so why not just SUM up the points in the Account table, for each Account?

For that, you'll need to select the Account Id and the sum of the points, then use a "group by" clause to total up the points to account "level".
And remember to include an "order by" clause as well, because that's the only way to guarantee the order the rows are returned in.

Your Answer

green tea is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.