create table Student
(Sno char(5) not null,
Sname varchar(20) unique,
Ssex char(1));
drop table student;
delete from student;
alter table student add time Datetime;
alter table drop time;
select * from instructor natural join teaches.
select name, course_id from instructor, teaches where instructor.ID = teaches.ID;
select name from instructor where name like '%char%' escape 'c';
select distinct name from instructor order by name
select distinct name from instructor order by name desc
select name from instructor where salary between 900000 and 10000000
select name from instructor where salary not between 900000 and 10000000
(select course_id from section where semester = 'Fall' and year = 2009)
union
(select course_id from section where semester = 'Spring' and year = 2010)
(select course_id from section where semester = 'Fall' and year = 2009)
intersect
(select course_id from section where semester = 'Spring' and year = 2010)
(select course_id from section where semester = 'Fall' and year = 2009)
except
(select course_id from section where semester = 'Spring' and year = 2010)
select name from instructor where salary is null
avg() min() max() sum() count()
select dept_name, avg (salary) from instructor group by dept_name;
select dept_name, avg (salary)
from instructor
where ID = 'Biology'
group by dept_name
having avg (salary) > 42000;
select distinct course_id from section where semester = 'Fall' and year= 2009
and course_id in
(select course_id from section where semester = 'Spring' and year= 2010);
(select course_id from section where semester = 'Fall' and year = 2009)
intersect
(select course_id from section where semester = 'Spring' and year = 2010)
select distinct course_i from section
where semester = ’Fall’ and year= 2009 and course_id not in
(select course_id from section where semester = ’Spring’ and year= 2010);
select id,name,dept from student
where dept in
(select dept from student where name = '柳橙')
EXISTS TRUE,那么就是查询条件成立,结果会显示出来。NOT EXISTS TRUE,则为FALSE,查询连接条件不成立。
select course_id from section as S
where semester = 'Fall' and year = 2009
and exists
(select * from section as Twhere semester = 'Spring' and year= 2010 and S.course_id= T.course_id)
select distinct S.ID, S.name from student as S
where not exists (
(select course_id from course where dept_name = 'Biology')
except
(select T.course_id from takes as T where S.ID = T.ID)
);
with dept_total (dept_name, value) as
(select dept_name, sum(salary)
from instructor group by dept_name),
dept_total_avg(value) as
(select avg(value) from dept_total)
select dept_name
from dept_total, dept_total_avg
where dept_total.value >= dept_total_avg.value
delete from instructor where dept_name = 'Finance';
update instructor set salary = salary * 0.1 where salary > 10000;
insert into coursevalues (’CS-437’, ’Database Systems’, ’Comp. Sci.’, 4);