0

I don't understand what key word or phrase that indicates a sub-query should be used for #2, and #3.

A. I noticed that whenever a field is mentioned in group by, it has to be mentioned in the select filed, but in the solution below, the reason why "film_id" was not mentioned in the first "select" statement is because it was under another "select" statement with parenthesis? B. Also my solution gave me a completely different answer. Why are we counting by "id"?

-- 2. Select the maximum number and the minimum number of screenings for a particular film.

My solution: SELECT max(id), min(id) from (id, count(film_id) as no_films from screenings group by film_id) a;

The solution: SELECT MAX(id), MIN(id) FROM (SELECT film_id, COUNT(id) AS id FROM screenings GROUP BY film_id) a;


For #3, I don't understand the thought process behind this. Why is "screenings" not defined or joined? When does one use the "as" when using the count statement?

-- 3. Select each film name and the number of screenings for that film.

My solution:

select f.name, 
(select count(id) as id from screenings s)
from films f
join films f ON s.film_id = f.id;       

The solution:

SELECT name, 
(SELECT COUNT(id) FROM screenings 
WHERE film_id = f.id)
FROM films f;
  • 1
    There is no "should", only "could". There is always more than 1 way to express things. "whenever a field is mentioned in group by, it has to be mentioned in the select"--no. "Why are we counting by "id""--I guess we want the number of rows with non-null ids in a group--because the documentation says that's how count(column) acts. "my solution gave me a completely different answer"--what does that mean? Use enough words to be clear. Please ask 1 specific researched non-duplicate question per post. It's not clear what that is here. Research/ask about 1 operator/function/keyword at a time. [help] – philipxy May 29 '20 at 02:49
  • 1
    Your questions when pinned down about bits of syntax & simple queries will be faqs. Before considering posting please read your textbook and/or manual & google any error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names & site:stackoverflow.com & tags; read many answers. PS If you read how aggregates like count work, it says that when there is no group by then the whole table is treated like 1 group. PS [Re SQL querying.](https://stackoverflow.com/a/33952141/3404097) – philipxy May 29 '20 at 04:23
  • You are jumping to (wrong) concusions. I am just trying to help you & my comments are relevant & helpful. You could google 'stackoverflow duplicate question'. Besides the [help], including [ask], you can find out more about using the site googling with 'site:meta.stackoverflow.com' & 'site:meta.stackexchange.com'. Let me know if you're still not sure what I mean. Can I clarify anything else? Good luck. – philipxy Jun 01 '20 at 01:48

0 Answers0