SlideShare a Scribd company logo
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

More Related Content

What's hot (14)

PPTX
Null values, insert, delete and update in database
Hemant Suthar
 
PDF
Sql alter table statement
Vivek Singh
 
PPTX
Lab1 select statement
Balqees Al.Mubarak
 
PPT
Retrieving data using the sql select statement
Syed Zaid Irshad
 
DOCX
Query
Raj Devaraj
 
DOCX
Sql reference from w3 schools
Sunil Kumar Gunasekaran
 
PDF
Mysql cheatsheet
Adolfo Nasol
 
PPTX
Where conditions and Operators in SQL
Raajendra M
 
PPT
e computer notes - Advanced subqueries
ecomputernotes
 
PDF
Oracle SQL Part 3
Gurpreet singh
 
PPT
SQL select statement and functions
Vikas Gupta
 
PPTX
2. DML_INSERT_DELETE_UPDATE
Amrit Kaur
 
PDF
Sql basics v2
Yousuf Akhtar Sultan
 
Null values, insert, delete and update in database
Hemant Suthar
 
Sql alter table statement
Vivek Singh
 
Lab1 select statement
Balqees Al.Mubarak
 
Retrieving data using the sql select statement
Syed Zaid Irshad
 
Sql reference from w3 schools
Sunil Kumar Gunasekaran
 
Mysql cheatsheet
Adolfo Nasol
 
Where conditions and Operators in SQL
Raajendra M
 
e computer notes - Advanced subqueries
ecomputernotes
 
Oracle SQL Part 3
Gurpreet singh
 
SQL select statement and functions
Vikas Gupta
 
2. DML_INSERT_DELETE_UPDATE
Amrit Kaur
 
Sql basics v2
Yousuf Akhtar Sultan
 

Viewers also liked (20)

PDF
Introduction to SQL
Ram Kedem
 
PDF
SQL Server
webhostingguy
 
PPTX
SQL – A Tutorial I
Gagan Deep
 
PDF
Introduction to sql database on azure
Antonios Chatzipavlis
 
PPTX
SQL | Computer Science
Transweb Global Inc
 
PDF
SQL in the Hybrid World
Tanel Poder
 
PDF
Aprenda SQL Server
Silva Alvarado Fabian
 
PPTX
Introduction to SQL
Ehsan Hamzei
 
PDF
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Beat Signer
 
PPTX
SQL JOIN
Ritwik Das
 
PDF
SQL Monitoring in Oracle Database 12c
Tanel Poder
 
PPTX
SQL Tutorial for Marketers
Justin Mares
 
PPTX
Sql Functions And Procedures
DataminingTools Inc
 
PPT
Density Function | Statistics
Transweb Global Inc
 
PPT
SQL : introduction
Shakila Mahjabin
 
PPTX
SQL - Structured query language introduction
Smriti Jain
 
PPTX
SQL Basics
Hammad Rasheed
 
PPT
SQL Tutorial - Basic Commands
1keydata
 
PPT
Sql ppt
Anuja Lad
 
PDF
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Beat Signer
 
Introduction to SQL
Ram Kedem
 
SQL Server
webhostingguy
 
SQL – A Tutorial I
Gagan Deep
 
Introduction to sql database on azure
Antonios Chatzipavlis
 
SQL | Computer Science
Transweb Global Inc
 
SQL in the Hybrid World
Tanel Poder
 
Aprenda SQL Server
Silva Alvarado Fabian
 
Introduction to SQL
Ehsan Hamzei
 
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Beat Signer
 
SQL JOIN
Ritwik Das
 
SQL Monitoring in Oracle Database 12c
Tanel Poder
 
SQL Tutorial for Marketers
Justin Mares
 
Sql Functions And Procedures
DataminingTools Inc
 
Density Function | Statistics
Transweb Global Inc
 
SQL : introduction
Shakila Mahjabin
 
SQL - Structured query language introduction
Smriti Jain
 
SQL Basics
Hammad Rasheed
 
SQL Tutorial - Basic Commands
1keydata
 
Sql ppt
Anuja Lad
 
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Beat Signer
 
Ad

Similar to retrieving data using SQL statements (20)

DOCX
Learning sql from w3schools
farhan516
 
PPTX
lect 2.pptx
HermanGaming
 
DOCX
Sql
Archana Rout
 
PPTX
Practical 03 (1).pptx
solangiirfan92
 
PPT
SQL || overview and detailed information about Sql
gourav kottawar
 
PDF
SQL Basics and Advanced for analytics.pdf
trg4294
 
PDF
SQL_BASIC AND ADVANCED.pdf
fayoyiwababajide
 
PDF
SQL notes 1.pdf
JitendraYadav351971
 
PPT
01 basic orders
Soufiane Hakam
 
DOCX
SQL Language
Baker Ahimbisibwe
 
PPTX
XII_IP_PPT[2].pptxhggggggggggggggggggggggggggggggggdj
debu9131
 
PDF
Database Systems - SQL - DDL Statements (Chapter 3/3)
Vidyasagar Mundroy
 
PDF
Basic sqlstatements
Subash T
 
PPSX
Sql2
LuisDeLeon74
 
PPT
CE 279 - WRITING SQL QUERIES umat edition.ppt
minusahsaaka
 
DOC
Introduction to sql
SARVESH KUMAR
 
PPT
Sql query [select, sub] 4
Dr. C.V. Suresh Babu
 
DOC
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
Newyorksys.com
 
PPT
asdasdasdasdsadasdasdasdasdsadasdasdasdsadsadasd
MuhamedAhmed35
 
PPTX
Dbms
JahnaviBhagat
 
Learning sql from w3schools
farhan516
 
lect 2.pptx
HermanGaming
 
Practical 03 (1).pptx
solangiirfan92
 
SQL || overview and detailed information about Sql
gourav kottawar
 
SQL Basics and Advanced for analytics.pdf
trg4294
 
SQL_BASIC AND ADVANCED.pdf
fayoyiwababajide
 
SQL notes 1.pdf
JitendraYadav351971
 
01 basic orders
Soufiane Hakam
 
SQL Language
Baker Ahimbisibwe
 
XII_IP_PPT[2].pptxhggggggggggggggggggggggggggggggggdj
debu9131
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Vidyasagar Mundroy
 
Basic sqlstatements
Subash T
 
CE 279 - WRITING SQL QUERIES umat edition.ppt
minusahsaaka
 
Introduction to sql
SARVESH KUMAR
 
Sql query [select, sub] 4
Dr. C.V. Suresh Babu
 
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
Newyorksys.com
 
asdasdasdasdsadasdasdasdasdsadasdasdasdsadsadasd
MuhamedAhmed35
 
Ad

More from Arun Nair (6)

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

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