sql programming joining of four tables - sql

select e.empno, e.ename, e.sal, d.deptno, d.dname, l.loc_id, l.loc_name, a.add1, a.add2, a.pin from emp e, address a, location l, dept d on inner join emp,deptno and on inner join emp,address and inner join dept,location having a.add2=rp and l.loc_name=kolkata;

select e.empno, e.ename, e.sal, d.deptno, d.dname, l.loc_id, l.loc_name, a.add1, a.add2, a.pin
from emp e
inner join dept d on d.deptno=e.deptno
inner joint location l on l.loc_id=d.loc_id
inner join address a on a.adress_id=e.address_id
where a.add2 = 'rp' and l.loc_name='kolkata';
Did you meant somethig like this?

Related

Select the manager name with the most employees

We have a table emp with columns empno, ename, job, mgr, hiredate, sal, comm, deptno
I have tried
SELECT m.ename, COUNT(e.empno) FROM emp e
INNER JOIN emp m ON e.empno = m.empno
GROUP BY m.ename HAVING COUNT(e.empno) = GREATEST(COUNT(e.empno));
My output is the names of the managers each with the value 1
How do we output the name of the manager with the most employees?
The following works:
SELECT COUNT(e.empno), m.ename FROM emp e
INNER JOIN emp m ON e.mgr = m.empno
GROUP BY m.ename HAVING GREATEST(COUNT(e.ename)) = COUNT(e.ename)
LIMIT 1;
First, fix the ON clause. Second, Use ORDER BY and LIMIT if you want one row:
SELECT m.ename, COUNT(e.empno)
FROM emp e INNER JOIN
emp m
ON m.empno = e.mgr
GROUP BY m.ename
ORDER BY COUNT(e.empno) DESC
LIMIT 1;
Your HAVING clause does not filter anything because any (non-NULL) value is equal to itself.

Rewriting uncorrelated subquery to correlated subquery

I am working with a default oracle scott database with additional table PROJECT, where there are two columns: projectno and empno.
I want to select names of employees with the highest salaries for each project.
I know how to do it with uncorrelated subquery:
SELECT p.projno,
e.sal,
e.ename
FROM emp e
INNER
JOIN proj_emp p
ON e.empno = p.empno
WHERE (e.sal, p.projno)
IN (SELECT MAX(e.sal),
p.projno
FROM emp e INNER JOIN proj_emp p
ON e.empno = p.empno
GROUP BY p.projno)
However, i was asked to do it with a correlated subquery written in a WHERE clause, but i am wondering if it is possible?
I would do :
SELECT t.*
FROM (SELECT p.projno, e.sal, e.ename,
DENSE_RANK() OVER (PARTITION BY p.projno ORDER BY e.sal DESC) AS Seq
FROM emp e INNER JOIN
proj_emp p
ON e.empno = p.empno
) t
WHERE Seq = 1;
EDIT : If you want to do it with correlated subquery then i would rewrite your query to make correlated :
SELECT p.projno, e.sal, e.ename
FROM emp e INNER JOIN
proj_emp p
ON e.empno = p.empno
WHERE e.sal = (SELECT MAX(e1.sal)
FROM emp e1 INNER JOIN
proj_emp p1
ON e1.empno = p1.empno
WHERE p1.projno = p.projno
);
Use window functions:
SELECT projno, sal, ename
FROM (SELECT p.projno, e.sal, e.ename,
MAX(e.sal) OVER (PARTITION BY p.projno) as max_sal
FROM emp e INNER JOIN
proj_emp p
ON e.empno = p.empno
) ps
WHERE sal = max_sal;

Using WHERE clause on a joined table

I am trying to find the best way to filter out rows by using conditions based on a joined table. As an example I am joining the employees and salary grade table based on the salary grade for each employee. Then I want to show only the employees that have the same grade as a certain employee (Blake). I used the following code:
SELECT e.ename, e.sal, sg.grade
FROM emp e JOIN salgrade sg
ON(e.sal BETWEEN sg.losal AND sg.hisal)
WHERE sg.grade = (SELECT sg.grade FROM emp e JOIN salgrade sg ON(e.sal BETWEEN sg.losal AND sg.hisal) WHERE e.ename = 'BLAKE')
ORDER BY e.sal DESC
Is there a more optimal way to write the query?
Here is one method that uses window functions:
SELECT es.*
FROM (SELECT e.ename, e.sal, sg.grade,
MAX(CASE WHEN e.ename = 'BLAKE' THEN sg.grade END) OVER () as blake_grade
FROM emp e JOIN
salgrade sg
ON e.sal BETWEEN sg.losal AND sg.hisal
) es
WHERE grade = blake_grade
ORDER BY e.sal DESC;
I wouldn't use a join in the select in the WHERE clause; rather, I would use an inner scalar subquery to pick up BLAKE's salary and then an outer scalar subquery to pick up his salgrade. Otherwise very similar to your query:
select e.ename, e.sal, s.grade
from emp e inner join salgrade s on e.sal between s.losal and s.hisal
where s.grade = ( select grade
from salgrade
where (select sal from emp where ename = 'BLAKE')
between losal and hisal
)
order by sal desc
;
Using the same idea, you could do away with the first join as well (by returning the losal and hisal for BLAKE as well as his salgrade), but perhaps that is taking it too far.
If this is just about not having to write the same code twice, you can use a WITH clause:
WITH emps_and_sals AS
(
SELECT e.ename, e.sal, sg.grade
FROM emp e
JOIN salgrade sg ON e.sal BETWEEN sg.losal AND sg.hisal
)
SELECT *
FROM emps_and_sals
WHERE grade = (SELECT grade FROM emps_and_sals WHERE ename = 'BLAKE')
ORDER BY sal DESC;

