[Cattle] off network database SQL combat (explanations)

1. Find all the information at the latest recruits

【answer】

hire_date there may be duplicate values, so it is necessary to find the maximum hire_date, and then screened to hire_date latest records are screened.

 

[Code]

1 SELECT * FROM employees
2 WHERE hire_date = (SELECT MAX(hire_date) FROM employees)
View Code

 

2, time to find recruits ranked third of all staff information

【answer】

Or there may be duplicate values ​​hire_date problem, so you need to find a third night of hire_date (sort here to remember with distinct weight), and then screened.

 

[Code]

1 SELECT * FROM employees
2 WHERE hire_date = (SELECT DISTINCT hire_date FROM employees
3                   ORDER BY hire_date DESC LIMIT 2, 1)
View Code

 

3, find the current salary details, and department number dept_no

【answer】

This is a good question pit Oh, because the title says "salary details, and their corresponding department number dept_no", so the salaries table is the main table, to be EDITORIAL? (I do not quite agree with this idea)

But I think there is no backstage scheduling problem, as long as the emp_no row a sequence could go.

 

[Code]

SELECT s.*, d.dept_no
FROM salaries s, dept_manager d
WHERE d.to_date = '9999-01-01' AND s.to_date = '9999-01-01' AND d.emp_no = s.emp_no
View Code

 

1 SELECT s.*, d.dept_no
2 FROM dept_manager d, salaries s
3 WHERE d.to_date = '9999-01-01' AND s.to_date = '9999-01-01' AND d.emp_no = s.emp_no
4 ORDER BY s.emp_no
View Code

 

4. Find all the staff have been assigned sectors last_name and first_name

【answer】

That some employees may not be assigned to departments, then simply connect to the employees left the department table table, that information will have on the department table, but there is no information on the employee table will not be screened out on the department table , in line with the subject of the request.

 

[Code]

1 SELECT e.last_name, e.first_name, d.dept_no
2 FROM dept_emp d LEFT JOIN employees e ON e.emp_no = d.emp_no
View Code

 

5, find all employees last_name and first_name and the corresponding department number dept_no

【answer】

Keep up with a counter-question just a bit, here is to make the employees table left connected to the department table, so that regardless of whether there is a corresponding department staff, can be displayed.

 

[Code]

1 SELECT e.last_name, e.first_name, d.dept_no
2 FROM employees e LEFT JOIN dept_emp d ON e.emp_no = d.emp_no
View Code

 

6. Find the entry salary of all employees time

【answer】

It should be note, because it is the salary at the time of entry is given to each employee, so it is necessary to add this condition e.hire_date = s.from_date.

 

[Code]

1 SELECT e.emp_no, s.salary
2 FROM employees e, salaries s 
3 WHERE e.emp_no = s.emp_no AND e.hire_date = s.from_date
4 ORDER BY e.emp_no DESC
View Code

 

7, the number rose to find salary rose more than 15 times the number of employees and their corresponding t emp_no

【answer】

GROUP BY function with the use of the polymerization, according to the classification emp_no, COUNT salary increase the number of recording each emp_no finally selected to be greater than 15.

HAVING COUNT oh to keep back the statement.

If you use the GROUP BY and aggregate functions is not very understanding, then you can poke here , I'm talking about feeling just fine.

 

[Code]

1 SELECT emp_no, COUNT(to_date) AS t
2 FROM salaries GROUP BY emp_no HAVING t > 15
View Code

 

8, find the current salary salary of all employees

【answer】

DISTINCT: If you act on a column, the same value in the same column will only appear once, if applied to all the columns, then the same values ​​for all columns are the same as before the same (the entire table can be used to weight).

ORDER BY: ORDER BY col DESC according col column in decreasing order, ASC ascending order.

 

[Code]

1 SELECT DISTINCT salary 
2 FROM salaries WHERE to_date = '9999-01-01'
3 ORDER BY salary DESC
View Code

 

9, to get the current salaries of all sectors of the current manager

【answer】

Similar Question 3

 

[Code]

