会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132382个问题
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 437楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 440楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 441楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 442楼

SQL语句练习.doc

老师关于实操练习我有几个疑问

问题一:第25题,给的答案似乎时没有考虑薪水要求在1200以上的雇员这个条件,以下是代码,请老师帮我看看,我写的对不对

我的代码

select department_id,avg(salary),max(salary) from employees where salary>1200 group by department_id having avg(salary) >1500 order by avg(salary) desc;

习题答案的代码:

select department_id,avg(salary),max(salary) from employees group by department_id having avg(salary) >1500 order by avg(salary) desc;


问题二:第29题 求比普通员工的最高薪水还要高的经理人名称

代码:

select distinct em.last_name from employees em where em.salary>(select max(e.salary) from employees e,employees em where e.manager_id = em.employee_id);

不明白为什么下面这句代码得出的是普通员工的最高薪水,我将max(salary)改成distinct e.employee_id,e.last_name得出的是全部员工,并没有排除是经理的员工

select max(e.salary) from employees e,employees em where e.manager_id = em.employee_id


问题三:31题求部门经理人中平均薪水最低的部门名称

虽然结果一样,但是答案给的代码计算的是最低的经理人薪水,而不是平均薪水最低的部门,请老师看一下我的代码是否正确,还是我理解的有问题

我的代码:

select rownum,department_id from(select m.department_id from employees e,employees m  where e.manager_id = m.employee_id group by m.department_id order by avg(m.salary) ) where rownum = 1;

答案:

select department_id from employees  where salary = ( select min(manager.salary) from  employees emp,employees manager where emp.manager_id = manager.employee_id);


问题四:33题 求薪水最高的第 6 到第 10 名雇员

答案给的代码运行后得出的并不是薪水的第6到第10 名

这是我的代码,请老师看一下

select * from(select rownum rw,last_name,salary from (select  last_name,salary from employees order by salary desc )) e where e.rw between 6 and 10;


问题五:13题  分别利用 like 操作符和正则表达式,查处名字中含有"S"或者"M"的人员

正则表达式不会写



JAVA 全系列/第三阶段:数据库编程/SQL 语言 444楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 446楼

我这边statement的部份一直报错是我少引用了什么吗?

public static void closeStatement(Statement statement){
    try {
        statement.close();
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
}



package com.itbaizhan;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * Jdbc tool class
 */
public class JdbcUtils {
    private static String url;
    private static String name;
    private static String pwd;
    static {
        try {
            Properties prop = new Properties();
            InputStream is = JdbTest2.class.getClassLoader().getResourceAsStream("jdbc.properties");
            prop.load(is);

            url = prop.getProperty("url");
            name = prop.getProperty("username");
            pwd = prop.getProperty("pwd");
            String drivername = prop.getProperty("driver");

            Class.forName(drivername);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //acquire database connection object
    public static Connection getConnection(){
        Connection connection = null;
        try {
            DriverManager.getConnection(url,name,pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
    public static void closeConnection(Connection connection){
        try {
            connection.close();
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }

        public static void commit(Connection connection){
            try {
                connection.commit();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        public static void rollback(Connection connection){
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        public static void closeStatement(Statement statement){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}


JAVA 全系列/第三阶段:数据库编程/JDBC技术 448楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 450楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637