
- •Laboratory work 4
- •Description and examples
- •The Building Blocks of Data Retrieval: select and from clauses
- •The select clause
- •The froMclause
- •The where clause
- •Use of simple conditions
- •Use of comparison operators
- •Logical operators.
- •Column expressions in where clause
- •Special operators
- •Operator in
- •Operator between
- •Operator like
- •Operators is null and is not null.
- •Oracle Lab tasks
- •Вариант 1
- •Вариант 2
- •Вариант 3
- •Вариант 4
- •Вариант 5
- •Вариант 6
- •Вариант 7
- •Вариант 8
- •Вариант 9
- •Вариант 10
- •Вариант 11
- •Вариант 12
- •Вариант 13
- •Вариант 14
- •Вариант 15
- •Вариант 16
- •Вариант 17
- •Вариант 18
- •Control questions
- •Appendices
- •Appendix a. Sql Oracle operators
- •Unary and binary operators
- •Precedence
- •Arithmetic Operators
- •Concatenation Operator
- •Comparison Operators
- •Logical Operators
- •Set Operators
- •Appendix b. Expressions
- •Simple Expressions
- •Cursor Expressions
- •Object Access Expressions
- •Decode Expressions
- •Expression List
- •Appendix c. Commentsin sql
The froMclause
The from clause contains table name or list of table names that are used to show table(s) from which the data should be retrieved.
One table. In simplest case only one table is used. In all previous examples only one table was used.
Multiple tables. If you use many table in FROM clause all rows of the first table concatenated with all rows of the second table. For example, the following query:
SELECT *
FROM FACULTY, DEPARTMENT;
lists all rows of FACULTY table concatenated with all rows of DEPARTMENT table. This query equivalent to Cartesian product operation of relational algebra.
Join two tables. In order to receive semantically sensible result it is necessary to concatenate FACULTY rows only with departments of the faculty.
Using the column FacNo that exists in both of the preceding tables, you may combine the information you had stored in the FACULTY table with information about the faculties departments from the DEPARTMENT table with the help of WHERE clause as it is shown in the next query:
SELECT *
FROM FACULTY, DEPARTMENT
WHERE FACULTY.FacNo = DEPARTMENT.FacNo;
The join that was used is called an equi-join because the goal is to match the values of a column in one table to the corresponding values in the second table.
Join many tables. You may join as many tables as it is necessary. For example, the following query list faculties and their teachers:
SELECT FACULTY.Name, TEACHER,Name
FROM FACULTY, DEPARTMENT, TEACHER
WHERE FACULTY.FacNo = DEPARTMENT.FacNo AND DEPARTMENT.DepNo = TEACHER.DepNo;
Note, in this example DEPARTMENT columns is not displayed, it is used only for joining faculties with teachers. (Usage of logical operators will be discussed later).
Finding the Correct Columns. When more than one table is used in the query you may specify list of selected columns from both tables. Column names may be qualified with table names. Column names may be also renamed with column aliases as it is done in the next query:
SELECT FACULTY.Name AS Faculty_name,
DEPARTMENT.Name AS Department_name,
DEPARTMENT.Fund AS Department_fund
FROM FACULTY, DEPARTMENT
WHERE FACULTY.FacNo = DEPARTMENT.FacNo;
NON-equi-joins. Because SQL supports an equi-join, you might assume that SQL also has a non-equi-join. You would be right! Whereas the equi-join uses an = sign in the WHERE statement, the non-equi-join uses everything but an = sign. For example, the following query lists deparments that have fund more than fund of the corresponding faculty:
SELECT DEPARTMENT.Name, DEPARTMENT.Fund
FROM FACULTY, DEPARTMENT
WHERE FACULTY.Fund < DEPARTMENT.Fund;
Pay attention that in spite of the fact that we have to join two tables in order to receive the necessary result but in the select list we use columns only from one table DEPARTMENT.
Correlated name for the table. In FROM clause table name may be correlated with any other name with the help of table alias. Such correlated name is used to refer to the table in other clauses of the SELECT statement with the name that differ from table name. Syntax of table alias definition is the following:
table_name table_alias_name
For example, the previous query may be rewritten in such a way:
SELECT d.Name, d.Fund
FROM FACULTY f, DEPARTMENT d
WHERE f.Fund < d.Fund;
In this example usage of table aliases is optional, but there may be such queries where table aliases are mandatory
Joining a table to itself. A table may be joined to itself. In such case usage of table aliases is mandatory because it is necessary to distinguish two occurrences of the table. In the following query we receive the list group pairs that have the same rating:
SELECT G1.Num, G2.Num, G1.Raiting
FROM SGROUP G1, SGROUP G2
WHERE G1.GrpNo = G2.GrpNo
Outer join. An outer join extends the result of a simple join. Anouter joinreturns all rows that satisfy the join condition and those rows from one table for which no rows from the other satisfy the join condition. Such rows are not returned by a simple join. To write a query that performs an outer join of tables A and B and returns all rows from A, apply the outer join operator (+) to all columns of B in the join condition. For all rows in A that have no matching rows in B, Oracle returnsNULLfor any select list expressions containing columns of B. In the next example we will receive the list of faculties and their deparments, faculties is present even if they have no departments
SELECT FACULTY.Name AS FacName, DEPARTMENT.Name AS DepName, DEPARTMENT.Fund AS DepFund
FROM FACULTY, DEPARTMENT
WHERE FACULTY.FacNo = DEPARTMENT.FacNo (+);
DUAL table. Oracle contains the table DUAL with no rows and columns. You may address to this table id you want to output any single value that do not selected (or calculated) from any table. For example, the following query outputs the current system date:
SELECT SYSDATE FROM DUAL;
Of cause, in order to receive this date you may use any existing table, for example:
SELECT SYSDATE FROM FACULTY;
but in this case the number of returned rows with current system date will be equal to the number of rows in FACULTY table.