當前位置:首頁 » 編程語言 » sql的題目及答案
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql的題目及答案

發布時間: 2022-06-14 20:17:00

sql資料庫題,急!馬上給答案,給高分!!

(1) 列出1994年出版的所有圖書的編號(BBH)及書名(BN)
SELECT BBH, BN FROM B WHERE BD LIKE '1994%'

(2) 列出借閱過書名為「資料庫概論」的學生的學號(SBH)和姓名(SN)
SELECT S.SBH, S.SN
FROM S, B, R
WHERE S.SBH = R.SBH AND R.BBH = B.BBH AND B.BN = '資料庫概論'

(3) 列出借閱過所有在1994年出版的圖書的學生的學號
SELECT DISTINCT SBH
FROM R WHERE BBH IN (SELECT BBH FROM R WHERE BD LIKE '1994%')

(4) 找出1994年借閱過圖書的學生的學號(SBH)及姓名(SN)
SELECT S.SBH, S.SN
FROM S, R WHERE S.SBH = R.SBH AND BD LIKE '1994%'

(5) 統計每年出版的所有圖書的總數(給出年代(BD)及當年的出版總數)
SELECT SUBSTR(BD, 1, 5), COUNT(BBH)
FROM B GROUP BY SUBSTR(BD, 1, 5)

(6) 找出沒有學生借閱過的圖書的編號(BBH)及書名(BN)
SELECT BBH, BN
FROM R WHERE BBH NOT IN (SELECT BBH FROM R)

Ⅱ sql題目,請給出答案並解釋

create view as select a.學號,a.姓名,a.性別,a.系名,b.課程名,b.成績,b.任課老師名 from 學生 as a left join (select 學號,課程名,成績,任課老師 from 成績 left join 任課 on 成績.課程名=任課.課程名) as b on a.學號=b.學號

Ⅲ SQL 題目,初學者,需要高手給答案。重謝!

create table card (
fundaccount int ,
bankno char(1),
cardno char(20)
)
insert into card
select 1,'1','111111111111'
union all
select 2,'2','111111111111'
union all
select 3,'2','222222222222222'

update card set cardno='100'+cardno from card
where bankno='2'
and len(LTRIM(RTRIM(cardno))) =12

select *,len(cardno) from card
---------------------
第二個懶的建表的,核心就是求出客戶代碼,然後在關聯
select top 5 客戶代碼 from ( select

客戶代碼,姓名,SUM(XX) AS XX FROM XXX

where way=1 and year(date)=1998 and month(date)=3
GROUP BY
客戶代碼,姓名
)
order by 成交金額

Ⅳ 資料庫sql題目

答案為B
其實從語法就可以排除的:where字句中不能出現聚合函數所以AD排除;出現group by字句,則select字句中查詢的列要麼分組要麼聚合,C選項中姓名列既沒分組也沒在聚合函數中。
下面說說思路:
想要查詢只選修1門課的學生,可以先查出每個學生選了多少門課(按學生分組group by),然後挑選出選課數為1的(對分組後得到的結果進行篩選,having)
如果還有問題請追問。

Ⅳ SQL測試題(註:最佳答案必須能在MySQL下運行)

/*
閑著沒事,瞅瞅網路上的問題,今天天晚了,先解決一個,另一個明兒個再說了!
第二道題也算已經搞定了!
環境 : mysql Ver 14.12 Distrib 5.0.45, for Win32 (ia32)
參考 :
exist與in 的區別
http://blog.csdn.net/change888/archive/2008/03/31/2232778.aspx
*/
/*********************************問題 1 **************************************/

drop table if exists s;
create table if not exists s (s varchar(32), sn varchar(32), sd varchar(32),
sa int);
insert into s values ('s1', '朱', '開發本部', 23);
insert into s values ('s2', '牛', '人事部', 25);
insert into s values ('s3', '楊', '財務部', 26);
insert into s values ('s4', '馬', '開發本部', 22);
insert into s values ('s5', '呂', '人事部', 27);
insert into s values ('s6', '於', '開發本部', 28);
insert into s values ('s7', '侯', '開發本部', 28);

drop table if exists c;
create table if not exists c (c varchar(32), cn varchar(32));
insert into c values ('c1', '軟體工程');
insert into c values ('c2', '計算機技術與科學');
insert into c values ('c3', '車輛工程');

