1)
select Cno,COUNT(Sno)
from sc
group by Cno;
分析:該語句對查詢結果按Cno的值分組,所有相同Cno值的元組為一組
然后對每一組作用集函數COUNT計算,以求得該組的學生人數。
如果分組后還要求按一定的條件對這些組進行篩選,最終只輸出滿足指定條件的組,則可以使用HAVING
短語指定篩選條件。select Cno,COUNT(Sno)
from sc
group by Cno
having? COUNT(*)〉3;select *
from Student
where Sno = '9500';
2)
兩個表的查詢。
seclect? ?Student.*,SC.*
from Student,SC
where? Student.Sno = SC.Sno;
3)
外連接
select Student.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade
from Student,SC
where Student.Sno = SC.Sno(*)
4)
帶有IN謂詞的子查詢
例:
查詢與“劉晨”在同一個系學習的學生
select Sno,Sname,Sdept
from Student
where Sdept IN
????????? (select Stept
??????????? from Student
??????????? Where Sname = '劉晨');
方法2:
select S1.Sno,S1.Sname,S1.Sdept
from Student S1,Student S2
where S1.Sdept = S2.Sdept ?and? S2.Sname = '劉晨';5)
對查詢結果排序
select?? Sno,Grade
from? SC
where? Cno? =? '3'
order? by? grade? desc;
6)
查詢學生總人數
select? ?count(*)
from?? Student;
計算1號課程的最高成績
select max(Grade)
from SC
where? Cno? = '1';??? //avg(),sum()一列值的總和。count()統計元組個數。
7)
Statement stmt=con.createStatement();
stmt.executeUpdate("Update bookTable set Title='Java2' where Author='zhang'");
stmt.executeUpdate("Delete from bookTable where Author='zhang'");
stmt.executeUpdate("Insert into bookTable(BookID,Author,Title) values(1,'Li Ming','Java2')"); //未給出的列,其值為NULL