Cattle off network SQL combat two brush | Day1

Disclaimer: This article is a blogger original article, please indicate the source. https://blog.csdn.net/weixin_44915703/article/details/91348696

Cattle off network SQL combat two brush | Day1

Recently started to review and consolidate SQL, doing every day doing notes in a word document, found that just 10 days after brushing all 61 exercises. Brush title two intend to re-organize topics and ideas, fought blog notes. This is for your own study notes, but also hope that're reading this blog you also find it useful ~

The format of each note roughly, three major sections:

  • Outline
  • Title (Title Description, ideas, codes, reference)
  • review

Topic Source: https://www.nowcoder.com/ta/sql then we start Day 1 notes it!

First, outline

Question number Knowledge Point
1 MAX () function
2 LIMIT and OFFSET usage
3 INNER JOIN to join two tables
4 INNER JOIN to join two tables
5 LEFT JOIN to join two tables
6 INNER JOIN / parallel query / ORDER BY reverse order

Second, the title

1. Find all the information at the latest recruits

  • Title Description

Find all the information at the latest recruits of
the CREATE TABLE employees(
emp_noint (11) the NOT NULL,
birth_dateDATE the NOT NULL,
first_nameVARCHAR (14) the NOT NULL,
last_nameVARCHAR (16) the NOT NULL,
genderchar (1) the NOT NULL,
hire_dateDATE the NOT NULL,
PRIMARY KEY ( emp_no));

  • Output Description
emp_no birth_date first_name last_name gender hire_date
10008 1958-02-19 Sniy Kalloufi M 1994-09-15
  • Ideas
    with MAX () function to find the "latest" record.

  • Code

SELECT * FROM employees
WHERE hire_date = (SELECT MAX(hire_date) FROM employees)

2. Find recruits ranked third time information of all employees

  • Title Description

Find recruits time ranked third from the staff all the information
the CREATE TABLE employees(
emp_noint (11) the NOT NULL,
birth_dateDATE the NOT NULL,
first_nameVARCHAR (14) the NOT NULL,
last_nameVARCHAR (16) the NOT NULL,
genderchar (1) the NOT NULL,
hire_dateDATE the NOT NULL ,
a PRIMARY KEY ( emp_no));

  • Output Description
emp_no birth_date first_name last_name gender hire_date
10005 1955-01-21 Kyoichi Maliniak M 1989-09-12
  • Thinking
  1. With LIMIT and OFFSET find the "bottom third" records;
  2. Note that, with distinct deduplication, because there may be multiple duplicate date.
  • Code
SELECT * FROM employees 
WHERE hire_date = 
(SELECT DISTINCT hire_date FROM employees ORDER BY hire_date DESC LIMIT 1 OFFSET 2)
  • Limit the use of the offset for use with
    for example, LIMIT 3 OFFSET 1which means, skip recording article (i.e., starting from the second record), and returns the next three records. That is ultimately obtained, the original clause 2,3,4 record.

    Reference: https://www.runoob.com/sqlite/sqlite-limit-clause.html

3. Find the current salary details, and department number dept_no

  • topic

Find (to_date = '9999-01-01') leading the current salary details of the various departments and their counterparts in the current number dept_no
the CREATE TABLE dept_manager(
dept_nochar (4) the NOT NULL,
emp_noint (11) the NOT NULL,
from_dateDATE the NOT NULL,
to_dateDATE the NOT NULL,
KEY a PRIMARY ( emp_no, dept_no));
the CREATE TABLE salaries(
emp_noint (. 11) the NOT NULL,
salaryint (. 11) the NOT NULL,
from_dateDATE the NOT NULL,
to_dateDATE the NOT NULL,
a PRIMARY KEY ( emp_no, from_date));

  • Output Description
emp_no salary from_date to_date dept_no
10002 72527 2001-08-02 9999-01-01 d001
10004 74057 2001-11-27 9999-01-01 d004
10005 94692 2001-09-09 9999-01-01 d003
10006 43311 2001-08-02 9999-01-01 d002
10010 94409 2001-11-23 9999-01-01 d006
  • Thinking
  1. The two tables are connected (inner join), by association emp_no
  2. Two "current" - the current leadership, current salary, with to_date = "9999-01-01" limit
  • Code
SELECT s.*, dm.dept_no
FROM salaries AS s INNER JOIN dept_manager AS dm
ON s.emp_no = dm.emp_no 
WHERE s.to_date = '9999-01-01' AND dm.to_date = '9999-01-01'
  • Several connections on
    Here Insert Picture Description
    this map Figure Wayne expressed very clearly, before I excerpt of the notes, now can not find the source, and the source of the network.

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

  • topic

