SQL 查詢語句基本練習

這是一些基本的查詢語句練習,所用編輯器爲Vscode,配置的是postgreSQL,這些習題是看B站視頻的時候寫的,比較簡單的沒有註釋語句,加了註釋的是一些在入門時感覺稍微麻煩的語句。

首先建立一個test數據庫,然後選定test數據庫建立4個數據表:student、course、score、teacher。

\l   --查看數據庫(MySQL:show databases;)
create database test;  --創建數據庫
\c test --選定數據庫(MySQL:use test)

 postgreSQL中查看數據庫、選定數據庫等少量命令與MySQL會有所不同。

-------------------------查詢練習
----學生表 Student : 學號 姓名 性別 出生年月日 所在班級
----課程表 Course : 課程號 課程名稱 教師編號
----成績表 Score : 學號 課程號 成績
----教師表 Teacher : 教師編號 教師名字 教師性別 出生年月日 職稱 所在部門
create table student(

    sno varchar(20) primary key,
    sname varchar(20) not null,
    ssex varchar(10) not null,
    sbirthday date,
    class varchar(20)
);
create table teacher(
    tno varchar(20) primary key,
    tname varchar(20) not null,
    tsex varchar(10) not null,
    tbirthday date,
    prof varchar(20) not null,
    depart varchar(20) not null
);
create table course(
    cno varchar(20) primary key,
    cname varchar(20) not null,
    tno varchar(20) not null,
    foreign key(tno) references teacher(tno)
);
create table score(
    sno varchar(20) not null,
    cno varchar(20) not null,
    degree decimal,
    foreign key(sno) references student(sno),
    foreign key(cno) references course(cno)
);
\dt ---查看數據庫中的數據表
\d student; ---查看數據表結構
\d course;
\d teacher;
\d score;

由於course表和score表需要連接student 表和teacher表中的外鍵,所以不能先建course表和score表,必須先創建student 表和teacher表。

創建好數據表後,往各個數據表中增添具體數據:

insert into student values('101','曾華','男','1977-09-01','95033');
insert into student values('102','匡明','男','1975-10-02','95031');
insert into student values('103','王麗','女','1976-01-23','95033');
insert into student values('104','李軍','男','1976-02-20','95033');
insert into student values('105','王芳','女','1975-02-10','95031');
insert into student values('106','陸君','男','1974-06-03','95031');
insert into student values('107','王尼瑪','男','1976-02-10','95033');
insert into student values('108','張全蛋','男','1975-02-10','95031');
insert into student values('109','趙鐵柱','男','1974-06-03','95031');
select * from student;
insert into teacher values('804','李誠','男','1958-12-02','副教授','計算機系');
insert into teacher values('856','張旭','男','1969-03-12','講師','電子工程系');
insert into teacher values('825','王萍','女','1972-05-05','助教','計算機系');
insert into teacher values('831','劉冰','女','1977-08-14','助教','電子工程系');
select * from teacher;
insert into course values('3-105','計算機導論','825');
insert into course values('3-245','操作系統','804');
insert into course values('6-166','數字電路','856');
insert into course values('9-888','高等數學','831');
select * from course;
insert into score values('103','3-245','86');
insert into score values('105','3-245','75');
insert into score values('109','3-245','68');
insert into score values('103','3-105','92');
insert into score values('105','3-105','88');
insert into score values('109','3-105','76');
insert into score values('103','3-105','64');
insert into score values('105','3-105','91');
insert into score values('109','3-105','78');
insert into score values('103','6-166','85');
insert into score values('105','6-166','79');
insert into score values('109','6-166','81');
select * from score;

下面是查詢語句的一些基本練習:

------select練習

