Exercises DDL-DML v1
Exercises DDL-DML v1
Instructions
DDLS
ALTER TABLE studentv1 MODIFY COLUMN `StudID` `StudID` CHAR(8) not null;
ALTER TABLE studentv1 ADD CONSTRAINT check_GWA CHECK (GWA BETWEEN 1 and 5);
ALTER TABLE studentv1 MODIFY COLUMN ‘TuitionBal’ ‘TuitionBalance’ DOUBLE(7,2) NULL DEFAULT 0;
DML
2. Add 5 records populating the mandatory fields (those which must not be null and those with check
constraints). Do not populate those with default values.
INSERT INTO studentv1 (StudID, StudLName, StudFName, Status, Sex, GWA) VALUES (12563, 'Quitoriano',
'Patricia', 'R', 'F', 1.25);
INSERT INTO studentv1 (StudID, StudLName, StudFName, Status, Sex, GWA) VALUES (12547, 'Bautista',
'Jericko', 'R', 'M', 1.50);
INSERT INTO studentv1 (StudID, StudLName, StudFName, Status, Sex, GWA) VALUES (12566, 'Regudo',
'Gwen', 'R', 'F', 1.75);
INSERT INTO studentv1 (StudID, StudLName, StudFName, Status, Sex, GWA) VALUES (12558, 'Panelo',
'Rei', 'R', 'M', 1.50);
INSERT INTO studentv1 (StudID, StudLName, StudFName, Status, Sex, GWA) VALUES (12584, 'Gersaniba',
'Lerwin', 'R', 'M', 1.00);
3. Update the GWA of the 5 records just added. Provide different GWA for each of the 5 new records.
4. Update the HomeAdrs field of the 5 new records with values such as ‘Cubao, QC’, ‘Mother Ignacia,
Quezon City’, ‘BGC,Taguig’ , ‘Sta. Mesa, Manila’, ‘Commonwealth, QC’
UPDATE studentv1 SET HomeAdrs = 'Mother Ignacia, Quezon City' WHERE StudID = 12558;
UPDATE studentv1 SET HomeAdrs = 'Sta. Mesa, Manila' WHERE StudID = 12566;
5. Update 5 more HomeAdrs fields of other records with ‘Ususan, Taguig, ‘Fort Bonifacio, Taguig’, ‘Bicutan,
Taguig’, ‘Sta. Mesa Heights, QC’, ‘Gilmore Ave., QC’
UPDATE studentv1 SET HomeAdrs = 'Sta. Mesa Heights, QC' WHERE StudID = 12370;
UPDATE studentv1 SET HomeAdrs = 'Gilmore Ave., QC' WHERE StudID = 12456;
6. Update campus field to ‘QC’ if the Address has ‘QC’ or ‘Quezon City’ in it
UPDATE studentv1 SET Campus = ‘QC’ WHERE HomeAdrs LIKE ‘%QC%’ OR HomeAdrs LIKE ‘%Quezon City
%’;
SQL
1. Display concatenated name of students (first name, mid init, last name) with GWA less than 1.76. Header
for student name should be ‘Honor Students’.
2. Sum up the total tuition balance per campus and display only those greater than 2k. Include in the result
set the Campus name.
SELECT Campus, SUM(TuitionalBalance) AS ‘Total TB’ FROM studentv1 GROUP BY Campus HAVING
SUM(TuitonBalance) > 2000;
3. Count how many are the girls , boys in each course. Provide headers.
SELECT Course, Sex, COUNT(Sex) as ‘Total Students’ FROM studentv1 GROUP BY Course, Sex;
4. Count how many students with tuition balance greater than 400 are from each campus. Sort the counts
in descending order. Display the campus together with the count
SELECT Campus, COUNT(StudID) AS ‘Total’ FROM studentv1 WHERE TuitionBalance > 400 GROUP BY
Campus ORDER BY Campus DESC;
5. Average the GWA per campus. Include only the IT and CS students. Sort average . Include the campus
name in the result set. Assign the appropriate headers for the displayed columns.
SELECT Campus, AVG(GWA) AS ‘Ave GWA’ FROM studentv1 WHERE Course IN (‘BSIT’, ‘BSCS’) GROUP BY
Campus ORDER BY AVG(GWA) DESC;
6. List down the last name, first name , course, gwa, and campus of those with surnames beginning with B,
C, D in alphabetical order.
SELECT StudLName, StudFName, Course, GWA, Campus FROM studentv1 WHERE StudLName LIKE ‘B%’ OR
StudLName LIKE ‘C%’ OR StudLName LIKE ‘D%’ ORDER BY StudLName ASC;
7. Average the GWA of regular and irregular students by course. Include also the count per status by course
SELECT Status, Course, AVG(GWA) AS ‘Average’, COUNT(Status) AS ‘Count Status’ FROM studentv1
GROUP BY Status, Course;
8. Count how many are regular , irregular by sex. Sort counts in descending order
SELECT Sex, Status, COUNT(Status) AS ‘Status Count’ FROM studentv1 GROUP BY Sex, Status;
9. I am looking for students who were admitted during years 2017, 2019, 2021. Display the student id, first
name, last name, year of admission, and campus.
10. Display all records - concatenated first name, middle initial and last name with spaces in between and the
course, address of students -
10.1 who were admitted between 2020 and 2022 and who are from Manila campus; OR
10.2 students admitted before 2020 from Taguig campus.