SQL编程基础常见题型练习
- 1. 基础查询
- 1.1. 基础查询
- 1.2. 简单处理查询结果
- 2. 条件查询
- 2.1. 基础排序
- 2.2. 基础操作符
- 2.3. 高级操作符
- 3. 高级查询
- 3.1. 计算函数
- 3.2. 分组查询
- 4. 多表查询
- 4.1. 子查询
- 4.2. 链接查询
- 4.3. 组合查询
- 5. 必会的常用函数
- 5.1. 条件函数
- 5.2. 日期函数
1. 基础查询
1.1. 基础查询
- 查询所有列
select * from user_profile;
- 查询多列
1.2. 简单处理查询结果
- 查询结果去重
select university
from user_profile
group by university;
- 查询结果限制返回行数
select device_id
from user_profile
where id in (1,2);
- 将查询后的列重新命名
select device_id
as user_infos_example
from user_profile limit 2;
代码示例:
drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`gpa` float);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4);
INSERT INTO user_profile VALUES(2,3214,'male',23,'复旦大学',4.0);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8);
INSERT INTO user_profile VALUES(6,2131,'male',28,'北京师范大学',3.3);
2. 条件查询
2.1. 基础排序
- 查找后排序
select device_id,age
from user_profile
order by age asc
# 降序:select device_id,age from user_profile order by age desc;
- 查找后多列排序
select device_id,gpa,age
from user_profile
order by gpa,age
- 查找后降序排列
select device_id,gpa,age
from user_profile
order by gpa desc,age desc
2.2. 基础操作符
- 查找学校是北大的学生信息
SELECT device_id,university FROM user_profile WHERE university LIKE '%北京大学%'
#使用like运行时间会更短点,虽然多写了几个符号
SELECT device_id,university FROM user_profile WHERE university = "北京大学";
- 查找年龄大于24岁的用户信息
select
device_id,
gender,
age,
university
from user_profile
where age > 24;
- 查找某个年龄段的用户信息
select
device_id,
gender,
age
from user_profile
where age between 20 and 23;
- 查找除复旦大学的用户信息
select
device_id,
gender,
age,
university
from user_profile
where university <> "复旦大学"
# where university != '复旦大学'
# where not university = '复旦大学'
# where university not in('复旦大学')
# where university not like '复旦大学'
- 用 where 过滤空值练习
select
device_id,
gender,
age,
university
from
user_profile
where age is not null and age <> "";
2.3. 高级操作符
- 高级操作符练习(1)
select device_id,gender,age,university,gpa
from user_profile
where gpa > 3.5 and gender in ('male');
- 高级操作符练习(2)
select device_id,gender,age,university,gpa
from user_profile
where university = "北京大学" or gpa > 3.7;
- Where in 和 Not in
select device_id,gender,age,university,gpa
from user_profile
where university IN ("北京大学","复旦大学","山东大学");
- 操作符混合运用
select device_id,gender,age,university,gpa
from user_profile
where (gpa > 3.5 and university = "山东大学" ) or (gpa > 3.8 and university = "复旦大学");
#SELECT device_id, gender, age, university,gpa from user_profile where gpa > 3.8 and university = '复旦大学' UNION SELECT device_id, gender, age, university,gpa from user_profile where gpa > 3.5 and university = '山东大学'
- 查找学校名称中含北京的用户
代码:
drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`gpa` float);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8);
INSERT INTO user_profile VALUES(6,2131,'male',28,'北京师范大学',3.3);
select device_id,age,university
from user_profile
where university like "%北京%";
# WHERE university REGEXP "北京"
3. 高级查询
3.1. 计算函数
- 查找 GPA 最高值
#select max(gpa)
#from user_profile
#where university = '复旦大学'
select gpa
from user_profile
where university = '复旦大学'
order by gpa desc
limit 1
- 查找男生人数以及平均 GPA
select count(gender) as male_num ,round(avg(gpa),1) as avg_gpa
from user_profile
where gender = 'male'
3.2. 分组查询
- 分组计算练习题
代码:
drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`gpa` float,
`active_days_within_30` float,
`question_cnt` float,
`answer_cnt` float
);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
select
gender,university,
count(device_id) as user_num,
avg(active_days_within_30) as avg_active_day,
avg(question_cnt) as avg_question_cnt
from user_profile
group by gender,university
- 分组过滤练习题
select
university,
avg(question_cnt) as avg_question_cnt,
avg(answer_cnt) as avg_answer_cnt
from user_profile
group by university
having
avg_question_cnt < 5 or avg_answer_cnt < 20;
- 分组排序练习题
#当题目出现关键词“每”,“各”的时候,我们就可以判断结果集是需要进行分组的
select
university,
avg(question_cnt) as avg_question_cnt
from user_profile
group by university
order by avg_question_cnt
4. 多表查询
4.1. 子查询
- 浙江大学用户题目回答情况
代码:
drop table if exists `user_profile`;
drop table if exists `question_practice_detail`;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`gpa` float,
`active_days_within_30` int ,
`question_cnt` int ,
`answer_cnt` int
);
CREATE TABLE `question_practice_detail` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`question_id`int NOT NULL,
`result` varchar(32) NOT NULL
);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong');
INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong');
INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong');
INSERT INTO question_practice_detail VALUES(4,6543,111,'right');
INSERT INTO question_practice_detail VALUES(5,2315,115,'right');
INSERT INTO question_practice_detail VALUES(6,2315,116,'right');
INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong');
INSERT INTO question_practice_detail VALUES(8,5432,118,'wrong');
INSERT INTO question_practice_detail VALUES(9,5432,112,'wrong');
INSERT INTO question_practice_detail VALUES(10,2131,114,'right');
INSERT INTO question_practice_detail VALUES(11,5432,113,'wrong');
# 子查询
#select device_id,question_id,resul
#from question_practice_detail
#where device_id = (
# select device_id
# from user_profile
# where university = "浙江大学"
#);
# 连接查询
select u.device_id,q.question_id,q.result
from question_practice_detail q,user_profile u
where u.device_id = q.device_id and u.university = "浙江大学";
4.2. 链接查询
- 统计每个学校的答过题的用户的平均答题数
select
university,
count(qpd.question_id) / count(distinct qpd.device_id) as avg_answer_cnt
from
question_practice_detail as qpd
inner join
user_profile as up
on
qpd.device_id = up.device_id
group by
university
- 统计每个学校各难度的用户平均刷题数
SELECT
u.university,
qd.difficult_level,
count(q.question_id)/count(distinct(q.device_id)) AS avg_answer_cnt
FROM question_practice_detail AS q
inner JOIN user_profile AS u
ON u.device_id=q.device_id
inner JOIN question_detail AS qd
ON q.question_id=qd.question_id
group by
university,difficult_level
- 统计每个用户的平均刷题数
代码:
drop table if exists `user_profile`;
drop table if exists `question_practice_detail`;
drop table if exists `question_detail`;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`gpa` float,
`active_days_within_30` int ,
`question_cnt` int ,
`answer_cnt` int
);
CREATE TABLE `question_practice_detail` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`question_id`int NOT NULL,
`result` varchar(32) NOT NULL
);
CREATE TABLE `question_detail` (
`id` int NOT NULL,
`question_id`int NOT NULL,
`difficult_level` varchar(32) NOT NULL
);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong');
INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong');
INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong');
INSERT INTO question_practice_detail VALUES(4,6543,111,'right');
INSERT INTO question_practice_detail VALUES(5,2315,115,'right');
INSERT INTO question_practice_detail VALUES(6,2315,116,'right');
INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong');
INSERT INTO question_practice_detail VALUES(8,5432,117,'wrong');
INSERT INTO question_practice_detail VALUES(9,5432,112,'wrong');
INSERT INTO question_practice_detail VALUES(10,2131,113,'right');
INSERT INTO question_practice_detail VALUES(11,5432,113,'wrong');
INSERT INTO question_practice_detail VALUES(12,2315,115,'right');
INSERT INTO question_practice_detail VALUES(13,2315,116,'right');
INSERT INTO question_practice_detail VALUES(14,2315,117,'wrong');
INSERT INTO question_practice_detail VALUES(15,5432,117,'wrong');
INSERT INTO question_practice_detail VALUES(16,5432,112,'wrong');
INSERT INTO question_practice_detail VALUES(17,2131,113,'right');
INSERT INTO question_practice_detail VALUES(18,5432,113,'wrong');
INSERT INTO question_practice_detail VALUES(19,2315,117,'wrong');
INSERT INTO question_practice_detail VALUES(20,5432,117,'wrong');
INSERT INTO question_practice_detail VALUES(21,5432,112,'wrong');
INSERT INTO question_practice_detail VALUES(22,2131,113,'right');
INSERT INTO question_practice_detail VALUES(23,5432,113,'wrong');
INSERT INTO question_detail VALUES(1,111,'hard');
INSERT INTO question_detail VALUES(2,112,'medium');
INSERT INTO question_detail VALUES(3,113,'easy');
INSERT INTO question_detail VALUES(4,115,'easy');
INSERT INTO question_detail VALUES(5,116,'medium');
INSERT INTO question_detail VALUES(6,117,'easy');
select
university,
difficult_level,
count(qpd.question_id) / count(distinct qpd.device_id) as avg_answer_cnt
from
question_practice_detail as qpd
inner join
user_profile as up
on
up.device_id=qpd.device_id
and
up.university="山东大学"
inner join
question_detail as qd
on
qd.question_id=qpd.question_id
group by
difficult_level
4.3. 组合查询
- 查找山东大学或者性别为男生的信息
select device_id , gender , age , gpa from user_profile where university = '山东大学'
union all
select device_id , gender , age , gpa from user_profile where gender = 'male'
5. 必会的常用函数
5.1. 条件函数
- 计算 25 岁以上和以下的用户数量
SELECT IF(age>=25,"25岁及以上","25岁以下") AS age_cut,count(*) AS number
FROM user_profile
GROUP BY age_cut;
select
(case
when age>=25 then '25岁及以上'
else '25岁以下' end) as age_cut,
count(*) as number
from user_profile
group by age_cut
- 查找不同年龄段的用户明细
select device_id,gender,
case
when age<20 then '20岁以下'
when age<25 then '20-24岁'
when age>=25 then '25岁及以上'
else '其他'
end age_cut
from user_profile;
5.2. 日期函数
- 计算用户 8 月每天的练题数量
select
day(date) as day,
count(question_id) as question_cnt
from question_practice_detail
where month(date)=8 and year(date)=2021
group by date