
A query is a request for retrieval of data from the database.
In SQL, queries are performed using the SELECT command.
In the following examples, the table s6a is used.
The syntax for the simplest SELECT statement is as
follows:
SELECT <select_list> FROM <table_name>
This statement retrieves the selected fields in <select_list>
of all the records in the table named <table_name>. The order of
the fields in each record of the output is the same as the order of the fields in <select_list>.
If all the fields are to be retrieved, use "*"
for <select_list>.
The order of the records appeared in the output is the same as the order of the records
in the table.
For examples:
Retrieve the whole table s6a: |
SELECT * FROM s6a |
Output:
class_num |
stud_id |
name |
email |
clc |
ue |
phy |
pm |
cs |
1 |
92114 |
Chan Wai Man, Raymond |
s92114@sample.edu.hk |
64 |
55 |
70 |
64 |
59 |
2 |
92133 |
Chow Chi Ling |
clchow@example.com |
70 |
62 |
62 |
59 |
70 |
3 |
94302 |
Fung Ching Man, Mandy |
mandyfung@example.com |
72 |
50 |
42 |
59 |
60 |
4 |
91131 |
Hung Wai Ming |
s91131@sample.edu.hk |
52 |
48 |
55 |
39 |
59 |
5 |
92153 |
Leung King |
kleung@example.com |
40 |
50 |
51 |
40 |
60 |
6 |
92211 |
Poon Kwok Fai |
kwokfai@testing.com.hk |
59 |
60 |
70 |
77 |
75 |
7 |
91194 |
Sung Hing Wah, Patrick |
patricksung@example.com |
70 |
72 |
81 |
69 |
70 |
8 |
97602 |
Tang Wing Chi |
s97602@sample.edu.hk |
80 |
79 |
70 |
72 |
69 |
9 |
92145 |
Wong Ka Tak, Kent |
klkwon@example.com |
35 |
40 |
52 |
50 |
42 |
10 |
93211 |
Yeung Chun |
cyeung@testing.com.hk |
69 |
80 |
77 |
60 |
52 |
11 |
96374 |
Lai Fung Chun |
s96374@sample.edu.hk |
78 |
75 |
69 |
65 |
61 |
12 |
94412 |
Chan Lai Yin |
cly@testing.com.hk |
43 |
59 |
53 |
61 |
60 |
13 |
98832 |
Man Fook Wing |
fwman@testing.com.hk |
55 |
77 |
34 |
40 |
51 |
14 |
95343 |
Chung Kwok Fai, Fred |
fredchung@example.com |
72 |
62 |
53 |
47 |
50 |
15 |
97233 |
Lee Lai May |
maylee@testing.com.hk |
67 |
71 |
56 |
60 |
55 |
|
Select the fields name, stud_id
and cs from the table s6a (note the order of the fields): |
SELECT name, stud_id, cs FROM
s6a |
Output:
name |
stud_id |
cs |
Chan Wai Man, Raymond |
92114 |
59 |
Chow Chi Ling |
92133 |
70 |
Fung Ching Man, Mandy |
94302 |
60 |
Hung Wai Ming |
91131 |
59 |
Leung King |
92153 |
60 |
Poon Kwok Fai |
92211 |
75 |
Sung Hing Wah, Patrick |
91194 |
70 |
Tang Wing Chi |
97602 |
69 |
Wong Ka Tak, Kent |
92145 |
42 |
Yeung Chun |
93211 |
52 |
Lai Fung Chun |
96374 |
61 |
Chan Lai Yin |
94412 |
60 |
Man Fook Wing |
98832 |
51 |
Chung Kwok Fai, Fred |
95343 |
50 |
Lee Lai May |
97233 |
55 |
|
Apart from a list of fields and "*",
the <select_list> after the keyword SELECT
can also be a list of expressions. An expression can be a constant, a field names
or any combination of the above connected by arithmetic operators (e.g. +, -, *, /)
and parentheses.
For example:
List the class numbers, names and
average marks of the five subjects of all the students in the class S6A: |
SELECT class_num, name, (clc
+ ue + pm + phy + cs) / 5 FROM s6a |
Sample output (system dependent):
class_num |
name |
Exp_3 |
1 |
Chan Wai Man, Raymond |
62.4000 |
2 |
Chow Chi Ling |
64.6000 |
3 |
Fung Ching Man, Mandy |
56.6000 |
4 |
Hung Wai Ming |
50.6000 |
5 |
Leung King |
48.2000 |
6 |
Poon Kwok Fai |
68.2000 |
7 |
Sung Hing Wah, Patrick |
72.4000 |
8 |
Tang Wing Chi |
74.0000 |
9 |
Wong Ka Tak, Kent |
43.8000 |
10 |
Yeung Chun |
67.6000 |
11 |
Lai Fung Chun |
69.6000 |
12 |
Chan Lai Yin |
55.2000 |
13 |
Man Fook Wing |
51.4000 |
14 |
Chung Kwok Fai, Fred |
56.8000 |
15 |
Lee Lai May |
61.8000 |
In the output, the system will create a "field name" by itself
(e.g. Exp_3 as shown above) for an expression appeared in the <select_list>.
The display format of the numerical value (in this case, Exp_3) is software
dependent. |
Sometimes we want to have a more meaningful descriptive name for a field name in the
output. This can be achieved by adding the keyword AS
and the new name (called alias) after the field name.
For example:
Select the fields name, stud_id
and cs from the table s6a, and displaying the
field name cs as Computer_studies. |
SELECT name, stud_id, cs
AS computer_studies FROM s6a |
Output:
name |
stud_id |
Computer_studies |
Chan Wai Man, Raymond |
92114 |
59 |
Chow Chi Ling |
92133 |
70 |
Fung Ching Man, Mandy |
94302 |
60 |
Hung Wai Ming |
91131 |
59 |
Leung King |
92153 |
60 |
Poon Kwok Fai |
92211 |
75 |
Sung Hing Wah, Patrick |
91194 |
70 |
Tang Wing Chi |
97602 |
69 |
Wong Ka Tak, Kent |
92145 |
42 |
Yeung Chun |
93211 |
52 |
Lai Fung Chun |
96374 |
61 |
Chan Lai Yin |
94412 |
60 |
Man Fook Wing |
98832 |
51 |
Chung Kwok Fai, Fred |
95343 |
50 |
Lee Lai May |
97233 |
55 |
|
Aliases are often used together with expressions:
List the class numbers, names and average marks of the five subjects of all the students in the class
S6A, and displaying the average marks as Average: |
SELECT class_num, name, (clc
+ ue + pm + phy + cs) / 5 AS Average FROM s6a |
Sample output (system dependent):
class_num |
name |
Average |
1 |
Chan Wai Man, Raymond |
62.4000 |
2 |
Chow Chi Ling |
64.6000 |
3 |
Fung Ching Man, Mandy |
56.6000 |
4 |
Hung Wai Ming |
50.6000 |
5 |
Leung King |
48.2000 |
6 |
Poon Kwok Fai |
68.2000 |
7 |
Sung Hing Wah, Patrick |
72.4000 |
8 |
Tang Wing Chi |
74.0000 |
9 |
Wong Ka Tak, Kent |
43.8000 |
10 |
Yeung Chun |
67.6000 |
11 |
Lai Fung Chun |
69.6000 |
12 |
Chan Lai Yin |
55.2000 |
13 |
Man Fook Wing |
51.4000 |
14 |
Chung Kwok Fai, Fred |
56.8000 |
15 |
Lee Lai May |
61.8000 |
|
