I’m struggling to understand what this query is doing:
SELECT branch_name, count(distinct customer_name)
FROM depositor, account
WHERE depositor.account_number = account.account_number
GROUP BY branch_name
What’s the need of GROUP BY?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Let’s take a step away from SQL for a moment at look at the relational trainging language Tutorial D.
Because the two relations (tables) are joined on the common attribute (column) name
account_number, we can use a natural join:(Because the result is a relation, which by definition has only distinct tuples (rows), we don’t need a
DISTINCTkeyword.)Now we just need to aggregate using
SUMMARIZE..BY:Back in SQLland, the
GROUP BY branch_nameis doing the same asSUMMARIZE..BY { branch_name }. Because SQL has a very rigid structure, thebranch_namecolumn must be repeated in theSELECTclause.