SQL语句练习

女生的网名这么多〃 提交于 2019-11-28 05:21:59

给两张表,连表查询各科成绩前三名

SELECT a.*,u.* from score a INNER JOIN user u on a.id=u.id

where (SELECT count(*) from score b where a.course=b.course and b.score>=a.score)<3

ORDER BY a.course,a.score desc

查询学生表每门课都大于80 分的学生姓名

select name from student group by name HAVING min(score) >80  and count(kemu) >=3

求排名第5到10的部门的平均工资(limit(a,b) a为偏移量,b为size

#limit后面如果只写一个整数n,那就是查询的前n条记录;如果后面带2个整数n 和 m,那么第一个数n就是查询出来队列的起点(从0开始),第二个是m是统计的总数目

select avg(salary) from department de group by dep order by avg(salary) desc limit(5,6)

求职员表name和department。分别表示员工姓名和所属部门,请写一条SQL语句查出每个部门的人数

select department.name, count(DISTINCT id) from department group by department.name

查询表A中存在ID重复三次以上的记录

Select * From A Where id in(select ID from A group by id having count(id)>3)

sql操作文档:

启动sql:sudo service mysql start

登录:mysql -u root 密码

创建数据库: create database helayel;

查看数据库:show databases;

切换数据库:use helayel

查看表:show tables;

创建表:create table 表名(id int(10), name varchar(20), age(10));

create table 表名(id int primary key, name varchar(50) not null);

表插入数据:

insert into 表名(列名a,列表b,列名c) values(值1,值2,值3)

eg: 2张表、学生表(id,name,age)和分数表(id、kemu、score)

约束:primary key(主键) —不能有重复记录,不能为空

给已有表加主键约束

alter table 表名

add CONSTRAINT pk_列名 PRIMARY KEY (列名);

修改已有表字段id为自增

alter table 表名

change column id id int not null auto_increment;

DEFAULT(默认值)—用于定义一些可有可无的字段

UNIQUE(唯一)----指定一列的值必须唯一 UNIQUE(phone)

FOREIGN KEY (外键) —确保数据完整性,表现表之间的关系;

NOT NULL(非空)

从github上下载sql文件到本地:git clone github的url

加载运行下载内容:source 地址.sql

教程:https://www.w3school.com.cn/sql/index.asp
实战:实验楼

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!