Friday, October 29, 2010

Oracle database FAQ

Find out 5th highest salary from emp table
SELECT DISTINCT(a.sal)
FROM EMP A WHERE 5 =
(SELECT COUNT(DISTINCT(b.sal))
FROM EMP B
WHERE a.sal <= b.sal);

How to find out Oracle version
select * from v$version

Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Solaris: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

How to find out last update time in a table
select scn_to_timestamp( max(ora_rowscn) ) from tableName;

How to login to SQLPLUS from command line
sqlplus user/password@sid

How to see execution plan for an SQL
SQL> set autotrace on
SQL> select count(*) from table_name;

SQL> explain plan set statement_id = '111' for select * from tableName;
SQL> select * from plan_table;

How to find out cpu time for an SQL?
SQL> TIMING START select_emp
SQL> SELECT * FROM employee ;
SQL> TIMING SHOW
select_emp timing for:
select_emp real: 1760

No comments: