LintCode-SQL-入门篇(一)

注:本案例题目来源LintCode,题解为个人刷题答案。

目录

1.查询,并设定某int字段为筛选条件(where)

2.删除 2020 年前的所有课

3.删除表中所有行

4. 向课程表中插入 SQL 课程信息

5.根据邮箱查询教师信息

6.查询有电子邮箱的中日籍教师

7. 查询教师 id 不为 1 和 3 的课程

8.查询教师的年龄并按升序排序


1.查询,并设定某int字段为筛选条件(where)

 题解:

-- Write your SQL Query here -- 
-- example: SELECT * FROM XX_TABLE WHERE XXX --
select id,name,student_count,created_at,teacher_id from courses where student_count>1000

2.删除 2020 年前的所有课

 题解:

-- Write your SQL Query here --
delete from courses where created_at<'2020-01-01';

3.删除表中所有行

 题解:

注意是删除行数据且不删表

-- Write your SQL Query here --
truncate table courses;

4. 向课程表中插入 SQL 课程信息

插入语句insert into

题解:

-- Write your SQL Query here --
insert into courses (id,name,student_count,created_at,teacher_id)
values('14','SQL','200','2021-02-25','1')

5.根据邮箱查询教师信息

 题解:

关键词“like”

-- Write your SQL Query here --
-- example: SELECT * FROM XX_TABLE WHERE XXX --
select name,email from teachers where email like '%@qq.com';

6.查询有电子邮箱的中日籍教师

 

题解:

注意整个条件打括号啊~

-- Write your SQL Query here --
-- example: SELECT * FROM XX_TABLE WHERE XXX --
select * from teachers where (country ='CN' or country = 'JP') and email is NOT NULL

7. 查询教师 id 不为 1 和 3 的课程

 题解:

不包括筛选可使用NOT IN,或者!=

-- Write your SQL Query here --
-- example: SELECT * FROM XX_TABLE WHERE XXX --
select name from courses where (teacher_id != 1) and (teacher_id != 3);

8.查询教师的年龄并按升序排序

 题解:

唯一值关键词‘distintct’;

排序关键词‘order by’,默认升序。如需降序可在句末标注‘desc’

-- Write your SQL Query here --
-- example: SELECT * FROM XX_TABLE WHERE XXX --
select distinct age from teachers order by age;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

miracle2me

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值