Professional Documents
Culture Documents
45 Useful Oracle Queries For Oracle Developers
45 Useful Oracle Queries For Oracle Developers
45 Useful Oracle Queries For Oracle Developers
1 of 23
Home
Android
Java
Spring
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
Heres a list of 40+ Useful Oracle queries that every Oracle developer must bookmark. These queries
range from date manipulation, getting server info, get execution status, calculate database size etc.
Subscribe
Get our Articles via Email. Enter your email.
Your E-Mail
Quickly returns the first day of current month. Instead of current month you want to find first day of
15-12-2014 16:32
2 of 23
Home
Android
Java
Spring
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
Latest Posts
1. Getting Started With Yeoman (Introduction to
Yeoman)
2. Navigating Spring Security from thick Client to
REST Webservice
3. HTML5 DataList Example
4. Auditing DML changes in Oracle
5. Java 8 Lambda Expressions Tutorial with
Examples
6. Java 8 Default Methods Tutorial
15-12-2014 16:32
3 of 23
Home
Android
Java
Spring
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
SELECT SYSDATE,
LAST_DAY (SYSDATE) "Last",
LAST_DAY (SYSDATE) - SYSDATE "Days left"
FROM DUAL;
7. Get number of days between two dates
Sponsors
Use this query to get difference between two dates in number of days.
SELECT ROUND ( (MONTHS_BETWEEN ('01-Feb-2014', '01-Mar-2012') * 30), 0)
num_of_days
FROM DUAL;
OR
SELECT TRUNC(sysdate) - TRUNC(e.hire_date) FROM employees;
Use second query if you need to find number of days since some specific date. In this example
number of days since any employee is hired.
8. Display each months start and end date upto last month of the year
This clever query displays start date and end date of each month in current year. You might want to
use this for certain types of calculations.
SELECT ADD_MONTHS (TRUNC (SYSDATE, 'MONTH'), i) start_date,
TRUNC (LAST_DAY (ADD_MONTHS (SYSDATE, i))) end_date
FROM XMLTABLE (
'for $i in 0 to xs:int(D) return $i'
PASSING XMLELEMENT (
d,
FLOOR (
MONTHS_BETWEEN (
ADD_MONTHS (TRUNC (SYSDATE, 'YEAR') - 1, 12),
SYSDATE)))
COLUMNS i INTEGER PATH '.');
9. Get number of seconds passed since today (since 00:00 hr)
15-12-2014 16:32
4 of 23
Home
Android
Java
Spring
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
FROM DUAL;
10. Get number of seconds left today (till 23:59:59 hr)
SELECT (TRUNC (SYSDATE+1) - SYSDATE) * 24 * 60 * 60 num_of_sec_left
FROM DUAL;
15-12-2014 16:32
5 of 23
Home
Android
Java
Spring
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
15-12-2014 16:32
6 of 23
Home
Android
Java
Spring
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
15-12-2014 16:32
7 of 23
Home
Android
Java
Spring
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
15-12-2014 16:32
8 of 23
Home
Android
Java
Spring
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
15-12-2014 16:32
9 of 23
Home
Android
Java
Spring
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
31. Get current session id, process id, client process id?
This is for those who wants to do some voodoo magic using process ids and session ids.
SELECT b.sid,
b.serial#,
a.spid processid,
b.process clientpid
FROM v$process a, v$session b
WHERE a.addr = b.paddr AND b.audsid = USERENV ('sessionid');
V$SESSION.SID AND V$SESSION.SERIAL# is database process id
V$PROCESS.SPID is shadow process id on this database server
V$SESSION.PROCESS is client PROCESS ID, ON windows it IS : separated THE FIRST # IS
THE PROCESS ID ON THE client AND 2nd one IS THE THREAD id.
SELECT ROWNUM,
SUBSTR (a.sql_text, 1, 200) sql_text,
TRUNC (
a.disk_reads / DECODE (a.executions, 0, 1, a.executions))
reads_per_execution,
a.buffer_gets,
a.disk_reads,
a.executions,
a.sorts,
a.address
15-12-2014 16:32
10 of 23
Home
Android
Java
Spring
ORDER BY 3 DESC)
WHERE ROWNUM < 10;
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
34. Oracle SQL query over the view that shows actual Oracle connections.
SELECT osuser,
username,
machine,
program
FROM v$session
ORDER BY osuser;
35. Oracle SQL query that show the opened connections group by the program that opens the
connection.
SELECT program application, COUNT (program) Numero_Sesiones
FROM v$session
GROUP BY program
ORDER BY Numero_Sesiones DESC;
36. Oracle SQL query that shows Oracle users connected and the sessions number for user
SELECT username Usuario_Oracle, COUNT (username) Numero_Sesiones
FROM v$session
GROUP BY username
ORDER BY Numero_Sesiones DESC;
37. Get number of objects per owner
SELECT owner, COUNT (owner) number_of_objects
FROM dba_objects
GROUP BY owner
ORDER BY number_of_objects DESC;
15-12-2014 16:32
11 of 23
Home
Android
Java
Spring
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
15-12-2014 16:32
12 of 23
Home
Android
Java
Spring
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
SELECT *
FROM employees
WHERE ROWID IN (SELECT MAX (ROWID) FROM employees);
(OR)
SELECT
MINUS
SELECT
FROM
WHERE
* FROM employees
*
employees
ROWNUM < (SELECT COUNT (*) FROM employees);
15-12-2014 16:32
13 of 23
Home
Android
Java
Spring
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
If you have some cool query that can make life of other Oracle developers easy, do share in comment
section.
Related Articles
15-12-2014 16:32
14 of 23
Home
Android
Java
Spring
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
22 Comments
Madiraju Krishna Chaitanya
Hi Viral Patel,Thanks a LOT for providing these useful Oracle Queries for us.Please keep up the
Good Work.All the Best.
Reply
15-12-2014 16:32
15 of 23
Home
Android
Java
Praveen
Spring
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
Tushar
krunal
Thank you
using above database queries i will solve db related problems in my many project.
Reply
Harshita
Good Job Viral, really useful article. Looking forward to more such stuff. :)
Reply
Chandra
junaid ahmad
Can you please give 50 different views of Data Dictionary in form of SQL Queries?
15-12-2014 16:32
16 of 23
Home
Android
Java
Spring
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Nishant Mishra
Web
More
Use USER_Views
Reply
Nishant Mishra
You can use USER_VIEWS to see the available views with the SCHEMA.
Describe the view and see the columns..
Use the column name to get the details
Reply
David
I have a requirement.
I have 5 attribute columns corresponding 5 days of the week from Monday to Friday.
The attribute values can be Yes or No.
If Wednesday and Friday are set to Yes, I need a query to retrieve all the wednesdays and Fridays
from a given date to sysdate.
Similarly, If Monday, Wednesday and Thursday are set to Yes, I need all the Mondays, Wednesdays
and Thursday from a given date(passed as parameter) to sysdate.
Could anyone please help me in writing this query?
Reply
Kevin
for Davids question on April 17th..Im sure this could probably be cleaned up some more and
I see that the Friday portion of this returns a friday before 01/01/2012 but this is a start
create table CVT_T1_DAYS as
(select '1' Mon, '0' Tue, '0' Wed, '1' Thu, '1' Fri from dual);
select dd, next_day(sysdate - (rownum * 7), dd) DATE_
from (SELECT 'MON' dd from CVT_T1_DAYS where mon = 1)
15-12-2014 16:32
17 of 23
Home
Android
Java
Spring
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
union
select dd, next_day(sysdate - (rownum * 7), dd) DATE_
from (SELECT 'TUE' dd from CVT_T1_DAYS where tue = 1)
connect by rownum <= round((sysdate - to_date('01/01/2012',
union
select dd, next_day(sysdate - (rownum * 7), dd) DATE_
from (SELECT 'WED' dd from CVT_T1_DAYS where wed = 1)
connect by rownum <= round((sysdate - to_date('01/01/2012',
union
select dd, next_day(sysdate - (rownum * 7), dd) DATE_
from (SELECT 'THU' dd from CVT_T1_DAYS where thu = 1)
connect by rownum <= round((sysdate - to_date('01/01/2012',
union
select dd, next_day(sysdate - (rownum * 7), dd) DATE_
from (SELECT 'FRI' dd from CVT_T1_DAYS where FRI = 1)
connect by rownum <= round((sysdate - to_date('01/01/2012',
order by date_
;
More
'dd/mm/yyyy'))/7)
'dd/mm/yyyy'))/7)
'dd/mm/yyyy'))/7)
'dd/mm/yyyy'))/7)
Reply
Kevin
Fernando Tlatilolpa
Hello and thanks Viral, I think you can include a query which return number of columns result of a
dynamic query, these usefull when you send a script inside a function and you dont know exactly
how many columns you obtain.
Once again thaks for your effort.
Reply
salim
15-12-2014 16:32
18 of 23
Home
Android
Java
Spring
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
Reply
Kevin
in response to Davids query.cleaned up the dates that come out prior to the date entered as well
as if you enter a date in the future. Both SQL and PL/SQL solutions are below. note, greater than,
equal, less than formatting might be affected by the tags
select dd, next_day(sysdate - (rownum * 7), dd) DATE_
from (SELECT 'MON' dd from CVT_T1_DAYS where mon = 1)
where to_date('&indate', 'mm/dd/yyyy') <= sysdate
and
next_day(sysdate - (rownum * 7), dd) >= to_date('&indate', 'mm/dd/yyyy'
connect by rownum <= round((sysdate - to_date('&indate', 'mm/dd/yyyy'))/7)
and next_day(sysdate - (rownum * 7), dd) >= to_date('&indate', 'mm/dd/yyyy'
union
select dd, next_day(sysdate - (rownum * 7), dd) DATE_
from (SELECT 'TUE' dd from CVT_T1_DAYS where tue = 1)
where to_date('&indate', 'mm/dd/yyyy') <= sysdate
and
next_day(sysdate - (rownum * 7), dd) >= to_date('&indate', 'mm/dd/yyyy'
connect by rownum <= round((sysdate - to_date('&indate', 'mm/dd/yyyy'))/7)
and next_day(sysdate - (rownum * 7), dd) >= to_date('&indate', 'mm/dd/yyyy'
union
select dd, next_day(sysdate - (rownum * 7), dd) DATE_
from (SELECT 'WED' dd from CVT_T1_DAYS where wed = 1)
where to_date('&indate', 'mm/dd/yyyy') <= sysdate
and
next_day(sysdate - (rownum * 7), dd) >= to_date('&indate', 'mm/dd/yyyy'
connect by rownum <= round((sysdate - to_date('&indate', 'mm/dd/yyyy'))/7)
and next_day(sysdate - (rownum * 7), dd) >= to_date('&indate', 'mm/dd/yyyy'
union
select dd, next_day(sysdate - (rownum * 7), dd) DATE_
from (SELECT 'THU' dd from CVT_T1_DAYS where thu = 1)
where to_date('&indate', 'mm/dd/yyyy') <= sysdate
and
next_day(sysdate - (rownum * 7), dd) >= to_date('&indate', 'mm/dd/yyyy'
connect by rownum <= round((sysdate - to_date('&indate', 'mm/dd/yyyy'))/7)
and next_day(sysdate - (rownum * 7), dd) >= to_date('&indate', 'mm/dd/yyyy'
union
select dd, next_day(sysdate - (rownum * 7), dd) DATE_
from (SELECT 'FRI' dd from CVT_T1_DAYS where FRI = 1)
where to_date('&indate', 'mm/dd/yyyy') <= sysdate
and
next_day(sysdate - (rownum * 7), dd) >= to_date('&indate', 'mm/dd/yyyy'
connect by rownum <= round((sysdate - to_date('&indate', 'mm/dd/yyyy'))/7)
and next_day(sysdate - (rownum * 7), dd) >= to_date('&indate', 'mm/dd/yyyy'
order by date_;
declare
v_datechosen date
:= to_date('&indate', 'mm/dd/yyyy');
15-12-2014 16:32
19 of 23
Home
Android
Java
v_sysdate
http://viralpatel.net/blogs/useful-oracle-queries/
Spring
date
Frameworks
:= sysdate;
Database
JavaScript
Web
More
v_dd char(5);
v_date date;
cursor dates is
select dd, next_day(v_sysdate - (rownum * 7), dd) DATE_
from (SELECT 'MON' dd from CVT_T1_DAYS where mon = 1)
where v_datechosen <= v_sysdate
and next_day(v_sysdate - (rownum * 7), dd) >= v_datechosen
connect by rownum <= v_weeksdiff and
next_day(v_sysdate - (rownum * 7), dd) >= v_datechosen
union
select dd, next_day(v_sysdate - (rownum * 7), dd) DATE_
from (SELECT 'TUE' dd from CVT_T1_DAYS where tue = 1)
where v_datechosen <= v_sysdate
and next_day(v_sysdate - (rownum * 7), dd) >= v_datechosen
connect by rownum <= v_weeksdiff and
next_day(v_sysdate - (rownum * 7), dd) >= v_datechosen
union
select dd, next_day(v_sysdate - (rownum * 7), dd) DATE_
from (SELECT 'WED' dd from CVT_T1_DAYS where wed = 1)
where v_datechosen <= v_sysdate
and next_day(v_sysdate - (rownum * 7), dd) >= v_datechosen
connect by rownum <= v_weeksdiff and
next_day(v_sysdate - (rownum * 7), dd) >= v_datechosen
union
select dd, next_day(v_sysdate - (rownum * 7), dd) DATE_
from (SELECT 'THU' dd from CVT_T1_DAYS where thu = 1)
where v_datechosen <= v_sysdate
and next_day(v_sysdate - (rownum * 7), dd) >= v_datechosen
connect by rownum <= v_weeksdiff and
next_day(v_sysdate - (rownum * 7), dd) >= v_datechosen
union
select dd, next_day(v_sysdate - (rownum * 7), dd) DATE_
from (SELECT 'FRI' dd from CVT_T1_DAYS where FRI = 1)
where v_datechosen <= v_sysdate
and next_day(v_sysdate - (rownum * 7), dd) >= v_datechosen
connect by rownum <= v_weeksdiff and
next_day(v_sysdate - (rownum * 7), dd) >= v_datechosen
order by date_;
begin
dbms_output.enable(1000000);
dbms_output.put_line(v_weeksdiff||' '||to_date('&indate', 'mm/dd/yyyy'));
open dates;
LOOP
FETCH dates into v_dd, v_date;
EXIT WHEN dates%NOTFOUND;
dbms_output.put_line(v_dd ||'
'||v_date);
15-12-2014 16:32
20 of 23
Home
Android
Java
end;
Spring
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
close dates;
Reply
Bharani
ans for q 8
WITH csv AS(
select (to_char(trunc(sysdate,MONTH),MM)-level) as Mnt from dual where
(to_char(trunc(sysdate,MONTH),MM)-level) >-1 connect by level < 20 )
select add_months(trunc(sysdate,'MONTH'),-csv.Mnt), add_months(trunc(sysdate,'MONTH'),csv.Mnt)-1 from dual ,csv
Reply
MVVSSARMA
pl answer to my query, I have a table emp (empno,login)- in my table an employee can login many
times in a day. Assume each employee 4 times logged into the table with employeeno 11,22,33,44
since one week. Now I want a query to list all employees 11,22,33,44 once per day.(total I need to get
7 records for each employee).
please suggest the ansewr
Reply
Dayanandsm
Daniel
15-12-2014 16:32
21 of 23
Home
Android
Java
Spring
very usefull !
I really appreciate that !
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
Reply
Malshan
Hi,
I want to select 1st,10th and 20th of the month by giving 1,10,20 as the parameters. is there the a
possible way?
plz help
Reply
Makal
rk goud
15-12-2014 16:32
22 of 23
Home
Android
Java
http://viralpatel.net/blogs/useful-oracle-queries/
Spring
Frameworks
Database
JavaScript
Web
More
Leave a Reply
Your email address will not be published. Required fields are marked *
Name *
Email *
Website
Comment
Note
To post source code in comment, use [code language] [/code] tag, for example:
[code java] Java source code here [/code]
[code html] HTML here [/code]
Post Comment
Categories
Home page
Site Pages
Struts
Spring
AJAX
PHP
Java
JavaEE
About viralpatel.net
Back to top
Join Us
Search
Advertise
Posts Feed
15-12-2014 16:32
23 of 23
Home
Android
General
Featured
FreeMarker Template
Java
Spring
Play Framework
HTML5
http://viralpatel.net/blogs/useful-oracle-queries/
Frameworks
Database
JavaScript
Web
More
Android
15-12-2014 16:32