You are on page 1of 35

Oracle SQL Review

Workshop 1 – SQL SELECT


Statements

Copyright © 2020, Oracle and/or its affiliates. All rights reserved.


Getting Started with SQL Review
• The purpose of these workshops is to give students a
strong foundation in SQL before diving into the
complexities of PL/SQL or other SQL related technology
• The following workshops are available:
− Workshop 1 : SELECT Statements
− Workshop 2 : Functions
− Workshop 3 : Joins
− Workshop 4 : Subqueries

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 3


Workshop 1 : SQL SELECT Statements
• This workshop covers the following objectives:
−Create a basic SQL statement including ORDER BY and WHERE
−Perform and display arithmetic calculations
−Construct a query using a column alias
−Apply the concatenation operator
−Use literal values in a SELECT statement
−Use DISTINCT syntax in a query to eliminate duplicate rows
−Use conditional syntax including BETWEEN, IN, LIKE,IS NULL
and IS NOT NULL in a query

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 4


Selecting Data
• SELECT is the keyword that retrieves columns from a
table
• The FROM clause specifies the tablename
• SELECT * FROM tablename retrieves all the data in a
table
• SELECT <column list> FROM tablename retrieves the
columns specified
• The WHERE clause specifies a condition that restricts
the rows returned by the SELECT statement

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 5


Table Layouts
• Click link below to see table schemas used in this
review:
−SQL Table Design document

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 6


Selecting Data
• Use the SELECT statement to select data from the
WF_COUNTRIES table
SELECT country_name
FROM wf_countries;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 7


Sorting
• ORDER BY specifies the display sequence of the result
• The keywords ASC or DESC can be added after the
column name to specify ascending or descending
sequence
SELECT country_name
FROM wf_countries
ORDER BY country_name;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 8


WHERE Clause
• When retrieving data from the database, you may need
to limit the rows of data that are displayed
• A WHERE clause contains a condition that must be
met, and it directly follows the FROM clause in a SQL
statement
• The syntax for the WHERE clause is:

WHERE column_name comparison_condition comparison_value

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 9


WHERE Clause
• If we want to limit the rows returned from the
wf_countries table to those rows where the value of
region_id is 5, we can add a WHERE clause
SELECT country_name
FROM wf_countries
WHERE region_id = 5;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 10


Try It!
• We will now try executing some SQL statements
• We will use the Oracle Application Express (APEX)
database environment to do this
• To request Oracle Application Express accounts please
see the APEX Startup Guide located on the Oracle
Academy Member Website

Oracle
APEX
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 11
Try It!
• Once logged into APEX, select SQL Workshop
• In SQL Workshop, select the SQL Commands option
• Enter the desired SQL code and click the Run button,
the results will be displayed in the lower pane

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 13


Try it!
• Create a list of all columns and rows in the employees
table. (click for solution)
SELECT * FROM employees;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 15


Try it!
• Create a list that includes the country_name and
region_id of all records in the wf_countries table (click
for solution)
SELECT country_name, region_id
FROM wf_countries;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 16


Try it!
• Create a list of department_ids and
department_names sorted by department_id
(click for solution)
SELECT department_id, department_name
FROM departments
ORDER BY department_id;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 15


Try it!
• Create a list of country names, capitols and area but
only for region 9, sort list by country_name
(click for solution)
SELECT country_name, capitol, area
FROM wf_countries
WHERE region_id = 9
ORDER BY country_name;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 18


Comparison Operators
• The following comparison operators may be used in a
WHERE clause:
• = equal to
• > greater than
• >= greater than or equal to WHERE area >= 6000

• < less than WHERE capitol = 'Buenos Aires'


• <= less than or equal to
WHERE date_of_independence >
• <> not equal to (or != or ^=) '01-Feb-1900'

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 19


BETWEEN, IN and LIKE Operators
• SQL has other operators that add functionality for
retrieving specific sets of data
• These include:
−BETWEEN…AND
−IN
−LIKE
−IS NULL, IS NOT NULL

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 20


