SlideShare une entreprise Scribd logo
1  sur  9
SKASC
OracleAssignment
TOPIC: RETRIEVING DATA USING
SQL “SELECT”STATEMENT
- Arun.P(13bca005)
SQL “SELECT” STATEMENT
SELECT statement:-
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).
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
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_PAY
SELECT 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 table
SELECT PROJNO, PRSTDATE, PRENDATE
FROM PROJECT
FOR UPDATE OF PRSTDATE, PRENDATE
-- 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 DISTINCT
o Aggregates
o GROUP BY clause
o HAVING clause
o ORDER BY clause
 The FROM clause in the underlying Query must not have:
o more than one table in its FROM clause
o anything other than one table name
o SelectExpressions
o 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.
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
SELECT first_name + ' ' + last_name FROM
employee;
Output:
first_name + ' ' + last_name
---------------------------------
Rahul Sharma
Anjali Bhagwat
Stephen Fleming
Shekar Gowda
Priya Chandra
You can also provide aliases as below.
SELECT first_name + ' ' + last_name AS
emp_name FROM employee;
Output:
emp_name
-------------
Rahul Sharma
Anjali Bhagwat
Stephen Fleming
Shekar Gowda
Priya Chandra

Contenu connexe

Tendances (14)

Null values, insert, delete and update in database
Null values, insert, delete and update in databaseNull values, insert, delete and update in database
Null values, insert, delete and update in database
 
Sql alter table statement
Sql alter table statementSql alter table statement
Sql alter table statement
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
Retrieving data using the sql select statement
Retrieving data using the sql select statementRetrieving data using the sql select statement
Retrieving data using the sql select statement
 
Query
QueryQuery
Query
 
Sql reference from w3 schools
Sql reference from w3 schools Sql reference from w3 schools
Sql reference from w3 schools
 
SQL Programming
SQL ProgrammingSQL Programming
SQL Programming
 
Mysql cheatsheet
Mysql cheatsheetMysql cheatsheet
Mysql cheatsheet
 
Where conditions and Operators in SQL
Where conditions and Operators in SQLWhere conditions and Operators in SQL
Where conditions and Operators in SQL
 
e computer notes - Advanced subqueries
e computer notes - Advanced subqueriese computer notes - Advanced subqueries
e computer notes - Advanced subqueries
 
Oracle SQL Part 3
Oracle SQL Part 3Oracle SQL Part 3
Oracle SQL Part 3
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
2. DML_INSERT_DELETE_UPDATE
2. DML_INSERT_DELETE_UPDATE2. DML_INSERT_DELETE_UPDATE
2. DML_INSERT_DELETE_UPDATE
 
Sql basics v2
Sql basics v2Sql basics v2
Sql basics v2
 

En vedette

En vedette (20)

Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
SQL Server
SQL ServerSQL Server
SQL Server
 
SQL – A Tutorial I
SQL – A Tutorial  ISQL – A Tutorial  I
SQL – A Tutorial I
 
Introduction to sql database on azure
Introduction to sql database on azureIntroduction to sql database on azure
Introduction to sql database on azure
 
SQL | Computer Science
SQL | Computer ScienceSQL | Computer Science
SQL | Computer Science
 
SQL in the Hybrid World
SQL in the Hybrid WorldSQL in the Hybrid World
SQL in the Hybrid World
 
Aprenda SQL Server
Aprenda SQL ServerAprenda SQL Server
Aprenda SQL Server
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
 
SQL Monitoring in Oracle Database 12c
SQL Monitoring in Oracle Database 12cSQL Monitoring in Oracle Database 12c
SQL Monitoring in Oracle Database 12c
 
SQL Tutorial for Marketers
SQL Tutorial for MarketersSQL Tutorial for Marketers
SQL Tutorial for Marketers
 
Sql Functions And Procedures
Sql Functions And ProceduresSql Functions And Procedures
Sql Functions And Procedures
 
Density Function | Statistics
Density Function | StatisticsDensity Function | Statistics
Density Function | Statistics
 
SQL : introduction
SQL : introductionSQL : introduction
SQL : introduction
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
Sql ppt
Sql pptSql ppt
Sql ppt
 
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
 

Similaire à retrieving data using SQL statements

Sql where clause
Sql where clauseSql where clause
Sql where clause
Vivek Singh
 
Sql where clause
Sql where clauseSql where clause
Sql where clause
Vivek Singh
 
