在上一篇文章的SQL入门初次尝试中,我们了解了如何安装SQL和SQL一些基本概念原理及SQL基础语句,在本篇我们会更进一步了解到SQL在数据分析过程中最常用的操作之一:从数据库中查询数据,让我们边学边练吧。
一、基本的查询语句
方法:在左侧栏目里在查询出单击右键选新建查询,然后在右侧查询框中编辑SQL语句。其中select后接字段名,from后接表明,where后接限定条件。写好SQL语句后点击运行就可以得到结果。
1.查询student表中的姓名与性别两列数据。
![8bbceb721b57acdf8e461f8e1d81a6fb.png](https://i-blog.csdnimg.cn/blog_migrate/5dd043070de0181d55e6f85e059779e2.png)
2.查询score表中的所有数据,'*'代表查询该表格中的全部数据练习查询成绩表:
![b49eb970490bf4abced5302c3c455bbd.png](https://i-blog.csdnimg.cn/blog_migrate/8a56e9ef7588da7bb4d88904560f91ae.png)
3.1查询前的字段前加distinct可以将该组数据去重;
![4d39d47233af2b47bd9e46d1bdaef8f9.png](https://i-blog.csdnimg.cn/blog_migrate/dd1c51cdcb2a420351b1bde259418c8b.png)
3.2如果要查询多个字段去重,将去除多个字段同时重复的数据。
![bcc5c252a055e65aab324d53a31d42aa.png](https://i-blog.csdnimg.cn/blog_migrate/7e66217700f9234d6e6d4cd8a4437b4d.jpeg)
二、理解SQL运行顺序
![b96d0f2e3079c2eb79429f3163b227cc.png](https://i-blog.csdnimg.cn/blog_migrate/7341491d848cb8ac81c16e971361b435.jpeg)
SQL运行顺序如上图所示,先定位从哪一张表中查找顺序(from XX),然后考虑要选择怎样的数据(where XX),最后再输出数据(select XX)。
三、运算符
![e80ccb86ca09bf018d5888756e933aee.png](https://i-blog.csdnimg.cn/blog_migrate/1ab19a1891b84048779ccb9130425d5c.png)
四、复杂的查询条件
where语句中加入各种运算符可以限定数据范围,增加更复杂的限定条件可以提取更精确的数据。
1.查询student表中姓名为猴子的数据。
![257f0b21e771e24b90d18f7f5e795fd2.png](https://i-blog.csdnimg.cn/blog_migrate/019571d17088c0cd7e31e49b314280d5.jpeg)
2.更复杂的如下,查询student表中姓名为猴子或者马云的数据,与此同时必须满足性别为男。注意要添加括号,否则可能会因运算顺序不对导致提取信息错误。
![9be557073bff96f086e9b13879e24ce1.png](https://i-blog.csdnimg.cn/blog_migrate/bd637181274d9f55cedb6d3e14167f9c.jpeg)
3.模糊查询。模糊查询的重点在where语句里运用'like'和'%'。其中'侯%'表示抽取以猴开头的行,'%猴'表示抽取以猴结的行,'%猴%'表示抽取带有猴这个字的行。下图查询student表中名字列带有'猴'字的行。
![195ad32a2afbe307da124f3d1d0b59e5.png](https://i-blog.csdnimg.cn/blog_migrate/fede6328509d494510a1a057a65b9d7f.jpeg)
五、课后练习题(搜索SQL进行练习巩固知识)
1.sql平台select basic(简单查询)
![abe4e0bf9ef4bc4004890232cc9612e0.png](https://i-blog.csdnimg.cn/blog_migrate/4d62787dd1982643fc0aa8d90d112cc3.jpeg)
网址:https://sqlzoo.net/wiki/SELECT_basics/zh
![1e556102a1fe0f444acc9284b3130881.png](https://i-blog.csdnimg.cn/blog_migrate/49dd1422d2e63bb3e3d62ec000ff79d4.png)
![79656ec06633ca39035952c5e7cac6b6.png](https://i-blog.csdnimg.cn/blog_migrate/5f4c5061f3ce0afbbd4ae73cee519fc9.png)
![10afb09e511c60c80de43502749b1150.png](https://i-blog.csdnimg.cn/blog_migrate/a361c08f92ff04fdc1ff92fe91721dd9.png)
![d144a2bb17013a738c12b67ee30775f6.png](https://i-blog.csdnimg.cn/blog_migrate/95a6cf804da770183d95a7fe944c8ec1.png)
2.sqlzoo平台 select from world(运算符练习)
网址:https://sqlzoo.net/wiki/SQLZOO:SELECT_from_WORLD_Tutorial/zh
![969c40659a24e51c221f6bc333e3e0f5.png](https://i-blog.csdnimg.cn/blog_migrate/cb36a771347c94d20912c7f82c672347.png)
![a1b6c0704815bc1d11372e1832339d29.png](https://i-blog.csdnimg.cn/blog_migrate/4f5785cd3234a04c6bd839592588bb64.png)
![2a52e4cded8a08906a8491d98713fd0f.png](https://i-blog.csdnimg.cn/blog_migrate/3fd3cc04ebc72f8b4e78c2c4aa7c36c4.png)
![eee40621054b59e3452a216d29f23a63.png](https://i-blog.csdnimg.cn/blog_migrate/25353f4f11530d0141c7e9f47dcbbdf5.png)
![6ba7a9a29082d2c0d5d15f6711fab199.png](https://i-blog.csdnimg.cn/blog_migrate/91582b402c23c7c85485f4a8fd6053aa.png)
![2372ebdbb523338eaae2734aaa444dfd.png](https://i-blog.csdnimg.cn/blog_migrate/fa924f76abeac9e091ac4372c385cb7d.png)
![b9fd683a15f86bc461969659c062cf27.png](https://i-blog.csdnimg.cn/blog_migrate/c68044c4a472aebd6b6f6dc83ea53143.png)
![033b925ede04e759acacd616b40c2d26.png](https://i-blog.csdnimg.cn/blog_migrate/4c877cb9f912b2dafbe57a900b46a326.png)
3.(选做)sqlzoo平台 select names(字符串模糊查询)
网址:https://sqlzoo.net/wiki/SELECT_names/zh
![7495d246ac992ad6795b37f5ccd45d7d.png](https://i-blog.csdnimg.cn/blog_migrate/0ab406e1661150798146aecfba9c912e.png)
![109b617b09fac66a92b01d073395fd6f.png](https://i-blog.csdnimg.cn/blog_migrate/33f86b6d40bfc046ca2a7faa3b60b439.png)
![40b25af2dd46a73fe7e762171fe098a8.png](https://i-blog.csdnimg.cn/blog_migrate/435a13d7f340335400e1b83831329814.png)
![fe4cbe13aa4bdb7626a554462aaf1f3e.png](https://i-blog.csdnimg.cn/blog_migrate/c5ed1133c243f8bd3f673f4cee30a14c.png)
![9b3a5bf2ff3d9d0d775df417c267f80d.png](https://i-blog.csdnimg.cn/blog_migrate/fc0963d10a2b8c9175e649f360d5ead7.png)
![ea21c4c3e93dbbdfc0e5d81e2b956ef5.png](https://i-blog.csdnimg.cn/blog_migrate/5e78a1949ac3aadfa7c807b177e88b5c.png)
![9d96b311cd77e6a42030ac265d584e29.png](https://i-blog.csdnimg.cn/blog_migrate/5b745e3394eaabb9cf5a03cf5167e496.png)
![5f2ffcfca9bb4287df704a8bbbedf0f3.png](https://i-blog.csdnimg.cn/blog_migrate/26405975044b6386d5a3d51cda706965.png)
![24fdc5b3de3f4e7f1be6ee4bacd6dede.png](https://i-blog.csdnimg.cn/blog_migrate/e884ea04955cd58fa79037b088b97202.png)
![2d42121baec698686c424747bc812547.png](https://i-blog.csdnimg.cn/blog_migrate/aa9ef8ee1dcd5ff6b6f0c836866a6e2d.png)
![a7585aebc408d170a05f5dd58cf73145.png](https://i-blog.csdnimg.cn/blog_migrate/21454c66985ec3004f4f36490c416bab.png)