数据库sql语句及字段名不区分大小写,值必须一一对应
CREATE TABLE student
(Sno varchar(20) NOT NULL,
Sname varchar(20) NOT NULL,
sex varchar(20) NOT NULL,
age INT NOT NULL,
dept varchar(20) NOT NULL, PRIMARY KEY (Sno)
);
CREATE TABLE course
(Cno varchar(20) NOT NULL,
Cname varchar(20) NOT NULL,
hours VARCHAR(20) NOT NULL,
PRIMARY KEY (Cno)
);
CREATE TABLE SC
(Sno varchar(20) NOT NULL,
Cno varchar(20) NOT NULL,
grade INT ,
PRIMARY KEY (Sno,Cno)
);
INSERT INTO student VALUES ('9512101','李勇','男',19,'计算系');
INSERT INTO student VALUES ('9512102','刘晨','男',20,'计算系');
INSERT INTO student VALUES ('9512103','王敏','女',20,'计算系');
INSERT INTO student VALUES ('9521101','张立','男',22,'信息系');
INSERT INTO student VALUES ('9521102','吴宾','女',21,'信息系');
INSERT INTO student VALUES ('9521103','张海','男',20,'信息系');
INSERT INTO student VALUES ('9531101','钱小力','女',18,'数学系');
INSERT INTO student VALUES ('9531102','王大力','男',19,'数学系');
INSERT INTO course VALUES ('C01','计算机文化学','70');
INSERT INTO course VALUES ('C02','VB','90');
INSERT INTO course VALUES ('C03','计算机网络','80');
INSERT INTO course VALUES ('C04','数据库基础','108');
INSERT INTO course VALUES ('C05','高等数学','180');
INSERT INTO course VALUES ('C06','数据结构','72');
INSERT INTO SC VALUES ('9512101','C01',90);
INSERT INTO SC VALUES ('9512101','C02',86);
INSERT INTO SC VALUES ('9512101','C06',NULL);
INSERT INTO SC VALUES ('9512102','C02',78);
INSERT INTO SC VALUES ('9512102','C04',66);
INSERT INTO SC VALUES ('9521102','C01',82);
INSERT INTO SC VALUES ('9521102','C02',75);
INSERT INTO SC VALUES ('9521102','C04',92);
INSERT INTO SC VALUES ('9521102','C05',50);
INSERT INTO SC VALUES ('9521103','C02',68);
INSERT INTO SC VALUES ('9521103','C06',NULL);
INSERT INTO SC VALUES ('9531101','C01',80);
INSERT INTO SC VALUES ('9531101','C05',95);
INSERT INTO SC VALUES ('9531102','C05',85);
commit;
select * from course
select * from student
SELECT * from sc
select * from sc where grade >=70 and grade <=80;
select max(grade) from sc where cno='C01' group by cno;
select sno, sum(grade) total
from sc
group by sno order by total desc limit 5
select sc.sno, s.sname, sum(sc.grade) total
from student s, sc
where s.sno = sc.sno
group by sc.sno
order by total desc
limit 5
select cno from sc group by cno
select max(grade),min(grade),avg(grade)
from sc
where cno='C02'
group by cno;
select dept, count(*) as '人数'
from student
group by dept;
select count(Sno) '人数',max(grade)
from sc
group by cno;
SELECT COUNT(Cno) score
from sc
GROUP BY Sno
ORDER BY score desc
LIMIT 3
select count(sno),avg(grade)
from sc
group by cno
ORDER BY avg(grade) DESC;
SELECT AVG(grade) avg, COUNT(cno) cnt
from sc
group by sno
having cnt > 2
select sno , sum(grade) cnt
from sc
GROUP BY sno
having cnt >= 200
SELECT s.sname, s.dept
from student s, sc
where sc.cno='C02'
and s.sno = sc.sno
select s.sname, sc.cno, sc.grade
from student s, sc
where sc.grade > 80
and s.sno = sc.sno
ORDER BY sc.grade desc
SELECT s.sname, s.sex, sc.grade
from student s, sc, course c
WHERE s.dept = '计算系' and s.sex = '男'
and c.cname = '数据库基础'
and sc.Sno = s.sno
and sc.Cno = c.cno
select sname, age
from student
where age in -- 子查询 in
(select age from student group by age having count(age) > 1)
select cno, cname
from course
where cno not in (select cno from sc group by cno) -- not in
create table new_sc as -- 将查询结果放在新表中的方法。as可省略
select s.sname, c.cname, sc.grade
from student s, course c, sc
where s.sno = sc.sno
and c.cno = sc.cno
and sc.grade is not null -- is not null 是不空的
select s.sname, s.sex, c.cname, sc.grade
from student s, course c, sc
where s.dept = '信息系'
and s.sno = sc.sno
and c.cno = sc.cno
UNION -- 联合查询
select s.sname, s.sex, c.cname, sc.grade
from student s, course c, sc
where s.dept = '计算系'
and s.sno = sc.sno
and c.cno = sc.cno
select s.sname, s.sex, c.cname, sc.grade
from student s, course c, sc
where s.dept in ('信息系', '计算系') -- 并列
and s.sno = sc.sno
and c.cno = sc.cno