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

SQL query is described with SELECT statement and consists of the followingclauses:

SELECT, FROM, WHERE, GROUP BY, HAVING, UNION, UNION ALL, INTERSECT, MINUS, ORDER BY.

There are some other clauses in SELECT statement that will not be studied. Among listed above clauses only SELECT and FROM are obligatory, all others – optional.

In this lab we will study general possibilities of SELECT, FROM and WHERE clauses.

    1. The Building Blocks of Data Retrieval: select and from clauses

      1. The select clause

In the simplest case SELECT clause contains list of the columns to be displayed and FROM identify the table name.

Display specified columns. To display specified columns they should be listed in SELECT clause. For example, the following query outputs faculty’s names and deans:

SELECT Name, Dean

FROM FACULTY;

Display all columns of the table. To display all columns of the table you may list all of them in SELECT clause or use ‘*’ character in the list. The following query outputs all FACULTY table:

SELECT *

FROM FACULTY;

Column name qualification with table name. Column name may be qualified with table name by using the following syntax:

table_name.column_name

The column qualification is needed when query uses many tables (see later) and different tables have the same column names. Column qualification may be used in any case even if there is no necessary to do it. For example, the following query is correct:

SELECT ROOM.Num, ROOM.Course, ROOM.Quantity

FROM ROOM;

Remove duplication. You may remove rows duplication in output result by using DISTINCT or UNIQUE keyword in SELECT clause. This keyword is applied to the rows but not to the individual column. For example, the following two query are equivalent and give list of all different posts that present in database:

SELECT DISTINCT Post SELECT UNIQUE Post

FROM TEACHER; FROM TEACHER;

The keyword ALL means that all duplicated rows must be output. It is a default value if mentioned above keywords are absent. The following two queries are equivalent:

SELECT ALL Post SELECT Post

FROM TEACHER; FROM TEACHER;

Expressions in SELECT list. List in SELECT clause may contain not only column names but also arbitrary expressions that are supported by SQL Oracle. For example the following query is correct:

SELECT Name, Salary, Salary + Commission, Commission * 100 / (Salary + Commission)

FROM TEACHER;

Literals in SELECT list. SELECT list item may be a literal or literal expression. During output column with literal will contain the literal in all rows. For example, the following query:

SELECT 'The teacher' Name, 'has salary' Salary, 'UAH'

FROM TEACHER;

may have the following output:

Name Salary

------------ ----------- ----------- ---------- ---

The teacher Ivanov has salary 500 UAH

The teacher Petrov has salary 470 UAH

Renaming of SELECT list items. Any item in SELECT list may be renamed with a help of so called “column aliases”. It is especially useful when it is necessary to refer to the item that is an expression. Column alias is also used as an column header during output of the query results. In the following query both items in SELECT list are renamed with column aliases:

SELECT Name AS Teacher_name,

Salary + Commission AS Total_salary

FROM TEACHER;

The keyword AS is optional.

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