LOGO
1
Lab_04: Basic SQLLab_04: Basic SQLLab_04: Basic SQLLab_04: Basic SQL
2
OutlineThe ORDER BY KeywordSQL ORDER BY SyntaxSQL NULL Values
The ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set by a specified column.
The ORDER BY keyword sort the records in ascending order by default.
If you want to sort the records in a descending order, you can use the DESC keyword.
SQL ORDER BY Syntax
SQL ORDER BY Syntax
SELECT column_name(s)FROM table_nameORDER BY
column_name(s) ASC|DESC
ORDER BY Example
P_IdLastName
FirstName
Address
City
1Hansen
OlaTimoteivn 10
Sandnes
2Svendson
ToveBorgvn 23
Sandnes
3Pettersen
KariStorgt 20
Stavanger
4 Nilsen TomVingvn 23
Stavanger
The "Persons" table:
Now we want to select all the persons from the table above, however, we want to sort the persons by their last name.
ORDER BY ExampleWe use the following SELECT statement:
SELECT * FROM Persons ORDER BY LastName
P_IdLastNa
meFirstNa
meAddress City
1 Hansen OlaTimoteivn 10
Sandnes
4 Nilsen TomVingvn 23
Stavanger
3Pettersen
KariStorgt 20
Stavanger
2Svendson
ToveBorgvn 23
Sandnes
The result-set will look like this:
ORDER BY DESC Example
Now we want to select all the persons from the table above, however, we want to sort the persons descending by their last name.
P_IdLastName
FirstName
Address
City
1Hansen
OlaTimoteivn 10
Sandnes
2Svendson
ToveBorgvn 23
Sandnes
3Pettersen
KariStorgt 20
Stavanger
4 Nilsen TomVingvn 23
Stavanger
The "Persons" table:
ORDER BY DESC Example
P_IdLastNa
meFirstNa
meAddress City
2Svendson
ToveBorgvn 23
Sandnes
3Pettersen
KariStorgt 20
Stavanger
4 Nilsen TomVingvn 23
Stavanger
1 Hansen OlaTimoteivn 10
Sandnes
We use the following SELECT statement:
SELECT * FROM PersonsORDER BY LastName DESC
The result-set will look like this:
SQL NULL Values
NULL values represent missing unknown data.
By default, a table column can hold NULL values.
This chapter will explain the IS NULL and IS NOT NULL operators.
SQL NULL Values
If a column in a table is optional, we can insert a new record or update an existing record without adding a value to this column. This means that the field will be saved with a NULL value.
NULL values are treated differently from other values.
NULL is used as a placeholder for unknown or inapplicable values.
Note: It is not possible to compare NULL and 0; they are not equivalent.
SQL Working with NULL Values
P_IdLastNa
meFirstNa
meAddress City
1 Hansen Ola Sandnes
2Svendson
ToveBorgvn 23
Sandnes
3Pettersen
Kari Stavanger
Look at the following "Persons" table:
Suppose that the "Address" column in the "Persons" table is optional. This means that if we insert a record with no value for the "Address" column, the "Address" column will be saved with a NULL value.
How can we test for NULL values?
It is not possible to test for NULL values with comparison operators, such as =, <, or <>.
We will have to use the IS NULL and IS NOT NULL operators instead.
SQL IS NULL
LastName FirstName Address
Hansen Ola Pettersen Kari
How do we select only the records with NULL values in the "Address" column?We will have to use the IS NULL operator:
SELECT LastName,FirstName,Address FROM Persons
WHERE Address IS NULL
The result-set will look like this:Tip: Always use IS NULL to look for NULL values
SQL IS NOT NULL
LastName FirstName Address
Svendson Tove Borgvn 23
How do we select only the records with no NULL values in the "Address" column?We will have to use the IS NOT NULL operator:
SELECT LastName,FirstName,Address FROM PersonsWHERE Address IS NOT NULL
The result-set will look like this: