Aggregate functions are often used with the GROUP BY clause of the SELECT statement.
The GROUP BY clause splits the result-set into groups of values and the aggregate function can be used to return a single value for each group.
Size: 365.25 KB
Language: en
Added: Oct 29, 2025
Slides: 24 pages
Slide Content
SQL LAB Ms Simmi S Assistant Professor Department of Computer Science UG Kristu Jayanti(Deemed To Be University Simmi S,Dept of CS
SQL Aggregate Functions An aggregate function is a function that performs a calculation on a set of values, and returns a single value. Aggregate functions are often used with the GROUP BY clause of the SELECT statement. The GROUP BY clause splits the result-set into groups of values and the aggregate function can be used to return a single value for each group. Simmi S,Dept of CS
SQL Aggregate Functions The most commonly used SQL aggregate functions are: MIN() - returns the smallest value within the selected column MAX() - returns the largest value within the selected column COUNT() - returns the number of rows in a set SUM() - returns the total sum of a numerical column AVG() - returns the average value of a numerical column Simmi S,Dept of CS
SQL Aggregate Functions MIN() - returns the smallest value within the selected column SELECT MIN( column_name ) FROM table_name WHERE condition ; SELECT MIN (Marks) AS Lowest_Marks FROM Marks; Simmi S,Dept of CS
SQL Aggregate Functions MAX() - returns the largest value within the selected column SELECT MAX( column_name ) FROM table_name WHERE condition ; SELECT MAX (Marks) AS Highest_Marks FROM Marks; Simmi S,Dept of CS
SQL Aggregate Functions COUNT() - returns the number of rows in a set SELECT COUNT ( column_name ) FROM table_name WHERE condition ; SELECT COUNT(*) AS Total_Students FROM Student; Simmi S,Dept of CS
SQL Aggregate Functions SUM() - returns the total sum of a numerical column SELECT SUM( column_name ) FROM table_name WHERE condition ; SELECT SUM (Marks) AS Total_Marks FROM Marks; Simmi S,Dept of CS
SQL Aggregate Functions AVG() - returns the average value of a numerical column SELECT AVG( column_name ) FROM table_name WHERE condition ; SELECT AVG (Marks) AS Average_Marks FROM Marks; Simmi S,Dept of CS
The SQL LIKE Operator The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign % represents zero, one, or multiple characters The underscore sign _ represents one, single character Simmi S,Dept of CS
The SQL LIKE Operator SELECT column1, column2, ... FROM table_name WHERE columnN LIKE pattern ; Select all customers that starts with the letter "a": SELECT * FROM Customers WHERE CustomerName LIKE 'a%' ; Simmi S,Dept of CS
The SQL LIKE Operator Return all customers that ends with the pattern 'es': SELECT * FROM Customers WHERE CustomerName LIKE '%es’ ; Return all customers that contains the pattern ' mer ': SELECT * FROM Customers WHERE CustomerName LIKE '% mer %’ ; Return all customers with a City starting with any character, followed by " ondon ": SELECT * FROM Customers WHERE City LIKE '_ ondon ' ; Simmi S,Dept of CS
The SQL IN Operator The IN operator allows you to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions. SELECT column_name (s) FROM table_name WHERE column_name IN ( value1 , value2 , ...); Return all customers from 'Germany', 'France', or 'UK' SELECT * FROM Customers WHERE Country IN ( 'Germany' , 'France' , 'UK' ); Simmi S,Dept of CS
The SQL BETWEEN Operator The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates. The BETWEEN operator is inclusive: begin and end values are included. SELECT column_name (s) FROM table_name WHERE column_name BETWEEN value1 AND value2; Selects all products with a price between 10 and 20: SELECT * FROM Products WHERE Price BETWEEN 10 AND 20 ; Simmi S,Dept of CS
The SQL ORDER BY The ORDER BY keyword is used to sort the result-set in ascending or descending order. SELECT column1 , column2, ... FROM table_name ORDER BY column1, column2, ... ASC | DESC ; SELECT * FROM Products ORDER BY Price; Simmi S,Dept of CS
GROUP BY The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country". The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns. Simmi S,Dept of CS
GROUP BY The following SQL statement lists the number of customers in each country: SELECT COUNT ( CustomerID ), Country FROM Customers GROUP BY Country; SELECT column_name (s) FROM table_name WHERE condition GROUP BY column_name (s) ORDER BY column_name (s); Simmi S,Dept of CS
The SQL HAVING Clause The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions. SELECT column_name (s) FROM table_name WHERE condition GROUP BY column_name (s) HAVING condition ORDER BY column_name (s); Simmi S,Dept of CS
The SQL HAVING Clause SELECT Stud_Dept , COUNT(*) AS Num_Students FROM student GROUP BY Stud_Dept HAVING COUNT(*) > 3; Simmi S,Dept of CS
SQL JOIN A JOIN clause is used to combine rows from two or more tables, based on a related column between them. (INNER) JOIN : Returns records that have matching values in both tables LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the right table RIGHT (OUTER) JOIN : Returns all records from the right table, and the matched records from the left table FULL (OUTER) JOIN : Returns all records when there is a match in either left or right table Simmi S,Dept of CS
SQL JOIN Simmi S,Dept of CS
I NNER JOIN : Returns records that have matching values in both tables Simmi S,Dept of CS
LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the right table. Simmi S,Dept of CS
RIGHT (OUTER) JOIN : Returns all records from the right table, and the matched records from the left table. Simmi S,Dept of CS
FULL (OUTER) JOIN : Returns all records when there is a match in either left or right table Simmi S,Dept of CS