PostgreSQL practice

Student Table Student
Create Table Student (Sid VARCHAR (. 6), Sname VARCHAR (10), Sage datetime, SSEX VARCHAR (10));
INSERT INTO Student values ('01 ',' Lei Zhao ',' 1990-01-01 ' , 'male');
INSERT INTO Student values ('02 ',' money power ',' 1990-12-21 ',' male ');
INSERT INTO Student values ('03', 'Sun wind', '1990- 05-20 ',' M ');
INSERT INTO Student values ('04', 'Li Yun', '1990-08-06', 'M');
INSERT INTO Student values ('05 ',' Zhou Mei ' , '1991-12-01', 'F');
INSERT INTO Student values ('06 ',' Wu Lan ',' 1992-03-01 ',' F ');
INSERT INTO Student values ('07', 'Zheng bamboo', '1989-07-01', 'female');
insert into Student values ('08 ', ' Wang Ju ',' 1990-01-20 ',' F ')
transcript SC
Create Table SC (Sid VARCHAR (10), Cid VARCHAR (10), Score decimal (18 is, 1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98)
课程表 Course
create table Course(Cid varchar(10),Cname varchar(10),Tid varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03')
教师表 Teacher
create table Teacher(Tid varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五')


Exercises:
1. Query "01" course than the "02" high course grade student information and course grade
*, the SELECT S Score.
From sdb "Student" S, sdb.sc.
The WHERE s.sid = sc.Sid
and in s.sid (SELECT a.sid
from sdb.sc A, B sdb.sc
WHERE a.sid = b.Sid
and a.score> b.score
and a.Cid = '01 '
and '02 = b.Cid ');

2. Query average score of 60 points or greater number of students and the student's name and student grade point average
the SELECT s.sid, Sname, AVG (Score)
from sdb.sc, sdb. "Student" AS S
the WHERE s.sid = sc. SID
Group by s.sid, Sname
HAVING AVG (Score)> = 60;

3. Query student achievement information exists in the SC table
the SELECT *
from sdb.sc, sdb "Student" AS S.
The WHERE s.sid = sc.Sid
and sc.score IS not null;

4. to query all students student number, student name, total enrollment, a total score of all the courses (results not shown as null)
the SELECT s.sid, Sname, COUNT (Cid), SUM (Score)
from sdb. "Student" left the Join sdb.sc S
ON s.sid = sc.Sid
Group by s.sid, Sname;

5. The number of inquiries "Lee" teacher surnamed
the SELECT COUNT (Tname)
from sdb.teacher AS t
the WHERE t.Tname like 'Lee%';

6. queries learned "John Doe" teachers to teach students information on
the SELECT *
from sdb. "Student" AS S, sdb.course AS c, t sdb.teacher AS, sdb.sc
the WHERE s.sid = sc.Sid
and = c.Cid sc.Cid
and c.Tid = t.Tid
and Tname = 'John Doe';

7. The inquiry did not learn the full information of the students for all courses
the SELECT *
from sdb. "Student" AS S
the WHERE s.sid in (the SELECT sc.Sid from sdb.sc Group by sc.Sid the HAVING COUNT (Cid) <3)

select s.Sid
from sdb."Student" as s
left join sdb.sc
on s.Sid=sc.Sid
group by s.Sid
having count(sc.Cid)<(select count(Cid) from sdb.course);

8. query exactly the same classmates, "01" was the learning of other students of the course information
the SELECT s.sid, Sname, Sage, SSEX
from sdb. "Student" S, sdb.sc
the WHERE s.sid = sc.Sid
and ! s.sid = '01'
Group by s.sid, Sname, Sage, SSEX
HAVING COUNT (Cid) = (SELECT COUNT (Cid) from sdb.sc WHERE Sid = '01 ');

9. query has at least the same information the students learn a lesson and number "01" students have learned
the SELECT *
from sdb. "Student" S
the WHERE in Sid (Sid the SELECT
from sdb.sc
the WHERE Cid in (the SELECT Cid
from sdb .sc
WHERE Sid = '01 '));

10. Query students never learned the name "John Doe," the teacher taught a course in any of
the SELECT Sname
from sdb. "Student" S
the WHERE Sid not in (
the SELECT s.sid
from sdb. "Student" AS S, sdb.course C AS, AS sdb.teacher T, sdb.sc
WHERE s.sid = sc.Sid
and sc.Cid = c.Cid
and c.Tid = t.Tid
and Tname like 'John Doe');

11 and above query two students failed the course number, name and grade point average
the SELECT s.sid, Sname, AVG (Score)
from sdb. "Student" S, sdb.sc
the WHERE s.sid sc = .sid
and s.sid Not in (SELECT DISTINCT Sid
from sdb.sc
WHERE Score> = 60)
Group by s.sid, Sname;

12. Retrieve "01" fraction is less than 60 courses, by score in descending order student information
SELECT S. *
From SDB. "Student" S, sdb.sc
WHERE s.sid = sc.Sid
and Cid = '01 '
and Score < 60
the Order by Score desc;

13. The average score display in descending grades and grade point average for all courses for all students of
the SELECT Score, AVG (Score)
from sdb.sc
Group by Score
the Order by AVG (Score) desc;

select Sid,
sum(case when Cid='01' then score else null end) score_01,
sum(case when Cid='02' then score else null end) score_02,
sum(case when Cid='03' then score else null end) score_03,
avg(score)
from sdb.sc
group by Sid
order by avg(score) desc;

14. Query all subjects highest score, lowest score and the average score is displayed in the following form: course ID, course name, highest score, lowest score, average score, pass rate, medium rate, good rate, excellent rate (to pass> = 60, is moderate: 70-80, is good: 80-90, was excellent:> = 90).
select sc.Cid, Cname course name, avg (score) average, max (score) the highest score, min (score) the lowest score,
(SUM (Case the when Score> = 1 the else the then 60 0 End) * 100 / COUNT ( Score)) pass rate,
(SUM (Case When Score> = 70 and Score <) Average rate of 80 the then. 1 the else 0 End) * 100 / COUNT (Score),
(SUM (Case When Score> = 80 and Score <90 the then 1 else 0 end) * 100 / count (score)) good rate,
(SUM (Case When Score> 90 = the then 1 else 0 end) * 100 / count (score)) excellent rates
from sdb.sc, sdb.course c
sc.Cid = c.Cid WHERE
Group by sc.Cid, the Cname

The number of points each segment 15. Statistics all subjects: course number, course name, [100-85], [85-70], [70-60], [60-0] and the percentage of
select sc.Cid Course Number , Cname course name,
(SUM (Case When Score> = 0 and Score <= 60 the then. 1 the else 0 End) * 1.00 / COUNT (Score)) AS failed 0_60,
(SUM (Case When Score> 60 and Score <= 70 then 1 else 0 end) * 1.00 / count (score)) as pass 60_70,
(SUM (Case When Score> 70 and Score <= 85 the then. 1 the else 0 End) * 1.00 / COUNT (Score)) AS well 70_85,
(sum (case when score> 85 and score <= 100 then 1 else 0 end) * 1.00 / count (score)) as excellent 85_100
from sdb.sc, sdb.course C
WHERE sc.Cid = c.Cid
Group by SC .Cid, Cname

16. Search record top three all subjects
the SELECT Cid, Sid, Score
from sdb.sc
the WHERE (the SELECT COUNT (*)
from A sdb.sc
the WHERE a.Cid = sc.Cid
and sc.score> a.score) <. 3
Order by Cid, Score desc;

select * from (select *,rank() over(partition by Cid order by score desc) as graderank from sdb.sc) a
where a.graderank <=3;

17. Inquiries about the only two courses of elective student number and name of
the SELECT s.sid, Sname
from sdb. "Student" S, sdb.sc
the WHERE s.sid = sc.Sid
Group by s.sid, Sname
the HAVING COUNT ( Cid) = 2;

18. The student information query name contains "wind" word of
the SELECT *
from sdb "Student" S.
The WHERE Sname like '% wind%';

19. The results are not repeated, the student elective query "John Doe" teacher grant program, the highest-achieving students information and scores
the SELECT s.sid, Sname, SSEX, Sage, Score
from sdb. "Student" AS S, sdb .sc
WHERE s.sid = sc.Sid
Group by s.sid, Sname, SSEX, Sage, Score
HAVING max (Score) = (SELECT max (Score)
from SDB. "Student" S AS, AS sdb.course C, T AS sdb.teacher, sdb.sc
WHERE s.sid = sc.Sid
and sc.Cid = c.Cid
and c.Tid = t.Tid
and Tname like 'John Doe');

Guess you like

Origin www.cnblogs.com/hole/p/11699702.html