-- 9/24 tee 2024_9_24_sql_log.txt use toyu; SELECT StuId FROM enroll; SELECT DISTINCT StuId FROM enroll; SELECT DISTINCT d.deptCode, d.deptName AS department FROM Department AS d WHERE d.schoolCode = 'CSE'; SELECT DISTINCT d.deptCode, d.deptName AS department WHERE d.schoolCode = 'CSE' FROM Department AS d; DROP SCHEMA IF EXISTS tinker; CREATE SCHEMA tinker; USE tinker; CREATE TABLE s2 SELECT * FROM toyu.student; CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)] [table_options] [partition_options] [IGNORE | REPLACE] [AS] query_expression SELECT * FROM s2; CREATE TEMPORARY TABLE s3 SELECT * FROM toyu.student; SELECT * FROM s3; CREATE TABLE s4 LIKE toyu.student; SELECT * FROM s4; INSERT INTO s4 SELECT * FROM toyu.student; SELECT * FROM s4; SHOW TABLES; -- Note that keys and constraints of student are missing in s2 and S3. DESC toyu.student; DESC s2; DESC s3; DESC s4; DROP TABLE s2; DROP TABLE s3; DROP TABLE s4; SHOW TABLES; DROP SCHEMA IF EXISTS tinker; UPDATE student SET major = 'CSCI'; UPDATE student SET major = 'CINF' WHERE lname = 'Hawk'; -- operators: -- student with credits in a range. SELECT DISTINCT * FROM Student WHERE ach BETWEEN 30 AND 70; SELECT DISTINCT * FROM Student WHERE ach >= 30 AND ach <= 70; -- student in selected majors SELECT DISTINCT * FROM Student WHERE major IN ('CSCI', 'CINF', 'ITEC'); SELECT DISTINCT * FROM Student AS s WHERE EXISTS (SELECT * -- a subquery FROM Enroll AS e WHERE e.stuId = s.stuId); SELECT DISTINCT s.* FROM Student AS s INNER JOIN Enroll AS e USING (stuId); SELECT DISTINCT s.* FROM student AS s WHERE s.lname LIKE '%k%'; SELECT 'Michael!' REGEXP '.*'; SELECT DISTINCT s.* FROM student AS s WHERE s.lname REGEXP '.*k.*'; SELECT DISTINCT s.* FROM student AS s WHERE s.lname REGEXP '.*[jkl].*'; SELECT DISTINCT s.fname, s.lname, c.classId, e.grade FROM student AS s, enroll AS e, class AS c WHERE s.stuId = e.stuId -- Join condition AND e.classId = c.classId -- Join condition AND c.semester = 'Fall' -- problem condition AND c.year = 2019; -- problem condition SELECT DISTINCT s.fname, s.lname, c.classId, e.grade FROM student AS s INNER JOIN enroll e ON (s.stuId = e.stuId) -- Join condition INNER JOIN class AS c ON (e.classId = c.classId) -- Join condition WHERE c.semester = 'Fall' -- Problem condition AND c.year = 2019; -- Problem condition SELECT DISTINCT s.fname, s.lname, c.classId, e.grade FROM student AS s INNER JOIN enroll e USING (stuId) -- Join condition INNER JOIN class AS c USING (classId) -- Join condition WHERE c.semester = 'Fall' -- Problem condition AND c.year = 2019; -- Problem condition SELECT DISTINCT CONCAT(s.fname, ' ', s.lname) AS student, d.deptName AS `minor department` FROM student AS s INNER JOIN department AS d ON (s.minor = d.deptCode); -- List the names of the students with their minors (in full name). SELECT DISTINCT CONCAT(s.fname, ' ', s.lname) AS student, d.deptName AS `minor department` FROM student AS s LEFT JOIN department AS d ON (s.minor = d.deptCode); SELECT DISTINCT CONCAT(s.fname, ' ', s.lname) AS student, d.deptName AS `minor department`, d.numStaff FROM student AS s LEFT JOIN department AS d ON (s.minor = d.deptCode); SELECT DISTINCT CONCAT(s.fname, ' ', s.lname) AS student, IFNULL(d.deptName, 'Undeclared') AS `minor department` FROM student AS s LEFT JOIN department AS d ON (s.minor = d.deptCode); -- student within 60 credits of the maximum number of ach any student may have. SELECT DISTINCT s.stuId, CONCAT(s.fname, ' ', s.lname) AS student, s.ach AS credits FROM student AS s WHERE s.ach + 60 >= (SELECT DISTINCT MAX(ach) FROM student); -- subqueries as derived tables. SELECT DISTINCT s.stuId, CONCAT(s.fname, ' ', s.lname) AS student, s.ach AS credits FROM student AS s INNER JOIN (SELECT DISTINCT MAX(ach) AS max FROM student) AS m -- an alias is required. WHERE s.ach + 60 >= m.max; WITH m AS (SELECT DISTINCT MAX(ach) AS max FROM student) SELECT DISTINCT s.stuId, CONCAT(s.fname, ' ', s.lname) AS student, s.ach AS credits FROM student AS s INNER JOIN m -- an alias is required. WHERE s.ach + 60 >= m.max; WITH t1 AS (SELECT MAX(ach) AS max FROM student) SELECT s.stuId, s.ach AS `ach credits`, t1.max - s.ach AS `diff from max credits of all` FROM student AS s, t1 ORDER BY `ach credits` DESC;