当前位置:网站首页>SQL 题目整理
SQL 题目整理
2022-07-25 09:26:00 【scwMason】
1.题目描述
对所有员工的当前(to_date='9999-01-01')薪水按照salary进行按照1-N的排名,相同salary并列且按照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`));思路
排名第几也就是说比他高的有几个人,所以一开始比较salary,统计有哪些人的salary比这个人高
实现
select s1.emp_no,s1.salary,count(distinct s2.salary) as rank
from salaries as s1,salaries as s2
where s1.to_date='9999-01-01' and s2.to_date='9999-01-01' and s1.salary<=s2.salary
group by s1.emp_no
order by s1.salary desc,s1.emp_no asc采用了复用表的方式,s1.salary<=s2.salary 就可以统计出比当前这个s1.salary高的有多少个,因为两个一样多的人排名相同,所以要用distinct去重一下,这样两个相同的salary,比他们多的数量也相同。然后排序先后两次就可以。
2.题目描述
汇总各个部门当前员工的title类型的分配数目,结果给出部门编号dept_no、dept_name、其当前员工所有的title以及该类型title对应的数目count
CREATE TABLE `departments` (
`dept_no` char(4) NOT NULL,
`dept_name` varchar(40) NOT NULL,
PRIMARY KEY (`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 IF NOT EXISTS `titles` (
`emp_no` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`from_date` date NOT NULL,
`to_date` date DEFAULT NULL);思路
分别需要对title和dept_no进行分组
实现
select part.dept_no,part.dept_name,tm.title,count(tm.title) as count
from(select dep.dept_no,dep.emp_no,t.title from dept_emp dep inner join titles t
on dep.emp_no=t.emp_no
where t.to_date='9999-01-01' and dep.to_date='9999-01-01') as tm inner join departments part
on tm.dept_no=part.dept_no
group by part.dept_no,tm.title
3.题目描述
film表
| 字段 | 说明 |
| film_id | 电影id |
| title | 电影名称 |
| description | 电影描述信息 |
CREATE TABLE IF NOT EXISTS film (
film_id smallint(5) NOT NULL DEFAULT '0',
title varchar(255) NOT NULL,
description text,
PRIMARY KEY (film_id));
category表
| 字段 | 说明 |
| category_id | 电影分类id |
| name | 电影分类名称 |
| last_update | 电影分类最后更新时间 |
CREATE TABLE category (
category_id tinyint(3) NOT NULL ,
name varchar(25) NOT NULL, `last_update` timestamp,
PRIMARY KEY ( category_id ));
film_category表
| 字段 | 说明 |
| film_id | 电影id |
| category_id | 电影分类id |
| last_update | 电影id和分类id对应关系的最后更新时间 |
CREATE TABLE film_category (
film_id smallint(5) NOT NULL,
category_id tinyint(3) NOT NULL, `last_update` timestamp);
查找描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类对应电影数量>=5部
思路
我们先找出所有类别中电影数量大于5的,建立一个新表,然后和其他表联合查询
实现
select c.name,count(fc.film_id)
from (select category_id,count(film_id) as categry_num from film_category
group by category_id having count(film_id)>=5) as fm,film as f,category as c,film_category as fc
where f.description like '%robot%' and
f.film_id=fc.film_id and
fm.category_id=c.category_id and
c.category_id=fc.category_id
4.题目描述
对于表actor批量插入如下数据,如果数据已经存在,请忽略,不使用replace操作
CREATE TABLE IF NOT EXISTS actor (
actor_id smallint(5) NOT NULL PRIMARY KEY,
first_name varchar(45) NOT NULL,
last_name varchar(45) NOT NULL,
last_update timestamp NOT NULL DEFAULT (datetime('now','localtime')))
| actor_id | first_name | last_name | last_update |
|---|---|---|---|
| '3' | 'ED' | 'CHASE' | '2006-02-15 12:34:33' |
思路
这里用到了一个ignore关键字,功能就是如果不存在该记录就插入,如果存在就忽略
实现
insert IGNORE into actor
values(3,'ED','CHASE','2006-02-15 12:34:33');如果是sqlite需要将or去掉
边栏推荐
- [recommended collection] with these learning methods, I joined the world's top 500 - the "fantastic skills and extravagance" in the Internet age
- MVC three-tier architecture understanding
- Probability theory and mathematical statistics 3 discrete random variables and probability distributions (Part 2)
- Vant problem record
- JDBC操作数据库详解
- Common methods of JS digital thousand bit segmentation
- 字符串切片的用法
- UE4 快速找到打包失败的原因
- Subtotal of rospy odometry sinkhole
- Terminal definition and wiring of bsp3 power monitor (power monitor)
猜你喜欢
![[necessary for growth] Why do I recommend you to write a blog? May you be what you want to be in years to come.](/img/f5/e6739083f0dce8da1d09d078321633.png)
[necessary for growth] Why do I recommend you to write a blog? May you be what you want to be in years to come.

小程序企业发放红包功能

用ESP32+TM1638实验NTP网络校时闹钟的ARDUINO代码

链表相关(设计链表及环链表问题)

emmet语法速查 syntax基本语法部分

拷贝过来老的项目变成web项目

@Import, conditional and @importresource annotations

Probabilistic robot learning notes Chapter 2

JDBC总结

小程序调起微信支付
随机推荐
See how a junior student of double non-2 (0 Internship) can get an offer from Alibaba and Tencent
FPGA basic advanced
The way of code neatness -- hit the pain point directly
严重 [main] org.apache.catalina.util.LifecycleBase.handleSubClassException 初始化组件
Pnpm Brief
无线中继采集仪的常见问题
Probability theory and mathematical statistics 4 continuous random variables and probability distributions (Part 1)
Reflection 反射
UE4 框架介绍
ISP image signal processing
小程序H5获取手机号方案
DHCP的配置(以华为eNSP为例)
Es6详解
CCF 201509-3 template generation system
Loam transformtoend function integrating IMU details
[recommended collection] with these learning methods, I joined the world's top 500 - the "fantastic skills and extravagance" in the Internet age
Common methods of nodejs version upgrade or switching
Probabilistic robot learning notes Chapter 2
Qt 6.2的下载和安装
Redis使用场景