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.
Related
I have a table emp (ename,eid,did,sal,sex) where column did is foreign key with another table dept (did,dname).
I want to get the max sal of the company along with name and did of the person who is getting it.
I am executing following query
select did,ename ,max(sal) from emp;
But the Result is :
ORA-00937: not a single-group group function
so My question is can't I have more than 1 columns ?
You can't use MAX aggregate function like this. Using MAX without a GROUP BY clause will just return one record with the maximum sal value. You are not allowed to place any more non-aggregated fields in the SELECT clause.
If you want to get the record having the maximum sal value, then you have to do a self-join:
select e1.*
from emp as e1
inner join (
select max(sal) as max_sal
from emp
) as e2 on e1.sal = e2.max_sal
Note: The above query will return more than one records from table emp, in case more than one records share the same maximum sal value.
Edit:
If you want to get the maximum salary per department then you have to include a GROUP BY clause in the derived table used:
select e1.*
from emp as e1
inner join (
select did, max(sal) as max_sal
from emp
group by did
) as e2 on e1.did = e2.did and e1.sal = e2.max_sal
You would have to group the results by the sal column:
select did,ename,max(sal) from emp group by sal
MAX operates over the group specified in a GROUP BY clause and finds the maximum value for each group. When not specifying a set of columns to group by, it finds the max for the entire results set.
You need to find the max sal, then you can query the rows that match:
select did, ename, sal
from emp
where sal = (select max(sal) from emp)
SELECT
COUNT(emp.empNo)
FROM
Employee emp
WHERE
NOT EXISTS (SELECT dept.empNo
FROM department dept
WHERE emp.empNo = dept.empNo);
What does the where condition(where emp.empNo = dept.empNo) signify in the above query? I get different results with and without the where condition. I'm new to Oracle. Can any one help me to understand?
your query displaying count of employees which are not present in emp table but present in dept table.
suppose we have two tables emp and dept :
emp dept
1 1
2 2
3 3
4 4
5 5
6
7
from the given table we have emp 1 to 5 in both of the tables but in dept table having 2 employees (6,7)which are not present in emp table and your query is displaying count for those emp i.e 2
The query means that you're looking for only those employees, for which it does not exist a department with the same empNo as the employee's empNo.
I guess this is a query to find those employees which are not managers of any department (if we assume that the department's empNo is the empNo of the department's manager).
Still, it would be better if you provide the schema of the employee and the department tables.
The query is basically looking for the number of employees who don't belong to a department.
NOT EXISTS means that the query enclosed returns no rows. So, for any employee where no matching rows are found in the Department table they are counted.
Same as saying
SELECT
COUNT(emp.empNo)
FROM
Employee emp
WHERE
emp.EmpNo NOT IN ( SELECT
empNo
FROM
department)
I need to select 2 column from table with matched data from another table or cell be null ,
table 1 named "emp" contain emp_name ,emp_id
table 2 named "salary" contain emp_sal, emp_id
I need to create select query
have all emp_name, emp_id and emp_sal (for only the employees will take sale) or be Null
thanks for help now ((((update ))))
first thanks for help
i used
SELECT emp.emp_id,emp.emp_name,salary.emp_sal FROM emp LEFT JOIN salary ON emp.emp_id = salary.emp_id;
it work but with a lot of duplication and i need to make this query with day ...
i create another table named "day" i need query appear day i entered in this table
this table have only one column and i record ((day user entered and saved in "day.user_day"))
i need to link this three tables
together
and lets make it easy we will change salary to attendance ...
i need to query all names and id in date and apear all employee what ever thy have time or not
like when i search only in day 4/8/2014
name id time
john 1 04/08/2014 06:00
man 2 null
scsv 3 04/08/2014 07:00
You want a LEFT JOIN:
SELECT * FROM emp LEFT JOIN salary ON emp.emp_id = salary.emp_id;
This will return all employees along with their emp_sal, which will be NULL if it's not in the salary table.
If what I read is right, you want to get the employee name and salary, returning all employees regardless if they have an entry in salary. If that is correct, this should work:
SELECT
e.emp_name,
e.emp_id,
s.emp_sal
FROM
emp AS e
LEFT JOIN salary AS s ON e.emp_id = s.emp_id
If, however, you only wanted the employees with an entry in the salary table, change LEFT JOIN to be INNER JOIN.
I am writing a simple query using in oracle database that finds the salary contribution by each department.
Here are my tables:
CREATE TABLE employee (
empid NUMBER,
fname VARCHAR2(20),
deptid NUMBER,
salary NUMBER
);
CREATE TABLE department (
deptid NUMBER,
deptname VARCHAR2(20)
);
Inserting data into this table:
INSERT INTO department VALUES (1, 'Sales');
INSERT INTO department VALUES (2, 'Accounting');
INSERT INTO employee VALUES (1,' John', 1,100);
INSERT INTO employee VALUES (2,' Lisa', 2,200);
INSERT INTO employee VALUES (3,' Jerry', 1,300);
INSERT INTO employee VALUES (4,' Sara', 1,400);
Now to find out the salary contribution in percentage by each department I am using below query:
select dept.deptname, sum(emp.salary)/(select sum(emp.salary) from employee emp)*100 as percentge from employee emp, department dept where dept.deptid=emp.deptid group by dept.deptname;
Is this efficient way of calculating my output or Is there any alternate way?
Please try:
select distinct a.*,
(sum(Salary) over(partition by a.DeptID))/(sum(Salary) over())*100 "Percent"
from department a join employee b on a.deptid=b.deptid
You don't need a subquery for this. You can use analytic functions:
select dept.deptname,
100*sum(emp.salary)/(sum(sum(emp.salary)) over ()) as percentage
from employee emp join
department dept
on dept.deptid = emp.deptid
group by dept.deptname;
I also changed the join syntax to use ANSI standard joins.
EDIT:
There is not a particular "issue" with using subqueries for this. A subquery does work. In general, though, subqueries are harder to optimize than the built-in features in Oracle (and in this case in ANSI SQL). In this simple case, I don't know if there is a performance difference.
As for analytic functions, they are a very powerful component of SQL and you should learn about them.
By with clause you can calculate sum for all departments once and then use it as parameter. On your example sum value for all departments calculated for each row and this will lead to performance loss.
with t as
(select sum(salary) as sum_salary from employee)
select dept.deptname, sum(emp.salary)/ sum_salary * 100 as percentge
from employee emp, department dept, t
where dept.deptid=emp.deptid group by dept.deptname, sum_salary;
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