Tuning SQL query if where clause uses non index column in Oracle - sql

I have a table emp with columns id, empname, sal, join_date and I have this query:
select *
from emp
where join_date between date1 and date2 ;
Only id has index no other other column has.
How can I tune this query it is on prod and customer don't let me create index or partition on join_date column. It takes 5-7 minutes to return just a few rows (10-15). How can I improve performance?

Related

i have a oracle table EMP columns are NAME,AGE,DEPT

i have a oracle table EMP columns are NAME,AGE,DEPT.
now i want to retrive data from EMP using "select statement";
Select Name, age, dept from Emp;
select Dept, age, emp from Emp;
Which one will take less time to retrieve data?
Or will the retrieve time not be different?
you can use
Select Name, Age, Dept From EMP
Or
if you have only Three columns in your EMP table than you also use
Select * from EMP.
both take same time..
in your question your second query is time consuming because you use, two time dept column in it.
for Better performance you can create index on EMP Table.

SQL - How do I add a count to my table which has been a result from a query

I've retrieved a count of the duplicates and their occurrences using the below code
select empID, count(empID) AS DUPLICATEempID
from employees
group by empID
having count (empID) > 1
I now want the table to include the number of rows returned (i.e. insert the number on the table returned)
thanks in advance.
In SAS, you can do this with a subquery:
select empId, DUPLICateempID, count(*) as NumDuplicates
from (select empID, count(empID) AS DUPLICATEempID
from employees
group by empID
having count (empID) > 1
) t
When you have an aggregation function without a group by, it applies the function to the whole table and re-merges the results.

How to get all the columns, but have to group by only 2 columns in sql query

I have a table Employees, which has Fields as below:
Employee_name,Employee_id,Employee_status,Employee_loc,last_update_time.
This table does not have any constraint.
I have tried the below query.
select Employee_name, count(1)
from Employees
where Employee_status = 'ACTIVE'
Group by Employee_name,Employee_loc
having count(Employee_name) > 1
order by count(Employee_name) desc
In the select, I need to get Employee_id too.. Can any one help on how to get that?
You can just add Employee_id to the query, and also add it to the group by clause. (Adding it to the grouping won't make any difference in the query results, assuming each employee name each employee id is unique).
If the grouping does make a difference, that implies that some combinations of employee name and location have more than one ID associated with them. Your query would therefore need to decide which ID to return, possibly by using an aggregate function.
SELECT EMPLOYEE_NAME, EMPLOYEE_ID, COUNT(1)
FROM
EMPLOYEES
WHERE
EMPLOYEE_NAME IN
(
SELECT EMPLOYEE_NAME
FROM EMPLOYEES
WHERE Employee_status = 'ACTIVE'
GROUP BY Employee_name,Employee_loc
HAVING COUNT(*) > 1
)
GROUP BY EMPLOYEE_NAME, EMPLOYEE_ID
You can also use partition by clause and select whichever columns you want to see irrespective of the columns you are using for aggregation.
A very short and simple explanation here - Oracle "Partition By" Keyword

How to return the most repeated value using a foreign key from a referring table

I need to create a SQL query to retrieve all the values from one unique row entry in a table called JOBS.
But to do so I need to use a table called EMPLOYEES which has a foreign key to the JOBS table.
The condition is to return the job_title from the table JOBS that has its job_id more times repeated in the EMPLOYEES table.
I need to do it with one SQL statement.
Desired output:
Stock Clerk
This is the query I have
SELECT job_id
FROM (SELECT job_id FROM employees GROUP BY job_id ORDER BY COUNT(*) desc)
WHERE ROWNUM <= 1;
but is not good enough because it is job_title from EMPLOYEES what I need.
Do you have any idea how can I do it? I am using an Oracle 11g database
This is how my tables look like:
EMPLOYEES
JOBS
SELECT job_title from JOBS where JOB_ID = (
SELECT job_id FROM employees GROUP BY job_id ORDER BY COUNT(*) desc LIMIT 1)
Don't remember offhand if you can use LIMIT 1 in oracle, but if not then
SELECT job_title from JOBS where JOB_ID = (
SELECT job_id FROM employees GROUP BY job_id ORDER BY COUNT(*) desc where ROWNUM=1)
Also note that if the top 2+ job id's are equal, it will only print one of them (i.e. if SA_REP and ST_CLERK both appeared 4 times, only one of them would be shown)
It's not clear what your requirements are for this, but the above queries would only return one of them

sort query result without selecting that column but sort by that column?

I have a query, I have to sort the result from the DB2 database. The query will select the columns empname,salary,status. But I have to sort the result using order by empno
But the query is not working.. This is the query.
select empname, salary, status from emp where salary>5000 order by empno
Can you update the query to sort by empno without using it in selecting columns?
Your syntax seems correct to me except dot(.) at the end. After removing dot if doesn't work...
Try something like
SELECT empname, salary, status
FROM (SELECT *
FROM emp
ORDER BY empno)
WHERE salary > 5000
Another syntax that may be easier, depending on how you think about it is using the with keyword. This explicitly creates a named temporary table with the desired ordering, then queries from that. The order of the new query will be the same as the temporary tables ordering.
WITH temp_table AS (SELECT *
FROM emp
ORDER BY empno)
SELECT empname, salary, status
FROM temp_table
WHERE salary > 5000;
The answer by #jaychapani is more concise and functionally does the same thing, but the with syntax is powerful for quite a few other use cases and visually separates the two, which can be helpful especially if you have a long subquery that does other things.
I used below query to solve this problem.
In this case we can sort query result without displaying the column:
WITH temp_table
AS (select distinct(s1.Name),s1.id
from students s1
where marks>75
order by right(s1.Name ,3) asc,s1.id asc
)
SELECT Name
FROM temp_table;
I'm not sure, but the fastest way on DB is something like this:
SELECT empname, salary, status
FROM (
select empname, salary, status, empno
from emp
where salary > 5000
order by empno ASC
)
try this
select empname, salary, status from emp where salary>5000 order by empno asc
make sure that columns and table name really exist.
have a look at: ORDER BY clause
Best Regards