BETWEEN...AND
• The BETWEEN...AND operator is used to select and
display rows based on a range of values
• The BETWEEN...AND condition is specified in the
WHERE clause
SELECT country_name, coastline
FROM wf_countries
WHERE coastline BETWEEN 500 AND 550;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 19


IN
• The IN condition is used to test whether a value is in a
specified set of values
• The example shown selects countries that are in region
5 or 9
SELECT region_id, country_name
FROM wf_countries
WHERE region_id IN (5,9);

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 20


LIKE
• The LIKE condition allows you to select rows that
match either literal strings or number patterns
• The % and the underscore (_) are wildcard characters
that you can use to construct a search string
• The % symbol represents any sequence of zero or more
characters
• The underscore (_) symbol represents a single
character.
SELECT country_name,national_holiday_name
FROM wf_countries
WHERE national_holiday_name LIKE
'%Independence%';

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 21


IS NULL, IS NOT NULL

SELECT country_name,
national_holiday_name
FROM wf_countries
IS NOT NULL returns the rows that have a value in the national_holiday_name column.

WHERE national_holiday_name
IS NULL;

SELECT country_name,
national_holiday_name
FROM wf_countries
WHERE national_holiday_name
IS NOT NULL;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 24


Try it!
• Create a list of all countries that begin with the letter
“S” (click for solution)
SELECT country_name
FROM wf_countries
WHERE country_name LIKE 'S%';

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 25


Try it!
• Create a list of countries that have a highest elevation
between 5000 and 6000 sorted from highest to lowest
(click for solution)
SELECT country_name, highest_elevation
FROM wf_countries
WHERE highest_elevation BETWEEN 5000 AND 6000
ORDER BY highest_elevation DESC;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 26


Try it!
• Create a list of employee’s names who do not receive
commission (click for solution)
SELECT first_name, last_name
FROM employees
WHERE commission_pct IS NULL;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 27


Calculations
• This example uses the multiplication operator to
calculate the new area of Benin, if a land reclamation
project increased its area by 2 percent

SELECT country_name, area, area * 1.02


FROM wf_countries
WHERE country_id = 229;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 26


Column Aliases
• The second example uses an alias to display the
calculated value as "New Area"

SELECT country_name, area, area * 1.02 "New Area"


FROM wf_countries
WHERE country_id = 229;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 27


Try it!
• Create a list of all employees showing current salary as
well as a 5% salary increase called “Salary Increase”
(click for solution)
SELECT first_name, last_name, salary, salary * 1.05 "Salary
Increase"
FROM employees;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 30


Concatenation
• Concatenation means to connect or link together in a
series. The concatenation operator is || (2 vertical bars
sometimes referred to as “pipes”)
• A literal value is a character, a number, or a date that is
included in the SELECT list and that is not a column
name or a column alias
• Literal values are often used with concatenation to
create readable text output
SELECT country_name || ' has an area of ' ||
area as "Readable Text"
FROM wf_countries;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 29


DISTINCT
• The DISTINCT keyword is used to eliminate duplicate
rows from the output of an SQL statement
• This example returns all the region IDs from the
WF_COUNTRIES table
SELECT region_id
FROM wf_countries;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 30


DISTINCT
• The DISTINCT keyword is used to eliminate duplicate
rows from the output of an SQL statement
• This example eliminates the duplicates
SELECT DISTINCT region_id
FROM wf_countries;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 31


Try it!
• Create a list of every employee's first name
concatenated to a space and the employee's last name,
and the email of all employees where the email
address contains the string "IN", sorted by email
address, create custom heading for employee name
(click for solution)
SELECT first_name || ' ' || last_name AS "Employee Name",
email
FROM employees
WHERE email LIKE '%IN%'
ORDER BY email;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 34


Try it!
• Create a list of all region_ids in the wf_countries table,
only show each region_id once (click for solution)
SELECT DISTINCT region_id
FROM wf_countries;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 35


Workshop 1 Completed!
• Now that you’ve reviewed SQL SELECT statements you
may want to complete some of the other review
modules:
− Workshop 1 : SELECT Statements
− Workshop 2 : Functions
− Workshop 3 : Joins
− Workshop 4 : Subqueries
−Challenge Practice Problems (Located at end of Workshop 4)

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 36

You might also like