开篇简介
题目来源于牛客网,边刷题边记录巩固,为了方便自己复习和分享。基础篇即入门教程,有些很简单的就不解释了,一步步来,无需着急,学徒心态。(有借鉴许多人的题解链接等,知识搬运工,侵删)
~
部分规则,照顾萌新
标点符号切记要英文的,不能中文的标点符号!!!
~
基础查询
SQL1 查询多列
描述
题目:现在运营同学想要用户的设备id对应的性别、年龄和学校的数据,请你取出相应数据
示例:user_profile
select device_id, gender, age, university
from user_profile
SQL2 查询所有列
select *
from user_profile
或者
select id, device_id, gender, age, university, province
from user_profile
SQL3 查询结果去重(DISTINCT)
select DISTINCT university
from user_profile
distinct这个关键字用来过滤掉多余的重复记录只保留一条,可以理解为过滤。
SQL4 查询结果限制返回行数(limit)
select device_id
from user_profile limit 2
利用 limit 来限制查询结果的返回行数
SQL5 将查询后的列重新命名(as)
select device_id as user_infos_example
from user_profile limit 2
只是在上一题基础上将列名用 as 修改了
排序
SQL36 查找后排序(order by)
select device_id, age
from user_profile
order by age ASC
order by 列名 按照’列名‘进行排序,asc/desc 升序/降序,根据要求使用(不写默认是asc 升序)
SQL37 查找后多列排序
以下三种都可以,因为order by 默认升序。
SELECT device_id,gpa,age from user_profile order by gpa,age;
SELECT device_id,gpa,age from user_profile order by gpa,age asc;
SELECT device_id,gpa,age from user_profile order by gpa asc,age asc;
条件查询
SQL6 查找学校是北大的学生信息(where)
select device_id, university
from user_profile
where university='北京大学'
where 后接条件
SQL7 查找年龄大于24岁的用户信息
select device_id, gender, age, university
from user_profile
where age > 24
SQL8 查找某个年龄段的用户信息(between … and …)
select device_id, gender, age
from user_profile
where age >= 20 and age <= 23
可以用 and 合并两个条件