0% found this document useful (0 votes)
4 views

Unit 2 - Retrieve Data using the SQL SELECT Statement

The document outlines the capabilities and syntax of SQL SELECT statements, including how to retrieve data, use arithmetic expressions, handle NULL values, and define column aliases. It also covers the use of concatenation operators and the DISTINCT keyword to manage duplicate rows, as well as the DESCRIBE command for viewing table structures. Additionally, it includes examples and best practices for writing SQL statements.

Uploaded by

alin.dobre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit 2 - Retrieve Data using the SQL SELECT Statement

The document outlines the capabilities and syntax of SQL SELECT statements, including how to retrieve data, use arithmetic expressions, handle NULL values, and define column aliases. It also covers the use of concatenation operators and the DISTINCT keyword to manage duplicate rows, as well as the DESCRIBE command for viewing table structures. Additionally, it includes examples and best practices for writing SQL statements.

Uploaded by

alin.dobre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Course code: BCT048

2 Retrieve Data using the SQL SELECT


Statement
Agenda

• Capabilities of SQL SELECT Statements


• Arithmetic expressions and NULL values in the
SELECT statement
• Column aliases
• Use of concatenation operator, literal character strings,
and the DISTINCT keyword
• DESCRIBE command

SQL SELECT Statement


Agenda

• Capabilities of SQL SELECT Statements


• Arithmetic expressions and NULL values in the
SELECT statement
• Column aliases
• Use of concatenation operator, literal character strings,
and the DISTINCT keyword
• DESCRIBE command

SQL SELECT Statement


SQL SELECT Statements
A SELECT statement retrieves information from the database. With a
SELECT statement, you can do the following:

• Projection: Selects the columns in a table that are returned by a


query. Selects a few or as many of the columns as required

• Selection: Selects the rows in a table that are returned by a query.


Various criteria can be used to restrict the rows that are retrieved

• Joins: Brings together data that is stored in different tables by


specifying the link between them

SQL SELECT Statement


Basic SELECT Statement

SELECT {* | [DISTINCT] column | expression [alias],...}


FROM table;
In its simplest form, a SELECT statement must include the following:
• A SELECT clause, which specifies the columns to be displayed
• A FROM clause, which identifies the table containing the columns
that are listed in the SELECT clause
In the syntax:
• SELECT - Is a list of one or more columns (* selects all columns)
• DISTINCT - Suppresses duplicates
• Column | expression - Selects the named column or the expression
• alias - Gives different headings to the selected columns
• FROM table - Specifies the table containing the columns

SQL SELECT Statement


Selecting All Columns

SELECT *
FROM departments;

• You can display all columns of data in a table by following the


SELECT keyword with an asterisk (*)
• You can also display all columns in the table by listing them after
the SELECT keyword

SQL SELECT Statement


Selecting Specific Columns

SELECT location_id, department_id


FROM departments;
• You can use the SELECT statement to display specific
columns of the table by specifying the column names,
separated by commas

SQL SELECT Statement


SQL Statements

• SQL statements are not case sensitive


• SQL statements can be entered on one or more lines
• Keywords cannot be abbreviated or split across lines
• Clauses are usually placed on separate lines
• Indents are used to enhance readability
• In SQL Developer, SQL statements can be optionally
terminated by a semicolon (;). Semicolons are
requiredwhen you execute multiple SQL statements

SQL SELECT Statement


Column Heading Defaults

SQL Developer:
• Default heading alignment: Left-aligned
• Default heading display: Uppercase
In SQL Developer, column headings are displayed in
uppercase and are left-aligned.
SELECT last_name, hire_date, salary
FROM employees;

SQL SELECT Statement


Agenda

• Capabilities of SQL SELECT Statements


• Arithmetic expressions and NULL values in the
SELECT statement
• Column aliases
• Use of concatenation operator, literal character strings,
and the DISTINCT keyword
• DESCRIBE command

SQL SELECT Statement


Arithmetic expressions

An arithmetic expression can contain column names,


constant numeric values, and the arithmetic operators.
Arithmetic Operators:
• Add: +
• Subtract: -
• Multiply: *
• Divide: /

• An arithmetic expression can contain column names, constant


numeric values, and the arithmetic operators
• You can use arithmetic operators in any clause of a SQL statement
(except the FROM clause)

SQL SELECT Statement


Using Arithmetic Operators
SELECT last_name, salary, salary + 300
FROM employees;

• The Oracle server ignores blank spaces before and


after the arithmetic operator
SQL SELECT Statement
Using Arithmetic Operators
Operator Precedence
• If an arithmetic expression contains more than one operator,
multiplication and division are evaluated first. If operators in an
expression are of the same priority, evaluation is done from left to
right
• You can use parentheses to force the expression that is enclosed by
the parentheses to be evaluated first

