You are on page 1of 3

JAVA ASSIGNMENT

SQL QUERIES

Create University Table


-- CREATE TABLE employee_management.University (
-- UniversityId INT PRIMARY KEY AUTO_INCREMENT,
-- Name VARCHAR(255) NOT NULL,
-- Address VARCHAR(255)
-- );

Procedure created for University


-- DELIMITER //
-- CREATE PROCEDURE InsertUniversity(IN UnivName VARCHAR(255), IN UnivAddress
VARCHAR(255))
-- BEGIN
-- INSERT INTO University (Name, Address) VALUES (UnivName, UnivAddress);
-- END //
-- DELIMITER ;

Adding Universities
-- CALL InsertUniversity('University1', '123 University Street');
-- CALL InsertUniversity('University2', '456 University Street');
-- CALL InsertUniversity('University3', '789 University Street');

Create Student Table


-- CREATE TABLE employee_management.Student (
-- ID INT PRIMARY KEY AUTO_INCREMENT,
-- Name VARCHAR(255) NOT NULL,
-- DOB DATE,
-- Email VARCHAR(255),
-- Contact VARCHAR(255),
-- Address VARCHAR(255),
-- UniversityId INT,
-- FOREIGN KEY (UniversityId) REFERENCES University(UniversityId)
-- );

Procedure for Student Table


-- DELIMITER //
-- CREATE PROCEDURE Insert20Students()
-- BEGIN
-- DECLARE counter INT DEFAULT 1;
-- WHILE counter <= 20 DO
-- CALL InsertStudent(
-- CONCAT('Student', counter),
-- '1990-01-01',
-- CONCAT('student', counter, '@example.com'),
-- '1234567890',
-- CONCAT('Address', counter),
-- 1
-- );
-- SET counter = counter + 1;
-- END WHILE;
-- END //

-- DELIMITER ;

//Call the 20 Students


-- CALL Insert20Students();

//Function to return the total Students


-- DELIMITER //
-- CREATE FUNCTION TotalStudents() RETURNS INT
-- READS SQL DATA
-- BEGIN
-- DECLARE total_students INT;
-- SELECT COUNT(*) INTO total_students FROM Student;
-- RETURN total_students;
-- END //
-- DELIMITER ;

-- SELECT TotalStudents();

//Display all Student


-- SELECT * FROM Student WHERE UniversityId = (SELECT UniversityId FROM University
WHERE Name = 'University1');

Using Joins to find Students in a particular university


-- SELECT s.ID, s.Name, s.DOB, s.Email, s.Contact, s.Address, u.Name AS UniversityName
-- FROM Student s
-- JOIN University u ON s.UniversityId = u.UniversityId
-- WHERE u.Name = 'University1';
CODING QUESTIONS

You might also like