Retrieving data

Introduction to Oracle SQL

Sara Billen

Curriculum Manager

Customizing output

Crossed wrench and screwdriver

  • Suppress duplicates
  • Manipulate strings
  • Arithmetic expressions
  • Order data
Introduction to Oracle SQL

DISTINCT

SELECT BillingCountry
FROM Invoice
| BillingCountry |
|----------------|
| Germany        |
| Germany        |
| USA            |
| Norway         |
| ...            |

* 412 rows selected

SELECT DISTINCT BillingCountry
FROM Invoice
| BillingCountry |
|----------------|
| Germany        |
| Norway         |
| USA            |
| ...            |

* 24 rows selected

Introduction to Oracle SQL

Manipulating strings - Concatenation operator

SELECT FirstName || LastName
FROM Customer
| FirstName || LastName       |
|-----------------------------|
| FrankHarris                 |
| HannahSchneider             |
| LucasMancini                |
| ...                         |
Introduction to Oracle SQL

Manipulating strings - Literal character strings

SELECT 'Full name: ' || FirstName || ' ' || LastName
FROM Customer
| 'Full name: ' || FirstName || ' ' || LastName |
|-----------------------------------------------|
| Full name: Frank Harris                       |
| Full name: Hannah Schneider                   |
| Full name: Lucas Mancini                      |
| ...                                           |
Introduction to Oracle SQL

Manipulating strings - Alternative quote operator

SELECT LastName || ': This employee's title is: ' || Title
FROM Employee
syntax error at or near "title"
LINE 1: SELECT LastName || ': This employee's title is: ' || Title
Introduction to Oracle SQL

Manipulating strings - Alternative quote operator

SELECT LastName || q'[: This employee's title is: ]' || Title
FROM Employee
| LastName || q'[, this employee's title is: ]' || Title |
|--------------------------------------------------------|
| Adams: This employee's title is: General Manager       |
| Edwards: This employee's title is: Sales Manager       |
| Park: This employee's title is: Sales Support Agent    |
| King: This employee's title is: IT Staff               |
| ...                                                    |
Introduction to Oracle SQL

Arithmetic expressions

Create expressions with number and date values by using arithmetic operators.

Operator Description
+ Add
- Subtract
* Multiply
/ Divide
Introduction to Oracle SQL

Arithmetic expressions

SELECT TrackName, Milliseconds, Milliseconds / 1000
FROM Track
| TrackName                | Milliseconds | Milliseconds / 1000 |
|--------------------------|--------------|---------------------|
| Killer Queen             | 182099       | 182.099             |
| Smells Like Teen Spirit  | 301296       | 301.296             |
| The Star Spangled Banner | 43232        | 43.232              |
| With Or Without You      | 299023       | 299.023             |
Introduction to Oracle SQL

Ordering rows

SELECT LastName, Title, City, HireDate 
FROM Employee
ORDER BY HireDate
| LastName | Title                 | City       | HireDate            |
|----------|-----------------------|------------|---------------------|
| Peacock  | Sales Support Agent   | Calgary    | 2002-04-01 00:00:00 |
| Edwards  | Sales Manager         | Calgary    | 2002-05-01 00:00:00 |
| Adams    | General Manager       | Edmonton   | 2002-08-14 00:00:00 |
| Park     | Sales Support Manager | Calgary    | 2003-05-03 00:00:00 |
| Johnson  | Sales Support Agent   | Calgary    | 2003-10-17 00:00:00 |
| Mitchell | IT Manager            | Calgary    | 2003-10-17 00:00:00 |
| King     | IT Staff              | Lethbridge | 2004-01-02 00:00:00 |
| Callahan | IT Staff              | Lethbridge | 2004-03-04 00:00:00 |
Introduction to Oracle SQL

Ordering rows

Order in descending order:

SELECT LastName, Title, City, HireDate 
FROM Employee
ORDER BY HireDate DESC

Order by multiple columns:

SELECT LastName, Title, City, HireDate 
FROM Employee
ORDER BY City, HireDate
Introduction to Oracle SQL

Let's practice!

Introduction to Oracle SQL

Preparing Video For Download...