In MariaDB, both the ORDER BY and GROUP BY clauses are used to sort and group data returned by your SELECT statements. Here’s how you can use them:
ORDER BY:
The `ORDER BY` clause is used in a SELECT statement to sort the results either in ascending or descending order.
Syntax:
```
SELECT column(s)
FROM table
ORDER BY column(s) [ASC|DESC];
```
- ASC is used for ascending order and DESC for descending order. If you do not specify anything, ASC is assumed.
Example:
```
SELECT EmployeeName, Salary
FROM Employees
ORDER BY Salary DESC;
```
In this example, the statement will select the EmployeeName and Salary from the Employees table, and sort the result in descending order by Salary.
GROUP BY:
The `GROUP BY` statement is used with the aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.
Syntax:
```
SELECT column(s), aggregate_function(column)
FROM table
WHERE condition
GROUP BY column(s);
```
Example:
```
SELECT Department, COUNT
FROM Employees
GROUP BY Department;
```
In this example, it will count EmployeeID in each Department grouping.
You can also use both clauses together in a SELECT statement:
```
SELECT Department, COUNT
FROM Employees
GROUP BY Department
ORDER BY COUNT DESC;
```