Documente Academic
Documente Profesional
Documente Cultură
net/blogs/useful-oracle-queries/
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
Date / Time related queries Get our Articles via Email. Enter your email.
Your E-Mail
1. Get the first day of the month
Quickly returns the first day of current month. Instead of current month you want to find first day of
1 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
SELECT CAST (TO_CHAR (LAST_DAY (SYSDATE), 'dd') AS INT) number_of_days 5. Java 8 Lambda Expressions Tutorial with
FROM DUAL; Examples
6. Get number of days left in current month 7. Compound Triggers in Oracle 11g - Tutorial with
2 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
OR
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.
3 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
SELECT table_name
FROM user_tables
WHERE table_name = 'TABLE_NAME';
4 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
SELECT username,
profile,
default_tablespace,
temporary_tablespace
FROM dba_users;
5 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
SELECT VALUE
FROM v$system_parameter
WHERE name = 'compatible';
(OR)
6 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
26. Find the size occupied by Data in a Database or Database usage details
Gives the size occupied by data in this database.
7 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
SELECT a.sid,
a.serial#,
b.username,
opname OPERATION,
target OBJECT,
TRUNC (elapsed_seconds, 5) "ET (s)",
TO_CHAR (start_time, 'HH24:MI:SS') start_time,
ROUND ( (sofar / totalwork) * 100, 2) "COMPLETE (%)"
FROM v$session_longops a, v$session b
WHERE a.sid = b.sid
AND b.username NOT IN ('SYS', 'SYSTEM')
AND totalwork > 0
8 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
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');
SELECT *
FROM ( 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
9 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
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.
36. Oracle SQL query that shows Oracle users connected and the sessions number for user
10 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
Output:
WITH csv
AS (SELECT 'AA,BB,CC,DD,EE,FF'
AS csvdata
FROM DUAL)
SELECT REGEXP_SUBSTR (csv.csvdata, '[^,]+', 1, LEVEL) pivot_char
FROM DUAL, csv
CONNECT BY REGEXP_SUBSTR (csv.csvdata,'[^,]+', 1, LEVEL) IS NOT NULL;
11 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
SELECT *
FROM employees
WHERE ROWID IN (SELECT MAX (ROWID) FROM employees);
(OR)
WITH tbl
AS (SELECT -2 num FROM DUAL
UNION
SELECT -3 num FROM DUAL
UNION
SELECT -4 num FROM DUAL),
sign_val
AS (SELECT CASE MOD (COUNT (*), 2) WHEN 0 THEN 1 ELSE -1 END val
FROM tbl
WHERE num < 0)
SELECT EXP (SUM (LN (ABS (num)))) * val
FROM tbl, sign_val
GROUP BY val;
12 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
SELECT 1
FROM TABLE_NAME
WHERE ROWNUM = 1;
If you have some cool query that can make life of other Oracle developers easy, do share in comment
section.
Related Articles
13 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
Send Me Tutorials
22 Comments
Hi Viral Patel,Thanks a LOT for providing these useful Oracle Queries for us.Please keep up the
Good Work.All the Best.
Reply
14 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
Reply
Reply
Thank you
using above database queries i will solve db related problems in my many project.
Reply
Good Job Viral, really useful article. Looking forward to more such stuff. :)
Reply
Reply
Can you please give 50 different views of Data Dictionary in form of SQL Queries?
15 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
Use USER_Views
Reply
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
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
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
16 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
Reply
Reply
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
17 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
Reply
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
declare
v_datechosen date := to_date('&indate', 'mm/dd/yyyy');
18 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
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);
19 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
Reply
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
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
Reply
20 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
Reply
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
Reply
21 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
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]
Post Comment
Home page Struts Spring AJAX PHP Java JavaEE About viralpatel.net Join Us Search Advertise Posts Feed
22 of 23 15-12-2014 16:32
45 Useful Oracle Queries for Oracle Developers http://viralpatel.net/blogs/useful-oracle-queries/
23 of 23 15-12-2014 16:32