From customer limit 5; customer id


Download 19.59 Kb.
bet3/4
Sana17.01.2023
Hajmi19.59 Kb.
#1097970
1   2   3   4
Bog'liq
Exercise dvdrental

SELECT count(address2) from address;
Or we can use * to count the total number of rows, including any with NULL in a column:
SELECT count(*) from address;
NULL is not counted by count, even though it is returned as a result by DISTINCT.
SELECT DISTINCT address2 from address;
SELECT count(DISTINCT address2) from address;
Exercises
What’s the most you’d have to pay to replace a lost film from this dvd rental store? What’s the least?
Use the count function to determine the number of different languages in the language table.
GROUP BY
GROUP BY is used to divide results into groups, where you then apply some summary function to each group. You will generally select the column you’re grouping by, and then a summary function. The most common operation is counting. We use count(*) to count the number of rows in each group.
SELECT customer_id, count(*) FROM rental GROUP BY customer_id;
We can use other functions as well.
SELECT customer_id, avg(amount) FROM payment GROUP BY customer_id;
You can group by multiple columns:
SELECT customer_id, amount, count(*) FROM payment
GROUP BY customer_id, amount;
Sort the above
SELECT customer_id, amount, count(*) FROM payment
GROUP BY customer_id, amount
ORDER BY customer_id, count(*) DESC;
All columns in the SELECT part of the statement have to be in the GROUP BY part, or you’ll get an error:
SELECT customer_id, amount, count(*) FROM payment GROUP BY customer_id;
dvdrental=# SELECT customer_id, amount, count(*) FROM payment GROUP BY customer_id;
ERROR: column "payment.amount" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT customer_id, amount, count(*) FROM payment GROUP BY c...

Download 19.59 Kb.

Do'stlaringiz bilan baham:
1   2   3   4




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling