You are on page 1of 18

F.

3 Computer Literacy

Database

St. Paul’s College .

SELECT * FROM Student WHERE


name=“_______________” AND
Class=“_____” AND
Class No.= “____”
Contents
Chapter 1 ............................................................................................................................................................ 2
Database Concept and Its Structure ................................................................................................................... 2
Chapter 2 ............................................................................................................................................................ 4
Introduction to Database Management System................................................................................................. 4
Chapter 3 ............................................................................................................................................................ 5
Basic SQL Query .................................................................................................................................................. 5
3.1 SELECT Statement ................................................................................................................................ 7
3.2 Computation ........................................................................................................................................ 8
3.3 Conditions ............................................................................................................................................ 9
3.3.1 Comparisons ............................................................................................................................. 9
3.3.2 Logics and multiples conditions .............................................................................................. 10
3.3.3 Pattern matching for text ........................................................................................................ 11
3.4 Ordering ............................................................................................................................................. 12
3.5 Tracing Output of SQL ........................................................................................................................ 13
Chapter 4 .......................................................................................................................................................... 16
Advanced Query ............................................................................................................................................... 16
4.1 Uniqueness ........................................................................................................................................ 16
4.2 Common Function .............................................................................................................................. 17

1|Page
Chapter 1
Database Concept and Its Structure

A database is a collection of tables where information are stored in a specified way.


Database are commonly used in daily life. The following shows some examples:

• Student discipline records


• Library book records
• Facebook profiles
• Contact lists

Basic structure
A database consists of the following:
1 fields
2 records
3 tables

As illustrated in the above diagram. A field is a single piece of information; a record (sometimes
called a row) is one complete set of fields; and a table is a collection of records.

2|Page
Primary Key*
A table usually contains a special field which helps us to uniquely identify a particular record. This
field is called a primary key. It must satisfy the following rules:
1 Each value of the field must be distinct among the table;
2 The field cannot be empty for each record.
For example, names and birthday cannot be primary key as they violate the first rule. Mobile
number cannot be a primary key either as there might be some persons who do not own a mobile
phone.
Some example of a primary key are shown below:
1 Student id
2 ISBN of a book
3 Facebook username

Exercise
1. The following diagram shows a part of a database.

(a) Fill in the boxes above to indicate the structure of the database.
(b) How many fields are there?
(c) Can date_of_birth be a primary key? Explain.
(d) Which of the above fields can be a primary key? Why?

3|Page
Chapter 2
Introduction to Database Management System

There are various implementations for databases in the real world. They use different ways to
store information in the hard disk. These databases usually come with a database management
system (DBMS). A DBMS is a computer program designed to manage a database and run
operations on the data requested by numerous users simultaneously.
Some well-known database management systems are MySQL, PostgreSQL, SQLite and Oracle. We
shall use SQLite in this set of notes. Every database supports Structured Query Language (SQL) for
executions of various operations.

2. Can we treat a spreadsheet (e.g. Microsoft Excel) as a database? Explain briefly.

2.1 Use of Online SQLite Viewer


SQLite Viewer is a free application which is available online.
http://inloop.github.io/sqlite-viewer/

4|Page
Chapter 3
Basic SQL Query
A database supports operations like insertion, update, deletion, sorting, searching and calculation.
However, there is no simple graphical way to perform the operation. We shall enter query as if we
are using the Google search engine. Structured Query Language is a language for managing
databases. Some examples are given below.
Descriptions
SELECT * FROM Student Display all fields of each record in Student table.
SELECT * FROM Student WHERE height>1.7 Similar but limited to record with height greater than 1.7m.
SELECT name, height FROM Student Display only the name and height fields of each student.

The school revised the design of the database and prepared some records for testing. The database
consists of the following tables:
1. Student
2. Teacher
We are going to complete the following tasks:
1. List all student information of a class;
2. List out all girls in a house;
3. List out all obese student;
4. Find the top 10 tallest boy;
5. Find the heaviest boy in Form 1;

5|Page
Table Structure
Student:
Field Name Data Type Description
sid INTEGER Student id number, used as primary key
name VARCHAR Student’s name
sex CHAR Student’s gender (M or F)
dob DATE Student’s date of birth
house CHAR 6 houses (B, G, Y, P, R, Y)
class VARCHAR Class, e.g. 1A, 1B, 1C, … , 6F
clsno INTEGER Class Number
height REAL Student’s height in m
weight REAL Student’s weight in kg
wisdom INTEGER Student’s Intelligence Quotient

Teacher:
Field Name Data Type Description
tid INTEGER Teacher id number, used as primary key
code VARCHAR Teacher’s Initials
name VARCHAR Teacher’s name
sex CHAR gender (M or F)

Data Type
Although a computer knows the ways to validate an inputted data in a database, the data type of
each field must be stated in advance.
INTEGER to store an integer
REAL to store a real number
CHAR to store a single character
VARCHAR to store text (single character or string)
DATE to store data in date format
BOOLEAN to store logical result TRUE or FALSE

6|Page
3.1 SELECT Statement
The select statement is used to list out all specified fields of each records in a table. Its syntax is
given below:
1. Field names are specified in order and separated by commas:

SELECT FieldList FROM TableName

2. Use an * to represent all the fields:

SELECT * FROM TableName

3. Use “AS” to rename a field title (alias); use a pair of “ ” or ‘ ’ to enclose alias with spaces:

SELECT Field AS “AnotherName” FROM TableName

Exercise
1. Write down the titles of the output of the SQL statement:

SELECT name, sex, house FROM Student

2. Write down the titles of the output of the SQL statement:

SELECT name, house, sex FROM Student

3. Write down an SQL statement for each of the following:

(a) List the names and sex of each student;


(b) List the names and height of each student.

7|Page
3.2 Computation
A select statement can be used to perform computation on certain expressions:

SELECT Expression FROM TableName

It supports the following arithmetic operators:


Operator Meaning
+, −, *, / Addition, Subtraction, Multiplication, Division

4. What is the output of the following statement?

SELECT 1+2*3-2

5. Write down a statement that lists out the name and weight (in lbs) of each student:

6. The body mass index (BMI) can be calculated according to formula below:
!"#$%& (#) *$)
BMI =
%"#$%& ! (#) ,)
Write down a statement to compute the name and BMI of each student.

8|Page
3.3 Conditions
It is also possible to select records which satisfy given conditions governing an individual student.

SELECT Expr FROM TableName WHERE Condition

Note. The computer picks each record and tests whether it satisfies the given conditions.
Therefore, we can use fields to impose a condition from the selected table.

• The condition given is checked against every individual record.


• All words not enclosed by a pair of quotes is considered to be a field name, an alias or a
keyword.

3.3.1 Comparisons

Operator Meaning
>, < Greater then, Less than
>=, <= Greater than or equal to, Less than or equal to
!=, <> Not equal to
= Equal to

1. To list out the name and height of students who are taller than 1.7m:

SELECT name, height FROM Student WHERE height>1.7

2. To list out the names of student in Class 1A: (notice the usage of quotes)

SELECT name FROM Student WHERE class= “1A”

7. For each of the following, write down an SQL statement:

(a) List out the name of students who are not studying in Class 1F;
(b) List out the names of all male teachers;
(c) List out the names and IQ of all gifted students. (A student is gifted if his or her
wisdom is at least 130.)

9|Page
3.3.2 Logics and multiples conditions

Operator Meaning
NOT Logical not
AND Logical and
OR Logical or
Note. The priority of computation is firstly NOT, AND, and finally OR.
1. To list out the names of all boys in Class 2F:

SELECT name, height FROM Student WHERE class=“2F” AND sex=“M”

2. To list out the names of student in Class 1A and Class 3B: (the given requirement is not an
individual condition.)

SELECT name FROM Student WHERE class=“3A” OR class=“3B”

3. To list out the names of all Oak house student who are in Class 1A or 1F:

SELECT name FROM Student WHERE house=“O” AND (class=“1A” OR class=“1F”)

4. To list out the names of student in Class 1A: (notice the usage of quotes)
SELECT name, weight/(height*height) AS BMI FROM Student WHERE sex=“M” AND
BMI >=30

8. Write down an SQL statement for each of the following:

(a) To list out the names of students in Class 1A, 1B and 1C;
(b) To list out the names of students who have height at least 150 but not taller than 170;
(c) To list out the names of girls in Class 1A and 1C;
(d) To list out the names of gifted girls in Class 4E and 4F.

10 | P a g e
3.3.3 Pattern matching for text

Operator Meaning
LIKE To look for similar pattern
To specify a pattern, we use a ‘_’ to indicate one arbitrary character and a ‘%’ to indicate any
number of arbitrary character.
1. To find out all teachers whose name begins with ‘S’:

SELECT name FROM Teacher WHERE name LIKE “S%”

2. To list out the names and classes of all F1 students:

SELECT name, class FROM Student WHERE class LIKE “1_”

3. To list out the names of all Yew house students whose names have an ‘a’ in the second letter:

SELECT name FROM Student WHERE house=“Y” AND name LIKE “_a%”

9. Write down an SQL statement for each of the following:

(a) List out the classes, names of students whose names ends with an “a”;
(b) List out the names of students whose surname is “Chan”.

