给定表 customer ,里面保存了所有客户信息和他们的推荐人。

id   | name | referee_id|
+------+------+-----------+
|    1 | Will |      NULL |
|    2 | Jane |      NULL |
|    3 | Alex |         2 |
|    4 | Bill |      NULL |
|    5 | Zack |         1 |
|    6 | Mark |         2 |

注:-- 假如expr1不为NULL,则 IFNULL(expr1, expr2) 的返回值为expr1; 否则其返回值为 expr2

  1. select name from customer where ifnull(referee_id,0) !=2

编写一个SQL查询,为下了 最多订单 的客户查找 customer_number 。测试用例生成后, 恰好有一个客户 比任何其他客户下了更多的订单。查询结果格式如下所示。

输入:
Orders 表:
+--------------+-----------------+
| order_number | customer_number |
+--------------+-----------------+
| 1            | 1               |
| 2            | 2               |
| 3            | 3               |
| 4            | 3               |
+--------------+-----------------+
输出:
+-----------------+
| customer_number |
+-----------------+
| 3               |
+-----------------+
解释:
customer_number 为 '3' 的顾客有两个订单,比顾客 '1' 或者 '2' 都要多,因为他们只有一个订单。
所以结果是该顾客的 customer_number ,也就是 3 。
注:-- 根据聚合函数排序

  1. SELECT
  2. customer_number
  3. FROM
  4. orders
  5. GROUP BY
  6. customer_number
  7. ORDER BY
  8. COUNT(customer_number) DESC
  9. LIMIT 1

编写SQL查询以查找每个部门中薪资最高的员工。按 任意顺序 返回结果表。查询结果格式如下例所示。

输入:
Employee 表:
+----+-------+--------+--------------+
| id | name  | salary | departmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Jim   | 90000  | 1            |
| 3  | Henry | 80000  | 2            |
| 4  | Sam   | 60000  | 2            |
| 5  | Max   | 90000  | 1            |
+----+-------+--------+--------------+
Department 表:
+----+-------+
| id | name  |
+----+-------+
| 1  | IT    |
| 2  | Sales |
+----+-------+
输出:
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Jim      | 90000  |
| Sales      | Henry    | 80000  |
| IT         | Max      | 90000  |
+------------+----------+--------+
解释:Max 和 Jim 在 IT 部门的工资都是最高的,Henry 在销售部的工资最高。-- in可以匹配多个值

  1. select
  2. b.name as Department ,
  3. a.name as Employee ,
  4. a.salary as Salary
  5. from
  6. Employee a
  7. left join
  8. Department b
  9. on a.departmentId = b.id
  10. where (a.departmentId , a.salary) in (select departmentId , max(salary) from Employee group by departmentId)

写一条SQL查询语句获取合作过至少三次的演员和导演的 id 对 (actor_id, director_id)

ActorDirector 表:
+-------------+-------------+-------------+
| actor_id    | director_id | timestamp   |
+-------------+-------------+-------------+
| 1           | 1           | 0           |
| 1           | 1           | 1           |
| 1           | 1           | 2           |
| 1           | 2           | 3           |
| 1           | 2           | 4           |
| 2           | 1           | 5           |
| 2           | 1           | 6           |
+-------------+-------------+-------------+

Result 表:
+-------------+-------------+
| actor_id    | director_id |
+-------------+-------------+
| 1           | 1           |
+-------------+-------------+
唯一的 id 对是 (1, 1),他们恰好合作了 3 次。

注:-- group by 一列就是把这一列相同的作为一组,多列就是多列相同的作为一组

  1. select actor_id,director_id
  2. from ActorDirector
  3. group by actor_id, director_id
  4. having count(*)>=3

返回的结果表单,以 travelled_distance 降序排列 ,如果有两个或者更多的用户旅行了相同的距离, 那么再以 name 升序排列

Users 表:
+------+-----------+
| id   | name      |
+------+-----------+
| 1    | Alice     |
| 2    | Bob       |
| 3    | Alex      |
| 4    | Donald    |
| 7    | Lee       |
| 13   | Jonathan  |
| 19   | Elvis     |
+------+-----------+

Rides 表:
+------+----------+----------+
| id   | user_id  | distance |
+------+----------+----------+
| 1    | 1        | 120      |
| 2    | 2        | 317      |
| 3    | 3        | 222      |
| 4    | 7        | 100      |
| 5    | 13       | 312      |
| 6    | 19       | 50       |
| 7    | 7        | 120      |
| 8    | 19       | 400      |
| 9    | 7        | 230      |
+------+----------+----------+

