Find the name, salary, Department name, salary level in the company and name of the leadership, salary of the leadership, level corresponding to the leadership, and employee name of each employee.

Source: Internet
Author: User
Tags dname

Find the name, salary, Department name, salary level in the company and name of the leadership, salary of the leadership, level corresponding to the leadership, and employee name of each employee.


Example:

The employee's name, salary, Department name, salary level in the company and the name of the leader, the salary of the leader, and the level corresponding to the leader are displayed.


This is a thinking question between emp, dept, and salgrade tables under oracle's default Scott user.


Analysis: first, identify the content in the salary level table (salgrade ).

select * from salgrade;


Query the name, salary, Department name, and salary of each employee in the company's salgrade)

SELECT distinct e.ename,e.sal,d.dname,g.grade e_grade FROM emp e,dept d,salgrade g,emp sWHERE (e.deptno=d.deptno) AND (e.sal BETWEEN g.losal AND g.hisal);


Add the name of the Supervisor

SELECT e.ename,e.sal,d.dname,g.grade,s.ename FROM emp e,dept d,salgrade g,emp s WHERE (e.deptno=d.deptno) AND (e.sal BETWEEN losal AND hisal) AND (s.empno=e.mgr);


Finally, determine the employee's manager name, the manager's salary, and the level corresponding to the manager's salary.

SELECT e.ename,e.sal,d.dname,g.grade e_grade,s.ename mgr_name,g2.grade m_gradeFROM emp e,dept d,salgrade g,emp s,salgrade g2WHERE (e.deptno=d.deptno) AND (e.sal BETWEEN g.losal AND g.hisal) AND (s.empno=e.mgr) AND (s.sal BETWEEN g2.losal AND g2.hisal);


From this example, we can see that for complex multi-table queries, we need to implement it step by step.


Further consideration:

How can I achieve this if I want to display the salary level according to the above result in a style such

1: pay-as-you-go

2: fourth class salary

3: Third-level salary

4: second-level salary

5: First class salary

You can only use the DECODE () function to implement this function.

SELECT e. ename, e. sal, d. dname, decode (g. grade, 5, 'first-level wage ', 4, 'second-level wage', 3, 'third-level wage ', 2, 'Fourth-level wage', 1, 'Wait 5') e_grade, s. ename mgr_name, s. sal mgr_sal, decode (g2.grade, 5, 'First wage ', 4, 'second wage', 3, 'third wage ', 2, 'Fourth wage ', 1, 'fifth-level wage ') m_gradeFROM emp e, dept d, salgrade g, emp s, salgrade g2WHERE (e. deptno = d. deptno) AND (e. sal BETWEEN g. losal AND g. hisal) AND (s. empno = e. mgr) AND (s. sal BETWEEN g2.losal AND g2.hisal)







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.