Group by, having , order by

In MySQL, GROUP BY, HAVING, and ORDER BY are clauses used in SQL queries to group, filter, and sort the result set. Here’s an explanation of each:

GROUP BY

The GROUP BY clause groups rows that have the same values into summary rows, like "find the total quantity sold for each product". It is often used with aggregate functions such as SUM(), AVG(), MAX(), MIN(), and COUNT().

HAVING

The HAVING clause is used to filter groups of rows created by the GROUP BY clause. It is similar to the WHERE clause but is used with aggregated data.

ORDER BY

The ORDER BY clause is used to sort the result set of a query by one or more columns. By default, it sorts in ascending order, but you can specify DESC to sort in descending order

Example:
-- Create Products table
CREATE TABLE Products (
    ProductID INT AUTO_INCREMENT PRIMARY KEY,
    ProductName VARCHAR(100) NOT NULL
);

-- Insert sample data into Products
INSERT INTO Products (ProductName) VALUES
('Product A'), -- ProductID 1
('Product B'), -- ProductID 2
('Product C'); -- ProductID 3

-- Create Customers table
CREATE TABLE Customers (
    CustomerID INT AUTO_INCREMENT PRIMARY KEY,
    CustomerName VARCHAR(100) NOT NULL
);

-- Insert sample data into Customers
INSERT INTO Customers (CustomerName) VALUES
('Customer X'), -- CustomerID 1
('Customer Y'), -- CustomerID 2
('Customer Z'); -- CustomerID 3

-- Create Sales table
CREATE TABLE Sales (
    SaleID INT AUTO_INCREMENT PRIMARY KEY,
    ProductID INT,
    CustomerID INT,
    SaleDate DATE,
    Quantity INT,
    UnitPrice DECIMAL(10, 2),
    TotalPrice DECIMAL(10, 2),
    FOREIGN KEY (ProductID) REFERENCES Products(ProductID),
    FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

-- Insert sample data into Sales
INSERT INTO Sales (ProductID, CustomerID, SaleDate, Quantity, UnitPrice, TotalPrice) VALUES
(1, 1, '2024-01-01', 2, 15.00, 30.00),
(2, 2, '2024-01-02', 1, 25.00, 25.00),
(1, 3, '2024-01-03', 3, 15.00, 45.00),
(3, 1, '2024-01-04', 4, 10.00, 40.00),
(1, 2, '2024-01-05', 1, 15.00, 15.00),
(2, 3, '2024-01-06', 2, 25.00, 50.00);


Example Queries
1. GROUP BY with ORDER BY

Get the total quantity sold for each product, ordered by total quantity sold in descending order.

SELECT ProductID, SUM(Quantity) AS TotalQuantity
FROM Sales
GROUP BY ProductID
ORDER BY TotalQuantity DESC;

Output:

+-----------+---------------+
| ProductID | TotalQuantity |
+-----------+---------------+
|         1 |             6 |
|         3 |             4 |
|         2 |             3 |
+-----------+---------------+

2. GROUP BY with HAVING

Get the products where the total quantity sold is greater than 3.

SELECT ProductID, SUM(Quantity) AS TotalQuantity
FROM Sales
GROUP BY ProductID
HAVING SUM(Quantity) > 3;


Output:
+-----------+---------------+
| ProductID | TotalQuantity |
+-----------+---------------+
|         1 |             6 |
|         3 |             4 |
+-----------+---------------+

3.GROUP BY with HAVING and ORDER BY
Get the products where the total quantity sold is greater than 3, ordered by total sales amount in descending order.
SELECT ProductID, SUM(Quantity) AS TotalQuantity, SUM(TotalPrice) AS TotalSales
FROM Sales
GROUP BY ProductID
HAVING SUM(Quantity) > 3
ORDER BY TotalSales DESC;

Output:

+-----------+---------------+
| ProductID | TotalQuantity |
+-----------+---------------+
|         1 |             6 |
|         3 |             4 |
+-----------+---------------+

4. GROUP BY with multiple columns

Get the total sales amount for each product per customer.
SELECT ProductID, CustomerID, SUM(TotalPrice) AS TotalSales
FROM Sales
GROUP BY ProductID, CustomerID
ORDER BY ProductID, CustomerID;

Output:

+-----------+------------+------------+
| ProductID | CustomerID | TotalSales |
+-----------+------------+------------+
|         1 |          1 |      30.00 |
|         1 |          2 |      15.00 |
|         1 |          3 |      45.00 |
|         2 |          2 |      25.00 |
|         2 |          3 |      50.00 |
|         3 |          1 |      40.00 |
+-----------+------------+------------+

Comments

Popular posts from this blog

KTU DBMS LAB CSL 333 BTech S5 - Dr Binu V P

KTU DBMS LAB CSL 333 Syllabus and Experiments

Creating a Database and Table ,executing queries - MySQL Lab Exercise