Result 表:
+----------+--------------------+
| name     | travelled_distance |
+----------+--------------------+
| Elvis    | 450                |
| Lee      | 450                |
| Bob      | 317                |
| Jonathan | 312                |
| Alex     | 222                |
| Alice    | 120                |
| Donald   | 0                  |
+----------+--------------------+
Elvis 和 Lee 旅行了 450 英里,Elvis 是排名靠前的旅行者,因为他的名字在字母表上的排序比 Lee 更小。
Bob, Jonathan, Alex 和 Alice 只有一次行程,我们只按此次行程的全部距离对他们排序。
Donald 没有任何行程, 他的旅行距离为 0。

注:order by travelled_distance desc,name ,一列travelled_distance倒序,一列正序

  1. select
  2. a.name,
  3. ifnull(sum(b.distance),0) as travelled_distance
  4. from Rides b
  5. right join Users a
  6. on b.user_id = a.id
  7. group by b.user_id
  8. order by travelled_distance desc,name

请写SQL查询出截至 2019-07-27(包含2019-07-27),近 30 天的每日活跃用户数(当天只要有一条活动记录,即为活跃用户)

输入:
Activity table:
+---------+------------+---------------+---------------+
| user_id | session_id | activity_date | activity_type |
+---------+------------+---------------+---------------+
| 1       | 1          | 2019-07-20    | open_session  |
| 1       | 1          | 2019-07-20    | scroll_down   |
| 1       | 1          | 2019-07-20    | end_session   |
| 2       | 4          | 2019-07-20    | open_session  |
| 2       | 4          | 2019-07-21    | send_message  |
| 2       | 4          | 2019-07-21    | end_session   |
| 3       | 2          | 2019-07-21    | open_session  |
| 3       | 2          | 2019-07-21    | send_message  |
| 3       | 2          | 2019-07-21    | end_session   |
| 4       | 3          | 2019-06-25    | open_session  |
| 4       | 3          | 2019-06-25    | end_session   |
+---------+------------+---------------+---------------+
输出:
+------------+--------------+
| day        | active_users |
+------------+--------------+
| 2019-07-20 | 2            |
| 2019-07-21 | 2            |
+------------+--------------+
解释:注意非活跃用户的记录不需要展示。

  1. select activity_date as day, count(distinct(user_id)) as active_users
  2. from Activity
  3. where activity_date between '2019-06-28' and '2019-07-27'
  4. group by activity_date

注:

  1. #这里如果改用datediff('2019-07-27', activity_date) < 30 要注意判断这样会算出2019-07-07往后30的数据
  2. #count函数需要注意
  3. #count(*):统计记录总数,包含重复的记录,以及为NULL或空的记录。
  4. #count(1):根据第一列统计记录总数,包含重复的记录,包含为NULL或空的值。也可以使用count(2)
  5. #count(列名):根据指定的列统计记录总数,包含重复的记录,不包括NULL或空的值。
  6. #count(distinct 列名):根据指定的列统计记录总数,不包含重复的记录,不包括NULL或空的值。

写出一个SQL 查询语句,计算每个雇员的奖金。如果一个雇员的id是奇数并且他的名字不是以'M'开头,那么他的奖金是他工资的100%,否则奖金为0。

Employees 表:
+-------------+---------+--------+
| employee_id | name    | salary |
+-------------+---------+--------+
| 2           | Meir    | 3000   |
| 3           | Michael | 3800   |
| 7           | Addilyn | 7400   |
| 8           | Juan    | 6100   |
| 9           | Kannon  | 7700   |
+-------------+---------+--------+
输出:
+-------------+-------+
| employee_id | bonus |
+-------------+-------+
| 2           | 0     |
| 3           | 0     |
| 7           | 7400  |
| 8           | 0     |
| 9           | 7700  |
+-------------+-------+

  1. select employee_id ,(CASE WHEN (employee_id % 2 )= 1 and name not like 'M%' THEN salary else 0 end) AS bonus
  2. from Employees
  3. order by employee_id

注:case when语句,判断奇数

