Lab 6
Lab 6
Instructor
Student Name
CMSID
Department
Semester
Lesson Set Introduction to limit & offset, Aggregate functions and
Delete specific data in the table
6
Purpose 1. To get a basic awareness limit & offset
2. To understand the Aggregate function
3. How and where we can use count, max, min, sum, and avg
4. Delete specific data from the table
Procedure 1. Students should read the Pre-lab Reading assignment before coming to the
lab.
2. Students should complete the Pre-lab Writing assignment before coming to
the lab.
3. Students should complete Labs 6.1 through 6.2 in sequence in the lab. Your
instructor will give further instructions on grading and completing the lab.
4. Students should complete the set of lab tasks before the next lab and get
them checked by their lab instructor.
Lab 6
2|Page
PRE-LAB READING ASSIGNMENT
The limit keyword is used to limit the number of rows returned in a query result.
It can be used in conjunction with the SELECT, UPDATE OR DELETE commands LIMIT
keyword syntax
Aggregate Functions
Aggregate functions in SQL Server are used to perform calculations on one or more
values and return the result in a single value. In SQL Server, all aggregate functions are
built-in functions that avoid NULL values except for COUNT (*). We mainly use these
functions with the GROUP BY and HAVING clauses of the SELECT statements in the
database query languages.
DBA generally used these functions for summarizing their data. When aggregate
functions are invoked with a particular set of input values multiple times, they always
return the same value. Therefore, they are also called deterministic functions. It is
noted that the aggregate functions cannot be nested, and the expression cannot be a
subquery.
Count ()
The COUNT () function returns the number of rows in a database table.
SELECT COUNT (*) AS total employees FROM employee.
Sum ()
The SUM () function returns the total sum of a numeric column.
SELECT SUM (*) AS total employees FROM employee.
Avg ()
The AVG () function calculates the average of a set of values.
SELECT AVG (*) AS total employees FROM employee.
Min ()
The MIN () aggregate function returns the lowest value (minimum) in a set of non-NULL
values.
3|Page
SELECT MIN (*) AS total employees FROM employee.
Max ()
The MAX () aggregate function returns the highest value (maximum) in a set of non-
NULL values.
SELECT MAX (*) AS total employees FROM employee.
Delete The SQL DELETE Query is used to delete the existing records from a table.
You can use the WHERE clause with a DELETE query to delete the selected rows,
otherwise, all the records would be deleted.
Syntax
The basic syntax of the DELETE query with the WHERE clause is as follows −
DELETE FROM table name
WHERE [condition].
If you want to DELETE all the records from the CUSTOMERS table, you do not need to
use the WHERE clause and the DELETE query would be as follows –
4|Page
Pre-lab writing assignment
Fill in the blanks 1. _____________ functions in SQL Server are used to perform
calculations on one or more values and return the result in a single
value
2. The SQL ________ Query is used to delete the existing records from a
table.
5|Page
Lab 6.2 Lab Tasks
1. Create a new table with the named employee and add 5 columns with constraints. The column
should be id, name, age, gender, and salary.
2. Write a query to find the first 3 employees with a where clause to find only male employees
5. Write a query to the record of the employee who has the lowest salary.
NOTE: This question is out of topic you need to learn this by yourself. First, understand what is commit
and rollback in SQL and then perform this question.
6. Roll back only the last changes you made in the database.
6|Page