RSS Feed

Examples of SQL Having & SQL Group By

SQL Having SQL Group By Null in SQL Group By


Some of the examples found on the Internet:

Find the average sales price for each beer.

Sells (bar, beer, price)

SELECT beer, AVG (price)
FROM Sells
GROUP BY beer;

Find, for each drinker, the average price of Bud at the bars they frequent.

Sells (bar, beer, price)
Frequents (drinker, bar)


SELECT drinker, AVG (price)
FROM Frequents, Sells
WHERE beer = 'Bud' AND
Frequents.bar = Sells.bar
GROUP BY drinker;

Find the average price of those beers that are either served in at least 3 bars or manufactured by Anheuser-Busch.

Beers (name, manf)
Sells (bar, beer, price)


SELECT beer, AVG (price)
FROM Sells
GROUP BY beer
HAVING
COUNT (*) > = 3 OR
beer IN(
SELECT name
FROM Beers
WHERE manf = 'Anheuser-Busch'
);

No comments:

Post a Comment