MySql实例关于ifnull,count,case when,group by(转力扣简单)的更多相关文章

  1. mysql case when group by实例

    mysql 中类似php switch case 的语句. select xx字段, case 字段 when 条件1 then 值1 when 条件2 then 值2 else 其他值 END 别名 ...

  2. MYSQL 行转列 以及基本的聚合函数count,与group by 以及distinct组合使用

    在统计查询中,经常会用到count函数,这里是基础的 MYSQL 行转列 以及基本的聚合函数count,与group by 以及distinct组合使用 -- 创建表 CREATE TABLE `tb ...

  3. 学习笔记 MYSQL报错注入(count()、rand()、group by)

    首先看下常见的攻击载荷,如下: select count(*),(floor(rand(0)*2))x from table group by x; 然后对于攻击载荷进行解释, floor(rand( ...

  4. mysl 常用函数 union all if ifnull exists case when

    1.union all UNION 操作符用于合并两个或多个 SELECT 语句的结果集.请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 S ...

  5. mysql 中sum (if())与case

    先来一个简单的sum select sum(qty) as total_qty from inventory_product group by product_id 这样就会统计出所有product的 ...

  6. MySql 里的IFNULL、NULLIF和ISNULL用法

    MySql 里的IFNULL.NULLIF和ISNULL用法 mysql中isnull,ifnull,nullif的用法如下: isnull(expr) 的用法: 如expr 为null,那么isnu ...

  7. MySql 里的IFNULL、NULLIF和ISNULL用法区别

    mysql中isnull,ifnull,nullif的用法如下: isnull(expr) 的用法:如expr 为null,那么isnull() 的返回值为 1,否则返回值为 0. mysql> ...

  8. 比较典型的带case的group by语句

    2005-05-09 胜 2005-05-09 胜 2005-05-09 负 2005-05-09 负 2005-05-10 胜 2005-05-10 负 2005-05-10 负 如果要生成下列结果 ...

  9. MySql学习(二) —— where / having / group by / order by / limit 简单查询

    注:该MySql系列博客仅为个人学习笔记. 这篇博客主要记录sql的五种子句查询语法! 一个重要的概念:将字段当做变量看,无论是条件,还是函数,或者查出来的字段. select五种子句 where 条 ...

随机推荐

  1. Android回到页面并刷新数据

    通过对Android Activity的生命周期的了解,需要在后退页面重写onResume()的方法. 建立自己更新数据的函数,并在onCreate()方法中调用. @Override protect ...

  2. SourceMonitor的安装

    SourceMonitor 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 中文名 SourceMonitor 软件大小 1743KB 软件语言 英文 软件类别  国外软件 ...

  3. Taro时间选择器+地址选择器

    时间地址选择器 https://www.manzj.net/topic/5c3c2123cd01b8af5bce4373

  4. 在Wireshark中使用过滤器——显示过滤器

    在Wireshark运行过程中选择搜索(Ctrl-F),第一个默认的搜索选项就是显示过滤器. 显示过滤器用于捕获文件,用来告诉Wireshark只显示那些符合过滤条件的数据包. 显示过滤器比捕获过滤器 ...

  5. linux中查看端口号使用情况

    百度一圈,以下是整理来的操作命令. 1.netstat -anp |grep (端口号) 这个方法可以直观看到对应端口号是否被使用. 2.netstat -nultp 这个方法可以看到该机上所有以用的 ...

  6. Spring Boot-@PropertySource注解

    @PropertySource:加载自己手动编写的资源文件 有关@ConfigurationProperties注解的作用请访问Spring Boot-@Value获取值和@Configuration ...

  7. Shiro-登陆流程认证-图解

  8. 如何让HTTPS站点评级达到A+? 还得看这篇HTTPS安全优化配置最佳实践指南

    0x00 前言简述 SSL/TLS 简单说明 描述: 当下越来越多的网站管理员为企业站点或自己的站点进行了SSL/TLS配置, SSL/TLS 是一种简单易懂的技术,它很容易部署及运行,但要对其进行安 ...

  9. 1903021116—吉琛—Java第七周作业—客户类测试

    项目 内容 课程班级博客链接 19信计班 这个作业要求链接 第七周作业链接 博客名称 学号-姓名-Java第七周作业-客户类测试 要求 每道题要有题目,代码(使用插入代码,不会插入代码的自己查资料解决 ...

  10. js 轮播图 (原生)

    注 : 此处内容较多, 只显示代码, 具体讲解看注释.  具体参考 "黑马 pink老师"   https://www.bilibili.com/video/BV1Sy4y1C7h ...