This lab practices writing SQL queries on a MySQL database.
Make sure your computer is setup to run Docker by following these setup instructions.
- Create a directory
cosc304_lab3
. - Download the
docker-compose.yml
file into thecosc304_lab3
directory. - Create a folder
ddl
incosc304_lab3
directory. Download the contents of theddl
folder into thecosc304_lab3\ddl
folder. - Open a command shell either directly on your machine or using VSCode. Make sure your current directory is
cosc304_lab3
. - Run the command
docker-compose up -d
- If everything is successful, the MySQL database will start on port 3306. If there is a port conflict, change the port to 3307 in the
docker-compose.yml
file. - Your database is
mydb
. There are other databases also created such asworkson
anduniversity
.
MySQL commands can be running using the command line within the Docker container. Run the command:
docker exec -it cosc304-mysql bash
This will start a command line session. Connect to MySQL using:
mysql -u root -p
OR
mysql -u testuser -p
The password is given in the docker-compose.yml
file (which you are encouraged to change). Note that all commands are terminated with a semi-colon (;
). Some useful commands are:
Function | Command |
---|---|
Listing all databases | show databases; |
Use database dbname |
use dbname; |
List all tables | show tables; |
Note that if any of the DDL scripts fail to run to setup your database. Then either run them using SQuirreL SQL or login to mysql command prompt and use source
command such as: source /docker-entrypoint-initdb.d/ShipmentMySQL.sql
.
SQuirreL is an open source graphical query tool capable of querying any JDBC-accessible database including Oracle, MySQL, and SQL Server.
Start up SQuirreL. Register our MySQL server with the information:
Name: MySQL Login name: root Password: (see docker-compose.yml file) Host name: localhost Port: (leave blank for default) Database: mydb
The university database should already be loaded. If you have any issues, using SQuirreL, create the tables and load the data for the university database using this DDL script.
Here is the database schema that we will be using for our practice SQL queries:
student (snum, sname, major, standing, age) faculty (fid, fname, deptid) course (cname, meets_at, room, fid); enrolled (snum, cname)
Here are the questions that you must answer using SQL. You can answer them in groups. Try to answer one challenge question and write your own for others and the next class!
- Return all students (name only) with 'SO' or 'JR' standing and a 'Computer Science' major.
- Return all courses (name and room only) taught by 'Richard Jackson'
- Return all students (number and name only) that are enrolled in a course offered in room 'R15' taught by a faculty member in department '68' and have 'SR' standing.
- Return all courses that have 'Data' in their title. Sort them by course name.
- For each department, return its id and the number of courses offered by faculty associated with that department. Sort the list in descending order of number of courses offered. Rename this count field as 'CoursesOffered'.
- For each student, return their number, name, and number of courses they are enrolled in. Students must appear in the list even if they are not registered for any courses. Sort in descending order of the number of courses enrolled in.
- Return a list of courses (name only and number of students enrolled) that are either offered by faculty in deptId = '11' OR by 'Ivana Teach' and have at least 2 students enrolled in them.
- Return the students who are above the average student age.
- Return all students that are not taking any courses.
- Return all 'Computer Science' majors who are younger than all 'Psychology' majors.
SELECT sname FROM student WHERE (standing = 'JR' OR standing = 'SO') AND major = 'Computer Science';
SELECT * FROM faculty F, course C WHERE F.fid = C.fid and F.fname = 'Richard Jackson';
SELECT S.snum, sname FROM student S, course C, faculty F, enrolled E WHERE S.snum = E.snum AND C.cname = E.cname AND C.fid = F.fid AND C.room = 'R15' AND F.deptId = '68' AND S.standing = 'SR';
SELECT * FROM course WHERE cname LIKE '%Data%' ORDER BY cname;
SELECT deptId, COUNT(cname) AS CoursesOffered FROM faculty F, course C WHERE F.fid = C.fid GROUP by deptId ORDER BY CoursesOffered DESC;
SELECT S.snum, sname, COUNT(cname) FROM student S LEFT OUTER JOIN enrolled E ON S.snum = E.snum GROUP BY S.snum, sname ORDER BY COUNT(cname) DESC;
SELECT C.cname, COUNT(*) FROM course C, enrolled E, faculty F WHERE C.cname = E.cname AND C.fid = F.fid AND (F.deptId = '11' OR F.fname = 'Ivana Teach') GROUP BY C.cname HAVING COUNT(*) >= 2;
SELECT * FROM student WHERE age > (SELECT AVG(age) FROM student);
SELECT * FROM student WHERE snum NOT IN (SELECT snum FROM enrolled);
SELECT * FROM student WHERE major = 'Computer Science' AND age < ALL (SELECT age FROM student WHERE major = 'Psychology');
- Return the maximum, minimum, and average number of courses students are enrolled in. For example, if one student is enrolled in 10 courses, that is the maximum. The minimum is 1 (not including 0 registrations). Take the average based on students who are enrolled in at least one course.
- Return all faculty members (fid, fname, and number of courses taught) who teach more courses than all other faculty. If more than 1 faculty member teaches the maximum, then show all that teach the maximum number of courses.
SELECT MAX(NumCourses), MIN(NumCourses), AVG(NumCourses) FROM (SELECT S.snum, sname, COUNT(*) as NumCourses FROM student S JOIN enrolled E ON S.snum = E.snum GROUP BY S.snum, sname) AS T;
SELECT F.fid, F.fname, COUNT(*) FROM faculty F, course C WHERE C.fid = F.fid GROUP BY F.fid, F.fname HAVING COUNT(*) >= ALL(SELECT COUNT(*) FROM course GROUP BY fid);