MariaDB [toyu]> EXPLAIN WITH ma AS -> (SELECT s.major AS deptCode, COUNT(s.stuId) AS numMajor -> FROM student AS s -> GROUP BY s.major), -> mi AS -> (SELECT s.minor AS deptCode, COUNT(s.stuId) AS numMinor -> FROM student AS s -> GROUP BY s.minor), -> f AS -> (SELECT f.deptCode, COUNT(f.facId) AS numFaculty -> FROM faculty AS f -> GROUP BY f.deptCode) -> SELECT d.deptCode, -> d.deptName, -> IFNULL(f.numFaculty, 0) AS numFaculty, -> IFNULL(ma.numMajor, 0) AS numMajor, -> IFNULL(mi.numMinor, 0) AS numMinor -> FROM department AS d LEFT JOIN ma USING (deptCode) -> LEFT JOIN mi USING (deptCode) -> LEFT JOIN f USING (deptCode); +------+-------------+------------+-------+---------------------+---------------------+---------+-----------------+------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +------+-------------+------------+-------+---------------------+---------------------+---------+-----------------+------+----------------------------------------------+ | 1 | PRIMARY | d | index | NULL | Department_name_ck | 122 | NULL | 7 | Using index | | 1 | PRIMARY | | ref | key0 | key0 | 17 | toyu.d.deptCode | 2 | | | 1 | PRIMARY | | ref | key0 | key0 | 17 | toyu.d.deptCode | 2 | | | 1 | PRIMARY | | ref | key0 | key0 | 17 | toyu.d.deptCode | 2 | | | 2 | DERIVED | s | index | Student_major_fk | Student_major_fk | 17 | NULL | 11 | Using index; Using temporary; Using filesort | | 3 | DERIVED | s | index | Student_minor_fk | Student_minor_fk | 17 | NULL | 11 | Using index; Using temporary; Using filesort | | 4 | DERIVED | f | index | Faculty_deptCode_fk | Faculty_deptCode_fk | 16 | NULL | 11 | Using index; Using temporary; Using filesort | +------+-------------+------------+-------+---------------------+---------------------+---------+-----------------+------+----------------------------------------------+ 7 rows in set (0.025 sec) MariaDB [toyu]> SELECT s.schoolCode, s.schoolName, -> COUNT(d.deptCode) AS n_departments -> FROM school AS s LEFT JOIN department AS d ON (s.schoolCode = d.schoolCode) -> GROUP BY s.schoolCode, s.schoolName; +------------+-------------------------------+---------------+ | schoolCode | schoolName | n_departments | +------------+-------------------------------+---------------+ | BUS | Business | 1 | | CSE | Science and Engineering | 4 | | EDU | Education | 0 | | HSH | Human Sciences and Humanities | 2 | +------------+-------------------------------+---------------+ 4 rows in set (0.003 sec) MariaDB [toyu]> CREATE OR REPLACE VIEW school_summary( -> schoolCode, schoolName, n_departments) AS -> SELECT s.schoolCode, s.schoolName, -> COUNT(d.deptCode) AS n_departments -> FROM school AS s LEFT JOIN department AS d ON (s.schoolCode = d.schoolCode) -> GROUP BY s.schoolCode, s.schoolName; Query OK, 0 rows affected (0.013 sec) MariaDB [toyu]> MariaDB [toyu]> SHOW CREATE VIEW school_summary; +----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+ | View | Create View | character_set_client | collation_connection | +----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+ | school_summary | CREATE ALGORITHM=UNDEFINED DEFINER=`yue`@`localhost` SQL SECURITY DEFINER VIEW `school_summary` AS select `s`.`schoolCode` AS `schoolCode`,`s`.`schoolName` AS `schoolName`,count(`d`.`deptCode`) AS `n_departments` from (`school` `s` left join `department` `d` on(`s`.`schoolCode` = `d`.`schoolCode`)) group by `s`.`schoolCode`,`s`.`schoolName` | cp850 | cp850_general_ci | +----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+ 1 row in set (0.001 sec) MariaDB [toyu]> MariaDB [toyu]> DESC school_summary; +---------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+-------+ | schoolCode | char(3) | NO | | NULL | | | schoolName | varchar(30) | NO | | NULL | | | n_departments | bigint(21) | NO | | 0 | | +---------------+-------------+------+-----+---------+-------+ 3 rows in set (0.023 sec) MariaDB [toyu]> MariaDB [toyu]> SELECT * -> FROM school_summary -> WHERE n_departments > 0; +------------+-------------------------------+---------------+ | schoolCode | schoolName | n_departments | +------------+-------------------------------+---------------+ | BUS | Business | 1 | | CSE | Science and Engineering | 4 | | HSH | Human Sciences and Humanities | 2 | +------------+-------------------------------+---------------+ 3 rows in set (0.001 sec) MariaDB [toyu]> DROP VIEW school_summary; Query OK, 0 rows affected (0.002 sec) MariaDB [toyu]> MariaDB [toyu]> -- get the full name of a student. MariaDB [toyu]> DELIMITER // MariaDB [toyu]> CREATE OR REPLACE FUNCTION GetStudentFullName( -> student_id INT -> ) -> RETURNS VARCHAR(61) -> DETERMINISTIC -> BEGIN -> DECLARE full_name VARCHAR(61); -> SELECT CONCAT(fname, ' ', lname) INTO full_name -> FROM Student -> WHERE stuId = student_id; -> RETURN full_name; -> END // Query OK, 0 rows affected (0.017 sec) MariaDB [toyu]> DELIMITER ; MariaDB [toyu]> MariaDB [toyu]> SELECT GetStudentFullName(100000); +----------------------------+ | GetStudentFullName(100000) | +----------------------------+ | Tony Hawk | +----------------------------+ 1 row in set (0.003 sec) MariaDB [toyu]> SELECT GetStudentFullName(100001); +----------------------------+ | GetStudentFullName(100001) | +----------------------------+ | Mary Hawk | +----------------------------+ 1 row in set (0.001 sec) MariaDB [toyu]> MariaDB [toyu]> MariaDB [toyu]> -- get the full name of a student. MariaDB [toyu]> DELIMITER $$ MariaDB [toyu]> MariaDB [toyu]> CREATE OR REPLACE FUNCTION GetStudentFullName( -> student_id INT -> ) -> RETURNS VARCHAR(61) -> DETERMINISTIC -> BEGIN -> DECLARE full_name VARCHAR(61); -> SELECT CONCAT(fname, ' ', lname) INTO full_name -> FROM Student -> WHERE stuId = student_id; -> RETURN full_name; -> END $$ Query OK, 0 rows affected (0.017 sec) MariaDB [toyu]> DELIMITER ; MariaDB [toyu]> MariaDB [toyu]> SELECT GetStudentFullName(100000); +----------------------------+ | GetStudentFullName(100000) | +----------------------------+ | Tony Hawk | +----------------------------+ 1 row in set (0.001 sec) MariaDB [toyu]> SELECT GetStudentFullName(100001); +----------------------------+ | GetStudentFullName(100001) | +----------------------------+ | Mary Hawk | +----------------------------+ 1 row in set (0.001 sec) MariaDB [toyu]> MariaDB [toyu]> MariaDB [toyu]> -- get the full name of a student. MariaDB [toyu]> DELIMITER $$ MariaDB [toyu]> MariaDB [toyu]> CREATE OR REPLACE FUNCTION GetStudentFullName( -> student_id INT -> ) -> RETURNS VARCHAR(61) -> DETERMINISTIC -> BEGIN -> DECLARE full_name VARCHAR(61); -> SELECT CONCAT(fname, ' ', lname) -- INTO full_name -> FROM Student -> WHERE stuId = student_id; -> RETURN full_name; -> END $$ ERROR 1415 (0A000): Not allowed to return a result set from a function MariaDB [toyu]> DELIMITER ; MariaDB [toyu]> DELIMITER // MariaDB [toyu]> MariaDB [toyu]> CREATE OR REPLACE PROCEDURE deptInfo(IN dCode VARCHAR(4), OUT numFaculty INT) -> BEGIN -> -- Display some information. -> SELECT d.deptName, d.SchoolCode, t1.n_majors, t2.n_minors -> FROM department AS d INNER JOIN -> (SELECT COUNT(stuId) AS n_majors -> FROM student -> WHERE major = dCode) AS t1 INNER JOIN -> (SELECT COUNT(stuId) AS n_minors -> FROM student -> WHERE minor = dCode) AS t2 -> WHERE d.deptCode = dCode; -> -> -- MySQL does not direct sending output to console. -> -- It is necessary to use a SQL statement. -> SELECT 'Debuggin comment can be put here.'; -> SELECT CONCAT('Faculty in the department: ', dCode) AS faculty; -> -> SELECT * -> FROM faculty AS f -> WHERE f.deptCode = dCode; -> -> SELECT COUNT(f.facId) INTO numFaculty -> FROM faculty AS f -> WHERE f.deptCode = dCode; -> END // Query OK, 0 rows affected (0.009 sec) MariaDB [toyu]> MariaDB [toyu]> DELIMITER ; MariaDB [toyu]> MariaDB [toyu]> SHOW CREATE PROCEDURE deptInfo; +-----------+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+ | Procedure | sql_mode | Create Procedure | character_set_client | collation_connection | Database Collation | +-----------+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+ | deptInfo | NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION | CREATE DEFINER=`yue`@`localhost` PROCEDURE `deptInfo`(IN dCode VARCHAR(4), OUT numFaculty INT) BEGIN SELECT d.deptName, d.SchoolCode, t1.n_majors, t2.n_minors FROM department AS d INNER JOIN (SELECT COUNT(stuId) AS n_majors FROM student WHERE major = dCode) AS t1 INNER JOIN (SELECT COUNT(stuId) AS n_minors FROM student WHERE minor = dCode) AS t2 WHERE d.deptCode = dCode; SELECT 'Debuggin comment can be put here.'; SELECT CONCAT('Faculty in the department: ', dCode) AS faculty; SELECT * FROM faculty AS f WHERE f.deptCode = dCode; SELECT COUNT(f.facId) INTO numFaculty FROM faculty AS f WHERE f.deptCode = dCode; END | cp850 | cp850_general_ci | utf8mb4_general_ci | +-----------+-----------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+ 1 row in set (0.001 sec) MariaDB [toyu]> MariaDB [toyu]> SET @numFaculty = 0; Query OK, 0 rows affected (0.000 sec) MariaDB [toyu]> SET @dCode = 'CSCI'; Query OK, 0 rows affected (0.000 sec) MariaDB [toyu]> CALL deptInfo(@dCode, @numFaculty); +------------------+------------+----------+----------+ | deptName | SchoolCode | n_majors | n_minors | +------------------+------------+----------+----------+ | Computer Science | CSE | 3 | 1 | +------------------+------------+----------+----------+ 1 row in set (0.006 sec) +-----------------------------------+ | Debuggin comment can be put here. | +-----------------------------------+ | Debuggin comment can be put here. | +-----------------------------------+ 1 row in set (0.099 sec) +---------------------------------+ | faculty | +---------------------------------+ | Faculty in the department: CSCI | +---------------------------------+ 1 row in set (0.237 sec) +-------+--------+--------+----------+---------------------+ | facId | fname | lname | deptCode | rank | +-------+--------+--------+----------+---------------------+ | 1011 | Paul | Smith | CSCI | Professor | | 1012 | Mary | Tran | CSCI | Associate Professor | | 1013 | David | Love | CSCI | NULL | | 1014 | Sharon | Mannes | CSCI | Assistant Professor | +-------+--------+--------+----------+---------------------+ 4 rows in set (0.357 sec) Query OK, 1 row affected (0.525 sec) MariaDB [toyu]> SELECT @dCode, @numFaculty; +--------+-------------+ | @dCode | @numFaculty | +--------+-------------+ | CSCI | 4 | +--------+-------------+ 1 row in set (0.001 sec) MariaDB [toyu]> MariaDB [toyu]> SET @dCode = 'ITEC'; Query OK, 0 rows affected (0.000 sec) MariaDB [toyu]> CALL deptInfo(@dCode, @numFaculty); +------------------------+------------+----------+----------+ | deptName | SchoolCode | n_majors | n_minors | +------------------------+------------+----------+----------+ | Information Technology | CSE | 2 | 2 | +------------------------+------------+----------+----------+ 1 row in set (0.001 sec) +-----------------------------------+ | Debuggin comment can be put here. | +-----------------------------------+ | Debuggin comment can be put here. | +-----------------------------------+ 1 row in set (0.105 sec) +---------------------------------+ | faculty | +---------------------------------+ | Faculty in the department: ITEC | +---------------------------------+ 1 row in set (0.232 sec) +-------+----------+-------+----------+-----------+ | facId | fname | lname | deptCode | rank | +-------+----------+-------+----------+-----------+ | 1017 | Deborah | Gump | ITEC | Professor | | 1019 | Benjamin | Yu | ITEC | Lecturer | +-------+----------+-------+----------+-----------+ 2 rows in set (0.341 sec) Query OK, 1 row affected (0.487 sec) MariaDB [toyu]> SELECT @dCode, @numFaculty; +--------+-------------+ | @dCode | @numFaculty | +--------+-------------+ | ITEC | 2 | +--------+-------------+ 1 row in set (0.001 sec) MariaDB [toyu]> MariaDB [toyu]> notee