1 SELECT d.dept_no, d.emp_no, s.salary
2 FROM dept_manager d, salaries s 
3 WHERE d.emp_no = s.emp_no AND d.to_date = '9999-01-01' AND s.to_date = '9999-01-01'
View Code

 

10, access to all non-manager employees emp_no

【answer】

Use NOT IN elected at employees but not dept_manager in emp_no record, then that is NOT IN the name suggests it, "not" means.

 

[Code]

1 SELECT e.emp_no FROM employees e
2 WHERE emp_no NOT IN(SELECT d.emp_no FROM dept_manager d)
View Code

 

11, all employees get the current manager

【answer】

If the meaning of the questions are clearly described in their own words do not show manager, you need to add this one e.emp_no! = M.emp_no, then required to join two tables query can be friends.

 

[Code]

1 SELECT e.emp_no, m.emp_no AS manager_no
2 FROM dept_emp e, dept_manager m 
3 WHERE e.dept_no = m.dept_no AND e.to_date = '9999-01-01'
4 AND m.to_date = '9999-01-01' AND e.emp_no != m.emp_no
View Code

 

12, to get the current staff of the highest salaries in all sectors relevant information

【answer】

It may be understood in two steps.

The first step: The two tables are connected by emp_no, and pick out the current staff working in all sectors, is currently 9999-01-01, did not say in the title, it is a small bug.

Step two: because of the need to give the highest wage information for all sectors, so we follow the sector grouping, the highest wage elected.

 

[Code]

1 SELECT d.dept_no, d.emp_no, s.salary
2 FROM dept_emp d, salaries s
3 WHERE d.emp_no = s.emp_no AND d.to_date = '9999-01-01' AND s.to_date = '9999-01-01'
4 GROUP BY d.dept_no HAVING MAX(s.salary)
View Code

 

13, according to the acquired title from titles grouped table

【answer】

The first step: The title grouping table

Step Two: After the record count packets having the same title, the process returns> to = 2.

 

[Code]

1 SELECT title, COUNT(title) AS t
2 FROM titles 
3 GROUP BY title HAVING t >= 2
View Code

 

14, titles obtained from the table are grouped by title, note for repeated emp_no be ignored.

【answer】

This problem must first understand the meaning of the title is clear, we want to find the meaning of the title is a title in accordance with the packet number of each of 2 or more (and can not contain duplicate, emp_no,)

For example, title emp_no

       1      1

       1       1

This can only be considered a COUNT oh. So go two steps away.

Step one: grouping according to title

Step Two: remove duplicate using DISTINCT emp_no, and then counted.

[Code]

1 SELECT title, COUNT(DISTINCT emp_no) AS t
2 FROM titles
3 GROUP BY title HAVING t >= 2
View Code

 

15, table lookup employees

【answer】

This problem is relatively simple, according to the meaning of the questions you can directly do it.

 

[Code]

1 SELECT * FROM employees
2 WHERE emp_no % 2 = 1 AND last_name != 'Mary'
3 ORDER BY hire_date DESC
View Code

 

16, the current statistics of each title corresponding to the type of employee salary corresponding to the current average wage

【answer】

Or two steps to understand.

The first step: first by the two tables emp_no

Step: According title packet, and belong to a title to be salay avg calculation.

 

[Code]

1 SELECT t.title, AVG(s.salary) AS avg
2 FROM titles t, salaries s
3 WHERE t.emp_no = s.emp_no AND t.to_date = '9999-01-01' AND s.to_date = '9999-01-01'
4 GROUP BY t.title
View Code

 

17, to get the current emp_no second most salary of employees and their corresponding salary salary

【answer】

Similar to the second question

 

[Code]

1 SELECT emp_no, salary FROM salaries
2 WHERE to_date = '9999-01-01' AND salary = (
3 SELECT DISTINCT salary FROM salaries
4     ORDER BY salary DESC LIMIT 1, 1
5 )
View Code

 

18、

Guess you like

Origin www.cnblogs.com/z1014601153/p/11310968.html