P. 1
SQL

SQL

|Views: 513|Likes:
Published by manishgtk

More info:

Categories:Types, Research
Published by: manishgtk on Jul 12, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PPT, PDF, TXT or read online from Scribd
See more
See less

06/19/2013

pdf

text

original

‡SELECT

‡INSERT
‡UPDATE
‡DELETE

‡CREATE
‡ALTER
‡DROP
‡RENAME
‡TRUNCATE

‡COMMIT
‡ROLLBACK
‡SAVEPOINT

Data Retrieval
Data Retrieval

Data manipulation language (DML)
Data manipulation language (DML)

Data definition language (DDL)
Data definition language (DDL)

Transaction Control Language (TCL)
Transaction Control Language (TCL)

Data Retrieval

SELECT

Data Retrieval

SELECT

Syntax :

SELECT [ DISTINCT ] ( [ table ] . * | expr [alias ], ... ]

FROM table [ alias ] , ...

[ WHERE [ join condition ] ... ]

[ AND [ row condition ] ... ]

[ OR [ another row condition ] ]

[ GROUP BY [ expr | column ]]

[ HAVING [ group condition ]]

[ ORDER BY [ expr | column ] [ ASC |DESC ] ] ;

SELECT STATEMENT

Consider this query:

Select the most recent registration data of

students. All the students must be listed even if

they do not have any registration details.

Consider two tables:

‡ stud_name, containing the id and names of all

students

‡ stud_reg, having the registration details for the

courses that the students opt for.

Approach 1:

SELECT n.id ID,

n.name Name,

r1.seq Sequence,

r1.sponsor Sponsor,

r1.fee Fee,

r1.regdate "REG DATE"

FROM stud_name n,

stud_reg r1

WHERE r1.stud_id(+) = n.id

AND (r1.rowid IS NULL

OR r1.seq = (SELECT MAX(r2.seq)

FROM stud_reg r2

WHERE r2.stud_id = n.id))

ORDER BY 1;

Approach 2:

SELECT n.id ID,

n.name Name,

TO_NUMBER('') Sequence,

'' Sponsor,

TO_NUMBER('') Fee,

'' "REG DATE"

FROM stud_name n

WHERE NOT EXISTS (SELECT 'x'

FROM stud_reg r

WHERE r.stud_id = n.id)

UNION

SELECT n.id ID,

n.name Name,

r1.seq Sequence,

r1.sponsor Sponsor,

r1.fee Fee,

TO_CHAR(r1.regdate) "REG

DATE"

FROM stud_name n,

stud_reg r1

WHERE r1.stud_id = n.id

AND r1.seq = (SELECT MAX(r2.seq)

FROM stud_reg r2

WHERE r2.stud_id = n.id);

(Approach 2

contd.)

Syntax :

SELECT [ DISTINCT ] ( [ table ] . * | expr [alias ], ... ]

FROM table [ alias ] , ...

[ WHERE [ join condition ] ... ]

[ AND [ row condition ] ... ]

[ OR [ another row condition ] ]

[ GROUP BY [ expr | column ]]

[ HAVING [ group condition ]]

[ ORDER BY [ expr | column ] [ ASC |DESC ] ] ;

SELECT STATEMENT

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->