lab_04: basic sql

13
LOGO 1 Lab_04: Basic SQL Lab_04: Basic SQL

Upload: austin-york

Post on 30-Dec-2015

36 views

Category:

Documents


2 download

DESCRIPTION

Lab_04: Basic SQL. Outline. The ORDER BY Keyword SQL ORDER BY Syntax SQL 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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lab_04:  Basic SQL

LOGO

1

Lab_04: Basic SQLLab_04: Basic SQLLab_04: Basic SQLLab_04: Basic SQL

Page 2: Lab_04:  Basic SQL

2

OutlineThe ORDER BY KeywordSQL ORDER BY SyntaxSQL NULL Values

Page 3: Lab_04:  Basic SQL

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.

Page 4: Lab_04:  Basic SQL

SQL ORDER BY Syntax

SQL ORDER BY Syntax

SELECT column_name(s)FROM table_nameORDER BY

column_name(s) ASC|DESC

Page 5: Lab_04:  Basic SQL

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.

Page 6: Lab_04:  Basic SQL

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:

Page 7: Lab_04:  Basic SQL

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:

Page 8: Lab_04:  Basic SQL

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:

Page 9: Lab_04:  Basic SQL

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.

Page 10: Lab_04:  Basic SQL

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.

Page 11: Lab_04:  Basic SQL

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.

Page 12: Lab_04:  Basic SQL

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

Page 13: Lab_04:  Basic SQL

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: