【如何成为SQL高手】第五关:select语句基本用法

‍ 博主介绍:
IT邦德,江湖人称jeames007,10年DBA工作经验
中国DBA联盟(ACDU)成员,目前从事DBA及程序编程

【如何成为SQL高手】第五关:select语句基本用法_第1张图片

SQL对于现在的互联网公司产研岗位几乎是一个必备技能,但仅会SQL的话,应该是什么都做不了。
1.如果你是数据分析师,你需要熟练地把自己脑子里的数据和指标需求翻译成SQL逻辑去查询数据,进而完成自己的数据分析报告等,你的产出是分析报告,而不是SQL代码;
2.如果你是数仓工程师(偏应用层),你需要根据业务逻辑去设计模型,编写调度任务去产出数据,以供业务人员使用,你的产出是数据模型和表;
3.如果你是算法工程师,你可能需要用SQL来实现用户标签、特征工程等工作,但是这些是为你的模型训练评估做基础准备工作,你的产出是可以提升某些指标的算法模型。

所以,SQL每个人都要用,但是用来衡量产出的并不是SQL本身,你需要用这个工具,去创造其它的价值。
IT邦德老师带你成为SQL高手,那我们开始吧~

文章目录

    • 1.表结构及信息
      • 1.1 tb_class
      • 1.2 tb_course
      • 1.3 tb_course
      • 1.3 tb_course
    • 2.实战案例

本文案例导入以下sql文本即可,通过百度网盘下载
mysql> source h:\db_school.sql
网盘链接:https://pan.baidu.com/s/1rvJhB6it8rvOMeqMjRAGlg?pwd=0hx1

在这里插入图片描述

1.表结构及信息

1.1 tb_class

【如何成为SQL高手】第五关:select语句基本用法_第2张图片

1.2 tb_course

【如何成为SQL高手】第五关:select语句基本用法_第3张图片

1.3 tb_course

【如何成为SQL高手】第五关:select语句基本用法_第4张图片

1.3 tb_course

【如何成为SQL高手】第五关:select语句基本用法_第5张图片

2.实战案例

1.查询所有班级的班级编号、所属学院和班级名称
select classNo,department,className from tb_class;

【如何成为SQL高手】第五关:select语句基本用法_第6张图片

2.从tb_class表中查询所有的学院名称。
select distinct department from tb_class;

【如何成为SQL高手】第五关:select语句基本用法_第7张图片

3.查询全体学生的详细信息
select * from tb_student;

4.查询全体学生的姓名、性别和年龄
select studentName,sex,year(now())-year(birthday) age from tb_student;

【如何成为SQL高手】第五关:select语句基本用法_第8张图片

5.查询全体学生的姓名、性别和年龄,要求用汉语显示目标列表的名称。
select studentName as ‘姓名’,
sex as ‘性别’,
year(now())-year(birthday) as ‘年龄’
from tb_student;

【如何成为SQL高手】第五关:select语句基本用法_第9张图片

6.查询课时大于等于48学时的课程名称和学分
select courseName,credit from tb_course
where courseHour >= 48;

【如何成为SQL高手】第五关:select语句基本用法_第10张图片

7.查询少数民族学生的姓名、性别、籍贯和民族
select studentName,sex,nation, native
from tb_student where nation <> ‘汉’;

【如何成为SQL高手】第五关:select语句基本用法_第11张图片

8.查询1997年出生的学生姓名、性别和具体日期。
select studentName,sex,birthday
from tb_student
where year(birthday)=1997;

【如何成为SQL高手】第五关:select语句基本用法_第12张图片

9.查询不是1997年出生的学生姓名、性别和具体日期。
select studentName,sex,birthday
from tb_student where year(birthday)!=1997;

【如何成为SQL高手】第五关:select语句基本用法_第13张图片

10.查询籍贯是北京、天津和上海的学生信息。
select * from tb_student
where native in (‘北京’,‘天津’,‘上海’);

【如何成为SQL高手】第五关:select语句基本用法_第14张图片

11.查询籍贯不是北京、天津和上海的学生信息。
select * from tb_student
where native not in (‘北京’,‘天津’,‘上海’);

12.查询2013年入学的学生全部信息。
select * from tb_student
where left(studentNo,4)=‘2013’;

【如何成为SQL高手】第五关:select语句基本用法_第15张图片

13.查询所有姓王的学生的学号、姓名和班级编号。
select studentNo,studentName,classNo
from tb_student where studentName like ‘王%’;

【如何成为SQL高手】第五关:select语句基本用法_第16张图片

14.查询所有不姓王的学生的学号、姓名和班级编号。
select studentNo,studentName,classNo from tb_student
where studentName not like ‘王%’;

15.查询姓名中包含‘林’字的学生的学号、姓名和班级编号。
select studentNo,studentName,classNo
from tb_student where studentName like ‘%林%’;

16.查询姓王的且姓名为三个字的学生的学号、姓名和班级编号。
select studentNo,studentName,classNo from tb_student
where studentName like ‘王%’ and CHAR_LENGTH(studentName)=3;

在这里插入图片描述

17.查询课程名称中包含’-‘符号的课程信息;
select * from tb_course where courseName like ‘%-%’;

18.查询课程名称中带有中文‘系统’的课程信息。
select * from tb_course where courseName like ‘%系统%’;

19.查询课程名称中含有‘管理’、‘信息’或者‘系统’的课程信息。
select * from tb_course
where courseName like ‘%管理%’ or courseName like ‘%信息%’ or courseName like ‘%系统%’;

【如何成为SQL高手】第五关:select语句基本用法_第17张图片

20.查询缺少先修课的课程信息。
select * from tb_course where priorCourse is null;

21.查询所有有先修课的课程信息
select * from tb_course where priorCourse is not null;

【如何成为SQL高手】第五关:select语句基本用法_第18张图片

22.查询学分大于等于3且学时数大于32的的课程名称、学分和学时数。
select courseName,credit,courseHour
from tb_course
where credit=3 and courseHour>32;

【如何成为SQL高手】第五关:select语句基本用法_第19张图片

23.查询籍贯是北京或者上海的学生的姓名、籍贯和民族。
select studentName,native, nation
from tb_student
where native in (‘北京’,‘上海’);

24.查询籍贯是北京或湖南的少数民族男生的姓名、籍贯和民族。
select studentName,native, nation
from tb_student where native in (‘北京’,‘湖南’)
and nation<>‘汉’;

25.查询学生的姓名、籍贯和民族,并将查询结果按姓名升序排序。
select * from tb_student order by studentName;

【如何成为SQL高手】第五关:select语句基本用法_第20张图片

26.查询学生选课成绩大于85分的学号、课程号和成绩信息,
并将查询结果先按学号升序排列,再按成绩降序排列。
select * from tb_score
where score>85
order by studentNo,score desc;

【如何成为SQL高手】第五关:select语句基本用法_第21张图片

27.查询成绩排名第3至第5的学生学号、课程号和成绩
select * from tb_score
order by score desc limit 2,3;

【如何成为SQL高手】第五关:select语句基本用法_第22张图片
大家点赞、收藏、关注、评论啦 微信公众号

你可能感兴趣的:(Mysql,SQL,sql,big,data,数据库)