学通4中数据库SQL教程练习和答案

简介: 编写一个SQL语句,输出下面的结果

SELECT字段的别名练习(答案)

编写一个SQL语句,输出下面的结果




mysql> select empno 员工号,salary 月薪, salary*14 14薪 from employees;+-----------+----------+-----------+| 员工号    | 月薪     | 14薪      |+-----------+----------+-----------+|         1 | 20000.00 | 280000.00 ||         2 | 19100.00 | 267400.00 ||         3 | 23900.00 | 334600.00 ||         4 | 15000.00 | 210000.00 ||         5 | 14200.00 | 198800.00 ||         6 |  9700.00 | 135800.00 ||         7 |  8900.00 | 124600.00 ||         8 | 14900.00 | 208600.00 ||         9 | 15000.00 | 210000.00 |+-----------+----------+-----------+9 rows in set (0.00 sec)

WHERE练习答案




mysql> select name,salary,salary*1.1 "updated salary",hire_date from employees  where hire_date<'2010-01-01';+-----------+----------+----------------+------------+| name      | salary   | updated salary | hire_date  |+-----------+----------+----------------+------------+| 周福生    | 20000.00 |      22000.000 | 2009-12-02 || 赵卫华    | 15000.00 |      16500.000 | 2009-11-12 |+-----------+----------+----------------+------------+2 rows in set (0.00 sec)


AND、OR 和NOT运算符练习





mysql>  select * from employees where deptno<>3 and salary>15000;
mysql>  select * from employees where not (deptno=3 or salary<=15000);


BETWEEN练习的答案





mysql> select name,salary from employees where salary between 10000 and 15000;+-----------+----------+| name      | salary   |+-----------+----------+| 赵六      | 15000.00 || 李明      | 14200.00 || 程娟      | 14900.00 || 赵卫华    | 15000.00 |+-----------+----------+4 rows in set (0.00 sec)

LIKE练习的答案




mysql> select email from employees where email like '_h%@gmail.com';+----------------------+| email                |+----------------------+| zhou@gmail.com       || chengjuan@gmail.com  || zhaoweihua@gmail.com |+----------------------+3 rows in set (0.00 sec)

ORDER BY练习答案







mysql> select name,deptno,hire_date from employees order by deptno,hire_date desc;+-----------+--------+------------+| name      | deptno | hire_date  |+-----------+--------+------------+| 赵六      |      1 | 2019-12-01 || 王五      |      1 | 2013-01-03 || 赵卫华    |      1 | 2009-11-12 || 孙军      |      2 | 2022-05-22 || 李四      |      2 | 2011-02-10 || 周福生    |      2 | 2009-12-02 || 李明      |      3 | 2021-09-11 || 钱杰      |      3 | 2019-06-12 || 程娟      |      3 | 2013-07-22 |+-----------+--------+------------+9 rows in set (0.00 sec)

NOT IN的坑练习答案





mysql> select deptno,dname from dept dwhere d.deptno not in (select e.deptno from emp e where e.deptno is not null);+--------+-----------+| deptno | dname     |+--------+-----------+|      5 | Operation |+--------+-----------+1 row in set (0.00 sec)
mysql> select deptno,dname from dept d where not exists (select 1 from emp e where d.deptno=e.deptno);+--------+-----------+| deptno | dname     |+--------+-----------+|      5 | Operation |+--------+-----------+1 row in set (0.00 sec)

INSERT练习答案


insert into employees(empno,name,deptno,salary) values(17,'张小英',1,DEFAULT);

UPDATE练习答案




update employees set salary=salary*1.1 where empno in (select managerno from departments);


DELETE练习答案


delete from employees where deptno=(select deptno from departments where managerno=2);


INNER JION内连接练习答案




select j.*,e.name,d.dname from job_history j join employees e on j.empno=e.empno join departments d on e.deptno=d.deptno;

自连接练习答案




select j1.empno from(select empno from job_history j1 where deptno=2) j1join(select empno from job_history j2 where deptno=3) j2on j1.empno=j2.empno;

外连接练习答案





select e.empno,name,start_date,j.deptno from job_history j right join employees e on j.empno=e.empno;