drop table if exists sc;
create table if not exists sc (s varchar(32), c varchar(32));
insert into sc values ('s1', 'c1');
insert into sc values ('s1', 'c2');
insert into sc values ('s1', 'c3');
insert into sc values ('s2', 'c1');
insert into sc values ('s2', 'c3');
insert into sc values ('s3', 'c2');
insert into sc values ('s4', 'c2');
insert into sc values ('s4', 'c3');
insert into sc values ('s5', 'c1');
insert into sc values ('s6', 'c3');

/* 1. 查詢選修課程名稱為 「軟體工程」 的學員學號和姓名 */
select s.s '學號', s.sn '姓名' from s where s.s in
(select sc.s from sc where sc.c in
(select c.c from c where c.cn = '軟體工程'));

/* 2. 查詢選修課程編號為 「C2」 的學員姓名和所屬單位 */
select s.sn '姓名', s.sd '所屬單位' from s where s.s in
(select sc.s from sc where sc.c = 'C2');

/* 3. 查詢選修課程編號 不 為 「C2」 的學員姓名和所屬單位 */
select s.sn '姓名', s.sd '所屬單位' from s where
s.s not in (select sc.s from sc where sc.c = 'C2')
and
s.s in (select sc.s from sc);

/* 4. 查詢選修全部課程的學員姓名和所屬單位 */
select s.sn '姓名', s.sd '所屬單位' from s where
(select count(DISTINCT sc.c) from sc where sc.s = s.s)
=
(select count(DISTINCT c.c) from c );

/* 5. 查詢選修了課程的學員人數 */
select count(DISTINCT sc.s) '人數' from sc;

/* 6. 查詢選修課程 >= 2 門的學員學號和所屬單位 (不得不用 CASE 語句了)*/
select s.sn '姓名', s.sd '所屬單位' from s where s.s in
(select CASE WHEN count(DISTINCT sc.c) >=2 THEN sc.s END from sc group by sc.s );

/* 運行結果
------------------------------------1
+------+------+
| 學號 | 姓名 |
+------+------+
| s1 | 朱 |
| s2 | 牛 |
| s5 | 呂 |
+------+------+
------------------------------------2
+------+----------+
| 姓名 | 所屬單位 |
+------+----------+
| 朱 | 開發本部 |
| 楊 | 財務部 |
| 馬 | 開發本部 |
+------+----------+
------------------------------------3
+------+----------+
| 姓名 | 所屬單位 |
+------+----------+
| 牛 | 人事部 |
| 呂 | 人事部 |
| 於 | 開發本部 |
+------+----------+
------------------------------------4
+------+----------+
| 姓名 | 所屬單位 |
+------+----------+
| 朱 | 開發本部 |
+------+----------+
------------------------------------5
+------+
| 人數 |
+------+
| 6 |
+------+
------------------------------------6
+------+----------+
| 姓名 | 所屬單位 |
+------+----------+
| 朱 | 開發本部 |
| 牛 | 人事部 |
| 馬 | 開發本部 |
+------+----------+
*/

/*********************************問題 2 **************************************/

drop table if exists s ;
create table if not exists s ( sno varchar(32), sname varchar(32));
insert into s values ('s1', '朱');
insert into s values ('s2', '牛');
insert into s values ('s3', '楊');
insert into s values ('s4', '馬');
insert into s values ('s5', '呂');
insert into s values ('s6', '於');
insert into s values ('s7', '侯');

drop table if exists c;
create table if not exists c ( cno varchar(32), cname varchar(32),
cteacher varchar(32));
insert into c values ('c1', '數學', '張');
insert into c values ('c2', '日語', '李'); /*假設李老師同時教授日語和英語*/
insert into c values ('c3', '英語', '李');

drop table if exists sc;
create table if not exists sc (sno varchar(32), cno varchar(32),
scgrade double);
insert into sc values ('s1', 'c1', 75);
insert into sc values ('s1', 'c2', 70);
insert into sc values ('s1', 'c3', 80);
insert into sc values ('s2', 'c1', 50);
insert into sc values ('s2', 'c3', 40);
insert into sc values ('s3', 'c1', 50);
insert into sc values ('s3', 'c2', 60);
insert into sc values ('s4', 'c1', 90);
insert into sc values ('s4', 'c2', 40);
insert into sc values ('s4', 'c3', 20);
insert into sc values ('s5', 'c1', 80);
insert into sc values ('s6', 'c1', 85);

