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

Exercises DDL-DML v1

1. The document provides instructions for hands-on SQL exercises involving data definition language (DDL), data manipulation language (DML), and basic SQL queries. 2. The exercises involve creating a database and table, importing data, altering the table structure, deleting and inserting records, updating fields, and writing SQL queries to select, group, filter, and aggregate data. 3. The goal is to practice common SQL tasks like creating and modifying database objects, manipulating data, and writing queries to analyze the student data.

Uploaded by

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

Exercises DDL-DML v1

1. The document provides instructions for hands-on SQL exercises involving data definition language (DDL), data manipulation language (DML), and basic SQL queries. 2. The exercises involve creating a database and table, importing data, altering the table structure, deleting and inserting records, updating fields, and writing SQL queries to select, group, filter, and aggregate data. 3. The goal is to practice common SQL tasks like creating and modifying database objects, manipulating data, and writing queries to analyze the student data.

Uploaded by

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

Hands-on Exercises 1 (DDL, DML and Basic SQL)

Instructions

DDLS

1. Create database named DB_Student


2. Import student table from student.csv file in DB_Student database
3. Alter the table based on the constraints assigned above and 3.1 below
3.1 Add another column, HomeAdrs, character size 30

ALTER TABLE studentv1 MODIFY COLUMN `StudID` `StudID` CHAR(8) not null;

ALTER TABLE studentv1 ADD PRIMARY KEY (`StudID`);

ALTER TABLE studentv1 MODIFY COLUMN StudLName VARCHAR(15) not null;

ALTER TABLE studentv1 MODIFY COLUMN StudFName VARCHAR(15) not null;

ALTER TABLE studentv1 MODIFY COLUMN StudMI CHAR(1);

ALTER TABLE studentv1 MODIFY COLUMN Status CHAR(2);

ALTER TABLE studentv1 ADD CONSTRAINT check_Status CHECK (Status in ('R','IR'));

ALTER TABLE studentv1 MODIFY COLUMN Sex CHAR(1);

ALTER TABLE.studentv1 ADD CONSTRAINT check_sex CHECK (Sex in ('F','M'));

ALTER TABLE studentv1 MODIFY COLUMN Course CHAR(5) default ‘BSIT’;

ALTER TABLE studentv1 ADD CONSTRAINT check_Course CHECK (Course in ('BSIT','BSCS','BSIS','BSME'));

ALTER TABLE studentv1 MODIFY COLUMN DateAdmitted DATE;

ALTER TABLE studentv1 MODIFY COLUMN GWA DOUBLE(3,2);

ALTER TABLE studentv1 ADD CONSTRAINT check_GWA CHECK (GWA BETWEEN 1 and 5);

ALTER TABLE studentv1 MODIFY COLUMN Campus VARCHAR(20);

ALTER TABLE studentv1 MODIFY COLUMN ‘TuitionBal’ ‘TuitionBalance’ DOUBLE(7,2) NULL DEFAULT 0;

ALTER TABLE studentv1 ADD HomeAdrs varchar(30);

DML

1. Delete records of BSME students


DELETE FROM db_student.studentv1 WHERE Course = 'BSME';

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.

UPDATE studentv1 SET GWA = 2.00 WHERE StudID = 12547;

UPDATE studentv1 SET GWA = 1.00 WHERE StudID = 12566;

UPDATE studentv1 SET GWA = 1.50 WHERE StudID = 12584;

UPDATE studentv1 SET GWA = 2.25 WHERE StudID = 12558;

UPDATE studentv1 SET GWA = 1.75 WHERE StudID = 12563;

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 = 'Cubao, QC' WHERE StudID = 12547;

UPDATE studentv1 SET HomeAdrs = 'Mother Ignacia, Quezon City' WHERE StudID = 12558;

UPDATE studentv1 SET HomeAdrs = 'BGC, Taguig' WHERE StudID = 12563;

UPDATE studentv1 SET HomeAdrs = 'Sta. Mesa, Manila' WHERE StudID = 12566;

UPDATE studentv1 SET HomeAdrs = 'Commonwealth, QC' WHERE StudID = 12584;

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 = 'Ususan, Taguig' WHERE StudID = 12345;


UPDATE studentv1 SET HomeAdrs = 'Fort Bonifacio, Taguig' WHERE StudID = 12346;

UPDATE studentv1 SET HomeAdrs = 'Bicutan, Taguig' WHERE StudID = 12355;

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
%’;

7. Update campus field to ‘Taguig’ if the Address has ‘Taguig’ in it

UPDATE studentv1 SET Campus = ‘Taguig’ WHERE HomeAdrs LIKE ‘%Taguig%;

8. Update campus field to ‘Manila’ if address is blank or null.

UPDATE studentv1 SET Campus = ‘Manila’ WHERE HomeAdrs = ‘ ‘ OR HomeAdrs is NULL;

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’.

SELECT CONCAT(StudFName, ‘ ‘, StudMI, ‘ ‘, StudLName) AS ‘Honor Student’, GWA FROM studentv1


WHERE GWA < 1.76;

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.

SELECT StudID, StudFName, StudLName, DateAdmitted, Campus FROM studentv1 WHERE


year(DateAdmitted) in (2017, 2019, 2021) ORDER BY year(DateAdmitted) DESC;

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.

SELECT CONCAT(StudFName, ‘ ’, StudMI, ‘ ’, StudLName) AS ‘Student Name’, Course, HomeAdrs FROM


studentv1 WHERE year(DateAdmitted) BETWEEN 2020 AND 2022 AND Campus = ‘Manila’ OR
year(DateAdmitted) < 2020 AND Campus = ‘Taguig’;

You might also like