select title from course where dept_name = 'Comp. Sci.' and course.credits = 3;
select distinct takes.id from instructor natural join teaches join takes using (course_id,sec_id,semester,year) where name = 'Einstein';
select max(salary) from instructor
select name from instructor where salary=(select Max(salary) from instructor)
select course_id, sec_id, count(ID) from section natural join takes where semester = 'Fall' and year = 2009 group by course_id, sec_id
select Max(cnt) from ( select Count(ID) as cnt from section natural join takes where semester = 'Fall' and year = 2009 group by course_id, sec_id )
with Fall2009 as ( select course_id, sec_id, count(ID) as cnt from section natural join takes where semester = 'Fall' and year = 2009 group by course_id, sec_id ) select course_id,sec_id from Fall2009 where cnt = (select max(cnt) from Fall2009)
/根據題意建立表/sql
/*建立表關係*/ create table Grade_points ( grade varchar (2), Points numeric(3, 1) ); /*插入值*/ insert into grade_points values ('A',4.0); insert into grade_points values ('A-',3.7); insert into grade_points values ('B+',3.3); insert into grade_points values ('B',3.0); insert into grade_points values ('B-',2.7); insert into grade_points values ('C+',2.3); insert into grade_points values ('C',2.0); insert into grade_points values ('C-',1.7);
select distinct sum(credits * points) from takes natural join course natural join grade_points where ID = 12345;
select distinct sum(credits * points) /sum(credits) AS res from takes natural join course natural join grade_points where ID = 12345;
select distinct sum(credits * points) /sum(credits) AS GPA from takes natural join course natural join grade_points group by ID;
update instructor set instructor.salary = instructor.salary * 1.10 where instructor.dept_name = 'Comp. Sci.';
delete from course where course_id not in (select distinct course_id from section);
insert into instructor select ID,name,dept_name,10000 from student where tot_cred >= 100;
person( driver_id, name, address) ar( license, model, year) ccident( report_number, date, location) wns (driver_id, license) artcipated ( report_number, license, driver_id, damage_amount)
select count(distinct driver_id) AS num_of_person from person natural join participated natural join accident where date between date ’1989-00-00’ and date ’1989-12-31’
insert into accident values (1239401,'1991-01-01','Wl');
delete frrm car where Model = 'Mazda' and license in ( select license from person p, owns o where p.name = ’John Smith’ and p.driver id = o.driver id );
select ID, case when score < 40 then 'F' when score < 60 then 'C' when score < 80 then 'B' else 'A' end as rank from marks;
select count(ID) as cnt from ( select ID, case when score < 40 then 'F' when score < 60 then 'C' when score < 80 then 'B' else 'A' end as rank from marks; )group by rank;
select dept_name from department where lower(dept_name) like '%sci%';
select distinct p.al from p,r1,r2 where p.al = r1.al or p.a1 = r2.al;
解: 當且僅當r1,r2非空時,p.a1要麼在r1中要麼在r2中。 當r1或r2空時,r1 X r2爲空。 當p是空時,顯然查詢結果是爲空的正解
銀行數據庫 branch(branch name, branch city, assets) customer (customer name, customer street, customer city) loan (loan number, branch name, amount) borrower (customer name, loan number) account (account number, branch name, balance ) depositor (customer name, account number)
(select customer_name from desositor) from (select customer_name from borrower)
select F.customer_name from customer F join customer S using (customer_street,customer_city) where S.customer_name = 'Smith';
select branch_name from acoount natural join depositor natural join customer where branch_city = 'Harrison';
圖3.20 僱員數據庫 employee (employee name, street, city) works (employee name, company name, salary) company (company name, city) manages (employee name, manager name)
select employee_name,city from employee where employee_name in ( select employee_name from works where company_name = 'First Bank Corporation' );
select employee_name,street,city from employee natural join works where company_name = 'First Bank Corporation' and salary > 10000;
select employee_name from works where employee_name not in ( select employee_name from works where company_name = 'First Bank Corporation' );
select employee_name from works where salary > ( select max(salary) from works where company_name = 'Small Bank Corporation' );
select C.company_name from company C join company P using (city) where P.company_name = 'Small Bank Corporation';
select company_name from works group by company_name having count (distinct employee_name) >= all( select count (distinct Employee_name) from works group by company_name);l
select company_name from works group by company_name having avg(salary) > ( select avg(salary) from works where company_name = 'First Bank Name');
update employee set city = 'Newtown' where employee_name = 'Johns';
update works set salary = case when salary * 1.10 <= 100000 then salary * 1.10 else salary * 1.03 end where company_name = 'First Bank Corporation' and employee_name in (select employee_name from managers);
select distinct student.name from student natural join takes join course using (course_id) where course.dept_name = 'Comp. Sci.';
select distinct ID,name from student where id not in((select distinct ID from takes where year < 2009 or(year = 2009 and semester ='Spring')));
select dept_name,max(salary) from instructor group by dept_name;
select min(MAX_SALARY) from(select dept_name,max(salary) as MAX_SALARY from instructor group by dept_name);
insert into course values ('CS-001', 'Weekly Seminar', '', 0);
insert into section values ('CS - 001', 1, 'Fall', 2009, NULL, NULL, NULL);
insert into takes select ID, 'CS - 001', 1, 'Fall', 2009, NULL from student where Dept_name = 'Comp. Sci.';
delete from takes where course_id = 'CS-001' and sec_id = 1 and year = 2009 and semester = 'Fall' and ID in( select ID from student where name = 'Chavez' );
delete from course where course_id = 'CS-001'; 若是在刪除語句以前,沒有先刪除課程段,則其對應課程段和學生的選課信息一塊兒被刪。由於在建表的DDL語句中加入了級聯刪除的設置。
delete from takes where course_id not in (select course_id from course where lower(title) like '%database%');
create table person ( driver_id varchar(50), name varchar(50), address varchar(50), primary key(driver_id) ); create table car ( license varchar(50), model varchar(50), year numeric(5,0), primary key (license) ); create table accident ( report_number varchar(10), dateion date, loaction varchar(50); primary key (report_number); ); create table owns ( driver_id varchar(50), license varchar(50), primary key(driver_id), foreign key (driver_id) references person, foreign key (license) references car ); create table participated ( report_num varchar(10), license varchar(50), driver_id varchar(50), damage_amount numeric(10,0), primary key (report_number,license), foreign key (report_number) references accident, foreign key (license) references car, foreign key (driver_id) references owns );
select count(report_number) from participated where license in (select license from person natural join owns where name = 'John Smith');
update participated set damage_amount where report_number = 'AR2197' and license = 'AABB2000';
with branchcount as ( select count(*) from Branch where branch_city = 'Brooklyn' ) select custumer_name from customer c where branchcount = (select count(distinct branch_name) from (customer natural join depositor natural join account natural join branch) as d where d.customer_name = c.customer_name);
select sum(amount) from loan;
select branch_name from branch where assets > some(select assets from branch where branch_city = 'Brooklyn');
select employee_name from works where company_name = 'First Bank Corporation':
select employee_name from employee E join works using (employee_name) join company C using (company_name) where E.city = C.city;
select P.employee_name from employee P,employee R,managers M where P.employee_name = M.employee_name and R.employee_name = M.manger_name and P.street = R.street and P.city = R.city;
with avg_salary as (select company_name,avg(salary) as val from works group by company_name) select employee_name from works natural join avg_salary where salary > val;
select company_name from works group by company_name having sum(salary) = (select Min(Sum(salary)) from works group by company_name);
update works set salary = salary * 1.10; where company_name = 'First Bank Corporation';
update works set salary = salary * 1.10; where company_name = 'First Bank Corporation' and employee_name in (select manager_name from managers);l
delete from works where company_name = 'First Bank Corporation';
member(memb no, name, age) book(isbn, title, authors, publisher) borrowed(memb no, isbn, date)
select distinct name from member natural join book natural join borrowed where publisher = 'McGraw-Hill';
select name from member where memb_no in (select memb_no from borrowed group by memb_no having count(*) = (select count(*) from book where publisher='McGraw-Hill'));
select name,publisher from member natural join borrowed natural join book group by name,publisher having count(isbn) > 5;
select(select count(*) from borrowed)/ (select count(*)from member)
select course id, semester, year, section id, avg (credits earned) from takes natural join student where year = 2009 group by course id, semester, year, section id having count (ID) >= 2;
解:takes 和 section 是經過一些共同的外鍵相連繫,每個takes的某一個元組不會由於增長額外的元祖。
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;
解:答案以下: select dept_name from (select dept name, sum(salary) from instructor group by dept name) P where P.val >= (select avg(sum(salary)) from instructor group by dept_name);