0% found this document useful (0 votes)
41 views3 pages

SQL Sagot

The document describes creating a database and table to store student records. It inserts 10 records into the table with details like name, age, gender, and level. It then uses a while loop to fetch each record and update the level field based on the student's age, using either IF-ELSE statements to change the level to lowercase or a CASE statement to change it to uppercase. The updated levels are then printed out.

Uploaded by

Adrian Andal
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)
41 views3 pages

SQL Sagot

The document describes creating a database and table to store student records. It inserts 10 records into the table with details like name, age, gender, and level. It then uses a while loop to fetch each record and update the level field based on the student's age, using either IF-ELSE statements to change the level to lowercase or a CASE statement to change it to uppercase. The updated levels are then printed out.

Uploaded by

Adrian Andal
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/ 3

1.

Create a database named LabDB and create a table named Records and put the
following data.
Code:
CREATE TABLE Records
(Id int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255),
FirstName varchar(255),
Age int,
Gender varchar(255),
Level varchar(255)
)

INSERT INTO Records (LastName, FirstName, Age, Gender, Level)


VALUES ('Santos', 'Mark Anthony', 17, 'Male', 'Freshman');

INSERT INTO Records (LastName, FirstName, Age, Gender, Level)


VALUES ('Nonat', 'Jayson', 15, 'Male', 'Sophomore');

INSERT INTO Records (LastName, FirstName, Age, Gender, Level)


VALUES ('Valencia', 'Nicole', 20, 'Female', 'Senior');

INSERT INTO Records (LastName, FirstName, Age, Gender, Level)


VALUES ('Campos', 'Jane', 19, 'Female', 'Junior');

INSERT INTO Records (LastName, FirstName, Age, Gender, Level)


VALUES ('Morales', 'Micah', 21, 'Female', 'Senior');

INSERT INTO Records (LastName, FirstName, Age, Gender, Level)


VALUES ('Atienza', 'Eldibert', 17, 'Male', 'Sophomore');

INSERT INTO Records (LastName, FirstName, Age, Gender, Level)


VALUES ('Dela Cruz', 'Philip', 16, 'Male', 'Freshman');

INSERT INTO Records (LastName, FirstName, Age, Gender, Level)


VALUES ('Ramos', 'Loisa', 21, 'Female', 'Senior');

INSERT INTO Records (LastName, FirstName, Age, Gender, Level)


VALUES ('Galang', 'Orlean', 18, 'Male', 'Junior');

INSERT INTO Records (LastName, FirstName, Age, Gender, Level)


VALUES ('Detera', 'Chin', 16, 'Female', 'Freshman');

2 – 4. Fetch each data from table Records using a while loop and evaluate them
using IF-ELSE and CASE Statements. If the returning age is 14 to 16, change the
level to Freshman. If 17 to 19, change it to Sophomore. If 20 to 22, change it to
Junior. Lastly, if 23 to 25, change it to Senior. If the returning age is not in
the given range, change their level to ‘Unknown’.
Querying data through IF-ELSE statement should display their level to LOWERCASE.
Else, using the CASE statement should display their level to UPPERCASE.
Display the updated level of each data using a PRINT statement.
Code: (IF-ELSE and LOWERCASE)
BEGIN

DECLARE @OrderCounter INT = 1;


DECLARE @Id INT;
DECLARE @Name AS VARCHAR(50);
DECLARE @Age INT;
DECLARE @Gender AS VARCHAR(50);

This study source was downloaded by 100000846644680 from CourseHero.com on 09-17-2022 18:34:16 GMT -05:00

https://www.coursehero.com/file/151052071/SQL-SAGOTtxt/
DECLARE @Level AS VARCHAR(50);

WHILE(@OrderCounter <= 10)


BEGIN

SET @Id = (SELECT Id FROM Records WHERE ID = @OrderCounter);


SET @Name = (SELECT CONCAT(FirstName,' ',LastName) FROM Records WHERE
ID = @OrderCounter);
SET @Age = (SELECT Age FROM Records WHERE ID = @OrderCounter);
SET @Gender = (SELECT Gender FROM Records WHERE ID = @OrderCounter);
SET @Level = (SELECT Level FROM Records WHERE ID = @OrderCounter);

IF(@Age BETWEEN 14 and 16)


BEGIN

UPDATE Records SET Level = LOWER('Freshman') WHERE Id =


@OrderCounter;
END

ELSE IF(@Age BETWEEN 17 and 19)


BEGIN
UPDATE Records SET Level = LOWER('Sophomore') WHERE Id =
@OrderCounter;
END

ELSE IF(@Age BETWEEN 20 and 22)


BEGIN
UPDATE Records SET Level = LOWER('Junior') WHERE Id =
@OrderCounter;

END

ELSE IF(@Age BETWEEN 23 and 25)


BEGIN
UPDATE Records SET Level = LOWER('Senior') WHERE Id =
@OrderCounter;

END

ELSE
BEGIN
UPDATE Records SET Level = 'Unknown' WHERE Id = @OrderCounter;
END

PRINT 'Id: ' + CONVERT(varchar(10),@Id) + ' | Name: ' + @Name + ' | Age: ' +
CONVERT(varchar(10),@Age) + ' | Gender: ' + @Gender + ' | Level: ' + @Level;
SET @OrderCounter += 1;

END
END

Code: (CASE and UPPERCASE)


BEGIN

DECLARE @OrderCounter INT = 1;


DECLARE @Id INT;

This study source was downloaded by 100000846644680 from CourseHero.com on 09-17-2022 18:34:16 GMT -05:00

https://www.coursehero.com/file/151052071/SQL-SAGOTtxt/
DECLARE @Name AS VARCHAR(50);
DECLARE @Age INT;
DECLARE @Gender AS VARCHAR(50);
DECLARE @Level AS VARCHAR(50);

WHILE(@OrderCounter <= 10)


BEGIN

SET @Id = (SELECT Id FROM Records WHERE ID = @OrderCounter);


SET @Name = (SELECT CONCAT(FirstName,' ',LastName) FROM Records WHERE ID =
@OrderCounter);
SET @Age = (SELECT Age FROM Records WHERE ID = @OrderCounter);
SET @Gender = (SELECT Gender FROM Records WHERE ID = @OrderCounter);
SET @Level = (SELECT Level FROM Records WHERE ID = @OrderCounter);

SET @Level = CASE @Level


WHEN 'Freshman' THEN UPPER(@Level)
WHEN 'Sophomore' THEN UPPER(@Level)
WHEN 'Junior' THEN UPPER(@Level)
WHEN 'Senior' THEN UPPER(@Level)
ELSE 'Unknown'
END

PRINT 'Id: ' + CONVERT(varchar(10),@Id) + ' | Name: ' + @Name + ' | Age: ' +
CONVERT(varchar(10),@Age) + ' | Gender: ' + @Gender + ' | Level: ' + @Level;
SET @OrderCounter += 1;

END

END

This study source was downloaded by 100000846644680 from CourseHero.com on 09-17-2022 18:34:16 GMT -05:00

https://www.coursehero.com/file/151052071/SQL-SAGOTtxt/
Powered by TCPDF (www.tcpdf.org)

You might also like