In this homework assignment, you will use SQL to store and query a database. You will learn the followings:
How to use expressions
This assignment has a total of four questions and 25 points.
You will also use SQLite as the DBMS. In contrast to many other database management systems (e.g., Oracle, DB2, and SQL Servier), SQLite is not a client–server database engine. Rather, it is embedded into the end program. This unique feature has led it to be adopted by billions of applications.
Jupyter is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Please install it using Anaconda for python 2.7.
If you are using Mac OS X or Linux, SQLite should be pre-installed. Open a terminal and type sqlite3. To exit, type ".exit"
If you are using Windows, please follow the instructions on here to install SQLite
ipython-sql is a jupyter notebook extension. It allows using SQL queries inside jupyter notebooks. Please install it using Anaconda:
Open a terminal and type conda install -c conda-forge ipython-sql
Pelase download and follow the steps on the test.ipynb to test your environment before you start your homework assignment.
The goal is to create a database to manage student and course information in computing science department (named cssys
), and then create three tables in this database. The first table is named students
, the second table is named courses
, and the third table is called transcript
.
To start, please execute the following cell to load the ipython-sql extension.
%load_ext sql
cssys
¶#REPLACE WITH YOUR CODE#
students
¶Please create a table named students
.
The students
table has six attributes: studentid, firstname, lastname, age, gender, gpa
id
- integerfirstname
- char(15)lastname
- char(15)age
- integergender
- char(1)gpa
- doubleid
is Primary Key#REPLACE WITH YOUR CODE#
courses
¶Please create a table named courses
.
The courses
table has four attributes: id, name, credit, prereq
id
- integername
- varchar(30)credit
- integerprereq
- integerid
, prereq
) is Primary Key#REPLACE WITH YOUR CODE#
transcript
¶Please create a table named transcript
.
The transcript
table has five columns: studentid, courseid, mark, semester, credit
studentid
- integercourseid
- integermark
- doublesemester
- integer (represented as year + 01:fall, 02:spring, 03:summer
)credit
- integerstudentid, courseid
) is Primary Key#REPLACE WITH YOUR CODE#
Please write SQL queries to delete the age attribute and as a Date of Birth (dob) attribute to table students
. Please decide on the type and the default value of this attribute and include it in your response. You can decide how to perform this using delete and recreate or modify.
#REPLACE WITH YOUR CODE#
students
.¶Please write SQL queries to insert the following rows to the students
table. Change the format of date of birth attribute value based on your definition of its type.
1001, adam, smith, 2000-01-03, m, 3.1
1002, alice, frank, 1999-03-11, f , 3.4
1003, bob, hal, 1999-09-01, m, 2
#REPLACE WITH YOUR CODE#
courses
.¶Please write SQL queries to insert the following rows to the courses
table.
100, programming, 3, NULL
110, math, 3, NULL
120, web, 4, NULL
301, networking, 4, 200
301, networking, 4, 150
301, networking, 4, 210
354, database, 3, 120
354, database, 3, 110
360, os, 3, 150
360, os, 3, 210
#REPLACE WITH YOUR CODE#
transcript
.¶Please write SQL queries to insert the following rows to the transcript
table.
1001, 100, 3, 201801, 3
1001, 110, 3.5, 201801, 3
1001, 120, 2.7, 201801, 4
1001, 301, 3.4, 201802, 4
1002, 100, 3, 201801, 3
1002, 110, 3.2, 201901, 3
1002, 301, 3.1, 201902, 4
1003, 100, 2.5, 201801, 3
1003, 120, 3.5, 201901, 4
1003, 301, 2.8, 201902, 4
1003, 354, 4, 201903, 3
1003, 360, 3.5, 201802, 3
#REPLACE WITH YOUR CODE#
Please write the SQL query for each of the requests below.
4.1 (1 point) Please write an SQL query to show all rows in the students
table.
#REPLACE WITH YOUR CODE#
4.2 (1 point) Please write an SQL query to show the rows whose credit
is 3
in the courses
table.
#REPLACE WITH YOUR CODE#
4.3 (1 point) Please write an SQL query to show the rows whose mark
is larger than 3 and credit
is no smaller than 3 in the transcript
table.
#REPLACE WITH YOUR CODE#
4.4 (1 point) Please write an SQL query to show studentid
, courseid
and mark
of all rows in the transcript
table.
#REPLACE WITH YOUR CODE#
4.5 (1 point) Please write an SQL query to show studentid
, courseid
and mark
of all rows in the transcript
table whose semester value is 201902
.
#REPLACE WITH YOUR CODE#
4.6 (1 point) Please write an SQL query to show distinct courseid
of all rows in the transcript
table.
#REPLACE WITH YOUR CODE#
4.7 (2 points) Please write an SQL query to show the firstname
and lastname
and gpa
from students
table and sort it based on gpa
.
#REPLACE WITH YOUR CODE#
4.8 (4 points) Please write an SQL query to compute lettergrade
of each row in the transcript
table, and show studentid
, courseid
and lettergrade
of all rows in the transcript
table. lettergrade
is computed as follows:
mark
>= 3.5, then lettergrade
= "A"mark
< 3.5, then lettergrade
= "B"mark
< 3, then lettergrade
= "C"mark
< 2.5, then lettergrade
= "D"mark
< 2, then lettergrade
= "F"#REPLACE WITH YOUR CODE#
Complete the code in this notebook hw1.ipynb, and submit it to through Canvas system to your HW1 activity. You can also include a pdf file where you can add your comments, thoughts, explanations about any of the questions.