select * from student;
select sname,ssex,class from student;
select distinct depart from teacher;   ---distinct:排重
select * from score where degree between 60 and 80;
---或者 select * from score where degree > 60 and degree < 80;
select * from score where degree in (85,86,88);
select * from student where class='95031' or ssex='女';
select * from student order by class desc;   ---降序排列(默認升序asc)
select * from score order by cno asc,degree desc;
select count(*) from student where class='95031';   ----count計數
--select max(degree) from score;
select sno,cno,degree from score where degree=(select max(degree) from score);
select avg(degree) ,cno from score group by cno; --查詢每門課的平均成績, avg:取平均數
---score表中至少有2名學生選修的並以3開頭的課程平均數
select avg(degree),cno from score group by cno having count(cno)>=2 and cno like '3%'; 
select sno from score where degree between 70 and 90;
---多表查詢同時輸出:查詢所有學生的sname,cno和degree列
--select sname from student;
--select cno,degree from score;
select sname,cno,degree from student,score where student.sno=score.sno;
select sno,cname,degree from course,score where course.cno=score.cno;
select sname,cname,degree from student,course,score where student.sno=score.sno and course.cno=score.cno;
---95031班學生每門課的平均分
select cno,avg(degree) from score where sno in(select sno from student where class='95031') group by cno;
---選修3-245課程高於109號同學3-245成績的所有同學記錄
--select degree from score where cno='3-245' and sno='109';
select score from score where cno='3-245' and degree > (select degree from score where cno='3-245' and sno='109');
---查詢和學號爲108,101的同學同年出生的所有同學的sno,sname,sbirthday
--select year(sbirthday) from student where sno in (108,101) ; ---year:提取年份
select sno,sname,sbirthday from student where year(sbirthday) in (select year(sbirthday) from student where sno in (108,101));
---張旭教師任課的學生成績
select degree from score where cno in (select cno from course where tno = (select tno from teacher where tname='張旭'));
---查詢選修某課程的同學多於5人的教師姓名
select tname from teacher where tno in (select tno from course where cno=(select cno from score group by cno having count(*)>5));
select * from student where class in ('95033','95031');
select cno from score where degree > 85;
---計算機系教師所教課程的成績表
select * from score where cno in (select cno from course where tno in (select tno from teacher where depart='計算機系'));
---計算機系與電子工程系不同職稱教師的tname和prof(不要相同職稱的記錄) --union的用法
select tname,prof from teacher where depart ='計算機系' and prof not in (select prof from teacher where depart='電子工程系') union select tname,prof from teacher where depart ='電子工程系' and prof not in (select prof from teacher where depart='計算機系');
---選修編號爲3-105課程且成績至少高於選修編號爲3-245的同學的cno,sno和degree   --any的用法
select cno,sno,degree from score where cno='3-105' and degree > any (select degree from score where cno='3-105') order by degree desc;
---選修編號爲3-105課程且成績高於選修編號爲3-245的同學的cno,sno和degree   --all的用法(不加all會報錯)
select cno,sno,degree from score where cno='3-105' and degree > all (select degree from score where cno='3-105') order by degree desc;
---查詢所有女老師和女同學的name,sex,birthday (不加as語句會默認列表名稱爲sname,ssex,sbirthday)
select sname as name,ssex as sex,sbirthday as birthday from student where ssex ='女' union select tname,tsex,tbirthday from teacher where tsex ='女';
---查詢所有任課教師的tname,depart(在課表中有課)
select tname,depart from teacher where tno in (select tno from course);
---查詢至少有2名男生的班號
select class from student where ssex='男' group by class having count(*)>1;
---student中不姓王的同學記錄
select * from student where sname not like '王%';
---查詢student中每個學生的姓名和年齡 --年齡=當年年份-出生年份 
select sname,year(now)-year(sbirthday) from student;
select max(sbirthday),min(sbirthday)  from student;
select * from student order by class ,year(now)-year(sbirthday) desc;
select * from course where tno in (select tno from teacher where tsex='男'); 
---查詢最高分同學的sno,cno,degree
select sno,cno,degree from score where degree = (select max(degree) from score);
select sname from student where ssex = (select ssex from student where sname='李軍');
select sname from student where ssex = (select ssex from student where sname='李軍') and class=(select class from student where sname='李軍');
select * from score where cno=(select cno from course where cname='計算機導論') and sno in (select sno from student where ssex='男');
select degree from score where sno in (select sno from student where sname like '王%');
select tno from teacher where prof in ('講師','助教');

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章