SQL AGGREGATE FUNCTIONS Advanced SQL Week 41 SQL Aggregate functions COUNT MIN MAX SUM AVG
SQL AGGREGATE FUNCTIONS SQL provides serval built-in numeric functions COUNT: The number of rows containing non-null values MIN: The minimum attribute value encountered in a given column MAX: The maximum attribute value encountered in a given column SUM: The sum of all values for a givenColumn AVG: The arithmetic mean (average) for a specified column
SQL AGGREGATE FUNCTIONS Examples If want to know the number of rows in the employee table we can use: SELECT COUNT(*) FROM employee;
SQL AGGREGATE FUNCTIONS Examples SELECT MIN(hours) AS minimumHours, MAX(hours) AS maximumHours, AVG(hours) AS averageHours FROM project WHERE projectID > 7;
Exercise Write an SQL query that Lists the number of products? Write an SQL query that calculates the total price of all products bought before 2013.11.20? Write an SQL query that calculates the average price? write an SQL query that lists all departments with the number of employee in each department?. Write an SQL query that lists the row with highest price?. Write SQL query that lists the row with lowest price?
Answers 1. SELECT COUNT(*) FROM product; 2. SELECT SUM( p_price ) AS total_price_before_2013 FROM product; 3. SELECT AVG( p_price ) FROM product SQL AGGREGATE FUNCTIONS
Answers 4. SELECT did, COUNT( eid ) AS numberOfEmploys FROM works_in GROUP BY did; 5. SELECT p_code, p_descript, MAX( p_price ) AS maximum_price FROM product; 6. SELECT p_code, p_descript, MIN( p_price ) AS minimum_price