154k views
5 votes
Suppose we have a database consisting of the following three relations FREQUENTS(DRINKER, BAR) SERVES(BAR, BEER) LIKES(DRINKER, BEER) The first indicates the bars each drinker visits, the second tells what beer each bar serves, the last indicates which beer each drinker likes to drink. Assume that each drinker likes at least one beer and frequent at least one bar and that a bar serves at least one beer. Express the queries below in SQL

Which bar is the most popular?

User Crowley
by
7.9k points

1 Answer

6 votes

Final answer:

To find the most popular bar, use a SQL query that groups the bars and counts their occurrences, then orders the result to get the most popular bar.

Step-by-step explanation:

To find the most popular bar, we need to count the number of times each bar appears in the 'FREQUENTS' relation. We can achieve this by grouping the bars and using the COUNT function. The bar with the highest count will be the most popular.

Here's the SQL query:

SELECT BAR, COUNT(*) AS visit_count FROM FREQUENTS GROUP BY BAR ORDER BY visit_count DESC LIMIT 1;

This query retrieves the 'BAR' column from the 'FREQUENTS' relation, counts the number of occurrences for each bar, and orders the result in descending order. The 'LIMIT 1' ensures that only the most popular bar is returned.

User Jo So
by
8.1k points