10. Write down an SQL statement for each of the following:

(a) List out the names of all Ginkgo girls who are in Form 4;
(b) List out the names of all Banyan boys who are not in Form 3.

11 | P a g e
3.4 Ordering
We can ask the database to sort the output by using keywords “ORDER BY”. It is by default sorted
in ascending order. Use the keyword “DESC” if a reverse order is needed.
1. To list out the names of student in alphabetical order:

SELECT name FROM Student ORDER BY name

2. To list out the names and height of students sorted by height in descending order:

SELECT name, height FROM Student ORDER BY height DESC

3. To list out the names and houses of students in ascending order. Sorted by their houses
first, then their names:

SELECT name, house FROM Student ORDER BY house, name

11. We want to list out the names of the ten tallest boys.

(a) Write down an SQL statement to sort the names of the boys according to their height;
(b) Describe how we can obtain the names of the ten tallest boys.

12. A teacher wants to find out the names of the cleverest boy and cleverest girl

(a) Write down all necessary SQL statement;


(b) Describe how the teacher can obtain the desired result.

12 | P a g e
3.5 Tracing Output of SQL
Suppose the teacher table and student table store the following records:
Student:

sid name sex house class height weight wisdom


1 John Tam M O 1A 1.43 50 100
2 Peter Lam M O 1A 1.56 60 120
3 Lily Mok F Y 1B 1.65 70 120
4 Helen Poon F B 1C 1.76 45 130
5 Kiki Chan F P 2B 1.85 62 133
6 Jojo Suen F G 2C 1.67 54 142
7 Alan Cheung M R 2E 1.85 76 110
8 Bobby Li M Y 3F 1.89 100 140
9 Angus Ma M P 5E 1.72 67 121
10 Angela Wu F G 6F 1.68 64 142

Teacher:
tid code sex name
1 KBK F Tina Kwok
2 ABC M Kingston Chan
3 BBC M Billy Chow
4 KHY M Teddy Yuen
5 DDY F Dennis Yik
6 PAD F Puzzle Dragon

13 | P a g e
13. Write down the first three records and the titles of the output of the statement:

SELECT name, sex FROM Teacher

14. Write down the output of the statement:

SELECT name, house FROM Student WHERE weight<60

15. Write down the output of the statement:

SELECT name, wisdom AS IQ FROM Student WHERE sex=“F” AND wisdom>=140

16. Write down the output of the statement:

SELECT name FROM Student WHERE name LIKE “%am” ORDER BY name DESC

14 | P a g e
17. Write down the output of the statement:

SELECT name FROM Teacher WHERE code LIKE “_B_” ORDER BY sex, name

18. Write down the output of the statement:

SELECT name FROM Student WHERE class LIKE “1_” AND weight>=50
ORDER BY sex, name DESC

19. Write down the output of the statement:

SELECT * FROM Teacher WHERE name LIKE “D%” OR name LIKE “%Y%”
ORDER BY sex

15 | P a g e
Chapter 4
Advanced Query
4.1 Uniqueness
Sometimes, we would like to display values only once to avoid duplication. For example, imagine
that we have a database about books stored in a library. It is not uncommon to find same copies of
a particular book. If we list out all titles simply by a select statement, the output may contain many
repeated results.
The select-distinct statement is used to list out all specified fields with no repetition. Its syntax is
given below.
1. Field names are specified in order and separated by commas:

SELECT DISTINCT FieldList FROM TableName

2. To list out all the classes in school:

SELECT DISTINCT class FROM Student

20. A teacher wants to find out the names of the cleverest boy and cleverest girl

(a) Write down a statement that lists out all the F1 classes;
(b) Suppose all the student in 1F left the school and their records are removed. How
many rows will there be in the output?

16 | P a g e
4.2 Common Function
The database supports the following operations:

• Finding maximum/ minimum; (MIN, MAX)


• Finding the total sum; (SUM)
• Finding the average value; (AVG)
• Finding the total number of records. (COUNT)
1. To list the height of the tallest and shortest student:

SELECT MAX(height), MIN(height) FROM Student

2. To list the average weight of the school:

SELECT AVG(class) FROM Student

3. To list the total number of students who filled in their date of birth:

SELECT COUNT(dob) FROM Student

4. To list the total number of students:

SELECT COUNT(*) FROM Student

5. To find the total weight of all students:

SELECT SUM(weight) FROM Student

21. Suppose there is a field July_temp_record_time which holds the submission time of the
July temperature record of a student.

(a) Can this field be empty? Why?


(b) Write a statement to list out the total number of students who have not yet handed in
the temperature record.

17 | P a g e

You might also like