You are on page 1of 34

Oracle Aliases

Oracle Aliases
• When you query data from a table, Oracle
uses the column names of the table for
displaying the column heading
• For example, the following statement returns
the first name and last name of employees:
• SELECT first_name, last_name FROM
employees ORDER BY first_name;
Oracle Aliases
• SELECT first_name, last_name FROM
employees ORDER BY first_name;
Oracle Aliases
• In this example, first_name and last_name 
column names were quite clear. However,
sometimes, the column names are quite
vague for describing the meaning of data such
as:
• SELECT lstprc, prdnm FROM long_table_name;
Oracle Aliases
• To better describe the data displayed in the
output, you can substitute a column alias for
the column name in the query results.
• For instance, instead of
using first_name and last_name, you might
want to use forename and surname for display
names of employees.
Oracle Aliases
• To instruct Oracle to use a column alias, you
simply list the column alias next to the column
name in the SELECT clause as shown below:
• SELECT first_name AS forename, last_name
AS surname FROM employees;
AS keyword
• The AS keyword is used to distinguish between
the column name and the column alias.
Because the AS keyword is optional, you can
skip it as follows:
• SELECT first_name forename, last_name
surname FROM employees;
Oracle Aliases
• Using Oracle column alias to make column
heading more meaningful
• By default, Oracle capitalizes the column
heading in the query result. If you want to
change the letter case of the column heading,
you need to enclose it in quotation marks (“”).
• SELECT first_name "Forename", last_name
"Surname" FROM employees;
Oracle Aliases
• SELECT first_name "Forename", last_name
"Surname" FROM employees;
Oracle Aliases
• If the column alias consists of only one word
without special symbols like space, you don’t
need to enclose it in quotation marks.
Otherwise, you must enclose the column
heading in quotation marks or you will get an
error.
• See the following query:
• SELECT first_name "First Name", last_name
"Family Name" FROM employees;
Oracle Aliases
See the following query:
• SELECT first_name "First Name", last_name
"Family Name" FROM employees;
• In this example, we used the column aliases "First
Name" and "Family Name" enclosed in quotation
marks. If you remove the quotation marks, Oracle
will issue the following error:
• ORA-00923: FROM keyword not found where
expected
Oracle Aliases
• Using Oracle column alias for expression
• Besides making the column headings more
meaningful, you can use the column alias for
an expression, for example:
• SELECT first_name || ' ' || last_name FROM
employees;
Oracle Aliases
• In the above query, we concatenated the first
name, space, and the last name to construct
the full name. As shown in the output, Oracle
used the expression for the column heading
which was not intuitive.
• The following query uses a column alias for the
expression:
• SELECT first_name || ' ' || last_name AS "Full
Name" FROM employees;
Oracle Aliases
• Similarly, the following statement uses a
column alias for gross profit calculation
expression:
• SELECT product_name, list_price -
standard_cost AS gross_profit FROM
products;
Oracle Aliases
• Using Oracle column alias with ORDER BY clause
• You can use the column alias in the ORDER BY clause to sort the
result set. See the following example:

