/ / Daily XP 3413 Exercise Exercise Count affiliations per university Now that your data…
- User
/ / Daily XP 3413 Exercise Exercise Count affiliations per university Now that your data is ready for analysis, let's run some example SQL queries on the database. You'll now use already known concepts such as grouping by columns and joining tables. In this exercise, you will find out which university has the most affiliations (through its professors). For that, you need both affiliations and professors tables, as the latter also holds the university_id. As a quick repetition, remember that joins have the following structure: SELECT table_a.column1, table_a.column2, table_b.column1, ... FROM table_a JOIN table_b ON table_a.column = table_b.column This results in a combination of table_a and table_b, but only with rows where table_a.column is equal to table_b.column. Instructions 70 XP Count the number of total affiliations by university. Sort the result by that count, in descending order. Show Answer (-70 XP) Incorrect submission Check the highlighted code. Could not find the first entry in the fields. Did you find this feedback helpful? Yes No query.sql 12345678 -- Count the total number of affiliations per university SELECT COUNT(), professors.university_id FROM affiliations JOIN professors ON affiliations.professor_id = professors.id -- Group by the university ids of professors GROUP BY professors.university_id ORDER BY COUNT() DESC; query result professors universities affiliations organizations count university_id 579 EPF 273 USG 162 UBE 133 ETH 75 UBA 40 UFR 36 UNE 35 ULA 33 UGE 7 UZH 4 USI Showing 11 out of 11 rows -- Count the total number of affiliations per university SELECT (*), professors.university_id FROM ___ JOIN ___ ON affiliations.professor_id = professors.id -- Group by the university ids of professors GROUP BY professors. ___ ___ count DESC;
- Assistant
To count the total number of affiliations per university and sort the results in descending order, you need to fill in the SQL query structure correctly. Here's how to construct the SQL query step by step:
- Use
COUNT(*)to count the number of affiliations. - Specify the tables to join:
affiliationsandprofessors. - Use the correct
JOINsyntax to join these tables on the relation you have defined (i.e.,affiliations.professor_idequalsprofessors.id). - Group the results by
professors.university_idto aggregate the counts per university.
- Use
- User
Incorrect submission Check the highlighted code. Could not find the first entry in the name.: COUNT(*) AS affiliation_count
- Assistant
It seems like the SQL environment you're using may not allow aliases on the
COUNT()function, or it might be expecting the syntax slightly different. You can still achieve the same result without using an alias—which is sometimes necessary depending on the SQL dialect.Here is the corrected SQL query without the alias for
COUNT(*):SELECT COUNT(*), professors.university_id FROM affiliations JOIN professors ON affiliations.professor_id = professors.id GROUP BY professors.university_id ORDER BY COUNT(*) DESC;Key Changes:
- Removed the alias
AS affiliation_countfromCOUNT(*).
Explanation:
- This query will still count the total number of affiliations per university and order the results in descending order by the count of affiliations.
- Make sure to ensure that your SQL environment can execute this query correctly. If it still raises errors, refer to the specific documentation or constraints of the SQL dialect you are working with.
Result:
Executing this query should give you the desired result: a list of university IDs alongside their total counts of affiliations, sorted by the count in descending order.
- Removed the alias
- User
count(*) must be used to call a parameterless aggregate function LINE 1: SELECT COUNT(), professors.university_id ^
- Assistant
It appears you are encountering an error with the syntax of the COUNT function. The correct usage of the
COUNTfunction requires the asterisk(*)to indicate that you want to count all records. If your SQL environment is rejecting the syntax, it's possible there's a misinterpretation of the query or an environmental issue.Here's the appropriate query once more, ensuring that we are calling
COUNT(*)correctly:SELECT COUNT(*) AS total_affiliations, professors.university_id FROM affiliations JOIN professors ON affiliations.professor_id = professors.id GROUP BY professors.university_id ORDER BY total_affiliations DESC;