/* 1. 沒有 選 修過「李」老師講授課程的所有學生姓名 */
select s.sname '姓名' from s where s.sno not in
(select sc.sno from sc where sc.cno in
(select c.cno from c where c.cteacher = '李'));

/* 2. 列出有二門以上(含兩門)不及格課程的學生姓名及其平均成績 */
select s.sname '姓名', AVG(sc.scgrade) '平均成績' from s, sc
where s.sno = sc.sno
and
(select count(sc.sno) from sc where sc.sno = s.sno
and sc.scgrade < 60 ) >= 2
group by s.sno;

/* 3. 列出既學過「C1」號課程,又學過「C2」號課程的所有學生姓名 */
select s.sname '姓名' from s where s.sno in
(select t1.sno from sc t1, sc t2
where t1.sno = t2.sno and t1.cno = 'c1' and t2.cno = 'c2');
/*或者*/
select s.sname '姓名' from s where s.sno in
(select sc.sno from sc where sc.cno = 'c1' and sc.sno in
(select t1.sno from sc t1 where t1.cno = 'c2'));

/* 4. 列出「C1」號課成績比「C2」號同學該門課成績高的所有學生的學號 */
select t1.sno '學號' from sc t1, sc t2
where t1.sno = t2.sno and t1.cno = 'c1'
and t2.cno = 'c2' and t1.scgrade > t2.scgrade;

/* 5. 列出「C1」成績比「C2」成績高的學生的學號及其「C1」和「C2」的成績 */
select t1.sno '學號', t1.scgrade 'C1成績', t2.scgrade 'C2成績' from sc t1, sc t2
where t1.sno = t2.sno and t1.cno = 'c1'
and t2.cno = 'c2' and t1.scgrade > t2.scgrade;

/* 運行結果
------------------------------------1
+------+
| 姓名 |
+------+
| 呂 |
| 於 |
| 侯 |
+------+
------------------------------------2
+------+----------+
| 姓名 | 平均成績 |
+------+----------+
| 牛 | 45 |
| 馬 | 50 |
+------+----------+
------------------------------------3
+------+
| 姓名 |
+------+
| 朱 |
| 楊 |
| 馬 |
+------+
------------------------------------4
+------+
| 學號 |
+------+
| s1 |
| s4 |
+------+
------------------------------------5
+------+--------+--------+
| 學號 | C1成績 | C2成績 |
+------+--------+--------+
| s1 | 75 | 70 |
| s4 | 90 | 40 |
+------+--------+--------+
*/

Ⅵ SQL多項選擇題,急求答案,最好有解析

1.答案A,D,E DESC是降序,省略ASC和DESC,默認為升序。
2.答案A,C,D,E 創建視圖時不允許在所用SELECT語句中使用ORDER BY、COMPUTE子句
3.C,D,E 執行存儲過程時可用WITH RECOMPLE選項進行重新編譯;只有當執行存儲過程的語句是批處理中的第一個語句,才可以直接通過名稱來調用存儲過程
4.C,E 使用SQL Server Management Studio不能運行命令行實用程序sqlcmd
5.C,D,F 一個局部變數只能在一個語句批中使用,使用SET語句只能對一個局部變數賦值,剛定義的局部變數的初值為空值
6.B 用戶定義函數可以有輸入參數和返回值

Ⅶ SQL server題目求解答

1
你上邊的createtabledept……和createtableemp……就是答案
2
select'姓名:'+ename+'出生日期:+'convert(varchar(10),hiredate,120)+'工資:'+cast(salasvarchar)fromemp
3
selecta.ename+'』smanageris'+b.enamefromempa,empbwherea.MGR=b.empno
4
selectenamefromempwhereLEN(ename)=(selectMAX(LEN(ename))fromemp)
5
selectCAST(SALasvarchar)fromemp
selectCONVERT(varchar,sal)fromemp
6
SQLServer:datetime和dateMySQL:datetime和date
7
仿造insertintoemp……那些自己加吧
8
SQLServer:selectgetdate()
MySQL:selectnow()
9
selecttop1YEAR(hiredate)fromempgroupbyYEAR(hiredate)orderbyCOUNT(*)desc
10
selectmax(year(hiredate))-min(year(hiredate))fromemp

