You are on page 1of 38

UNSW Business School | Information Systems and Technology Management

INFS1603/COMM1822 Business Databases

Week 1: Intro to SQL


Ice Breaker

source: LinkedIn
Things you need to know
q You must only attend the tutorial that you have enrolled in.
q Please be prepared!
§ Before class: Email your tutor the homework - this will be checked and marked.
§ During class:
§ Week 1: Introduction to SQL
§ We will run through the SQL Demo Script (download from Moodle)
§ Copy paste the script into SQL Developer and follow along
§ Weeks 2 - 3: ER Modelling
§ Apply theory taught in lecture
§ Go through examples and exercises
§ Complete lab exercises on ER modelling
§ Week 4 onwards: SQL
§ We will run through the MCQ’s from the previous weeks HW
§ We will run through the SQL Demo Script (download from Moodle)
§ Copy paste the script into SQL Developer and follow along
Things you need to know - Online

q Punctuality
§ Please be in the Collaborate Ultra classroom 10 - 15 minutes early.
q Attendance
§ Attendance will be marked virtually, if you exit the classroom or do not
participate this will be recorded.
§ Please attend the correct virtual workshop.
q Communication
§ Please have a working headset or microphone for verbal
communication.
§ Do not talk over others, please use the “Raise the Hand” function.
§ Please be in a quiet environment - no background disruptions e.g.
barking dog, music, television noise etc.
Assessment Overview
Clarifying attendance and lab requirements
q Attendance and lab exercises:
§ To get full attendance, you must come to class and fully participate.
§ Business School requires 80% minimum attendance.
§ Homework: From Week 2, you will have to hand in the relevant HW from Moodle
§ See “Lab Exercises (Homework)” on Moodle
§ Be sure to use the HW submission template provided.
§ HW should be emailed to your tutor prior to the start of the lab with the subject in
the format of “INFS1603/COMM1822: Week x HW”
§ This will contribute to your 10% “Lab Exercises” mark.
q If you are sick or unable to attend the lab, e-mail the the HW prior to class anyways as this may
be marked pending special considerations approval
§ Contact the special considerations unit via MyUNSW within 3 days of the tutorial
§ See list of valid circumstances here: https://student.unsw.edu.au/special-consideration.
Admin
Please note that only your UNSW
The best way to contact your
email account should be used for
lecturer or tutor is via email
formal notices and correspondence
or MS Teams.
regarding the course.

The subject of your e-mail should begin


Always sign your email with with the course code and should be
your name and student signed with your name and zID.
number. (Correspondence
originating from non-UNSW Please use ‘INFS1603’ or ‘COMM1822’ in
email accounts will not elicit the subject of email
a response.) “INFS1603: Cannot Login”

Moodle will be utilised for


important course
communications to the class
i.e. notices, assignment
information and course content.
INFS1603: Classes and Contacts
Time Class Name UNSW email

Tuesday 18:00 - 20:00 T18A Shilpa Sanjeev shilpasanjeev@unsw.edu.au

Wednesday 11:00 - 13:00 W11A Charmaine Batulan c.batulan@student.unsw.edu.au

Wednesday 18:00 - 20:00 W18A Shilpa Sanjeev shilpasanjeev@unsw.edu.au

COMM1822: Classes and Contacts


Time Class Name UNSW email

Tuesday 16:00 - 18:00 T16A Rafiya Harun r.harun@unsw.edu.au

Tuesday 18:00 - 20:00 T18A Rafiya Harun r.harun@unsw.edu.au

Wednesday 14:00 - 16:00 W14A Charmaine Batulan c.batulan@student.unsw.edu.au

Wednesday 16:00 - 18:00 W16A Shilpa Sanjeev shilpasanjeev@unsw.edu.au


PASS Class

q Week 2 – Week 4: Thursday 6pm – 7pm

q Week 5 – Week 10: Thursday 3pm – 4pm

q PASS Tutor: Sisi Hu


Group Assignment

