Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
BD / Labs / English / Lab4-SELECT statement basic features.doc
Скачиваний:
22
Добавлен:
20.02.2016
Размер:
347.14 Кб
Скачать

Laboratory work 4

The SELECT statement in SQL Oracle. Basic features

Contents

1. Purpose of the lab 2

2. Theoretical backgrounds 3

2.1. Query syntax 3

2.2. Purpose 4

2.3. Prerequisites 4

2.4. Keywords and Parameters 4

3. Description and examples 6

3.1. The Building Blocks of Data Retrieval: SELECT and FROM clauses 6

3.1.1. The SELECT clause 6

3.1.2. The FROM clause 7

3.2. The WHERE clause 8

3.2.1. Use of simple conditions 8

3.2.1.1. Use of comparison operators 8

3.2.1.2. Logical operators. 9

3.2.1.3. Column expressions in WHERE clause 9

3.2.2. Special operators 9

3.2.2.1. Operator IN 9

3.2.2.2. Operator BETWEEN 10

3.2.2.3. Operator LIKE 10

3.2.2.4. Operators IS NULL and IS NOT NULL. 11

4. Oracle Lab tasks 12

4.1. Вариант 1 12

4.2. Вариант 2 13

4.3. Вариант 3 13

4.4. Вариант 4 14

4.5. Вариант 5 14

4.6. Вариант 6 15

4.7. Вариант 7 16

4.8. Вариант 8 16

4.9. Вариант 9 18

4.10. Вариант 10 19

4.11. Вариант 11 19

4.12. Вариант 12 20

4.13. Вариант 13 21

4.14. Вариант 14 22

4.15. Вариант 15 23

4.16. Вариант 16 23

4.17. Вариант 17 24

4.18. Вариант 18 25

5. Control questions 25

6. Appendices 26

6.1. Appendix A. SQL Oracle operators 26

6.1.1. Unary and binary operators 26

6.1.2. Precedence 26

6.1.3. Arithmetic Operators 27

6.1.4. Concatenation Operator 27

6.1.5. Comparison Operators 27

6.1.6. Logical Operators 28

6.1.7. Set Operators 28

6.2. Appendix B. Expressions 29

6.2.1. Simple Expressions 30

6.2.2. Compound Expressions 31

6.2.3. Variable Expressions 31

6.2.4. Built-In Function Expressions 31

6.2.5. User-Defined Function Expressions 31

6.2.6. Type Constructor Expressions 31

6.2.7. CAST Expressions 32

6.2.8. CURSOR Expressions 32

6.2.9. Object Access Expressions 33

6.2.10. DECODE Expressions 33

6.2.11. Expression List 33

6.3. Appendix C. Comments in SQL 33

  1. Purpose of the lab

  • To study general SQL Oracle possibilities in searching data in database tables.

  • To acquire practical skills in searching data in database tables by using SQL*Plus.

  1. Theoretical backgrounds

    1. Query syntax

    1. Purpose

To retrieve data from one or more tables.

    1. Prerequisites

For you to select data from a table, the table must be in your own schema or you must have the SELECTprivilege on the table

    1. Keywords and Parameters

DISTINCT | UNIQUE  

returns only one copy of each set of duplicate rows selected (these two keywords are synonymous). Duplicate rows are those with matching values for each expression in the select list.  

ALL  

returns all rows selected, including all copies of duplicates. The default is ALL.  

*  

selects all columns from all tables listed in the FROM clause.  

schema

is the schema containing the selected table. If you omit schema, Oracle assumes the table is in your own schema.  

table.*

selects all columns from the specified table. You can use the schema qualifier to select from a table other than your own. A query that selects rows from two or more tables is a join. For more information about joins, see Lab???.  

expr

selects an expression. See the syntax description of expr in this Lab later. A column name in this list can be qualified with schema only if the table containing the column is qualified with schema in the FROM clause.  

c_alias

provides a different name for the column expression and causes the alias to be used in the column heading. The AS keyword is optional. The alias effectively renames the select list item for the duration of the query. The alias can be used in the ORDER BY, but not other clauses in the query.  

FROM

 

specifies the table or subquery list from which data is selected.  

table

is the name of a table from which data is selected.  

t_alias

provides a correlation name for the table or subquery for evaluating the query and is most often used in a correlated query. Other references to the table throughout the query must refer to the alias.  

subquery

is a query that determines a table from which data is selected.

WHERE  

restricts the rows selected to those that satisfy one or more conditions.

condition can be any valid SQL condition that is based on the columns from tables in FROM clause. See the syntax description of condition in Lab2 Appendix.

outer_join applies only if the FROM clause specifies more than one table. This special form of condition requires Oracle to return all the rows that satisfy the condition, as well as all the rows from one of the tables for which no rows of the other table satisfy the condition. For more information, including rules and restrictions that apply to outer joins, see Lab???.

If one of the elements in the FROM clause is actually a nested table, you cannot specify the outer-join syntax in the WHERE clause.

If you omit this clause, Oracle returns all rows from the tables, in the FROM clause.  

GROUP BY  

 

 

 

groups the selected rows based on the value of exp for each row, and returns a single row of summary information for each group

Expressions in GROUP BY clause can contain any columns in the tables in the FROM clause, regardless of whether the columns appear in the select list.  

HAVING  

restricts the groups of rows returned to those groups for which the specified condition is TRUE. If you omit this clause, Oracle returns summary rows for all groups.

Specify GROUP BY and HAVING after the WHERE clause. If you specify both GROUP BY and HAVING, they can appear in either order.  

See also the syntax description of expr in Appendix of this Lab and the syntax description of condition in Appendix of Lab2.  

UNION | UNION ALL | INTERSECT | MINUS  

 

are set operators that combine the rows returned by two SELECT statements into a single result. The number and datatypes of the columns selected by each component query must be the same, but the column lengths can be different.

If you combine more than two queries with set operators, Oracle evaluates adjacent queries from left to right. You can use parentheses to specify a different order of evaluation.

For information on these operators, see description of set operators in ??? .  

Restrictions:

  • To reference a column, you must use an alias to name the column.

  • You cannot specify the ORDER BY in the subqueryof these operators.

ORDER BY  

 

 

orders rows returned by the statement. Without this clause, no guarantee exists that the same query executed more than once will retrieve rows in the same order.

  • expr orders rows based on their value forexpr. The expression is based on columns in the select list or columns in the tables in theFROMclause.

  • position orders rows based on their value for the expression in this position of the select list;positionmust be an integer.

  • c_alias orders rows based on their value of the column (expression) with this alias in select list.

  • ASCandDESCspecify either ascending or descending order.ASC is the default.

You can specify multiple expressions in the ORDER BY clause. Oracle first sorts rows based on their values for the first expression. Rows with the same value for the first expression are then sorted based on their values for the second expression, and so on. Oracle sorts nulls following all others in ascending order and preceding all others in descending order.  

Restrictions:

  • If you have specified the DISTINCToperator in this statement, this clause cannot refer to columns unless they appear in the select list.

  • If you specify GROUP BYclause in the same statement, thisORDER BYclause is restricted to the following expressions:

- Constants

- Aggregate functions

- Expressions identical to those in the group by clause

- Expressions involving the above expressions that evaluate to the same value for all rows in a group.

Соседние файлы в папке English