Example:
SELECT last_name, salary, 12*salary+commission_pct
FROM employees;

SELECT last_name, salary, 12*(salary+commission_pct)


FROM employees;

SQL SELECT Statement


Null Value

• Null is a value that is unavailable, unassigned,


unknown, or inapplicable
• Null is not the same as zero or a blank space

SELECT job_id, salary, commission_pct


FROM employees;

• By default, SQL Developer uses the literal, (null), to identify null


values. It can be set to something more relevant to you
(Preferences -> Advanced Parameters -> Display Null values As)

SQL SELECT Statement


Null Values in Arithmetic Expressions

• Arithmetic expressions containing a null value evaluate


to null
SELECT last_name,12*(salary+commission_pct)
FROM employees;

• If any column value in an arithmetic expression is null, the result is


null. For example, if you attempt to perform division by zero, you
get an error. However, if you divide a number by null, the result is a
null or unknown

SQL SELECT Statement


Agenda

• Capabilities of SQL SELECT Statements


• Arithmetic expressions and NULL values in the
SELECT statement
• Column aliases
• Use of concatenation operator, literal character strings,
and the DISTINCT keyword
• DESCRIBE command

SQL SELECT Statement


Defining a Column Alias

A column alias:
• Renames a column heading
• Is useful with calculations
• Immediately follows the column name (optional AS
keyword between the column name and the alias)
• Requires double quotation marks if it contains spaces
or special characters, or if it is case-sensitive

• An alias cannot be referenced in the column list that


contains the alias definition

SQL SELECT Statement


Defining a Column Alias

A column alias:
• Renames a column heading
• Is useful with calculations
• Immediately follows the column name (optional AS
keyword between the column name and the alias)
• Requires double quotation marks if it contains spaces
or special characters, or if it is case-sensitive

• An alias cannot be referenced in the column list that


contains the alias definition

SQL SELECT Statement


Using Column Aliases
SELECT last_name AS name,commission_pct com
FROM employees;

SELECT last_name "Name" , salary*12 "Annual


Salary"
FROM employees;

SQL SELECT Statement


Agenda

• Capabilities of SQL SELECT Statements


• Arithmetic expressions and NULL values in the
SELECT statement
• Column aliases
• Use of concatenation operator, literal character strings,
and the DISTINCT keyword
• DESCRIBE command

SQL SELECT Statement


Concatenation Operator

A concatenation operator:
• Links columns or character strings to other columns
• Is represented by two vertical bars (||)
• Creates a resultant column that is a character
expression
SELECT last_name||job_id AS "Employees"
FROM employees;

SQL SELECT Statement


• If you concatenate a null value with a character string,
the result is a character string (LAST_NAME || NULL
results in LAST_NAME)
• You can also concatenate date expressions with other
expressions or columns

Literal Character Strings


• A literal is a character, a number, or a date that is
included in the SELECT statement
• Date and character literal values must be enclosed
within single quotation marks
• Each character string is output once for each row
returned
SQL SELECT Statement
Using Literal Character Strings
SELECT last_name ||' is a '||job_id AS
"Employee Details"
FROM employees;

SELECT last_name ||': 1 Month salary =


'||salary Monthly
FROM employees;

SQL SELECT Statement


Duplicate Rows

The default display of queries is all rows, including


duplicate rows.
SELECT department_id
FROM employees;

SELECT distinct department_id


FROM employees;
• The DISTINCT qualifier affects all the selected columns,
and the result is every distinct combination
• You may also specify the keyword UNIQUE, which is a
synonym for the keyword DISTINCT
SQL SELECT Statement
Agenda

• Capabilities of SQL SELECT Statements


• Arithmetic expressions and NULL values in the
SELECT statement
• Column aliases
• Use of concatenation operator, literal character strings,
and the DISTINCT keyword
• DESCRIBE command

SQL SELECT Statement


Table Structure

• Use the DESCRIBE command to display the structure of


a table or select the table in the Connections tree and
use the Columns tab to view the table structure.

DESC[RIBE] tablename

SQL SELECT Statement


Using the DESCRIBE Command

DESCRIBE employees

• Data types: NUMBER(p,s), VARCHAR2(s), DATE.


SQL SELECT Statement
Quiz

Identify the SELECT statements that execute successfully:

a) SELECT first_name, last_name, job_id, salary AS ‘yearly sal’


FROM employees;

b) SELECT first_name, last_name, job_id, salary AS yearly sal


FROM employees;

c) SELECT first_name, last_name, job_id, salary AS yearly_sal


FROM employees;

d) SELECT first_name, last_name job_id, salary AS “yearly_sal”


FROM employees;

SQL SELECT Statement


Practice 2

This practice covers the following topics:

• Selecting all data from different tables


• Describing the structure of tables
• Performing arithmetic calculations and specifying column names

SQL SELECT Statement

You might also like