You are on page 1of 4

Retrieving data using the SQL Select Statement

Select has the ability to show raw information, but also to edit information before it’s
displayed. It represents the reason that databases exists: to analyze and transform
information.
It can :
 perform mathematical analysis on numerical data
 perform string manipulation on text data
 format raw numbers for financial report
 abbreviate lengthy data
 sort rows of data according to different data types
 aggregate multiple rows of information in various ways
 create organizational hierarchies
 show summary of data at various levels
 join related information from multiple sources and present its findings at various
levels of detail and aggregation
Example of a select syntax:
SELECT select_list
FROM table_expression
[ WHERE ... ]
[ START WITH start_expression ]
CONNECT BY [NOCYCLE] { PRIOR parent_expr = child_expr | child_expr =
PRIOR parent_expr }
[ ORDER SIBLINGS BY column1 [ ASC | DESC ] [, column2 [ ASC | DESC ] ]
...
[ GROUP BY ... ]
[ HAVING ... ]

Clauses in the Select Statement:


1. The expression list (>=0 columns)
2. The FROM clause (>= 1 tables )
Only 1 and 2 are required for a valid Select Statement.
1. Pseudocolumns are defined by the Oracle system for every table. They are not
actually stored with the table and they return different values for each row of the
table.
Two pseudocolumns that are more common and can be useful:
a) ROWNUM – This is the system-assigned number for a row. If you want to
number the rows of a select statement. But beware ROWNUM is assigned before
the OrderBy clause of a select statement.
b) ROWID – This is the system-assigned physical address for a given row. It can
change from time to time, for example if we export and then import the database.
The Distinct or Unique Clause is used to identify a unique set of values from a
table.
The Distinct clause searches for columns given in the select statement that are
duplicated and returns one unique line of output.
*- Asks for all the columns from the table in the order they are defined in the table
structure.
The data can be transformed by the Select Statement with the help of expressions
Expressions can transform data after it is retrieved from the database and before
the data is produced as the Select Statement output.
Expressions consist of literal values, arithmetic and other operators and SQL
FUNCTIONS.
Literal values: number, character or string, date, interval.
Expressions can be used in:
 A SELECT statement column list, and its WHERE, HAVING and ORDER
BY clause
 Hierarchical queries with the CONNECT BY, START WITH and ORDER
BY clause
 INSERT statement with the VALUES clause
 UPDATE statement SET clause and WHERE clause
 DELETE statement WHERE clause.
Operators and operator precedence :
1. Parentheses
2. Multiplication, Division
3. Addition, Subtraction.
Functions
Functions generally :
- Receive one or more bits of incoming data, known as “parameters”
- Perform some tasks on the data
- Produce a single answer representing the results of the function’s tasks.
The capabilities of select are:
1. Projection
2. Selection
3. Joining
These are also the three fundamental concepts that drive a relational database.
1. Refers to the ability of the select statement to choose a subset of the columns in a
given table.
2. Refers to the ability of the select statement to choose a subset of the rows in a
given table. (WHERE clause)
3. We specify to the database that there is common information shared by at least
two tables and we want to link the data from those tables together according to the
common information that they share.
Describe How Schema Objects Work
 Tables
All the data about a table, its name, columns and the datatypes of those columns are all
stored in a set of system-defined tables that are collectively known as the “data
dictionary”. The Oracle System manages those tables. So even data about your data
(metadata) is stored in tables.
 Constraints
Are rules and restrictions on the sort of data that is added into a table. It is not a database
object but is listed in the data dictionary. If we try to modify, add or delete a row to/from
a table and in so doing violates a constraint then the entire SQL statement will fall with
an execution error.
 View
It has a name like a table and you can execute Insert, Update and Delete statements on a
it. But a view is not a table and it doesn’t store data . It is a Select statement that is stored
in a database with a name assigned to it.
 Indexes
Are used on retrieving data. The difference in performance is potentially significant.
Any future queries that happen to reference any indexed data will:
- Perform an analysis to determine if the query will benefit by using the
index
- If yes, then redirect the focus temporarily on the index, and obtain direct
location of the appropriate rows.
 Sequences
A sequence is a counter, it issues numbers in a particular series. The primary purpose of a
Sequence is to support the process of adding rows in a particular table and providing the
next appropriate value for the PRIMARY KEY for the table.
Provide a mechanism to count off PRIMARY KEY values.
 Synonyms
A synonym is an object that associates an alias with an existing object.
A private synonym is owned by a user and a public synonym is owned by the system user
PUBLIC. Synonyms are used to mask more complex objects names and simplify table
references.

Test :
1. b. ok
2. b, c. ok
3. b. ok
4. b, c. ok
5. a,b,c ok
6. a,d. ok
7. b ok
8. b no -> C
9. c ok
10. d ok
11. b ok
12. b ok
13. d ok
14. a ok
15. a ok
Tricky questions :
1. How many results does it return the following select statement ?
Select DISTINCT FIRST_NAME, LAST_NAME,ZIP_CODE
2. Is this correct? If yes, what is the result of the selection ?
Select ROWNUM, * FROM CUSTOMERS

You might also like