Skip to content

Commit

Permalink
add: group by
Browse files Browse the repository at this point in the history
  • Loading branch information
thutasann committed Jan 31, 2025
1 parent 1eb7eee commit 00efd56
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion sql/full_course/aggregates/group_by.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
-- GROUP BY = aggregate all rows by a specific column
-- often used with aggregate functions
-- ex. SUM(), MAX(), MIN(), AVG(), COUNT()
-- ex. SUM(), MAX(), MIN(), AVG(), COUNT()

SELECT * FROM transactions;

ALTER TABLE transactions
ADD COLUMN order_date DATETIME;

--------

SELECT MAX(amount), order_date
FROM transactions
GROUP BY order_date;

SELECT COUNT(amount), customer_id
FROM transactions
GROUP BY customer_id
HAVING COUNT(amount) > 1 AND customer_id IS NOT NULL;

0 comments on commit 00efd56

Please sign in to comment.