DBMS Prac. Complete
DBMS Prac. Complete
- 4
I. ORDER BY
II. GROUP BY
III. HAVING Clause
Theory:
SQL clauses help us to retrieve a set or bundles of records from the table. SQL clauses help us to
specify a condition on the columns or the records of a table.
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
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.
The HAVING clause was added to SQL because the WHERE keyword cannot be used with
aggregate functions.
Program:
); Grade INT,
(6, 5, 4, 95),
10
Results:
GROUP BY StudentID
HAVING AVG(Grade) > 85
GROUP BY StudentID
HAVING AVG(Grade) > 85
ORDER BY AverageGrade DESC;
11
Practical No.- 5
Theory:
A SQL 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.
Program:
CREATE DATABASE CollegeDB;
USE CollegeDB;
12
CREATE TABLE Enrollments (
EnrollmentID INT PRIMARY KEY,
StudentID INT,
CourseID INT,
Grade DECIMAL(5, 2),
FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);
13
SELECT Students.FirstName, Students.LastName, Courses.CourseName
FROM Students
INNER JOIN Enrollments ON Students.StudentID = Enrollments.StudentID
INNER JOIN Courses ON Enrollments.CourseID = Courses.CourseID;
Results:
INNER JOIN:
14
LEFT JOIN:
RIGHT JOIN:
CROSS JOIN:
15