You need to solve the assignment in a group of 5 (4


if needed) students – all group members need to be
from the same class

Assign one member of the team as team leader

Submit a signed group cover sheet and enroll in the


correct Moodle group by the end of Week 2

9
Things to consider when choosing group
members

q Do they have the same outcome


expectation as you (e.g. are all team
members aiming for a HD or a credit)
q Time schedule – is everyone available
for a meeting to discuss about the
assignment together at a specified
time every week?
q Location – if there are face-to-face
meetings, where is everyone situated?
SeQueL

11
SQL

q SQL = Structured Query Language = Sequel


q SQL is the first standard database language.
q Originally developed by D. Chamberlin and R. Boyce at IBM.
q The most common SQL standard is the ANSI/ISO SQL. Originally defined in 1988,
SQL-86, it has been undergone major revisions in 1992, SQL-92, and 1999, SQL-99.
The latest revision is SQL:2011.
q Microsoft, Oracle, and other vendors have introduced deviations from ANSI SQL.
q As a relational language, SQL has three main component:
§ Data Definition Language (DDL)
§ Data Manipulation Language (DML)
§ Data Control Language (DCL)

12
Understanding SQL Query Structures

q The SELECT statement is used to retrieve and display data from one or more tables.

q “SELECT FROM WHERE”


§ SELECT clause tells which attributes of the tuples matching the condition are
produced as part of the answer.
§ FROM clause gives the names of relation(s).
§ WHERE clause is a condition that tuples must satisfy in order to match the
query.

13
Understanding SQL Query Structures

Example Relations/Tables:

PRODUCT (P_Code, P_Description, P_Indate, P_Onhand, P_Min, P_Price, P_Discount,


V_Code)

VENDOR (V_Code, V_Name, V_Contact, V_AreaCode, V_Phone, V_State, V_Order)

(see next slide for contents)

14
15
Understanding SQL Query Structures

q SQL allows us to use the keyword ALL or * to specify that all tuples are to be
selected.

SELECT ALL
FROM PRODUCT;

or

SELECT *
FROM PRODUCT;

* : is a “wild card.”

16
Understanding SQL Query Structures

q SQL supports the elimination of duplicates by using the keyword DISTINCT.

SELECT DISTINCT Std_Name


FROM STUDENTS;

Question: How could there be duplicates, are relations not duplicate free?

17
Understanding SQL Query Structures

SELECT <columns>
FROM <table>

q List all product descriptions:


SELECT P_Description
FROM PRODUCT;

q List all vendors and their contact names:


SELECT V_Name, V_Contact
FROM VENDOR;

etc.

18
Understanding SQL Query Structures

q Produce a list of product description, product indate and product price produced by
vendor whose vendor code is “21344”:

SELECT P_Description, P_Indate, P_Price, V_Code


FROM PRODUCT
WHERE V_Code = 21344;

etc.

19
Mathematical Operators for SQL

q Mathematical operators that can be used in a WHERE clause


= equal to
< less than
<= less than or equal to
> greater than
>= greater than or equal to
<> not equal to

20
Mathematical Operators for SQL

q Create a list of product description, product indate and product price for products
sold by vendor that are not coded “21344”.

SELECT P_Description, P_Indate, P_Price, V_Code


FROM PRODUCT
WHERE V_Code <> 21344;

q Create a list of product description, product onhand, product minimum, and product
price for products with product code less than “1558-QWI”.

SELECT P_Description, P_Onhand, P_Min, P_Price


FROM PRODUCT
WHERE P_Code < ‘1558-QWI’ ;

21
ASCII Codes in SQL

q All characters/signs are assigned an ASCII


(American Standard Code for Information
Interchange) code by the computer.
q Examples are below; see manual or online for more
information on ASCII codes.
q The comparisons of strings are made from left to
right. This is useful when comparing names.
However, it also may create problems:
§ “2” is sorted as if greater than “11” (because “2”
> “1”).
§ “01/01/2020” is sorted before “12/31/2015”
(because “0” < “1”).
§ Recommendation: use the date/number format
instead of string.

