Restricting data

Introduction to Oracle SQL

Sara Billen

Curriculum Manager

Filtering rows

Filter

 

  • Comparison operators
  • Comparison keywords
  • Logical operators
Introduction to Oracle SQL

WHERE

SELECT FirstName, LastName, Country 
FROM Customer
WHERE Country = 'Portugal'
| FirstName | LastName  | Country  |
|-----------|-----------|----------|
| João      | Fernandes | Portugal |
| Madalena  | Sampaio   | Portugal |
Introduction to Oracle SQL

Comparison operators

Operator Description
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
= Equal
<> Not equal
SELECT UnitPrice
FROM Track
WHERE UnitPrice <> 0.99
Introduction to Oracle SQL

Comparison keywords

  Additional WHERE clause features

Introduction to Oracle SQL

Comparison keywords - BETWEEN

SELECT Name, Milliseconds 
FROM Track
WHERE Milliseconds BETWEEN 100000 AND 200000
| Name              | Milliseconds |
|-------------------|--------------|
| Perfect           | 188133       |
| Right Through You | 176117       |
| We Die Young      | 152084       |
| Put You Down      | 196231       |
| ...                              |
Introduction to Oracle SQL

Comparison keywords - IN

SELECT EmployeeId, LastName, Title
FROM Employee
WHERE EmployeeId IN (4, 5, 6)
| EmployeeId | LastName | Title               |
|------------|----------|---------------------|
| 4          | Park     | Sales Support Agent |
| 5          | Johnson  | Sales Support Agent |
| 6          | Mitchell | IT Manager          |
Introduction to Oracle SQL

Comparison keywords - LIKE

Select rows that match a character pattern by using LIKE

 

Symbol Description
% Represents any sequence of zero or more characters
_ Represents any single character
Introduction to Oracle SQL

Comparison keywords - LIKE

SELECT LastName
FROM Employee
WHERE LastName LIKE '_a%'
| LastName |
|----------|
| Park     |
| Callahan |
Introduction to Oracle SQL

Logical operators

  Logical operators

Introduction to Oracle SQL

Logical operators - AND

SELECT InvoiceId, BillingCountry, Total
FROM Invoice
WHERE BillingCountry = 'Australia' 
      AND Total > 4
| InvoiceId | BillingCountry | Total |
|-----------|----------------|-------|
| 66        | Australia      | 5.94  |
| 250       | Australia      | 13.86 |
| 305       | Australia      | 8.91  |
Introduction to Oracle SQL

Logical operators - OR

SELECT InvoiceId, BillingCountry, Total
FROM Invoice
WHERE BillingCountry = 'Australia' 
      OR Total > 4
| InvoiceId | BillingCountry | Total |
|-----------|----------------|-------|
| 3         | Belgium        | 5.94  |
| 4         | Canada         | 8.91  |
| 5         | USA            | 13.86 |
| 10        | Ireland        | 5.94  |
| ...                                |
Introduction to Oracle SQL

Logical operators - NOT

SELECT InvoiceId, BillingCountry, Total
FROM Invoice
WHERE BillingCountry NOT IN ('Australia', 'Canada', 'United Kingdom')
| InvoiceId | BillingCountry | Total |
|-----------|----------------|-------|
| 1         | Germany        | 1.98  |
| 2         | Norway         | 3.96  |
| 3         | Belgium        | 5.94  |
| 5         | USA            | 13.86 |
| ...                                |
Introduction to Oracle SQL

Recap

WHERE: filter rows based on a condition

  • Comparison operators: =, <, >, <=, >=, <>
  • Comparison keywords: BETWEEN, IN, LIKE
  • Logical operators: AND, OR, NOT
Introduction to Oracle SQL

Let's practice!

Introduction to Oracle SQL

Preparing Video For Download...