retrieving data using sql statements

11

Click here to load reader

Upload: arun-nair

Post on 02-Aug-2015

26 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: retrieving data using SQL statements

SKASC

Oracle Assignment

TOPIC : RETRIEVING DATA USING SQL “SELECT”STATEMENT

- Arun.P(13bca005)

SQL “SELECT” STATEMENT

SELECT statement:-

Page 2: retrieving data using SQL statements

Purpose:

Use a SELECT statement or subquery to retrieve data from one or more tables, object tables, views, object views, or materialized views.

If part or all of the result of a SELECT statement is equivalent to an existing materialized view, then Oracle Database may use the materialized view in place of one or more tables specified in the SELECTstatement. This substitution is called query rewrite. It takes place only if cost optimization is enabled and the QUERY_REWRITE_ENABLED parameter is set to TRUE. To determine whether query write has occurred, use the EXPLAIN PLAN statement.

Syntax

Query [ORDER BY clause] [result offset clause]

[fetch first clause][FOR UPDATE clause][WITH {RR|RS|CS|UR}]

A SELECT statement consists of a query with an optional ORDERBY CLAUSE, an optional result offset clause an optional fetch first clause an optional FOR UPDATE CLAUSE and optionally isolation level. The SELECT statement is so named because the typical first word of the query construct is SELECT. (Query includes the VALUES expression and UNION, INTERSECT, and EXCEPT expressions as well as SELECT expressions).

Page 3: retrieving data using SQL statements

The SQL SELECT statement returns a result set of records from one or more tables.[1][2]

A SELECT statement retrieves zero or more rows from one or more database tables or database views. In most applications, SELECT is the most commonly used Data Manipulation Language (DML) command. As SQL is a declarative programming language, SELECT queries specify a result set, but do not specify how to calculate it. The database translates the query into a "query plan" which may vary between executions, database versions and database software. This functionality is called the "query optimizer" as it is responsible for finding the best possible execution plan for the query, within applicable constraints.

The SELECT statement has many optional clauses:

WHERE specifies which rows to retrieve. GROUP BY groups rows sharing a property so that

an aggregate function can be applied to each group. HAVING selects among the groups defined by the GROUP BY

clause. ORDER BY specifies an order in which to return the rows. AS provides an alias which can be used to temporarily rename

tables or columns.

The ORDERBY CLAUSE  guarantees the ordering of the ResultSet. The result offset clause  and the fetch first clause  can be used to fetch only a subset of the otherwise selected rows, possibly with an offset into the result set. The  FOR UPDATE CLAUSE makes the result set's cursor

Page 4: retrieving data using SQL statements

updatable. The SELECT statement supports the FOR FETCH ONLY clause. The FOR FETCH ONLY clause is synonymous with the FOR READ ONLY clause.

You can set the isolation level in a SELECT statement using the WITH {RR|RS|CS|UR} syntax.

For queries that do not select a specific column from the tables involved in the SELECT statement (for example, queries that use COUNT(*)), the user must have at least one column-level SELECT privilege or table-level SELECT privilege. See GRANT statement for more information.

Example

-- lists the names of the expression -- SAL+BONUS+COMM as TOTAL_PAY and-- orders by the new name TOTAL_PAYSELECT FIRSTNME, SALARY+BONUS+COMM AS TOTAL_PAY FROM EMPLOYEE ORDER BY TOTAL_PAY-- creating an updatable cursor with a FOR UPDATE clause -- to update the start date (PRSTDATE) and the end date (PRENDATE)-- columns in the PROJECT tableSELECT PROJNO, PRSTDATE, PRENDATE FROM PROJECT FOR UPDATE OF PRSTDATE, PRENDATE

Page 5: retrieving data using SQL statements

-- set the isolation level to RR for this statement only SELECT * FROM Flights WHERE flight_id BETWEEN 'AA1111' AND 'AA1112' WITH RR

A SELECT statement returns a ResultSet. A cursor is a pointer to a specific row in ResultSet. In Java applications, all ResultSets have an underlying associated SQL cursor, often referred to as the result set's cursor. The cursor can be updatable, that is, you can update or delete rows as you step through the ResultSet if the SELECT statement that generated it and its underlying query meet cursor updatability requirements, as detailed below. The FOR UPDATE clause can be used to ensure a compilation check that the SELECT statement meets the requiremments of a updatable cursors, or to limit the columns that can be updated.

Requirements for updatable cursors and updatable ResultSets

Only simple, single-table SELECT cursors can be updatable. The SELECT statement for updatable ResultSets has the same syntax as the SELECT statement for updatable cursors. To generate updatable cursors:

The SELECT statement must not include an ORDER BY clause. The underlying Query must be a SelectExpression. The SelectExpression in the underlying Query must not include:

o DISTINCTo Aggregates

Page 6: retrieving data using SQL statements

o GROUP BY clauseo HAVING clauseo ORDER BY clause

The FROM clause in the underlying Query must not have:o more than one table in its FROM clauseo anything other than one table nameo SelectExpression so subqueries

If the underlying Query has a WHERE clause, the WHERE clause must not have subqueries

.

Statement dependency system

The SELECT depends on all the tables and views named in the query and the conglomerates (units of storage such as heaps and indexes) chosen for access paths on those tables. CREATE INDEX does not invalidate a prepared SELECT statement. A DROP INDEX statement invalidates a prepared SELECT statement if the index is an access path in the statement. If the SELECT includes views, it also depends on the dictionary objects on which the view itself depends (see CREATE VIEW statement).

Any prepared UPDATE WHERE CURRENT or DELETE WHERE CURRENT statement against a cursor of a SELECT depends on the SELECT. Removing a SELECT through a java.sql.Statement.closerequest invalidates the UPDATE WHERE CURRENT or DELETE WHERE CURRENT.

Page 7: retrieving data using SQL statements

The SELECT depends on all aliases used in the query. Dropping an alias invalidates a prepared SELECT statement if the statement uses the alias.

How to use expressions in SQL SELECT Statement?

Expressions combine many arithmetic operators, they can be used in SELECT, WHERE and ORDER BY Clauses of the SQL SELECT Statement.

Here we will explain how to use expressions in the SQL SELECT Statement. About using expressions in WHERE and ORDER BY clause, they will be explained in their respective sections.

The operators are evaluated in a specific order of precedence, when more than one arithmetic operator is used in an expression. The order of evaluation is: parentheses, division, multiplication, addition, and subtraction. The evaluation is performed from the left to the right of the expression.

SELECT Statement Example?

If we want to display the first and last name of an employee combined together, the SQL Select Statement would be like

Page 8: retrieving data using SQL statements

SELECT first_name + ' ' + last_name FROM

employee;

Output:

first_name + ' ' + last_name---------------------------------Rahul SharmaAnjali BhagwatStephen FlemingShekar GowdaPriya Chandra

You can also provide aliases as below.

SELECT first_name + ' ' + last_name AS emp_name

FROM employee;

Output:

emp_name-------------Rahul SharmaAnjali BhagwatStephen FlemingShekar GowdaPriya Chandra

Page 9: retrieving data using SQL statements