0% found this document useful (0 votes)
18 views4 pages

Unit 3 SQL Prac Notes Ex1

The document provides SQL commands for creating tables for Students, Courses, and Enrollments, along with record insertion examples. It includes various SQL queries to retrieve specific data such as student names from a city, course details, and enrollment counts. Additionally, it demonstrates how to display student names alongside their enrolled courses and cities.

Uploaded by

unnatibariya046
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)
18 views4 pages

Unit 3 SQL Prac Notes Ex1

The document provides SQL commands for creating tables for Students, Courses, and Enrollments, along with record insertion examples. It includes various SQL queries to retrieve specific data such as student names from a city, course details, and enrollment counts. Additionally, it demonstrates how to display student names alongside their enrolled courses and cities.

Uploaded by

unnatibariya046
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/ 4

Course Name : Structured Query Language Using SQL+ (DSC-C-BCA-243-P)

Desirabe Practical Exercise = 1

.................... Table creation .....................

create table Students


(StudentID int PRIMARY KEY,
Name Varchar2(50),
Age int,
City Varchar2(20)
);

create table Courses


(CourseID int PRIMARY KEY,
CourseName Varchar2(50),
Credits int
);

create table Enrollments


(EnrollmentID int PRIMARY KEY,
StudentID int REFERENCES Students(StudentID),
CourseID int REFERENCES Courses(CourseID)
);

..................... Record insertion .......................

Insert into Students Values (1, 'Rahul', 20, 'Delhi');


Insert into Students Values (2, 'Priya', 22, 'Mumbai');
Insert into Students Values (3, 'Alok', 21, 'Bangalore');
Insert into Students Values (4, 'Neha', 19, 'Kolkata');
Insert into Students Values (5, 'Suresh', 23, 'Chennai');

Insert into Courses Values (101, 'Mathematics', 4);


Insert into Courses Values (102, 'Science', 3);
Insert into Courses Values (103, 'History', 3);
Insert into Courses Values (104, 'English', 4);
Insert into Courses Values (105, 'Geography', 3);

Insert into Enrollments values (1,1,101);


Insert into Enrollments values (2,2,102);
Insert into Enrollments values (3,3,103);
Insert into Enrollments values (4,4,104);
Insert into Enrollments values (5,5,105);

..................... Queries.......................................

1) Retrieve the names of all students who are from Delhi.

SQL> select Name from students where city = 'Delhi';

O/P=
NAME
--------------------------------------------------
Rahul

2) List the courses with their corresponding credits.

SQL> select CourseName, Credits from Courses;

O/P=
COURSENAME CREDITS
-------------------------------------------------- ----------
Mathematics 4
Science 3
History 3
English 4
Geography 3

3) Display the names and ages of students who are older than 20 years.

SQL> select name, age from students where age > 20;

O/P=
NAME AGE
-------------------------------------------------- ----------
Priya 22
Alok 21
Suresh 23

4) Show the details of courses taken by students names Priya.

SQL> select courses.courseid, courses.coursename, courses.credits


from courses, students, enrollments
where students.name = 'Priya' and
students.studentid = enrollments.studentid and
enrollments.courseid = courses.courseid;
O/P=
COURSEID COURSENAME CREDITS
---------- -------------------------------------------------- ----------
102 Science 3

5) Count the number of students enrolled in each course.

SQL> select courseid, count(studentid) from enrollments


group by courseid;

O/P=
COURSEID COUNT(STUDENTID)
---------- ----------------
102 1
101 1
104 1
105 1
103 1

6) Find the total number of credits for all courses.

SQL> select sum(credits) "total credits" from courses;

O/P=
total credits
-------------
17
7) Display the student names and the courses they are enrolled in.
SQL> select students.name, courses.coursename
from students, courses, enrollments
where students.studentid = enrollments.studentid and
enrollments.courseid = courses.courseid;
O/P=
NAME COURSENAME
--------------------------------------------------
--------------------------------------------------
Rahul Mathematics
Priya Science
Alok History
Neha English
Suresh Geography

8) Find the student(s) who are enrolled in the course 'Mathematics'.


SQL> select students.studentid, students.name, courses.coursename
from students, courses, enrollments
where courses.coursename = 'Mathematics' and
courses.courseid = enrollments.courseid and
enrollments.studentid = students.studentid;
O/P=
STUDENTID NAME COURSENAME
---------- --------------------------------------------------
--------------------------------------------------
1 Rahul Mathematics

9) List the courses alongwith the number of students enrolled in each course.

SQL> select courses.coursename, count(enrollments.studentid)


from courses join enrollments
on (courses.courseid = enrollments.courseid)
group by courses.coursename;
O/P=
COURSENAME COUNT(ENROLLMENTS.STUDENTID)
-------------------------------------------------- ----------------------------
Geography 1
Mathematics 1
Science 1
English 1
History 1

10) Show the student names, course names and cities of all students
alongwith course names they are enrolled in.

SQL> select students.name, courses.coursename, students.city


from students, courses, enrollments
where students.studentid = enrollments.studentid and
enrollments.courseid = courses.courseid;
O/P=
NAME COURSENAME
CITY
--------------------------------------------------
-------------------------------------------------- --------------------
Rahul Mathematics
Delhi
Priya Science
Mumbai
Alok History
Bangalore
Neha English
Kolkata
Suresh Geography
Chennai

***** * *****

You might also like