🧵MySQL GROUP BY 🧵👇
#mysql #Database #100DaysOfCode #CodeNewbie #PHP #programming
The GROUP BY clause groups a set of rows into a set of summary rows by values of columns or expressions.
The GROUP BY clause returns one row for each group. In other words, it reduces the number of rows in the result set.
We often use the GROUP BY clause with aggregate functions such as SUM, AVG, MAX, MIN, and COUNT.
The aggregate function that appears in the SELECT clause provides information about each group.
Example-1: GROUP BY with aggregate functions
SELECT
status, COUNT(*)
FROM
orders
GROUP BY status;
Example-2: MySQL GROUP BY with HAVING clause
SELECT
YEAR(orderDate) AS year,
SUM(quantityOrdered * priceEach) AS total
FROM
orders
WHERE
status = 'Shipped'
GROUP BY
year
HAVING
year > 2003;
Latest MySQL is not supporting GROUP BY without aggregate functions, to enable GROUP BY execute bellow command,
SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
💕🙏
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.
