`
DavyJones2010
  • 浏览: 154218 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

MySQL: A small practice for where/group by/having/order by clause

阅读更多
# Here is an example for five sql caluse
# Pay attention to the sequence of the four clause
# select *** from *** where *** group by *** having *** order by *** limit ***
select stu_name, avg(stu_score) as avg_score from stu where stu_name != (select stu_name from stu where stu_score = 90) group by stu_name having avg_score> 30 order by avg_score desc;

#################################################################################
# Examples are as below
# 1. Create a table
create table stu (stu_name varchar(20), stu_course varchar(20), stu_score int);

# 2. Insert data
mysql> insert into stu values('zhangsan', 'Math', 90);
Query OK, 1 row affected

mysql> insert into stu values('zhangsan', 'Literature', 50);
Query OK, 1 row affected

mysql> insert into stu values('zhangsan', 'Geograph', 40);
Query OK, 1 row affected

mysql> insert into stu values('lisi', 'Literature', 55);
Query OK, 1 row affected

mysql> insert into stu values('lisi', 'Politic', 4
5);
Query OK, 1 row affected

mysql> insert into stu values('wangwu', 'Politic', 30);
Query OK, 1 row affected

# 3. Purpose: Try to find the average score for the students whose invalid course count is bigger than 2.

## 3.1 Try to find the average score for all the students
select stu_name, avg(stu_score) from stu group by stu_name;

## 3.2 Try to find the average score for the students whose invalid course count is bigger than 2
select stu_name, avg(stu_score) as avg_score, sum(stu_score < 60) as invalid_count from stu group by stu_name having invalid_count >= 2;

 

分享到:
评论

相关推荐

    MySQL异常:[Err] 1055 – Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nona…

    在MySQL中,错误提示 "[Err] 1055 – Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column…” 指的是在执行包含`GROUP BY`子句的查询时,你试图对未在`GROUP BY`子句中...

    mysql遇到Expression.docx

    在该模式下,MySQL不允许在`SELECT`列表、`HAVING`条件或`ORDER BY`列表中引用未在`GROUP BY`子句中出现的非聚合列,除非这些列与`GROUP BY`中的列有函数上的依赖关系。例如,如果你正在根据员工的部门进行分组,...

    Firmware-master.zip

    * Official Website: http://px4.io (License: BSD 3-clause, [LICENSE.md](https://github.com/PX4/Firmware/blob/master/LICENSE.md)) * Supported airframes: * [Multicopters]...

    java红酒网站源码-gatk4:https://github.com/broadinstitute/gatk的猎鹰镜像

    3-Clause 许可下发布(请参阅 参考资料)。 GATK4 旨在将来自代码库和代码库的成熟工具整合到一个简化的框架下,并使选定的工具能够在本地集群上或使用 . 它还包含许多新开发的工具,这些工具在早期版本的工具包中不...

    MySQL5.7中的sql_mode默认值带来的坑及解决方法

    在正常项目开发过程中,如果MySQL版本从5.6升级到5.7版本。作为DBA在考虑数据库版本升级带来的影响时,一般会有几个注意点...ERROR 1055 (42000): Expression #3 of XXXXXX list is not in GROUP BY clause and contain

    解决MySQL 5.7.9版本sql_mode=only_full_group_by问题

    在MySQL 5.7.9版本中,引入了一个新的默认SQL模式`ONLY_FULL_GROUP_BY`,这个模式旨在增强SQL查询的严谨性,防止在GROUP BY语句中出现未聚合的列,以避免不确定的结果。然而,这也导致了某些在旧版本中能够正常运行...

    mysql不支持group by的解决方法小结

    MySQL中的`GROUP BY`语句用于对数据进行分组,并且通常与聚合函数(如`COUNT`, `SUM`, `AVG`, `MAX`, `MIN`)一起使用,以计算每个分组的汇总信息。然而,在MySQL 5.7.x及更高版本中,默认启用了`ONLY_FULL_GROUP_BY...

    强大的国外注入工具-darkMySQLi.py

    --orderby=COL Use a orderby clause in your dump --proxy=PROXY Use a HTTP proxy to connect to the target url --output=FILE.TXT Output results of tool to this file 实例: darkc0de:darkMySQLi ...

    DBFound2开发平台介绍文档

    #WHERE_CLAUSE# ORDER BY t.teacher_code ]]&gt; &lt;/sql&gt; ;=${@timefrom}"/&gt; ;=${@timeto}"/&gt; ${@teacher_code}"/&gt; ${@teacher_name}"/&gt; &lt;/query&gt; &lt;/model&gt; ``` 在这个例子中,我们定义了一个名为`load`的查询...

    反混淆神器CyberChef-v9.49加密解密工具下载

    CyberChef是一款新型的开源的分析和解密数据工具,用于在 Web 浏览器中执行各种“网络”操作。 这些操作包括创建hexdumps、简单编码(如XOR或Base64)、复杂加密(如AES,DES和Blowfish)、数据压缩和解压缩、计算...

    7-Zip 免费压缩解压软件

    7-Zip是一款开源的免费压缩...部分代码遵循BSD 3-clause许可证。此外,代码的某些部分也有非授权限制。读取7-Zip License信息。 你可以在任何计算机上使用7-Zip,包括商业机构的计算机。你不需要注册或支付7-Zip

    BPG 图片格式转换工具。

    Romeolight Bpgconv 2.5 便携版 Description : Batch conversion tool for BPG images Version : 2.5 Website : http://www.romeolight.com/products/bpgconv License : BSD-2-Clause Binaries : BPGconv.exe

    ICS delphixe10源码版

    all developers having contributed (When no name is given, the change is by F. Piette). I can't list all contributors here but I want to specially thanks two specially active contributors: - Arno ...

    局域网测速包.rar

    软件公司老是说局域网有问题于是就用这个检测 ...iPerf3采用新版BSD许可(BSD 3-clause License)。 iPerf3的官方网站:http://software.es.net/iperf/index.html iPerf3的Github网站:https://github.com/esnet/iperf

    Python库 | clause-1.1.2.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:clause-1.1.2.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    jquery文件

    * files not expressly granted by Microsoft, whether by implication, estoppel * or otherwise. Insofar as a script file is dual licensed under GPL, * Microsoft neither took the code under GPL nor ...

    《Pro Oracle SQL》Chapter7 Advanced Grouping -- 7.2HAVING Clause

    3. **复杂查询**:`HAVING`可以与其他SQL子句如`ORDER BY`、`UNION`、`INTERSECT`和`EXCEPT`一起使用,构建更复杂的查询逻辑。 4. **效率**:由于`HAVING`在聚合后进行过滤,它可能在大数据集上影响查询性能。因此...

    微软内部资料-SQL性能优化5

    Normally, only the columns in the WHERE clause are helpful in determining useful indexes, but for a covering index, all columns must be included. If all columns needed for the query are in the index,...

    mysql中You can’t specify target table for update in FROM clause错误解决方法

    在MySQL数据库中,"You can’t specify target table for update in FROM clause"是一个常见的错误提示,意味着在同一个SQL语句中,你不能直接在一个`UPDATE`或`DELETE`语句的`FROM`子句中引用你想要更新或删除的表...

    初中英语语法(固定搭配)公式汇总(完整版).docx

    - She comes from a small town in England. #### 十七、装满 - **公式**: be full of / be filled with - **例句**: - The basket is full of apples. - The room was filled with laughter. #### 十八、高兴...

Global site tag (gtag.js) - Google Analytics