SQL cow brush off questions (continually updated)

SQL cattle off topic

Topic links: https://www.nowcoder.com/ta/sql

  1. Find all the information at the latest recruits

    select * 
    from 
    employees
    where 
    hire_date = (select max(hire_date) from employees);
  2. Find recruits ranked No. 3 time employees all the information

    SELECT  *  
    from  
    the Employees 
    Order  by  
    the hire_date 
    desc  - descending 
    limit 2 , . 1 ; - from 3, an offset, i.e., item 3
  3. Find current salary details of the current (to_date = '9999-01-01') leadership of the various departments and their corresponding department number dept_no

    select s.*,d.dept_no
    from salaries as s
    join dept_manager as d
    on d.emp_no=s.emp_no
    where s.to_date='9999-01-01'
    and d.to_date='9999-01-01';
  4. Find all the staff have been assigned sectors last_name and first_name

    select e.last_name,e.first_name,d.dept_no
    from employees as e
    join dept_emp as d
    on e.emp_no = d.emp_no;
  5. Find all employees last_name and first_name and the corresponding department number dept_no, but also show employees are not assigned specific sectors

    SELECT e.LAST_NAME, e.first_name, d.dept_no
     from the Employees AS E
     left  the Join dept_emp AS D - sitting on the main table, the right table supplement, complement empty NULL 
    ON e.emp_no = d.emp_no;
  6. Find all employees recruited when the salary situation, given emp_no and salary, and in reverse order according to emp_no

    select e.emp_no,s.salary
    from salaries as s
    join employees as e
    where e.emp_no = s.emp_no
    and e.hire_date = s.from_date
    order by
    e.emp_no
    desc;
  7. Find the number rose rose more than 15 times the salary of the employee number and its corresponding t emp_no

    count (): number of statistical records

    Also you need to group by emp_no records each employee's record in a display in

    select emp_no,count(emp_no) as t
    from salaries
    group by
    emp_no
    having t > 15;
  8. Find all current employees (to_date = '9999-01-01') specific salary salary, the same salary for the show only once, and are displayed in reverse order

    distinct: remove duplicate

    select distinct salary-- 去除重复
    from salaries
    where to_date = '9999-01-01'
    order by
    salary
    desc;
  9. Get the current salary situation in all sectors of the current manager, gives dept_no, emp_no and salary, currently represents to_date = '9999-01-01'

    select d.dept_no,d.emp_no,s.salary
    from dept_manager as d
    join salaries as s
    on d.emp_no=s.emp_no
    where d.to_date='9999-01-01'
    and s.to_date='9999-01-01';
  10. Get all non-manager employees emp_no

    select emp_no
    from employees
    where emp_no not in (select emp_no from dept_manager);
  11. Get all the current staff of manager, if the current manager is not in their own words the results show that the current representation to_date = '9999-01-01'.

    SELECT e.emp_no, m.emp_no AS manager_no
     from dept_emp AS E
     the Join dept_manager AS m
     ON e.dept_no = m.dept_no
     WHERE e.emp_no <> m.emp_no - this is that they do not display manager, <> Not equal 
    and E .to_date = ' 9999-01-01 ' 
    and m.to_date = ' 9999-01-01 ' ;
  12. Get the current staff of the highest salaries in all sectors relevant information given dept_no, emp_no and their corresponding salary

    select d.dept_no,d.emp_no,max(s.salary)
    from dept_emp as d
    join salaries as s
    on d.emp_no = s.emp_no
    group by
    d.dept_no;
  13. Obtained from the title titles table grouped, in groups of more than 2, and gives the corresponding title number t.

    select title,count(title) as t
    from titles
    group by
    title
    having t >= 2;
  14. Find all emp_no odd employees table, and last_name employee information is not Mary's, and arranged in reverse order hire_date

    select * from employees
    where emp_no%2 =1
    and last_name <> 'Mary'
    order by
    hire_date
    desc;
  15. Current statistics of each title type corresponds to the current average wage of employees (to_date = '9999-01-01') corresponding salary. The results are given title, and the average wage avg.

    select t.title,avg(s.salary)
    from titles as t
    join salaries as s
    on t.emp_no = s.emp_no
    where s.to_date='9999-01-01'
    and t.to_date='9999-01-01'
    group by
    t.title;
  16. Obtaining a second plurality of current emp_no (to_date = '9999-01-01') and its corresponding salary employees salary salary

    SELECT , emp_no,, the salary from ual relations
     WHERE TO_DATE = ' 9999-01-01 ' 
    Order  by 
    the salary 
    desc 
    limit . 1 , . 1 ;   - in descending order, the second to take
  17. Find the current salary (to_date = '9999-01-01') ranked second and more employees number emp_no, salary salary, last_name and first_name, are not allowed to use order by

    select e.emp_no,s.salary,e.last_name,e.first_name
    from employees as e
    join salaries as s
    on e.emp_no=s.emp_no
    where s.to_date='9999-01-01'
    order by
    s.salary
    desc
    limit 1,1;

    Without using order by: nested need to combine and select max

    SELECT e.emp_no, max (the salary), e.LAST_NAME, e.first_name - where a max (the salary) 
    from   ual relations AS S, the Employees AS E
     WHERE s.emp_no = e.emp_no
     and the salary <   - less than the maximum salary the largest is the second largest 
    (
     SELECT  max (the salary)
     from ual relations
     WHERE TO_DATE =  ' 9999-01-01 ' 
    );
  18. Find last_name and first_name and corresponding dept_name all employees, including temporary employees are not assigned sector

    Double left connection

    select e.last_name,e.first_name,de.dept_name
    from employees as e
    left join dept_emp as d
    on e.emp_no=d.emp_no
    left join departments as de
    on d.dept_no=de.dept_no;
  19. Find employee number 10001 emp_no since its entry salary salary increase value growth

    By entry time to sort the query:

    If the direct use of the salary maximum - minimum, it may be the last time a pay cut.

    SELECT ( 
    (SELECT salary FROM salaries WHERE emp_no = 10001 ORDER BY to_date DESC LIMIT 1) -
    (SELECT salary FROM salaries WHERE emp_no = 10001 ORDER BY to_date ASC LIMIT 1)
    ) AS growth
  20. Find all the staff since the entry salary increase, the number of employees is given emp_no and its corresponding salary increase growth, and growth in ascending order

    - the two results of the query, as two tables, for the Join 
    SELECT S2.emp_no, (S2.salary - S1.salary) AS Growth
     from  
    ( SELECT e.emp_no, s.salary 
      from the Employees AS E 
      the Join ual relations AS S 
      ON e.emp_no = s.emp_no 
      and e.hire_date = s.from_date) AS S1   - all employees of the workers owned 
    the Join  
    ( the SELECT e.emp_no, s.salary 
      from the employees AS E 
      the Join salaries ASS 
      ON e.emp_no = s.emp_no 
      and s.to_date = ' 9999-01-01 ' ) AS S2    - all employees of current wages 
    ON S1.emp_no = S2.emp_no
     the Order  by Growth;
  21.  

Guess you like

Origin www.cnblogs.com/mussessein/p/11627027.html