Application development using Microsoft SQL Server 2000
Application development using Microsoft SQL Server 2000Application development using Microsoft SQL Server 2000
Application development using Microsoft SQL Server 2000
webhostingguy
 

Similaire à retrieving data using SQL statements (20)

ADVANCED MODELLING.pptx
ADVANCED MODELLING.pptxADVANCED MODELLING.pptx
ADVANCED MODELLING.pptx
 
MULTIPLE TABLES
MULTIPLE TABLES MULTIPLE TABLES
MULTIPLE TABLES
 
Les18
Les18Les18
Les18
 
SQL Query
SQL QuerySQL Query
SQL Query
 
Sql where clause
Sql where clauseSql where clause
Sql where clause
 
Sql where clause
Sql where clauseSql where clause
Sql where clause
 
Nested queries in database
Nested queries in databaseNested queries in database
Nested queries in database
 
Assignment 3
Assignment 3Assignment 3
Assignment 3
 
Application development using Microsoft SQL Server 2000
Application development using Microsoft SQL Server 2000Application development using Microsoft SQL Server 2000
Application development using Microsoft SQL Server 2000
 
Lecture 3. MS SQL. Cursors.
Lecture 3. MS SQL. Cursors.Lecture 3. MS SQL. Cursors.
Lecture 3. MS SQL. Cursors.
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
Assg2 b 19121033-converted
Assg2 b 19121033-convertedAssg2 b 19121033-converted
Assg2 b 19121033-converted
 
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQueryPPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
 
Les06
Les06Les06
Les06
 
Oracle SQL Part 2
Oracle SQL Part 2Oracle SQL Part 2
Oracle SQL Part 2
 
PL/SQL - CURSORS
PL/SQL - CURSORSPL/SQL - CURSORS
PL/SQL - CURSORS
 
SignalR & SQL Dependency
SignalR & SQL DependencySignalR & SQL Dependency
SignalR & SQL Dependency
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
 
STRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESSTRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIES
 
Oracle performance tuning for java developers
Oracle performance tuning for java developersOracle performance tuning for java developers
Oracle performance tuning for java developers
 

Plus de Arun Nair (6)

SAP in PRECOT MERIDIAN Ltd.
SAP in PRECOT MERIDIAN Ltd.SAP in PRECOT MERIDIAN Ltd.
SAP in PRECOT MERIDIAN Ltd.
 
basic programs in C++
basic programs in C++ basic programs in C++
basic programs in C++
 
c++
c++ c++
c++
 
remote method invocation
remote method invocationremote method invocation
remote method invocation
 
process models- software engineering
process models- software engineeringprocess models- software engineering
process models- software engineering
 
BITCOIN TECHNOLOGY(AAappZZ)
BITCOIN TECHNOLOGY(AAappZZ)BITCOIN TECHNOLOGY(AAappZZ)
BITCOIN TECHNOLOGY(AAappZZ)
 

retrieving data using SQL statements

  • 1. SKASC OracleAssignment TOPIC: RETRIEVING DATA USING SQL “SELECT”STATEMENT - Arun.P(13bca005)
  • 2. SQL “SELECT” STATEMENT SELECT statement:- 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}]
  • 3. 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). 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.
  • 4.  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 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_PAY SELECT FIRSTNME, SALARY+BONUS+COMM AS TOTAL_PAY FROM EMPLOYEE ORDER BY TOTAL_PAY
  • 5. -- creating an updatable cursor with a FOR UPDATE clause -- to update the start date (PRSTDATE) and the end date (PRENDATE) -- columns in the PROJECT table SELECT PROJNO, PRSTDATE, PRENDATE FROM PROJECT FOR UPDATE OF PRSTDATE, PRENDATE -- 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
  • 6. 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 DISTINCT o Aggregates o GROUP BY clause o HAVING clause o ORDER BY clause  The FROM clause in the underlying Query must not have: o more than one table in its FROM clause o anything other than one table name o SelectExpressions o 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).
  • 7. 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. 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.
  • 8. 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 SELECT first_name + ' ' + last_name FROM employee; Output: first_name + ' ' + last_name --------------------------------- Rahul Sharma Anjali Bhagwat Stephen Fleming Shekar Gowda Priya Chandra You can also provide aliases as below. SELECT first_name + ' ' + last_name AS emp_name FROM employee; Output: emp_name ------------- Rahul Sharma