22
Logical (Boolean) Operators in SQL

q OR
q AND
q NOT

23
Logical (Boolean) Operators in SQL

q List products where the vendor code is ‘21344’ or ‘24288’:

SELECT P_Description, P_Indate, P_Price, V_Code


FROM PRODUCT
WHERE V_Code = 21344 OR V_Code = 24288;

q List products where either the product indate is after July 15, 2015 and the product
price is less than 50.00 – or the vendor code is 24288.

SELECT P_Description, P_Indate, P_Price, V_Code


FROM PRODUCT
WHERE NOT (P_Price < 50 AND P_Indate > ‘07/15/15’) OR V_Code = 24288;

24
Special Operators in SQL

q BETWEEN is used to define range limits.


q IS NULL is used to check whether an attribute value is null.
q LIKE is used to check for similar character strings.
q IN is used to check whether an attribute value matches a value contains within a
subset of listed values.
q EXISTS is used to check whether an attribute has a value.

25
Special Operators in SQL

q List the products with prices between 50 and 100.

SELECT *
FROM PRODUCT
WHERE P_Price BETWEEN 50.00 AND 100.00;

or

SELECT *
FROM PRODUCT
WHERE P_Price >= 50.00 AND P_Price <= 100.00;

26
Special Operators in SQL

q List the details of all vendors whose last name begins with “Smith.”

SELECT V_Name, V_Contact, V_AreaCode, V_Phone


FROM VENDOR
WHERE V_Contact LIKE ‘Smith%’;

% : wild card

27
Special Operators in SQL

q List the contents of the product table where the product price is $ 50 or $ 100.

SELECT *
FROM PRODUCT
WHERE P_Price = 50.00 OR P_Price = 100.00;

or

SELECT *
FROM PRODUCT
WHERE P_Price IN (50.00, 100.00);

28
Special Operators in SQL

q List the details of products with existing (not-NULL) vendor codes.

SELECT *
FROM PRODUCT
WHERE V_Code EXISTS;

or

SELECT *
FROM PRODUCT
WHERE NOT ISNULL (V_Code);

29
Ordering SQL Results

q ORDER BY <columns> : produces a list in ascending order

q ORDER BY <columns> [DESC] : produces a list in descending order

30
Ordering SQL Results

q List the details of product table listed by product price in ascending order:

SELECT P_Description, P_Indate, P_Price, V_Code


FROM PRODUCT
ORDER BY P_Price ;

31
Ordering SQL Results

q List the details of of products with an in-date before 15 September 1999 and a price
less than A$ 50.
q Put the results in ascending order of vendor code and descending order of price.

SELECT P_Description, P_Indate, P_Price, V_Code


FROM PRODUCT
WHERE P_Indate < ‘9/15/99’ AND P_Price <= 50.00
ORDER BY V_Code, P_Price DESC;

32
Lab Script!

From Moodle download:


q Lab Manual
q This week’s lab demo
scripts

Oracle Setup:
q Usernames and
passwords have been
sent to your UNSW
email account
Homework

Please check Moodle

34
SQL Homework (Week 1, Week 4 – 10)

q The homework file may have three sections (sometimes two sections):

q Multiple Choice Questions – you do not need to submit answers to these.

q Hands-On Assignments – you have to submit both the SQL script and the SQL
output. You can submit the output as a screen capture or as a query report (export
of the data).

q Advanced Challenge – you have to submit both the SQL script and the SQL output.
You can submit the output as a screen capture or as a query report (export of the
data).
SQL Homework

q Note: There are 2 sections of HW for week 1 to be submitted in


week 2:
§ 1A and 1B
§ Have to do both to get the homework mark.
Questions
q Course Outline:
https://www.business.unsw.edu.au/degrees-courses/course-
outlines/INFS1603

q If there are any other questions, please


post your questions on Moodle, MS
Teams or email your tutors.

You might also like