• SELECT
• product_name,
• list_price - standard_cost AS gross_profit
• FROM
• products
• ORDER BY
• gross_profit DESC;
Oracle Aliases
• A table alias improves the readability of the query
and reduces the number of keystrokes.
• SELECT e.first_name employee,
• m.first_name manager
• FROM
• employees e
• INNER JOIN employees m
• ON
• m.employee_id = e.employee_id;
BETWEEN keyword
• The BETWEEN operator allows you to specify a
range to test. When you use the BETWEEN
operator to form a search condition for rows
returned by a SELECT statement, only rows
whose values are in the specified range are
returned.
• The following illustrates the syntax of
the BETWEEN operator:
• expression [ NOT ] BETWEEN low AND high
BETWEEN keyword
• In this syntax:
• A) low and high
• The  low and high specify the lower and upper values
of the range to test. The low and high values can be
literals or expressions.
• B) expression
• is the expression to test for in the range defined
by low and high. To be able to compare, the data
types of expression, low, and high must be the same.
BETWEEN keyword
• C)AND operator
• The AND operator acts as a placeholder
to separate between low and high.
• The BETWEEN operator returns true if the
value of expression is greater than or
equal (>=) to low and less than or equal
to high.
• value >= low AND value <= high
.
Between Keyword
• The NOT BETWEEN operator negates the
result of the BETWEEN operator.
• The BETWEEN operator is often used in the
WHERE clause of the SELECT, DELETE, and
UPDATE statement.
Example
• Let’s look at some examples of using the
Oracle BETWEEN operator.
• A) Oracle BETWEEN numeric values example
• The following statement returns products whose
standard costs are between 500 and 600:
• SELECT product_name, standard_cost FROM
products WHERE standard_cost BETWEEN 500
AND 600 ORDER BY standard_cost;
Example
• In this example, we compared the values in the
standard cost ( standard_cost) column with a
range from 500 to 600. The query returned only
products whose standard costs are between
that range:
• Oracle BETWEEN numbers exampleTo query
products whose standard costs are not between
500 and 600, you add the NOT operator to the
above query as follows:
Example
• SELECT product_name, standard_cost FROM
products WHERE standard_cost NOT
BETWEEN 500 AND 600 ORDER BY
product_name;
Example
• Oracle BETWEEN dates example
• Let’s use the orders table in the sample
database for the demonstration:
• The following statement returns the orders
placed by customers between December 1,
2016, and December 31, 2016:
Example
• SELECT order_id, customer_id, status,
order_date FROM orders WHERE order_date
BETWEEN DATE '2016-12-01' AND DATE '2016-
12-31' ORDER BY order_date;
AS keyword
• The AS command is used to rename a column
or table with an alias.
• An alias only exists for the duration of the
query.
LIKE Operator
• Sometimes, you want to query data based on a
specified pattern. For example, you may want to
find contacts whose last names start with 'St' or
first names end with 'er'. In this case, you use
the Oracle LIKE operator.
• The syntax of the Oracle LIKE operator is as
follows:
• expresion [NOT] LIKE pattern [ ESCAPE
escape_characters ]
LIKE Operator
• In this syntax, we have:
• 1) expression
• The expression is a column name or an expression that
you want to test against the pattern.
• 2) pattern
• The pattern is a string to search for in the expression.
The pattern includes the following wildcard characters:
• % (percent) matches any string of zero or more character.
• _ (underscore) matches any single character.
LIKE Operator
• 3) escape_character
• The escape_character is a character that appears in front
of a wildcard character to specify that the wildcard should
not be interpreted as a wildcard but a regular character.
• The escape_character, if specified, must be one character
and it has no default value.
• The LIKE operator returns true if the expression matches
the pattern. Otherwise, it returns false.
• The NOT operator, if specified, negates the result of
the LIKE operator.
Example
• Let’s take some examples of using the Oracle LIKE
operator to see how it works.
• We will use the contacts table in the sample database
for the demonstration:
• A) % wildcard character examples
• The following example uses the % wildcard to find the
phones of contacts whose last names start with 'St':
• SELECT first_name, last_name, phone FROM contacts
WHERE last_name LIKE 'St%' ORDER BY last_name;
Example
• The following picture illustrates the result:
• In this example, we used the pattern:
• 'St%'
• The LIKE operator matched any string that
starts with 'St' and is followed by any number
of characters e.g., Stokes, Stein, or Steele, etc.
Example
• To find the phone numbers of contacts whose
last names end with the string 'er', you use the
following statement:
• SELECT first_name, last_name, phone FROM
contacts WHERE last_name LIKE '%er' ORDER
BY last_name;
Example
• The pattern:
• %er
• matches any string that ends with the 'er' string.
• To perform a case-insensitive match, you use
either LOWER() or UPPER() function as follows:
• UPPER( last_name ) LIKE 'ST%' LOWER(last_name
LIKE 'st%'
• For example, the following statement finds emails
of contacts whose first names start with CH:

You might also like