Categories: PLSQL Tutorials

How to find SQL_ID for SQL query step by step?

In my previous article I have given ways to find out the Username for SQL query. I got question in my mind that many SQL developers will be facing issue to find the SQL_ID for SQL query step by step. There are multiple ways to find out the SQL_ID for SQL query in oracle. The most useful way is you require to execute some System views to get SQL_ID information. Before executing the System SQL views we require to know that if you can use these system views extensively it may cause the performance issue. You can use the following system views :

  1. V$SQL
  2. V$SQLAREA
  3. V$SQLSTATS

Queries to find SQL_ID for SQL Query using V$SQLSTATS :

In this section I would like to give the steps to find SQL_ID for SQL Query with using V$SQLSTATS view. The V$SQLSTATS view is one of the most important view which is faster than V$SQL view so DBA’s recommends this view to find SQL_ID extensively.

Lets Say we require to find out the SQL_ID for following query :

Select * from Employees where employee id In ( 1,2,3,4,5);

You can directly use the following syntax to find the SQL Id :

Select SQL_ID from V$SQLSTATS Where SQL_TEXT = ‘SQL Query’;

Query :

Select SQL_ID from V$SQLSTATS Where SQL_TEXT = ‘Select * from Employees where employee id In ( 1,2,3,4,5)’;

Output :

SQL_ID

-------------

56rrstssrdsr1

The above query will give you the SQL_ID with simple steps.

Queries to find SQL_ID for SQL Query using V$SESSION :

You can also use V$SESSION view to find out the SQL_ID for executed SQL query. But by this statement you can only able to find out the SQL_ID for last executed SQL statement.

Lets say the following query is last executed query :

Select * from Employees where employee id In ( 1,2,3,4,5);

You can utilize the following query to find out the SQL_ID

Select Prev_SQL_ID from V$SESSION where SID=System_Context(‘Amit_DBA’,’SID’);

The above query will give you following output :

SQL_ID

-------------

56rrstssrdsr1

Oracle 18c SET FEEDBACK Command :

You can use the Set feedback command of oracle 18c. The SQL_ID parameter of set feedback command will easily give you the SQL_ID for sql or plsql executed.

Kindly execute following command on SQLPlus.

SET FEEDBACK ON SQL_ID;

Then execute any query :

Select Count(*)from Employees;

Output will be :

12

SQL_ID: 9r8kgzntdn9sq

Queries to find SQL_ID for SQL Query using V$SQLAREA :

You can use V$SQLAREA view to find out the SQL_ID easily.

Query :

SELECT * FROM V$SQLAREA WHERE sql_text=’Select count(*) from Employees’;

These are multiple ways to find out SQL_ID for SQL query step by step. I hope you like this article. If you like this article or if you have issues with same kindly comment in comments section.

Amit S

Oracle Consultant with vast experience in Oracle BI and PL/SQL Development. Amiet is the admin head of this website who contributes by preparing tutorials and articles related to database technologies. He is responsible to manage the content and front-end of the website.

View Comments

Share
Published by
Amit S

Recent Posts

The Essential Guide to Cryptocurrency Exchange Platform Development for Beginners and Beyond

Introduction Cryptocurrencies took the world by storm, setting up a new financial system and breaking…

5 months ago

Top 20 System Administrator Interview Questions and answers

In my previous article I have given Top 20 technical support interview questions with its…

8 months ago

Desktop Support Scenario Based Interview Questions

In my previous articles I have given 15 most asked desktop support interview questions with…

8 months ago

How Do Business Analysts Use SQL for Their Needs?

A business analyst is someone who is versed in processes of data analysis used for…

8 months ago

Top 15 Control-M Interview Questions with Answers

In my previous article I have already given top questions and answers for Desktop support…

8 months ago

Top 20 SQL Interview Questions for Production Support

In my previous article I have given unix production support interview questions and answers. In…

8 months ago