高级sql查询的几个小例子

数据库表与之前所描述的数据库表相同,在此不再赘述。

Using the university schema that you have write the following queries. In some cases you might need to insert extra data to show the effect of a particular feature.

1. Insert each student as an instructor of department ‘拳脚学院’, with salary=40000

insert into instructor
	select ID, name,'拳脚学院',40000
	from student
	where ID>76543 OR ID<76543 

2. Now delete all the newly added "instructors" above (note: already existing students who happened to have salary=40000 should not get deleted)

delete from instructor
where name in (select name
		from student)
3. Update the salary of each instructor to 10000 times the number of course sections they

have taught.

update instructor
set salary = (select count(teaches.ID)*10000+29000
	    from teaches
	    where teaches.ID=instructor.ID
	    group by ID) 
4. The university rules allow an F grade to be overridden by any pass grade (for example, A).
Now, lists students who have fail grades that have not been overridden. For each student
as such, information displayed (in one row) should involve:
(1) Identifier of student
(2) Name of student

(3) Count of F grades that have not been overridden.

5. In one result, list the instructors who have never taught any courses and the students
who have never registered for any courses. For each person, information displayed (in one
row) should involve:
(1) Id of the person
(2) Name of the person

(3) Role of the person. The value of role should be ‘student’ or ‘instructor’.

select ID,name,'student' role
from student 
where student.ID not in (select ID
			from takes)
union
select ID,name,'teacher' role
from instructor
where instructor.ID not in (select ID 
			    from teaches)


猜你喜欢

转载自blog.csdn.net/weixin_40304882/article/details/80670856