You are on page 1of 24

LAB 5: ADVANCED SQL

STATEMENTS AND FUNCTIONS


• Objective: Dive deeper into SQL with advanced statements and
functions to manipulate and retrieve data effectively.
SQL UPDATE STATEMENT
• The UPDATE statement modifies existing records in a table.
Always ensure to use the WHERE clause to specify which
record(s) to update.
EXAMPLE: SQL UPDATE
• UPDATE Courses SET Credits = 4 WHERE CourseName =
'Algorithms';
EXERCISE: SQL UPDATE
• Modify the 'CourseName' of the course with ID 101 to 'Advanced
Databases'.
SQL DELETE STATEMENT
• The DELETE statement removes one or more records from a table.
Be cautious, and always use the WHERE clause to avoid deleting
all records.
EXAMPLE: SQL DELETE
• DELETE FROM Students WHERE Age < 18;
EXERCISE: SQL DELETE
• Remove all courses from the 'Courses' table that have less than 3
credits.
SQL SELECT TOP
STATEMENT
• The SELECT TOP clause is used to specify the number of top
records to return from a table.
EXAMPLE: SQL SELECT
TOP
• SELECT TOP 3 * FROM Students ORDER BY Age DESC;
EXERCISE: SQL SELECT
TOP
• Retrieve the top 5 courses from the 'Courses' table with the highest
credits.
SQL ORDER BY
STATEMENT
• The ORDER BY statement is used to sort the result set based on
one or more columns.
EXAMPLE: SQL ORDER
BY
• SELECT * FROM Students ORDER BY Name ASC;
EXERCISE: SQL ORDER
BY
• Retrieve all records from the 'Courses' table ordered by
'CourseName' in descending order.
SQL ALIASES
• Aliases are temporary names given to table or column for the
purpose of a specific SQL query.
EXAMPLE: SQL ALIASES
• SELECT CourseID AS ID, CourseName AS Course FROM
Courses;
EXERCISE: SQL ALIASES
• Rename 'ID' column to 'StudentID' and 'Name' column to
'StudentName' in the 'Students' table for your query result.
SQL MIN() AND MAX()
FUNCTIONS
• MIN() function returns the smallest value of the selected column.
MAX() function returns the largest value of the selected column.
EXAMPLE: MIN() AND
MAX()
• SELECT MIN(Age) AS Youngest, MAX(Age) AS Oldest FROM
Students;
EXERCISE: MIN() AND
MAX()
• Find the course with the minimum credits and the course with the
maximum credits in the 'Courses' table.
SQL COUNT(), AVG() AND
SUM() FUNCTIONS
• COUNT() returns the number of rows that matches a specified
criteria.
• AVG() returns the average value of a numeric column.
• SUM() returns the total sum of a numeric column.
EXAMPLE: COUNT(),
AVG(), SUM()
• SELECT COUNT(*) AS TotalStudents, AVG(Age) AS
AverageAge, SUM(Fees) AS TotalFees FROM Students;
EXERCISE: COUNT(),
AVG(), SUM()
• Count the total number of courses, average credits, and total credits
in the 'Courses' table.
ADVANCED SQL
COMMANDS
• Understanding these advanced SQL commands and functions is
crucial for efficient database management and data analysis.
FEEDBACK AND
QUESTIONS
• Students are encouraged to provide feedback and ask questions at
the end of the lab.

You might also like