Dies ist eine alte Version des Dokuments!
LU04d - SQL-DQL: Selects with Aggregate Functions
SQL (Structured Query Language) provides powerful tools to perform calculations and data analysis on database records. Among these tools, aggregate functions play a crucial role in summarizing data by performing operations on multiple rows and returning a single result. This document focuses on five commonly used SQL aggregate functions: MIN, MAX, COUNT, SUM, and AVG, along with practical examples to illustrate their usage.
1. MIN Function...
The MIN function returns the smallest value in a specified column. This is particularly useful when you need to find the lowest value in a dataset, such as the minimum price of a product or the earliest date in a list of events.
Example:
SELECT MIN(price) AS LowestPrice FROM products;
In this example, the query returns the lowest price from the „products“ table.
2. MAX Function
The MAX function works oppositely to MIN, returning the largest value in a specified column. It’s commonly used to find the highest values, such as the maximum salary in a company or the latest date in a schedule.
Example:
SELECT MAX(salary) AS HighestSalary FROM employees;
Here, the query retrieves the highest salary from the „employees“ table.
3. COUNT Function
The COUNT function counts the number of rows that match a specified condition or simply counts all rows in a column. This is useful for determining the size of datasets, such as counting the number of orders placed by a customer or the total number of employees in a department.
Example:
SELECT COUNT(*) AS NumberOfOrders FROM orders WHERE customer_id = 123;
This query returns the total number of orders placed by a customer with customer_id 123.