0% found this document useful (0 votes)
15 views40 pages

DBMS Prac. Complete

The document outlines practical exercises for creating a database using SQL clauses such as ORDER BY, GROUP BY, and HAVING, along with examples of how to implement these clauses. It also covers SQL JOIN commands, detailing different types of joins (INNER, LEFT, RIGHT, FULL) and their usage in combining records from multiple tables. The document includes SQL code for creating tables, inserting data, and executing queries to retrieve and manipulate data.

Uploaded by

ubantumalik2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views40 pages

DBMS Prac. Complete

The document outlines practical exercises for creating a database using SQL clauses such as ORDER BY, GROUP BY, and HAVING, along with examples of how to implement these clauses. It also covers SQL JOIN commands, detailing different types of joins (INNER, LEFT, RIGHT, FULL) and their usage in combining records from multiple tables. The document includes SQL code for creating tables, inserting data, and executing queries to retrieve and manipulate data.

Uploaded by

ubantumalik2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Practical No.

- 4

Objective: -Create a database using the following SQL Clauses:

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:

CREATE TABLE Students ( );

StudentID INT PRIMARY KEY,

FirstName VARCHAR(20), CREATE TABLE Enrollments (

LastName VARCHAR(20), EnrollmentID INT PRIMARY KEY,

Age INT, StudentID INT,

EnrollmentDate DATE CourseID INT,

); Grade INT,

FOREIGN KEY (StudentID)


REFERENCES Students(StudentID),
CREATE TABLE Courses (
FOREIGN KEY (CourseID)
CourseID INT PRIMARY KEY,
REFERENCES Courses(CourseID)
CourseName VARCHAR(20),
);
Credits INT
9
(7, 2, 3, 80),

INSERT INTO Students (StudentID, (8, 1, 3, 91),


FirstName, LastName, Age, EnrollmentDate)
(9, 5, 2, 89);
VALUES

(1, 'Gulshan', 'Kumar', 20, '2022-08-15'),


SELECT FirstName, LastName, Age,
(2, 'Himanshu', 'Yadav', 21, '2022-09-10'), EnrollmentDate

(3, 'Khushi', 'Srivastava', 19, '2022-08-20'), FROM Students

(4, 'Manish', 'Verma', 20, '2022-09-15'), ORDER BY Age ASC;

(5, 'MD.Saquib', 'Ansari', 22, '2022-09-05');

SELECT StudentID, AVG(Grade) AS


AverageGrade
INSERT INTO Courses (CourseID,
CourseName, Credits) FROM Enrollments

VALUES GROUP BY StudentID;

(1, 'Mathematics', 4),

(2, 'Physics', 3), SELECT StudentID, AVG(Grade) AS


AverageGrade
(3, 'Chemistry', 3),
FROM Enrollments
(4, 'Computer Science', 5);
GROUP BY StudentID

HAVING AVG(Grade) > 85;


INSERT INTO Enrollments (EnrollmentID,
StudentID, CourseID, Grade)

VALUES SELECT StudentID, AVG(Grade) AS


AverageGrade
(1, 1, 1, 85),
FROM Enrollments
(2, 1, 2, 90),
GROUP BY StudentID
(3, 2, 1, 88),
HAVING AVG(Grade) > 85
(4, 3, 2, 78),
ORDER BY AverageGrade DESC;
(5, 4, 3, 92),

(6, 5, 4, 95),
10
Results:

ORDER BY Age ASC:

GROUP BY Student ID:

GROUP BY StudentID
HAVING AVG(Grade) > 85

GROUP BY StudentID
HAVING AVG(Grade) > 85
ORDER BY AverageGrade DESC;

11
Practical No.- 5

Objective: -Create a database using JOIN SQL Commands.

Theory:

A SQL JOIN clause is used to combine rows from two or more tables, based on a related column
between them.

Types of SQL JOINs

 (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;

CREATE TABLE Students (


StudentID INT PRIMARY KEY,
FirstName VARCHAR(20),
LastName VARCHAR(20),
Age INT,
EnrollmentDate DATE
);

CREATE TABLE Courses (


CourseID INT PRIMARY KEY,
CourseName VARCHAR(20),
Credits INT
);

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)
);

INSERT INTO Students (StudentID, FirstName, LastName, Age, EnrollmentDate)


VALUES
(1, 'Gulshan', 'Kumar', 20, '2022-08-15'),
(2, 'Ajay', 'Singh', 21, '2023-09-10'),
(3, 'Ravikant', 'Yadav', 22, '2023-07-20'),
(4, 'Ashutosh', 'Singh', 23, '2023-08-15'),
(5, 'Abdul', 'Bhai', 20, '2023-09-05');

INSERT INTO Courses (CourseID, CourseName, Credits)


VALUES
(101, 'Mathematics', 3),
(102, 'Physics', 4),
(103, 'Chemistry', 3),
(104, 'Computer Science', 5);

INSERT INTO Enrollments (EnrollmentID, StudentID, CourseID, Grade)


VALUES
(1, 1, 101, 85.50),
(2, 1, 102, 90.00),
(3, 2, 103, 88.00),
(4, 3, 102, 78.00),
(5, 4, 103, 92.50),
(6, 5, 104, 95.00),
(7, 2, 103, 80.00),
(8, 1, 103, 91.00),
(9, 5, 102, 89.50);

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;

SELECT Students.FirstName, Students.LastName, Courses.CourseName


FROM Students
LEFT JOIN Enrollments ON Students.StudentID = Enrollments.StudentID
LEFT JOIN Courses ON Enrollments.CourseID = Courses.CourseID;

SELECT Students.FirstName, Students.LastName, Courses.CourseName


FROM Students
RIGHT JOIN Enrollments ON Students.StudentID = Enrollments.StudentID
RIGHT JOIN Courses ON Enrollments.CourseID = Courses.CourseID;

SELECT Students.FirstName, Students.LastName, Courses.CourseName


FROM Students
FULL OUTER JOIN Enrollments ON Students.StudentID = Enrollments.StudentID
FULL OUTER JOIN Courses ON Enrollments.CourseID = Courses.CourseID;

SELECT Students.FirstName, Students.LastName, Courses.CourseName


FROM Students
CROSS JOIN Courses;

Results:

INNER JOIN:

14
LEFT JOIN:

RIGHT JOIN:

CROSS JOIN:

15

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy