SQL Notes Week 4 SC1
SQL Notes Week 4 SC1
Programming Studio 1
SQL
Santha Sumanasekara
RMIT Classification: Trusted
An SQL query
can be as simple
as this.
What is SQL?
- SQL stands for Structured Query Language.
SQLiteStudio
Demo
RMIT Classification: Trusted
Available in SQLite
format,
downloadable from
Sample
Databases and
Tools module.
World Database
Demo
RMIT Classification: Trusted
RMIT Classification: Trusted
RMIT Classification: Trusted
RMIT Classification: Trusted
RMIT Classification: Trusted
SQL in 10 Minutes!
Represents all
- Display all the countries in the world! attributes.
SELECT *
FROM country;
RMIT Classification: Trusted
Selecting Columns
Choose which
attributes to be
displayed
- Display names and their continents of all the countries in the world!
This clause
specifies that
Changing Display Order results to be
displayed on
Alphabetical order
of Country Names.
SQL in 10 Minutes!
- 5 Minutes to go!
RMIT Classification: Trusted
This clause
This binary
Selecting Rows operator connects
two conditions.
Counting Rows
With a better
heading!
Where clause
SQL in 10 Minutes!
- Time’s up!
SQL in 3 weeks!
- In our first example, we use two boolean operators “=“ and “>=“,
say countries in Europe and population greater than 10 Million.
- Other operators: <, <=, <>, are all possible.
- Sometimes we find that some attribute values in some rows are not
defined, unspecified, not known yet.
- For example, we do not know the population of some islands in
Antarctica.
- They are denoted by special value called NULL. (It’s not the character
string ‘NULL’, it’s a special value).
RMIT Classification: Trusted
% is a wildcard
that stands for 0
Partial Matching in WHERE or more
characters.
clause
- List all “Island Nations” – more specifically where the word “Island” in
their name.
- This is partial matching.
- We use LIKE operator for partial matching.
What if we have
to compare
Using Sets in WHERE clause against 100
values?
Eliminating Duplicates
This shows Oceania 28 times,
Asia 51 times, etc.
- Sometimes result sets contain duplicates.
- At best they are just an annoyance, however, they can lead into
incorrect outcomes, specially if counting of results is used.
- Use SELECT DISTINCT, in place of SELECT Correctly shows 7
- Display names of continents continents.
SELECT continent
FROM country;
SELECT count(continent)
FROM country;
ORDER BY clause
Alphabetical order
ORDER BY clause
Reverse Alphabetical order
ORDER BY clause
- You can have multiple attributes to sort on: say irst sort on continent,
then sort on descending order of population.