美文网首页
SQL题练习

SQL题练习

作者: Cracks_Yi | 来源:发表于2017-08-19 10:38 被阅读0次

    SQL语法总结:http://www.w3school.com.cn/sql/sql_func_min.asp
    SQL练习:https://www.nowcoder.com/ta/sql

    1.

    查找最晚入职员工的所有信息
    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 WHERE hire_date = (SELECT MAX(hire_date) FROM employees)
    

    SELECT * FROM employees ORDER BY hire_date DESC LIMIT 1
    

    2.

    查找入职员工时间排名倒数第三的员工所有信息
    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));

    LIMIT m,n : 表示从第m+1条开始,取n条数据;
    LIMIT n : 表示从第0条开始,取n条数据,是limit(0,n)的缩写。

    SELECT * FROM employees ORDER BY hire_date DESC LIMIT 2,1
    

    3.

    查找各个部门当前(to_date='9999-01-01')领导当前薪水详情以及其对应部门编号dept_no
    CREATE TABLE dept_manager (
    dept_no char(4) NOT NULL,
    emp_no int(11) NOT NULL,
    from_date date NOT NULL,
    to_date date NOT NULL,
    PRIMARY KEY (emp_no,dept_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 s.*, dept_no
    FROM salaries s 
    INNER JOIN dept_manager d
    ON d.emp_no = s.emp_no 
    WHERE s.to_date = '9999-01-01'
    AND d.to_date = '9999-01-01'
    

    4.

    查找所有已经分配部门的员工的last_name和first_name
    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 e.last_name, e.first_name, d.dept_no 
    FROM dept_emp d, employees e
    WHERE d.emp_no = e.emp_no
    

    5.

    查找所有员工的last_name和first_name以及对应部门编号dept_no,也包括展示没有分配具体部门的员工
    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 e.last_name, e.first_name, d.dept_no
    FROM employees e
    LEFT JOIN dept_emp d
    ON e.emp_no = d.emp_no
    

    6.查找所有员工入职时候的薪水情况

    查找所有员工入职时候的薪水情况,给出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));

    SELECT e.emp_no, s.salary
    FROM employees e, salaries s
    WHERE e.emp_no = s.emp_no
    AND hire_date = from_date
    ORDER BY e.emp_no DESC
    

    查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t
    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));

    HAVING用于对聚合结果做限制。这里有一个错误,涨薪大于15次,那么t应该>16。

    SELECT emp_no, COUNT(*) AS t
    FROM salaries s
    GROUP BY emp_no
    HAVING t > 15
    

    找出所有员工当前(to_date='9999-01-01')具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示
    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 DISTINCT salary
    FROM salaries
    WHERE to_date = '9999-01-01'
    ORDER BY salary DESC
    




    .查找employees表

    查找employees表所有emp_no为奇数,且last_name不为Mary的员工信息,并按照hire_date逆序排列
    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
    WHERE emp_no % 2 != 0
    AND last_name != 'Mary'
    ORDER BY hire_date DESC
    

    相关文章

      网友评论

          本文标题:SQL题练习

          本文链接:https://www.haomeiwen.com/subject/bcttdxtx.html