Find employees all have been assigned sectors last_name and first_name
the CREATE TABLE dept_emp(
emp_noint (11) the NOT NULL,
dept_nochar (4) the NOT NULL,
from_dateDATE the NOT NULL,
to_dateDATE the NOT NULL,
PRIMARY KEY ( emp_no, dept_no));
the CREATE TABLE employees(
emp_noint (11) NULL the NOT,
birth_dateDATE the NOT NULL,
first_nameVARCHAR (14) the NOT NULL,
last_nameVARCHAR (16) the NOT NULL,
genderchar (. 1) the NOT NULL,
hire_dateDATE the NOT NULL,
a PRIMARY KEY ( emp_no));

  • Output Description
last_name first_name dept_no
Facello Georgi d001
Omission Omission Omission
Piveteau Duangkaew d006
  • Ideas
    To screen out "has been assigned sector employees", two tables can be connected with the INNER JOIN keyword emp_no. 3 similar problems.

  • Code

SELECT e.last_name, e.first_name, de.dept_no
FROM employees AS e
INNER JOIN dept_emp AS de ON e.emp_no = de.emp_no

5. Find all employees last_name and first_name and the corresponding department number dept_no

  • topic

Find all employees last_name and first_name and the corresponding department number dept_no, but also to show not assigned a specific sector employees
the CREATE TABLE dept_emp(
emp_noint (11) the NOT NULL,
dept_nochar (4) the NOT NULL,
from_dateDATE the NOT NULL,
to_dateDATE the NOT NULL,
PRIMARY KEY ( emp_no, dept_no));
the CREATE TABLE employees(
emp_noint (. 11) the NOT NULL,
birth_dateDATE the NOT NULL,
first_nameVARCHAR (14) the NOT NULL,
last_nameVARCHAR (16) the NOT NULL,
genderchar (. 1) the NOT NULL,
hire_dateDATE the NOT NULL,
a PRIMARY KEY ( emp_no));

  • Output Description
last_name first_name dept_no
Facello Georgi d001
Omission Omission Omission
Lock Mary NULL (in sqlite in here is empty, MySQL is NULL)
  • Ideas
    different from the title 4, "but also did not show the distribution sector-specific staff," this time with a LEFT JOIN to join two tables. Table dept_emp left connection table employees, the employees table will return all the data, even if no corresponding data table dept_emp.

    JOINS usage can be viewed topic references 3.

  • Code

SELECT e.last_name, e.first_name, de.dept_no
FROM employees AS e
LEFT JOIN dept_emp AS de ON e.emp_no = de.emp_no

6. Find the salaries of all staff induction time

  • topic

查找所有员工入职时候的薪水情况,给出emp_no以及salary, 并按照emp_no进行逆序
CREATE TABLE employees (
emp_no int(11) NOT NULL,
birth_date date NOT NULL,
first_name varchar(14) NOT NULL,
last_name varchar(16) NOT NULL,
gender char(1) NOT NULL,
hire_date date NOT NULL,
PRIMARY KEY (emp_no));
CREATE TABLE salaries (
emp_no int(11) NOT NULL,
salary int(11) NOT NULL,
from_date date NOT NULL,
to_date date NOT NULL,
PRIMARY KEY (emp_no,from_date));

  • 输出描述
emp_no salary
10011 25828
省略 省略
10001 60117
  • 思路
  1. 用emp_no关键字连接两张表;
  2. 「所有员工入职时候的薪水情况」,即通过 employees表的hire_date = salaries表的from_date 限制;
  3. 「按照emp_no进行逆序」,即用ORDER BY 对emp_no进行排序。
  • 代码
  1. 方法一:INNER JOIN连接两张表
SELECT s.emp_no, s.salary
FROM employees AS e INNER JOIN salaries AS s
ON e.emp_no = s.emp_no AND e.hire_date = s.from_date
ORDER BY s.emp_no DESC
  1. 方法二:并列查询
SELECT s.emp_no, s.salary
FROM employees AS e, salaries AS s
WHERE e.emp_no = s.emp_no AND e.hire_date = s.from_date
ORDER BY s.emp_no DESC
  • JOIN the INNER VS parallel query
    Q: These two methods are different?
    A: connection within the intersection after taking about two tables to form a new table, two tables are still tied with FROM or two tables. If you also want to use the new operating table within the connector. From an efficiency point of view should be parallel query FROM faster, because there is no formation of a new table. This problem no difference from the point of view the effect of two methods.

    Source: Cattle customer network, Author: wasrehpic; https://www.nowcoder.com/questionTerminal/23142e7a23e4480781a3b978b5e0f33a

Third, review

It corresponds to the outline, combing classified Day1 of six questions again,

Question number Knowledge Point
1 Use function MAX ()
2 Use clause LIMIT, with OFFSET
3、4、5、6 Table connection (INNER JOIN / LEFT JOIN)
6 ORDER BY clause to sort

This is my first blog - love, please continue to pay attention, thank you!

Guess you like

Origin blog.csdn.net/weixin_44915703/article/details/91348696