Ⅷ sql的4道題目.要正確答案

回如下,希望對您有幫助.
1.Inner Join 把兩個表連接在一起,返回兩個表中相匹配的記錄

Left outer join,左側表所有的記錄都返回,右側匹配的記錄返回,沒有匹配的返回Null

Right outer join 與Left outer join相反,右側的記錄返回,左側返回匹配的記錄,沒有匹配返回Null

Cross join 兩個表的笛卡兒積,返回所有可能的值,不允許有連接條件!

2.select 科目,avg(成績) from 成績表 group by 科目
order by 科目

3.建立索引是提高select語句最好的方法
(1).使用exists關鍵字檢查結果集:不要用count(*)來檢查結果集中是否包含行。
(2).使用標准聯接代替嵌套查詢:在執行嵌套查詢時,SQL server將先執行內部的子查詢,然後將查詢結果返回給外部查詢的作為檢索的數據源,最後執行外部的主查詢。而在執行包含標准聯接的查詢時,SQL server將要執行的僅僅是一個查詢。
(3).有效避免整表掃描,使用索引。
(4).在like子句的匹配條件的開始使用了%,若在like子句的匹配條件的開始使用了%,那麼包含這個like分句的查詢將會調用整表掃描。

4.這個..簡單的select * from table 結果這個是根據不同資料庫不同類型表而有所區別的,要分情況來說的,一般認為是記錄在資料庫裡面的物理位置吧,不過這樣答也不完全正確,表是分有索引和無索引的,不同類型在不同資料庫也有不同的處理方法 .
如果一次查詢出來多個數據集,那麼可以減少對資料庫的連接和查詢(這是很耗時的)

那麼如果你想要一次查詢出多個數據集,如果這些數據集與檢索出的一批數據相關,因此可能用臨時表將那些ID抽出來,然後再根據這些ID查其他的記錄。
這里需要臨時表。
臨時表一般存在tempdb里,不會有大問題的。

Ⅸ SQL題目求答案

/*創建Moonfox_db資料庫*/
use master
if exists(select * from sysdatabases where name='Moonfox_db')
drop database Moonfox_db
create database Moonfox_db
on
(
name='Moonfox_db_data',
filename='D:\Visual Studio 2008 & Sql server 2005\Sql server\Moonfox_db.mdf',
size=10,
filegrowth=2MB
)
log on
(
name='Moonfox_db_log',
filename='D:\Visual Studio 2008 & Sql server 2005\Sql server\Moonfox_db.ldf',
size=5,
filegrowth=20%
)/*創建Department表*/
use Moonfox_db
if exists(select * from sysobjects where name='Department')
drop table Department
create table Department
(
DID int identity (1,1)primary key,--部門編號,主鍵
Dname nvarchar(20),--部門名稱
Address nvarchar(50),--部門地址
Photo decimal(12,0),--電話
)/*創建Employee表*/
use Moonfox_db
if exists(select * from sysobjects where name='Employee')
drop table Employee
create table Employee
(
EID int identity (1,1)primary key,--職工編號,主鍵
Ename varchar(10),--職工名
Gender nchar(2) check(Gender='男' or Gender='女'),--性別,添加限制
Position nvarchar(10) check(Position='員工' or Position='組長' or Position='經理'),--職務,添加限制
Address nvarchar(50),--家庭地址
DID int,--部門編號,外鍵
foreign key(DID) references Department(DID)--外鍵約束
)
/*創建Care表*/
use Moonfox_db
if exists(select * from sysobjects where name='Care')
drop table Care
create table Care
(
CID int identity (1,1)primary key,--保健卡編號,主鍵
EID int,--職工號,外鍵
foreign key(EID) references Employee(EID),--外鍵約束
CheckDate datetime,--檢查身體日期
PhysicalCondition nvarchar(4) check(PhysicalCondition='一般' or PhysicalCondition='差' or PhysicalCondition='好'),--健康狀況
)
/*創建Care表約束*/
alter table Care
add
constraint DF_CheckDate default(getdate()) for CheckDate--預設,默認凈時間為當前計算機時間 路徑自己修改,試圖自己做,選擇語句自己寫。我該睡覺了,抱歉,你試著在sql server中運行下,我等著休息,也不知道寫的有沒有錯誤,沒時間幫你寫省下的了。不急著用的話我明天幫你寫吧。