Professional Documents
Culture Documents
SQL - Guided Practice
SQL - Guided Practice
Important Instructions:
Coverage:
1. DDL_DML_DQL_DCL_TCL
2. Constraints
3. Operators
4. Functions
DDL_DML_DQL_DCL_TCL
Scenario: ABC College wants to develop a University Management System (UMS) to store information on
students who join their college. The database should contain
Student’s personal information as well as student’s academic details.
It should store information on subjects taught at the university in various departments.
It should also store marks obtained by each student in each semester subject wise as well as the
GPA for each semester.
GPA is a rating calculated in a scale of ten considering the individual subject marks obtained and subjects
weightage % in a semester.
Now it is time to develop the required table structure, but the constraint from client is that all queries must
be written using only ANSI SQL Syntax.
Problem Statement:
Using the following and identify the tables and columns needed for University Management System.
Student information should have registration_number, name, branch, contact #, DOB, Date of
joining, Address, Email id.
Information on subjects like subject code, subject name and weightage for calculating GPA.
Student’s marks scored in each subject, semester wise.
Finally overall result of the student comprising GPA scored for a semester, scholarship eligibility.
Associates should use the above case and identify the tables and columns for building the UMS system.
IMPORTANT NOTE: The number of subjects varies from semester to semester and the university also
changes the number of subjects or swaps the subjects in a particular semester. The database table
design should be in such a way that any new subject additions or subject removal should not impact the
database design that is I should not add columns or alter tables. The design should be flexible for
changes.
Page 1
C2: Protected
Guided Practice Exercises – SQL
i. Create Student_Info_<employee_id> table – This table is used for storing the student personal
information.
a. Reg_Number –Varchar
b. Student_Name - Varchar(30)
c. Branch – Varchar
d. Contact_Number – Varchar
e. Date_of_Birth-Date,
f. Date_of_Joining-Date
g. Address-Varchar(250)
h. Email_id-Varchar(250)
ii. Create Subject_Master_<employee_id> table – This table is used for storing the subjects’
information which are delivered in the university.
a. Subject_Code--varchar2(10)
b. Subject_Name- Varchar,
c. Weightage- Number(3),
iii. Create Student_Marks_<employee_id>table -- This table holds the marks obtained by a student
in a particular subject in a semester. The marks are stored as records in this table. For example if
a student S1 scores 50% in networks and 70% in microprocessor in semester 4. The table will
have two records
Reg Number Subject Code Semester Marks
S1 NWS 4 50
S1 MIC 4 70
Any new subject addition does not need a change in table design all it needs a new subject code and a
new row in this table.
a. Reg_Number - Varchar
b. Subject_Code - varchar2(10)
c. Semester-Number(3)
d. Marks-Number (3)
Deliverables Expected:
All Tables creation as per details mentioned above
NOTE: Use the data mentioned in Appendix1 section to load the tables.
Page 2
C2: Protected
Guided Practice Exercises – SQL
Deliverables Expected:
Appendix 1:
Student_Info Table_<employee_id>:
Subject_Master table_<employee_id>: Green ones are semester 1 and blue ones are semester 2
subjects.
EE01DCF DCF 30
EC02MUP Microprocessor 40
MB03MAR Marketing 20
Techniques
Page 3
C2: Protected
Guided Practice Exercises – SQL
EI05IP Instrumentation 40
Precision
For students James and Manio records needs to be inserted for semester 1 for the first three subjects in
the subject_master tables.
For the remaining students the marks needs to be inserted for all the subjects for the all the semesters.
MC1013 EE01DCF 1 75
01
MC1013 EC02MUP 1 65
01
MC1013 MC06DIP 1 70
01
BEC111 EE01DCF 1 55
402
BEC111 EC02MUP 1 80
402
BEC111 MC06DIP 1 60
402
BEEI101 EE01DCF 1 85
204
BEEI101 EC02MUP 1 78
204
BEEI101 MC06DIP 1 80
204
BEEI101 MB03MAR 2 75
204
BEEI101 EI05IP 2 65
204
BEEI101 CPSC02DS 2 75
204
Page 4
C2: Protected
Guided Practice Exercises – SQL
MB1113 EE01DCF 1 65
05
MB1113 EC02MUP 1 68
05
MB1113 MC06DIP 1 63
05
MB1113 MB03MAR 2 85
05
MB1113 EI05IP 2 74
05
MB1113 CPSC02DS 2 62
05
Student_Results_<employee_id>: The 5 student results needs to be calculated for the semester and
stored. For data per se load the table with some arbitrary GPA.
MC101301 1 7.5 Y
BEC111402 1 7.1 Y
BEEI101204 1 8.3 Y
BEEI101204 2 6.9 N
MB111305 1 6.5 N
MB111305 2 6.8 N
Page 5
C2: Protected
STUDENT_COURSES
Constraints
Exercise 1:
Create a table named “COURSE_INFO” & “Student_Info” with following column name, data type, data
size, and following constraints:
COURSE_CODE – PRIMARY KEY
COURSE_NAME – NOT NULL.
STUDENT_ID –PRIMARY KEY
Column Name Data Type Data Size
COURSE_CODE varchar 10
COURSE_NAME varchar 20
COURSE_DESCRIPTION varchar 25
COURSE_START_DATE Date
COURSE_DURATION int
NO_OF_PARTICIPANTS int
COURSE_TYPE Char(3)
STUDENT_ID varchar 10
FIRST_NAME varchar 20
LAST_NAME varchar 25
Exercise2:
STUDENT_ID varchar 10
COURSE_CODE varchar 20
Page 6
C2: Protected
Guided Practice Exercises – SQL
Exercise3:
Create a table for the CMS application where the course fees are maintained.
Create a table Course_Fees with the following columns and CHECK constraints
Add the following constraints:
Course_Code - FOREIGN KEY referencing Course_Info tables Course_Code column.
Base_Fees should be greater than 15000
Base_Fees should be greater than Special_Fees.
Discount should be between 5 and 15 %.
Course Fees
COURSE_CODE varchar 10
BASE_FEES int 10
SPECIAL_FEES int 10
DISCOUNT int 5
Operators
Prerequisite 1:
Associates should ensure that the tables specified in the document are available in the My SQL
database, with each table followed by the employee ID.
Pre-requisite 2:
Load the table with necessary data using the DML statements.
Input Data:
<EMPLOYEE_ID>.COURSE_INFO
COURSE_NAME varchar2 20
COURSE_DESCRIPTION varchar2 25 0
COURSE_START_DATE Date
Page 7
C2: Protected
Guided Practice Exercises – SQL
COURSE_DURATION Number
NO_OF_PARTICIPANTS Number
COURSE_TYPE Char(3)
<EMPLOYEE_ID>.STUDENT_INFO
FIRST_NAME varchar2 20
LAST_NAME varchar2 25
<EMPLOYEE_ID>.STUDENT_COURSES
Page 8
C2: Protected
Guided Practice Exercises – SQL
<EMPLOYEE_ID>. COURSE_FEES
BASE_FEES Number 10
SPECIAL_FEES Number 10
DISCOUNT Number 5
Exercise1:
Calculate the total fees (base fees + Special fees) for the all the courses and display the course code
along with the total fees.
Exercise2:
Calculate the discount fees for all the courses and display the course code and discount fees.
Discount fees = discount* (base fees + Special fees)/100
[Hint: Use the course_fees table for this.]
Exercise3:
Display the names of all the courses, the course duration of which is greater than 10 and number
of participants is less than 20.
[Hint: Use the courses_info table for this.]
Exercise4:
Display the course code whose base fees are greater than 100 or special fees are less than
1000.
[Hint: Use the course_fees table for this.]
Exercise5:
Exercise6:
Page 9
C2: Protected
Guided Practice Exercises – SQL
Display the students’ ID, first name whose first name is different from their last name.
[Hint: Use the student_info table for this.]
Exercise7:
Select all the courses whose base fee is in the range 100 and 3000.
[Hint: Use the course_fees table for this.]
Exercise8:
Display the students ID, and first name, whose first name starts with ‘A’
[Hint: Use the student_info table for this.]
Exercise9:
Display the students ID, first name whose first name has a character ‘o’
[Hint: Use the student_info table for this.]
Exercise10:
Display the names of all the courses where the course description is Null.
[Hint: Use the courses_info table for this.]
Functions
This case study is to develop a Course Management System (CMS) for ABC University. The following are
the two use cases for which the database needs to be designed.
Add Course:
Retrieve Course:
The courses to be added will have the following attributes: Course Code, Course Name, Number of
participants, Course Description, Course Duration, Course start date, and Course Type.
Insert 2 records in course_fees table with base fees as 300 and 175.
Page 10
C2: Protected
Guided Practice Exercises – SQL
Exercise1: Write a query which will display the total number of records in Course_Info table.
Exercise2: Develop a query which will gives the sum of all base fees of all courses in the
Course_Fees table.
Pre-requisite: We will use the Course_Info and Course_Fees tables for doing this lend a hand. Add a
new column Infra_Fees in course_fees with type number(5,3). For all the records in update the
Infra_Fees with some values say 45.751, 43.453 etc.
Hints:
Exercise3: Develop a query which will display the course name and course Infra fees of all the course.
The infra fee should be rounded to one decimal point.
Exercise4: Develop a query which will list all the course code and course names in Course_Info table
where in the first letter should be capital letter.
Exercise5: Develop a query which will display all the Course Name in upper case.
Exercise6: Develop a query which will display all the characters between 1 and 3 of the Course
Description column for all the courses in the Course_Info table.
Pre-requisite:
• Insert 2 records in course_fees table with base fees as 300 and 175.
• Insert 3 records in course_info table each course with course type CLR,EL, OF
Exercise7:
Write a query which will convert Student_Info's Student_Id to Number and add 100000 and display it for
all the students in the Student_Info table.
Exercise8:
Write a query which will convert Base_Fees into Varchar from the Course_Fees table.
'The Base Fees Amount for the course name’ <Course Name>’ is ’<Base Fees>
Page 11
C2: Protected