Five MySQL channels a day --- 1

  1. Find all information about the latest employee

     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`));
    ---------------------------------
    select * from employees order by hire_date desc limit 1 
  2. Find all the information of the employee whose time rank is the third from the bottom

    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`));
    ----------------------------------
    select * from employees order by hire_date desc limit 1 offset 2(offset:Line number, starting at 0)
  3. Find the last name and first name of all employees who have assigned departments

    CREATE TABLE `dept_emp` (
    `emp_no` int(11) NOT NULL,
    `dept_no` char(4) NOT NULL,
    `from_date` date NOT NULL,
    `to_date` date NOT NULL,
    PRIMARY KEY (`emp_no`,`dept_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`));
    ------------------------------------
    select last_name,first_name,dept_no 
    from dept_emp join  employees
    on dept_emp.emp_no = employees.emp_no
  4. Find the last name and first name of all employees and the corresponding department number dept No., including displaying the employees without specific department assignment.

    CREATE TABLE `dept_emp` (
    `emp_no` int(11) NOT NULL,
    `dept_no` char(4) NOT NULL,
    `from_date` date NOT NULL,
    `to_date` date NOT NULL,
    PRIMARY KEY (`emp_no`,`dept_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`));
    ---------------------------------
    select last_name,first_name,dept_no 
    from employees left join  dept_emp
    on dept_emp.emp_no = employees.emp_no
  5. Find the salary of all employees at the time of entry, give EMP no and salary, and reverse the order according to 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`));
    ---------------------------------
    select e.emp_no,salary 
    from employees e left join salaries s
    on e.emp_no = s.emp_no
    and e.hire_date = s.from_date
    order by e.emp_no desc

Keywords: PHP

Added by ghe on Thu, 31 Oct 2019 22:24:00 +0200