You are on page 1of 3

How To Use Subqueries in the FROM clause?

If you have a query returning many rows of data, and you want to perform another
query on those rows, you can put the first query as a subquery in the FROM clau
se of the second query. The following statement shows you how to use a subquery
as base table for the main query:
SQL> SELECT * FROM (
2 SELECT first_name, last_name, department_name
3 FROM employees e, departments d
4 WHERE e.department_id = d.department_id
5 ) WHERE department_name LIKE 'S%' ORDER BY last_name;
FIRST_NAME LAST_NAME DEPARTMENT_NAME
----------------- ---------------------- ---------------
Ellen Abel Sales
Sundar Ande Sales
Mozhe Atkinson Shipping
Amit Banda Sales
Elizabeth Bates Sales
Sarah Bell Shipping
......
How To Count Groups Returned with the GROUP BY Clause?
If you use the COUNT(*) function on groups returned with the GROUP BY clause, it
will count the number of rows within each group, not the number of groups. If y
ou want to count the number of groups, you can put the GROUP BY query into a sub
query and apply the COUNT(*) function on the main query as shown in the followin
g tutorial exercise:
SQL> SELECT first_name, COUNT(*) FROM employees
GROUP BY first_name HAVING COUNT(*) > 1;
FIRST_NAME COUNT(*)
-------------------- ----------
Peter 3
Michael 2
Steven 2
John 3
Julia 2
William 2
Karen 2
Kevin 2
......
SQL> SELECT COUNT(*) FROM (
SELECT first_name, COUNT(*) FROM employees
GROUP BY first_name HAVING COUNT(*) > 1
);
COUNT(*)
----------
13
How To Return Top 5 Rows?
If you want the query to return only the first 5 rows, you can use the pseudo co
lumn called ROWNUM in the WHERE clause. ROWNUM contains the row number of each r
eturning row from the query. The following statement returns the first 5 rows fr
om the employees table:
SQL> SELECT employee_id, first_name, last_name
FROM employees WHERE ROWNUM <= 5;
EMPLOYEE_ID FIRST_NAME LAST_NAME
----------- -------------------- -------------
100 Steven King
101 Neena Kochhar
102 Lex De Haan
103 Alexander Hunold
104 Bruce Ernst
What Is a Transaction?
A transaction is a logical unit of work requested by a user to be applied to the
database objects. Oracle server introduces the transaction concept to allow use
rs to group one or more SQL statements into a single transaction, so that the ef
fects of all the SQL statements in a transaction can be either all committed (ap
plied to the database) or all rolled back (undone from the database).
How To Start a New Transaction?
There is no SQL statement to explicitly start a new transaction. Oracle server i
mplicitly starts a new transaction with the following two conditions:
The first executable statement of a new user session will automatically start a
new transaction.
The first executable statement after a previous transaction has been ended will
automatically start a new transaction.
How To End the Current Transaction?
There are several ways the current transaction can be ended:
Running the COMMIT statement will explicitly end the current transaction.
Running the ROLLBACK statement will explicitly end the current transaction.
Running any DDL statement will implicitly end the current transaction.
Disconnecting a user session will implicitly end the current transaction.
Killing a user session will implicitly end the current transaction.
How To Create a Testing Table?
If you want to practice DML statements, you should create a testing table as sho
wn in the script below:
>cd (OracleXE home directory)
>.\bin\sqlplus /nolog
SQL> connect HR/fyicenter
Connected.
SQL> CREATE TABLE fyi_links (id NUMBER(4) PRIMARY KEY,
url VARCHAR2(16) NOT NULL,
notes VARCHAR2(16),
counts NUMBER(4),
created DATE DEFAULT (sysdate));
Table created.
You should keep this table for to practice other tutorial exercises presented in
this collection.
How To Commit the Current Transaction?
If you have used some DML statements updated some data objects, and you want to
have the updates to be permanently recorded in the database, you can use the COM
MIT statement. It will make all the database changes made in the current transac
tion become permanent and end the current transaction. The following tutorial ex
ercise shows you how to use COMMIT statements:
SQL> connect HR/fyicenter
SQL> INSERT INTO fyi_links (url, id)
2 VALUES ('fyicenter.com', 101);
SQL> INSERT INTO fyi_links (url, id)
2 VALUES ('centerfyi.com', 110);
SQL> SELECT * FROM fyi_links;
ID URL NOTES COUNTS CREATED
------- ---------------- ---------- ---------- ---------
101 fyicenter.com 07-MAY-06
110 centerfyi.com 07-MAY-06
SQL> COMMIT;
Commit complete.
How To Rollback the Current Transaction?
If you have used some DML statements updated some data objects, you find a probl
em with those updates, and you don't want those updates to be permanently record
ed in the database, you can use the ROLLBACK statement. It will remove all the d
atabase changes made in the current transaction and end the current transaction.
The following tutorial exercise shows you how to use ROLLBACK statements:
SQL> connect HR/fyicenter
SQL> INSERT INTO fyi_links (url, id)
2 VALUES ('google.com', 102);
SQL> INSERT INTO fyi_links (url, id)
3 VALUES ('myspace.com', 103);
SQL> SELECT * FROM fyi_links;
ID URL NOTES COUNTS CREATED
------- ---------------- ---------- ---------- ---------
101 fyicenter.com 07-MAY-06
110 centerfyi.com 07-MAY-06
102 google.com 07-MAY-06
103 myspace.com 07-MAY-06
SQL> ROLLBACK;
Rollback complete.
SQL> SELECT * FROM fyi_links;
ID URL NOTES COUNTS CREATED
------- ---------------- ---------- ---------- ---------
101 fyicenter.com 07-MAY-06
110 centerfyi.com 07-MAY-06
As you can see, the two new records inserted into the table were removed by the
ROLLBACK statement.
What Happens to the Current Transaction If a DDL Statement Is Executed?
If a DDL statement is executed, the current transaction will be committed and en
ded. All the database changes made in the current transaction will become perman
ent. This is called an implicit commit by a DDL statement. The following tutoria
l exercise shows you that the CREATE TABLE statement forced the current transact
ion to be committed and ended. The subsequent ROLLBACK statement has no effects
on the closed transaction.
SQL> connect HR/fyicenter
SQL> INSERT INTO fyi_links (url, id)
2 VALUES ('oracle.com', 112);

SQL> INSERT INTO fyi_links (url, id)
2 VALUES ('sql.com', 113);
SQL> CREATE TABLE fyi_temp AS (SELECT * FROM fyi_links);
Table created.
SQL> ROLLBACK;
Rollback complete.
SQL> SELECT * FROM fyi_links;
ID URL NOTES COUNTS CREATED
------- ---------------- ---------- ---------- ---------
101 fyicenter.com 07-MAY-06
110 centerfyi.com 07-MAY-06
112 oracle.com 07-MAY-06
113 sql.com 07-MAY-06

You might also like