SQL Part 4 – Query involving single table only
Learning Outcomes:
- Use SQL to maintain a simple relational database, and manipulate its data or retrieve the required information in at most three tables.
- View, sort, and select contents by filtering, and create different view
- Use appropriate operators and expressions such as arithmetic operators and expressions, comparison operators, logical operators and the in, between and like operators. to perform specific operations
- Perform sub-queries (for one sub-level only)
Relevant past paper:
DSE ICT Elec A(SP-2016): SP 3bde. PP 1cii, 3ab. 2012
1bi,ii,ciii,3bi. 2013 1a, 4dii,e. 2014 1a, 2c. 2015 1bcd. 2016 1abc, 2a,bii.
AS CA(2000-2013): 2000 10ai,iii. 2001 10ai. 2002 4a, 7a.
2003 8abd. 2004 10bde. 2005 8abcef. 2006 9ab. 2007 10abd. 2008 8ab. 2009 8abd.
2010 5aii,b, 8ab. 2011 7b, 10bc. 2012 9bf. 2013 9abc.
Basic format of a query:
- SELECT <field name 1>, <field name 2>…
FROM <table name>
WHERE <conditions>;
- SELECT * à all the fields would be selected, * means all
- SELECT <field name 1> AS <display name 1> à the output would show your display name(which would be more meaningful, we call this alias 別名) instead of the field name
- WHERE clause is optional. Do not use aggregate function in WHERE clause
WHERE <conditions>
Arithmetic
operators: +, -, *, /
Comparison
operators: >, <, =, >=, <=, <>
Logical
operators: AND, NOT, OR
- NOT has highest precedence, followed by AND, and then OR
- e.g. A OR B AND C à work the same as A OR (B AND C). So, if your intention is (A OR B) AND C, the parentheses () must be present
IN,
BETWEEN, LIKE operators:
- IN:
WHERE <field name> IN (“A”, “B”, “C”);
If there is “A”, “B” or “C” in the field, that particular record would be selected. This operator is commonly used in subquery which is in following section. - BETWEEN:
WHERE <field name> BETWEEN “A01” AND “A10”;
If the value of the field is between “A01” and “A10”, inclusively, that particular record would be selected. - LIKE:
WHERE <field name> LIKE “_BCD%”;
This one look for the pattern “BCD” in the field.
_ represents any single character.
% represents string of any length (including absent of any string).
Built-in
functions related to numeric data: See SQL part 2
Order by:
- SELECT <field name 1>, <field name 2>…
FROM <table name>
WHERE <conditions>
ORDER BY <field name 1> ASC, <field name 2> DESC;
The query result will be displayed based on the
lexicographical order(the character order in the encoding table) of the
selected field. ASC means ascending order. DESC means descending order.
Group by:
- SELECT <field name 1>, <field name 2>
FROM <table name>
WHERE <conditions>
GROUP BY <field name 1>;
Usually this is used with aggregate function: see SQL part
2.
E.g. You are interested in the total number of the entities
in a group.
- SELECT <field name 1>, COUNT(*)
FROM <table name>
WHERE <conditions>
GROUP BY <field name 1>;
<field name 1>
|
COUNT(*)
|
A
|
5
|
B
|
10
|
* Group by…Having:
- SELECT <field name 1>, <field name 2>
FROM <table name>
WHERE <condition 1>
GROUP BY <field name 1>
HAVING <condition 2>;
Condition 1 provides information on which records should be
selected. Aggregate function should not be used here.
Condition 2 provides information on which records should be
included in the grouped analysis. This is based on some aggregate functions
(see SQL part 2).
e.g. You want to find out the students who have borrowed
more than 100 books, and their corresponding number of books borrowed in 2019.
Each record represents 1 borrowed item. You would not know
which student has borrowed more than 100 books in year 2019 unless you really
count the number for each student. By using the “group by” function, you can
count the number of books borrowed by each student. Yet, you are only interested
in those who have borrowed more than 100 books. So you can select those by
using the “having clause” which uses aggregate function.
- SELECT StuID, COUNT(*)
FROM Borrow
WHERE YEAR(Dateofborrow) = 2019
GROUP BY StuID
HAVING COUNT(*) > 100;
Related past paper: DSE Elec A: SP 3c. AS CA: 2005 8fi. 2011 10c.
* Sub-query:
This is essentially a query nested in another query. Both
queries can involve the same table or different tables.
- SELECT <field name 1>
FROM <table name>
WHERE <field name 2> IN (SELECT…FROM…WHERE…);
Related past paper: DSE Elec A: SP 3c. 2013 4e. AS CA: 2003
8bd. 2004 10d. 2005 8fii. 2012 7b. 2013 9c.
- SELECT <field name 1>
FROM <table name>
WHERE <field name 2> NOT IN (SELECT…FROM…WHERE…);
Suppose the subquery return the result A, B, C. Those
records would NOT be selected when <field name 2> =A, =B or =C. I.e. All
records would be selected except those with <field name 2> =A, =B or =C.
* Tips: For those questions, the negative words “not” or “never” usually appear in the exam question.
* Tips: For those questions, the negative words “not” or “never” usually appear in the exam question.
Related past paper: DSE Elec A: SP 3be. 20121ciii. 2015 1d. 2016 1c. AS CA: 2004 10be. 2005 8e. 2007 10d. 2009 8d. 2012 9f.