"Double Group By" in Oracle SQL?

As I was learning SQL statements I encountered one example (regarding the demo SCOTT database), I have no idea how to solve.
In which department(s) are all salgrades present?
My most promising approach is to group all salgrades and departments in the joined tables emp, dept and salgrade:
SELECT s.grade AS "Salgrade",
d.dname AS "Department ID"
FROM emp e INNER JOIN dept d ON(e.deptno = d.deptno)
INNER JOIN salgrade s ON(e.sal BETWEEN s.losal AND s.hisal)
GROUP BY d.dname, s.grade
Executing this gives me the following results:
If I could group this another time by department, COUNT(*) could give me the number of different salgrades per department. Then I could compare this number (with HAVING) to the following subselect:
(SELECT COUNT(*)
FROM salgrade)
Is there any possibility to group a table which already contains
GROUP BY?
Is there another (better) approach I could use?
I am using an apex-oracle-server with "Application Express 4.2.4.00.07"
Minor change from your version, by removing the grouping inside, and this version, first generates, salgrade and department of all employees, and then doing a grouping outside, counting distinct salary grades.
SELECT Department_ID
FROM
(
SELECT s.grade AS Salgrade,
d.dname AS Department_ID
FROM emp e
INNER JOIN dept d ON(e.deptno = d.deptno)
INNER JOIN salgrade s ON(e.sal BETWEEN s.losal AND s.hisal)
)
GROUP BY Department_ID
HAVING COUNT(distinct Salgrade) = ( SELECT count(1) FROM salgrade);
I found an even easier solution now:
SELECT d.dname
FROM emp e INNER JOIN dept d ON(e.deptno = d.deptno)
INNER JOIN salgrade s ON(e.sal BETWEEN s.losal AND s.hisal)
GROUP BY d.dname
HAVING COUNT(DISTINCT s.grade) = (SELECT COUNT(*) FROM salgrade);
Simple way would be - if performance is not a problem.
SELECT
COUNT(DISTINCT [Salgrade]) AS [COUNT]
,[Department ID]
FROM (SELECT s.grade AS "Salgrade",
d.dname AS "Department ID"
FROM emp e INNER JOIN dept d ON(e.deptno = d.deptno)
INNER JOIN salgrade s ON(e.sal BETWEEN s.losal AND s.hisal)
GROUP BY d.dname, s.grade) DEPT_SALE
GROUP BY [Department ID]
There could be better solutions though if we know more of your base tables - emp & salegrade

using multiple table in SQL

I've got three tables:
department, employee and telephone
employee has columns EmpNo, Surname, Firstname and DeptNo.
telephone has columns EmpNo and Extension.
department has columns DeptNo and DeptName.
What I am trying to get is the full name, department name and telephone extension.
I think I am almost there but its not working yet.
My query
SELECT e.Firstname, e.Surname, d.DeptName, t.Extension
FROM employee AS e
INNER JOIN department AS d
INNER JOIN telephone AS t
ON e.DeptNo = d.DeptNo
ON t.EmpNo = e.EmpNo;
To fix your problem, move one of the ON clauses before the next JOIN
SELECT e.Firstname, e.Surname, d.DeptName, t.Extension
FROM employee AS e
INNER JOIN department AS d
ON e.DeptNo = d.DeptNo
INNER JOIN telephone AS t
ON t.EmpNo = e.EmpNo;
The ON clause must follow the JOIN that it is modifying.
So:
INNER JOIN department d ON e.DeptNo = d.DeptNo
and
INNER JOIN telephone t ON t.EmpNo = e.EmpNo;
Your query is wrong. Join is not in correct format. Use like below
SELECT e.Firstname, e.Surname, d.DeptName, t.Extension
FROM employee AS e
inner join department AS d
ON e.DeptNo = d.DeptNo
inner join telephone AS t
ON t.EmpNo = e.EmpNo;