Guided Practice Exercises – SQL
Important Instructions:
Please read the document thoroughly before you code.
Please do not change the Business Requirements.
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.
Exercise1: Understand how to create Tables
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.
Exercise2: Use appropriate DDL and DML statements
Problem Statement:
Problem # 1 Creating Tables: Create following tables using My SQL Client and DDL’s.
Page 1
©Copyright 2020, Cognizant Technology Solutions, All Rights Reserved
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)
iv. Create Student_Result_<employee_id> table -- For storing the student results.
a. Reg_Number-Varchar
b. Semester-Number(3)
c. GPA-Number (5,3)
d. Is_Eligible_Scholarship char(3)
Deliverables Expected:
All Tables creation as per details mentioned above
Exercise3: Loading tables using DML:
NOTE: Use the data mentioned in Appendix1 section to load the tables.
Page 2
©Copyright 2020, Cognizant Technology Solutions, All Rights Reserved
C2: Protected
Guided Practice Exercises – SQL
a) Load student information into Student_Info table.
b) Load information on subjects into Subject_Master table.
c) Load marks obtained by students in each subject in each semester into Student_Marks table.
d) Load the GPA of the student obtained in each semester into Student_Result table along with the
information whether the student is eligible for scholarship or not.
Deliverables Expected:
All Tables creation as per details mentioned above
Appendix 1:
Student_Info Table_<employee_id>:
Reg_Nu Student_Nam Branch Contact_Num Date_of Date_of_ Address Email_id
mber e ber _Birth Joining
MC1013 James MCA 9714589787 12-jan- 08-jul- No james.mc
01 1984 2010 10,South a@yahoo.
Block,Nive com
a
BEC111 Manio ECE 8912457875 23-feb- 25-jun- 8/12,Park manioma
402 1983 2011 View,Siee @gmail.co
ra m
BEEI101 Mike EI 8974567897 10-feb- 25-aug- Cross mike.jame
204 1983 2010 villa,NY
[email protected] om
MB1113 Paulson MBA 8547986123 13-dec- 08-aug- Lake paul.son
05 1984 2010 view,NJ @rediffma
il.com
Subject_Master table_<employee_id>: Green ones are semester 1 and blue ones are semester 2
subjects.
Subject_Cod Subject_Name Weightage
e
EE01DCF DCF 30
EC02MUP Microprocessor 40
MC06DIP Digital Image 30
Processing
MB03MAR Marketing 20
Techniques
Page 3
©Copyright 2020, Cognizant Technology Solutions, All Rights Reserved
C2: Protected
Guided Practice Exercises – SQL
EI05IP Instrumentation 40
Precision
CPSC02DS Data Structures 40
Student_Marks_<employee_id>: The marks need to be loaded as follows.
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.
Reg Subject Code Semest Marks
Number er
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
©Copyright 2020, Cognizant Technology Solutions, All Rights Reserved
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.
Reg Number Semester CGPA Is_Eligible_Sc
holarship
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
©Copyright 2020, Cognizant Technology Solutions, All Rights Reserved
C2: Protected
STUDENT_COURSES
Guided Practice Exercises – SQL
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)
Column Name Data Type Data Size
STUDENT_ID varchar 10
FIRST_NAME varchar 20
LAST_NAME varchar 25
ADDRESS varchar 150
Exercise2:
Create a table named Student_Courses with the following FOREIGN KEYs:
Student_Id – FOREIGN KEY referencing Student_Info table’s Student_id column.
Course Code - FOREIGN KEY referencing Course_Info table’s Course_Code column
Student Courses
Column Name Data Type Data Size
STUDENT_ID varchar 10
COURSE_CODE varchar 20
Page 6
©Copyright 2020, Cognizant Technology Solutions, All Rights Reserved
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
Column Name Data Type Data Size
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
Column Name Data Type Data Size
COURSE_CODE varchar2 10 Primary Key
COURSE_NAME varchar2 20
COURSE_DESCRIPTION varchar2 25 0
COURSE_START_DATE Date
Page 7
©Copyright 2020, Cognizant Technology Solutions, All Rights Reserved
C2: Protected
Guided Practice Exercises – SQL
COURSE_DURATION Number
NO_OF_PARTICIPANTS Number
COURSE_TYPE Char(3)
<EMPLOYEE_ID>.STUDENT_INFO
Column Name Data Type Data
Size
STUDENT_ID varchar2 10 Primary
Key
FIRST_NAME varchar2 20
LAST_NAME varchar2 25
ADDRESS varchar2 150
<EMPLOYEE_ID>.STUDENT_COURSES
Column Name Data Type Data
Size
STUDENT_ID varchar2 10 Foreign
Key
COURSE_CODE varchar2 20 Foreign
Key
Page 8
©Copyright 2020, Cognizant Technology Solutions, All Rights Reserved
C2: Protected
Guided Practice Exercises – SQL
<EMPLOYEE_ID>. COURSE_FEES
Column Name Data Type Data
Size
COURSE_CODE varchar2 10 Foreign
key
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:
Select all the courses whose base fee > 200.
[Hint: Use the course_fees table for this.]
Exercise6:
Page 9
©Copyright 2020, Cognizant Technology Solutions, All Rights Reserved
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
Case Study Scenario:
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:
To add the course details into the course management system.
Retrieve Course:
Retrieve the courses stored in the system and display it.
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.
Pre-requisite: Use the Course_Info and Course_Fees table.
Insert 2 records in course_fees table with base fees as null.
Insert 2 records in course_fees table with base fees as 300 and 175.
Page 10
©Copyright 2020, Cognizant Technology Solutions, All Rights Reserved
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:
Use joins wherever needed
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:
Use the Course_Info and Course_Fees table.
• Insert 2 records in course_fees table with base fees as null.
• 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
Pre-requisite: Let us use the Student_Info and Course_Fees table.
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.
And display in the following format
'The Base Fees Amount for the course name’ <Course Name>’ is ’<Base Fees>
Page 11
©Copyright 2020, Cognizant Technology Solutions, All Rights Reserved
C2: Protected