Demo Commands
Demo Commands
USE student;
first_name VARCHAR(50),
last_name VARCHAR(50),
dob DATE,
address VARCHAR(100)
);
subject VARCHAR(50),
marks INT,
grade CHAR(1)
);
VALUES
WHERE student_id = 1;
Update the birthdate for a specific student in Student_Personal_Info
UPDATE Student_Personal_Info
WHERE student_id = 2;
SET marks = 90
WHERE student_id = 5;
COMMIT or ROLLBACK
-- Start a transaction explicitly
START TRANSACTION;
UPDATE Student_Personal_Info
WHERE student_id = 1;
COMMIT;
ROLLBACK
START TRANSACTION;
-- Update a record in Student_Personal_Info
UPDATE Student_Personal_Info
WHERE student_id = 1;
ROLLBACK;
The AS clause in SQL is used to give a column or table a temporary alias, or a different name, in the
result set of a query.
FROM Student_Personal_Info;
Example 4: Calculate the total number of students and the average marks in a specific subject
SELECT COUNT(*) AS total_students, AVG(marks) AS average_marks
FROM Student_Academic_Info
WHERE subject = 'Math';
GROUP BY clause in SQL is used to group rows that have the same
values in specified columns into summary rows, like those returned
by aggregate functions.
FROM table
SELECT clause: Specifies the columns you want to display in the result set.
GROUP BY clause: Lists the columns based on which you want to group the data.
Aggregate function(s): Perform calculations on the grouped data, like counting, summing,
averaging, finding minimum or maximum, etc.
-- Example: Count the number of students in each subject
SELECT subject, COUNT(*) AS total_students
FROM Student_Academic_Info
GROUP BY subject;
FROM table
HAVING condition;
FROM Student_Academic_Info
GROUP BY subject
-- Example: Find subjects with more than two students who scored above 90
FROM Student_Academic_Info
GROUP BY subject
-- Example: Retrieve student names in ascending order based on last names and first names
SELECT first_name, last_name
FROM Student_Personal_Info
ORDER BY last_name ASC, first_name ASC;
INTERSECT:
Retrieve the common student records that exist in both tables.
-- Example: Retrieve common student records between both tables with INTERSECT
SELECT student_id, first_name, last_name FROM Student_Personal_Info
INTERSECT
SELECT student_id, first_name, last_name FROM Student_Academic_Info;
EXCEPT:
Retrieve student records unique to the "Student_Personal_Info" table.
-- Example: Retrieve student records unique to the first table with EXCEPT
SELECT student_id, first_name, last_name FROM Student_Personal_Info
EXCEPT
SELECT student_id, first_name, last_name FROM Student_Academic_Info;
VIEWs
7.7 VIRTUAL TABLES: CREATING A VIEW (open textbook 2)
Stored procedure
DELIMITER //
BEGIN
SELECT *
FROM student_personal_info
SELECT *
FROM student_academic_info
END //
DELIMITER ;
Explanation:
Inside the procedure, we perform two SELECT statements to retrieve the student details
from the "student_personal_info" and "student_academic_info" tables based on the
provided studentID.
We use the DELIMITER command to change the delimiter temporarily to allow the use of
semicolons within the stored procedure definition.
CALL GetStudentDetails(1);