![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
sql题库
Dkui
夯实根基,蓄积勃发。
展开
-
查找入职员工时间排名倒数第三的员工所有信息
题目描述查找入职员工时间排名倒数第三的员工所有信息,为了减轻入门难度,目前所有的数据里员工入职的日期都不是同一天结题思路1.先把入职时间按照从小到大的排序 SELECT hire_date FROM employees2 ORDER BY hire_date DESC2.在通过limit拿到倒数第三的入职时间SELECT * FROM tableName LIMIT i, n # tableName:表名 # i:为查询结果的索引值(默认从0开始)原创 2020-06-19 17:28:44 · 1663 阅读 · 0 评论 -
employees表所有emp_no为奇数,且last_name不为Mary(注意大小写)的员工信息,并按照hire_date逆序排列
题目描述查找employees表所有emp_no为奇数,且last_name不为Mary(注意大小写)的员工信息,并按照hire_date逆序排列(题目不能使用mod函数)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)原创 2020-06-15 18:41:32 · 1075 阅读 · 0 评论 -
先排序然后只取出奇数的内容
对于employees表中,输出first_name排名(按first_name升序排序)为奇数的first_nameCREATE 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 N原创 2020-06-10 21:36:45 · 404 阅读 · 1 评论 -
查找最晚入职员工的所有信息
题目描述查找最晚入职员工的所有信息,为了减轻入门难度,目前所有的数据里员工入职的日期都不是同一天(sqlite里面的注释为–,mysql为comment)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原创 2020-06-09 19:45:02 · 183 阅读 · 0 评论 -
mysql注意点
优化limit分页我们日常做分页需求时,一般会用 limit 实现,但是当偏移量特别大的时候,查询效率就变得低下。反例:select id,name,age from employee limit 10000,10复制代码正例://方案一 :返回上次查询的最大记录(偏移量)select id,name from employee where id>10000 limit 10.//方案二:order by + 索引select id,name from employee orde原创 2020-06-05 18:33:33 · 3524 阅读 · 0 评论 -
尽量避免向客户端返回过多数据量
假设业务需求是,用户请求查看自己最近一年观看过的直播数据。反例://一次性查询所有数据回来select * from LivingInfo where watchId =useId and watchTime >= Date_sub(now(),Interval 1 Y)正例://分页查询select * from LivingInfo where watchId =useId and watchTime>= Date_sub(now(),Interval 1 Y) limit o原创 2020-06-05 18:25:44 · 236 阅读 · 0 评论 -
union all合并两个或多个 SELECT 语句的结果集。
SQL UNION 语法SELECT column_name(s) FROM table_name1UNIONSELECT column_name(s) FROM table_name2原创 2020-06-05 17:20:08 · 730 阅读 · 0 评论 -
查询出荣誉表的类型大于2的所有老师id
语句SELECT DISTINCT a.teacher_id,a.typeFROM sta_sci_paper aJOIN ( SELECT type FROM sta_sci_paper GROUP BY type HAVING COUNT(type) >4AND type='1' ) bON a.type = b.type;原创 2020-06-04 09:27:44 · 154 阅读 · 0 评论 -
只出现一次的最大数字
题目描述2、解题思路首先对每个数字出现次数统计select num,count(num) from my_numbers group by num having count(num)=1然后找出最大的数字select max(num) as num语法SELECT max(num) AS num FROM( select num, count (num)FROM my_numbersGROUP BY numHAVING cout (num) = 1) a...原创 2020-05-22 19:11:54 · 154 阅读 · 0 评论 -
平面上的最近距离
题目描述解题思路1# 两表联查2# 去重,where p1.x<>p2.x or p1.y<>p2.y3# 距离计算公式sqrt(min(power((p1.x-p2.x),2)+power((p1.y-p2.y),2))),我把取最小值放在根号里面sql语句select round(sqrt(min(power((p1.x-p2.x),2)+power((p1.y-p2.y),2))),2) as shortestfrom point_2d p1,point_2d原创 2020-05-20 18:15:56 · 304 阅读 · 0 评论 -
统计各专业学生人数
题目描述学生表专业表SELECT dept_name, ifnull(z_studen.cou, 0) AS '人数'FROM ( SELECT dept_id, COUNT(*) AS cou FROM z_studen GROUP BY dept_id ) z_studenRIGHT JOIN z_department ON z_department.id = z_studen.dept_id...原创 2020-05-18 19:21:02 · 10040 阅读 · 0 评论 -
查询员工的累计薪水
Employee 表保存了一年内的薪水信息。请你编写 SQL 语句,来查询一个员工三个月内的累计薪水,但是不包括最近一个月的薪水。结果请按 ‘Id’ 升序,然后按 ‘Month’ 降序显示。示例:输入:| Id | Month | Salary ||----|-------|--------|| 1 | 1 | 20 || 2 | 1 | 20 ...原创 2020-02-15 21:10:55 · 2068 阅读 · 0 评论 -
查询回答率最高的问题
题目描述解题思路求什么比率一般都是:(sum(case when `action` like 'answer' then 1 else 0 end) / sum(case when `action` like 'show' then 1 else 0 end)) as rate语句select question_id as survey_logfrom(select (sum(...原创 2020-02-14 19:23:37 · 220 阅读 · 0 评论 -
员工奖金
题目描述选出所有 bonus < 1000 的员工的 name 及其 bonus。Table:Employee+-------+--------+-----------+--------+| empId | name | supervisor| salary |+-------+--------+-----------+--------+| 1 | John | ...原创 2020-02-10 21:48:09 · 239 阅读 · 0 评论 -
当选者
——limit用法,选择第一个结果表: Candidate+-----+---------+| id | Name |+-----+---------+| 1 | A || 2 | B || 3 | C || 4 | D || 5 | E |+-----+---------+ 表: V...原创 2020-02-09 20:03:23 · 218 阅读 · 0 评论 -
给定数字的频率查询中位数
Numbers 表保存数字的值及其频率。+----------+-------------+| Number | Frequency |+----------+-------------|| 0 | 7 || 1 | 1 || 2 | 3 || 3 | 1 ...原创 2020-02-09 19:59:50 · 622 阅读 · 0 评论 -
至少有5名直接下属的经理
题目描述Employee 表包含所有员工和他们的经理。每个员工都有一个 Id,并且还有一列是经理的 Id。解题思路1# 创建子表,用ManagerId分组,统计总数count(Id) as cou2# 与Employee表,联查3、提交记录select Namefrom(select ManagerId,count(Id) as cou from Employeegroup...原创 2020-02-07 20:37:08 · 226 阅读 · 0 评论 -
员工薪水中位数
题目描述:Employee 表包含所有员工。Employee 表有三列:员工Id,公司名和薪水。+-----+------------+--------+|Id | Company | Salary |+-----+------------+--------+|1 | A | 2341 ||2 | A | 341 |...原创 2020-02-07 20:33:14 · 1283 阅读 · 0 评论 -
游戏玩法分析 III
——用户变量,系统变量,Join,IF,累加和或前缀和Table: Activity+--------------+---------+| Column Name | Type |+--------------+---------+| player_id | int || device_id | int || event_date | date...原创 2020-02-07 20:23:29 · 392 阅读 · 0 评论 -
游戏玩法分析 II
——right join 和子查询Table: Activity+--------------+---------+| Column Name | Type |+--------------+---------+| player_id | int || device_id | int || event_date | date || ga...原创 2020-02-05 19:53:03 · 260 阅读 · 0 评论 -
游戏玩法分析 I
——知识点:min (max) 和 group by活动表 Activity:+--------------+---------+| Column Name | Type |+--------------+---------+| player_id | int || device_id | int || event_date | date ...原创 2020-02-04 20:01:06 · 448 阅读 · 0 评论 -
交换工资 (使用 UPDATE 和 CASE...WHEN)
题目描述sql架构create table if not exists salary(id int, name varchar(100), sex char(1), salary int)Truncate table salaryinsert into salary (id, name, sex, salary) values ('1', 'A', 'm', '2500')insert ...原创 2019-12-13 20:42:28 · 336 阅读 · 0 评论 -
换座位
题目描述sql架构Create table If Not Exists seat(id int, student varchar(255))Truncate table seatinsert into seat (id, student) values ('1', 'Abbot')insert into seat (id, student) values ('2', 'Doris')i...原创 2019-12-12 22:01:24 · 367 阅读 · 0 评论 -
有趣的电影
题目描述SQL架构Create table If Not Exists cinema (id int, movie varchar(255), description varchar(255), rating float(2, 1))Truncate table cinemainsert into cinema (id, movie, description, rating) values...原创 2019-12-11 21:43:35 · 160 阅读 · 0 评论 -
通过学院或年级或专业或班级获取民族人数
通过学院或年级或专业或班级获取民族人数 SELECT nationality, count(*) as statisticsFROM st_studentWHERE college = #{college} <if test="fieldTwo !=null and fieldTwo!=''"> AND field_two = #{fi...原创 2019-12-11 15:18:30 · 219 阅读 · 0 评论 -
超过5名学生的课
题目描述sql架构Create table If Not Exists courses (student varchar(255), class varchar(255))Truncate table coursesinsert into courses (student, class) values ('A', 'Math')insert into courses (student, ...原创 2019-12-10 20:08:44 · 101 阅读 · 0 评论 -
大的国家
题目描述sql架构Create table If Not Exists World (name varchar(255), continent varchar(255), area int, population int, gdp int)Truncate table Worldinsert into World (name, continent, area, population, gd...原创 2019-12-09 19:55:57 · 112 阅读 · 0 评论 -
行程和用户
题目描述sql架构Create table If Not Exists Trips (Id int, Client_Id int, Driver_Id int, City_Id int, Status ENUM('completed', 'cancelled_by_driver', 'cancelled_by_client'), Request_at varchar(50))Create t...原创 2019-12-07 21:32:04 · 298 阅读 · 0 评论 -
删除重复的电子邮箱
题目描述编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。+----+------------------+| Id | Email |+----+------------------+| 1 | john@example.com || 2 | bob@example.com || 3 | jo...原创 2019-12-05 20:21:32 · 771 阅读 · 0 评论 -
部门工资前三高的所有员工
sql架构Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, DepartmentId int)Create table If Not Exists Department (Id int, Name varchar(255))Truncate table Employeeinsert int...原创 2019-12-04 10:42:16 · 142 阅读 · 0 评论 -
部门工资最高的员工
题目描述sql结构Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, DepartmentId int)Create table If Not Exists Department (Id int, Name varchar(255))Truncate table Employeeinser...原创 2019-12-03 20:19:23 · 96 阅读 · 0 评论 -
从不订购的客户
题目架构sql结构Create table If Not Exists Customers (Id int, Name varchar(255))Create table If Not Exists Orders (Id int, CustomerId int)Truncate table Customersinsert into Customers (Id, Name) values ...原创 2019-12-01 21:38:40 · 101 阅读 · 0 评论 -
查找重复的电子邮箱
题目描述sql结构Create table If Not Exists Person (Id int, Email varchar(255))Truncate table Personinsert into Person (Id, Email) values ('1', 'a@b.com')insert into Person (Id, Email) values ('2', 'c@d....原创 2019-11-30 10:48:55 · 195 阅读 · 0 评论 -
超过经理收入的员工
题目描述sql架构Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, ManagerId int)Truncate table Employeeinsert into Employee (Id, Name, Salary, ManagerId) values ('1', 'Joe', '70...原创 2019-11-30 10:43:49 · 83 阅读 · 0 评论 -
第N高的薪水
题目描述编写一个 SQL 查询,获取 Employee 表中第 n 高的薪水(Salary)。±—±-------+| Id | Salary |±—±-------+| 1 | 100 || 2 | 200 || 3 | 300 |±—±-------+例如上述 Employee 表,n = 2 时,应返回第二高的薪水 200。如果不存在第 n 高的薪水...原创 2019-11-29 20:08:19 · 118 阅读 · 0 评论 -
连续出现的数字
题目描述sql架构Create table If Not Exists Logs (Id int, Num int)Truncate table Logsinsert into Logs (Id, Num) values ('1', '1')insert into Logs (Id, Num) values ('2', '1')insert into Logs (Id, Num) va...原创 2019-11-28 20:01:20 · 319 阅读 · 0 评论 -
分数排名
题目描述SQL架构Create table If Not Exists Scores (Id int, Score DECIMAL(3,2))Truncate table Scoresinsert into Scores (Id, Score) values ('1', '3.5')insert into Scores (Id, Score) values ('2', '3.65')i...原创 2019-11-28 11:54:39 · 257 阅读 · 0 评论 -
第二高的薪水
题目描述SQL架构Create table If Not Exists Employee (Id int, Salary int)Truncate table Employeeinsert into Employee (Id, Salary) values ('1', '100')insert into Employee (Id, Salary) values ('2', '200')...原创 2019-11-26 15:22:52 · 86 阅读 · 0 评论 -
组合两个表(左连接)
题目描述SQL架构Create table Person (PersonId int, FirstName varchar(255), LastName varchar(255))Create table Address (AddressId int, PersonId int, City varchar(255), State varchar(255))Truncate table Pe...原创 2019-11-26 11:34:08 · 2117 阅读 · 0 评论 -
重新格式化部门表
题目SQL架构Create table If Not Exists Department (id int, revenue int, month varchar(5))Truncate table Departmentinsert into Department (id, revenue, month) values (‘1’, ‘8000’, ‘Jan’)insert into Dep...原创 2019-11-25 20:54:46 · 140 阅读 · 0 评论