Union练习的答案






select name,hire_date,'创始人' 资深程度 from employees where hire_date <'2010-01-01'unionselect name,hire_date,'老员工' 资深程度 from employees where hire_date between '2010-01-01' and '2019-12-31'unionselect name,hire_date,'新员工' 资深程度 from employees where hire_date >'2019-12-31';


HAVING练习


select empno,count(*) from job_history group by empno having count(*)>1;


子查询练习答案





select name,salaryfrom employeeswhere salary>(            select avg(salary)            from employees  6              );
NAME           SALARY---------- ----------周福生          20000王五            19100李四            23900



IN运算符中的子查询练习答案





select name from employeeswhere empno not in (                     select distinct empno                     from job_history                    );



子查询和连接的练习答案


select name  from employees left join job_history using (empno) where start_date is null;



exists的练习




select name from employees where not exists   (select 1 from job_history where employees.empno=job_history.empno);


SELECT子句中的子查询练习答案


select dname,(select sum(salary) from employees where deptno=d.deptno) 部门工资总和  from departments d;


PARTITION BY的练习答案









select e2.*from(   select e1.* ,   rank() over (partition by deptno order by hiredate) as rank_date   from employees e1)  e2where e2.rank_date=2;


CASE表达式练习的答案








select name, hiredate,case when hiredate<'2010-01-01' then '创始人' when hiredate between '2010-01-01' and '2019-12-31' then '老员工'else '新员工' end 资深程度from employeeswhere hiredate is not null;


CTE练习答案







with em_ch as( select * from employees where empno in        (select distinct empno from job_history))select * from em_ch;

视图练习的答案






create view emp_qq asselect empno,name,salary,hiredate,email from employees where email like '%@qq.com'order by hiredatewith check option;
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
SQL 机器学习/深度学习 人工智能
从“写SQL”到“聊数据”:NL2SQL如何用自然语言解锁数据库?
本文系统性地阐述了自然语言转SQL(NL2SQL) 技术如何让非技术背景的业务分析师实现数据自助查询,从而提升数据驱动决策的效率与准确性。
从“写SQL”到“聊数据”:NL2SQL如何用自然语言解锁数据库?
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
2090 152
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
1436 156
|
SQL 人工智能 Linux
SQL Server 2025 RC1 发布 - 从本地到云端的 AI 就绪企业数据库
SQL Server 2025 RC1 发布 - 从本地到云端的 AI 就绪企业数据库
1027 5
SQL Server 2025 RC1 发布 - 从本地到云端的 AI 就绪企业数据库
|
SQL 存储 监控
SQL日志优化策略:提升数据库日志记录效率
通过以上方法结合起来运行调整方案, 可以显著地提升SQL环境下面向各种搜索引擎服务平台所需要满足标准条件下之数据库登记作业流程综合表现; 同时还能确保系统稳健运行并满越用户体验预期目标.
623 6
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
2219 5
|
SQL Oracle 关系型数据库
Oracle数据库创建表空间和索引的SQL语法示例
以上SQL语法提供了一种标准方式去组织Oracle数据库内部结构,并且通过合理使用可以显著改善查询速度及整体性能。需要注意,在实际应用过程当中应该根据具体业务需求、系统资源状况以及预期目标去合理规划并调整参数设置以达到最佳效果。
880 8
|
SQL 人工智能 Java
用 LangChain4j+Ollama 打造 Text-to-SQL AI Agent,数据库想问就问
本文介绍了如何利用AI技术简化SQL查询操作,让不懂技术的用户也能轻松从数据库中获取信息。通过本地部署PostgreSQL数据库和Ollama模型,结合Java代码,实现将自然语言问题自动转换为SQL查询,并将结果以易懂的方式呈现。整个流程简单直观,适合初学者动手实践,同时也展示了AI在数据查询中的潜力与局限。
1649 8
|
SQL 人工智能 Linux
SQL Server 2025 RC0 发布 - 从本地到云端的 AI 就绪企业数据库
SQL Server 2025 RC0 发布 - 从本地到云端的 AI 就绪企业数据库
637 5