当前位置:文档之家› SQL常见面试题

SQL常见面试题

SQL常见面试题
SQL常见面试题

SQL 常见面试题

1.用一条SQL语句查询出每门课都大于80分的学生姓名

name kecheng fenshu

张三语文81

张三数学75

李四语文76

李四数学90

王五语文81

王五数学100

王五英语90

A: select distinct name from table where name not in (select distinct name from table where fenshu<=80)

2.学生表如下:

自动编号学号姓名课程编号课程名称分数

1 2005001 张三0001 数学69

2 2005002 李四0001 数学89

3 2005001 张三0001 数学69

删除除了自动编号不同,其他都相同的学生冗余信息

A: delete tablename where 自动编号not in(select min(自动编号) from tablename group by 学号,姓名,课程编号,课程名称,分数)

3. 表A(单位名称,单位帐号), 表B(单位编号,个人账号)

列出各单位的名称,账号,以及单位的人数

select https://www.doczj.com/doc/b1332483.html,, A.dwzh, isnull(Ct.Quantity,'0') as Quantity from A

left join

(select dwzh, count(*) as Quantity from B

group by dwzh) as Ct

on A.dwzh = Ct.dwzh

4. 股票表(股票代码,买卖类型,数量)

按照股票代码列出,买的数量,卖的数量。

select isnull(a.StockID, b.StockID), isnull(a.S,'0'), isnull(b.B,'0') from (

select StockID,sum(quantity) as S from stocks

where sType = 's'

group by StockID ) a

full join (

select StockID,sum(quantity) as B from stocks

where sType = 'b'

group by StockID ) b

on a.StockID = b.StockID

5. select * from tempT where ','+ tempT.description + ',' like '%,1,%'

SQL Server 数据库的高级操作

(1) 批处理

(2) 变量

(3) 逻辑控制

(4) 函数

(5) 高级查询

*/

(1)批处理

将多条SQL语句作为一个整体去编译,生成一个执行计划,然后,执行!理解批处理的关键在于"编译",对于由多条语句组成的一个批处理,

如果在编译时,其中,有一条出现语法错误,将会导致编译失败!create table t

(

a int,

b int

)

-- 注释

-- 如果多行注释中包含了批处理的标识符go

-- 在编译的过程中代码将会被go分割成多个部分来分批编译

-- 多行注释的标记将会被分隔而导致编译出错

-- 以下几条语句是三个非常经典的批处理

-- 你猜一下会添加几条记录!

/*

insert into t values (1,1)

go

*/

insert into t values (2,2)

go

/*

insert into t values (3,3)

*/

go

-- 查询看添加了几条记录

select * from t

truncate table t

(2)变量

-- 全局变量

SQL Server中全局变量由系统定义、系统维护,用户一般仅可对其进行读取!-- 查看SQL Server版本

print @@version

-- 服务器名称

print @@servername

-- 系统错误编号

insert into t values ('a','a')

print @@error

insert into t values ('a','a')

if @@error = 245

print 'Error'

-- SQL Server 版本的语言信息

print @@LANGUAGE

-- 一周的第一天从星期几算起

print @@datefirst

-- CPU 执行命令所耗费时间的累加

print @@cpu_busy

-- 获取最近添加的标识列的值

create table tt

(

a int identity(3, 10),

b int

)

insert into tt (b) values (1)

print @@identity

select * from tt

-- 局部变量

局部变量由用户定义,仅可在同一个批处理中调用和访问

declare @intAge tinyint

set @intAge = 12

print @intAge

declare @strName varchar(12)

select @strName = 'state'

print @strName

select au_lname, @strName from authors

(3)逻辑控制

-- IF条件判断

declare @i int

set @i = 12

if (@i > 10)

begin -- {

print 'Dadadada!'

print 'Dadadada!'

end -- }

else

begin

print 'XiaoXiao!'

print 'XiaoXiao!'

end

-- While循环控制

declare @i int;

set @i = 12;

print @i

return;

while (@i < 18)

begin

print @i;

set @i = @i + 1;

if @i < 17

continue;

if @i > 15

break;

end;

-- CASE 分支判断

select au_lname, state, '犹他州' from authors where state = 'UT'

select au_lname, state, '密西西比州' from authors where state = 'MI' select au_lname, state, '肯塔基州' from authors where state = 'KS' select au_lname, state,

case state

when 'UT' then '犹他州'

when 'MI' then '密西西比州'

when 'KS' then '肯塔基州'

when 'CA' then '加利福利亚'

else state

end

from authors

(4.1)系统函数

-- 获取指定字符串中左起第一个字符的ASC码

print ascii('ABCDEF')

-- 根据给定的ASC码获取相应的字符

print char(65)

-- 获取给定字符串的长度

print len('abcdef')

-- 大小写转换

print lower('ABCDEF')

print upper('abcdef')

-- 去空格

print ltrim(' abcd dfd df ')

print rtrim(' abcd dfd df ')

-- 求绝对值

print abs(-12)

-- 幂

-- 3 的2 次方

print power(3,2)

print power(3,3)

-- 随机数

-- 0 - 1000 之间的随机数

print rand() * 1000

-- 获取圆周率

print pi()

-- 获取系统时间

print getdate()

-- 获取3天前的时间

print dateadd(day, -3 , getdate())

-- 获取3天后的时间

print dateadd(day, 3 , getdate())

-- 获取3年前的时间

print dateadd(year, -3 , getdate())

-- 获取3年后的时间

print dateadd(year, 3 , getdate())

-- 获取3月后的时间

print dateadd(month, 3 , getdate())

-- 获取9小时后的时间

print dateadd(hour, 9 , getdate())

-- 获取9分钟后的时间

print dateadd(minute, 9 , getdate())

-- 获取指定时间之间相隔多少年

print datediff(year, '2005-01-01', '2008-01-01')

-- 获取指定时间之间相隔多少月

print datediff(month, '2005-01-01', '2008-01-01')

-- 获取指定时间之间相隔多少天

print datediff(day, '2005-01-01', '2008-01-01')

-- 字符串合并

print 'abc' + 'def'

print 'abcder'

print 'abc' + '456'

print 'abc' + 456

-- 类型转换

print 'abc' + convert(varchar(10), 456)

select title_id, type, price from titles

-- 字符串连接必须保证类型一致(以下语句执行将会出错)

-- 类型转换

select title_id + type + price from titles

-- 正确

select title_id + type + convert(varchar(10), price) from titles

print '123' + convert(varchar(3), 123)

print '123' + '123'

print convert(varchar(12), '2005-09-01',110)

-- 获取指定时间的特定部分

print year(getdate())

print month(getdate())

print day(getdate())

-- 获取指定时间的特定部分

print datepart(year, getdate())

print datepart(month, getdate())

print datepart(day, getdate())

print datepart(hh, getdate())

print datepart(mi, getdate())

print datepart(ss, getdate())

print datepart(ms, getdate())

-- 获取指定时间的间隔部分

-- 返回跨两个指定日期的日期和时间边界数

print datediff(year, '2001-01-01', '2008-08-08')

print datediff(month, '2001-01-01', '2008-08-08')

print datediff(day, '2001-01-01', '2008-08-08')

print datediff(hour, '2001-01-01', '2008-08-08')

print datediff(mi, '2001-01-01', '2008-08-08')

print datediff(ss, '2001-01-01', '2008-08-08')

-- 在向指定日期加上一段时间的基础上,返回新的datetime 值print dateadd(year, 5, getdate())

print dateadd(month, 5, getdate())

print dateadd(day, 5, getdate())

print dateadd(hour, 5, getdate())

print dateadd(mi, 5, getdate())

print dateadd(ss, 5, getdate())

-- 其他

print host_id()

print host_name()

print db_id('pubs')

print db_name(5)

-- 利用系统函数作为默认值约束

drop table ttt

create table ttt

(

stu_name varchar(12),

stu_birthday datetime default (getdate())

)

alter table ttt

add constraint df_ttt_stu_birthday default (getdate()) for stu_birthday insert into ttt values ('ANiu', '2005-04-01')

insert into ttt values ('ANiu', getdate())

insert into ttt values ('AZhu', default)

sp_help ttt

select * from ttt

(4.2)自定义函数

select title_id

from titles

where type = 'business'

select stuff(title_id,1,3,'ABB'), type

from titles

where type = 'business'

select count(title_id) from titles where type = 'business'

select title_id from titles where type = 'business'

select *,count(dbo.titleauthor.title_id)

FROM dbo.authors INNER JOIN

dbo.titleauthor ON dbo.authors.au_id = dbo.titleauthor.au_id

select au_id, count(title_id)

from titleauthor

group by au_id

SELECT dbo.authors.au_id, COUNT(dbo.titleauthor.title_id) AS '作品数量' FROM dbo.authors left outer JOIN

dbo.titleauthor ON dbo.authors.au_id = dbo.titleauthor.au_id GROUP BY dbo.authors.au_id

order by '作品数量'

-- 自定义函数的引子(通过这个子查询来引入函数的作用)

-- 子查询

-- 统计每个作者的作品数

-- 将父查询中的作者编号传入子查询

-- 作为查询条件利用聚合函数count统计其作品数量

select au_lname,

(select count(title_id)

from titleauthor as ta

where ta.au_id = a.au_id

) as TitleCount

from authors as a

order by TitleCount

-- 是否可以定义一个函数

-- 将作者编号作为参数统计其作品数量并将其返回

select au_id, au_lname, dbo.GetTitleCountByAuID(au_id) as TitleCount from authors

order by TitleCount

-- 根据给定的作者编号获取其相应的作品数量

create function GetTitleCountByAuID(@au_id varchar(12))

returns int

begin

return (select count(title_id)

from titleauthor

where au_id = @au_id)

end

-- 利用函数来显示每个作者的作品数量

create proc pro_CalTitleCount

as

select au_id, au_lname, dbo.GetTitleCountByAuID(au_id) as TitleCount from authors

order by TitleCount

go

-- 执行存储过程

execute pro_CalTitleCount

-- vb中函数定义格式

function GetTitleCountByAuID(au_id as string) as integer

.......

GetTitleCountByAuID = ?

end function

-- SALES 作品销售信息

select * from sales

-- 根据书籍编号查询其销售记录(其中,qty 表示销量)

select * from sales where title_id = 'BU1032'

-- 根据书籍编号统计其总销售量(其中,qty 表示销量)

select sum(qty) from sales where title_id = 'BU1032'

-- 利用分组语句(group by),根据书籍编号统计每本书总销售量(其中,qty 表示销量)select title_id, sum(qty) from sales group by title_id

-- 是否可以考虑定义一个函数根据书籍编号来计算其总销售量

-- 然后,将其应用到任何一条包含了书籍编号的查询语句中

select title_id, title, dbo.GetTotalSaleByTitleID(title_id) as TotalSales

from titles

order by TotalSales

-- 定义一个函数根据书籍编号来计算其总销售量

create function GetTotalSaleByTitleID(@tid varchar(24))

returns int

begin

return(select sum(qty) from sales where title_id = @tid)

end

-- 统计书籍销量的前10位

-- 其中,可以利用函数计算结果的别名作为排序子句的参照列

select top 10 title_id, title, dbo.GetTotalSaleByTitleID(title_id) as TotalSales

from titles

order by TotalSales desc

-- 根据书籍编号计算其销量排名

create function GetTheRankOfTitle(@id varchar(20))

returns int

begin

return(select count(TotalSales)

from titles

where ToalSales >(

select TotalSales

from titles

where title_id=@id))

end

-- 根据书籍编号计算其销量排名

select dbo.GetTheRankOfTitle('pc1035') from titles

select count(title_id) + 1

from titles

where dbo.GetTotalSaleByTitleID(title_id) > dbo.GetTotalSaleByTitleID('pc1035') -- 删除函数

drop function GetRankByTitleId

-- 根据书籍编号计算其销量排名

create function GetRankByTitleId(@tid varchar(24))

returns int

begin

return (select count(title_id) + 1

from titles

where dbo.GetTotalSaleByTitleID(title_id) > dbo.GetTotalSaleByTitleID(@tid)) end

-- 在查询语句中利用函数统计每本书的总销量和总排名

select title_id, title,

dbo.GetTotalSaleByTitleID(title_id) as TotalSales,

dbo.GetRankByTitleId(title_id) as TotalRank

from titles

order by TotalSales desc

-- 查看表结构

sp_help titles

-- 查看存储过程的定义内容

sp_helptext GetRankByTitleId

sp_helptext sp_helptext

sp_helptext xp_cmdshell

-- [ORDER DETAILS] 订单详细信息

select * from [order details]

select * from [order details] where productid = 23

-- 根据产品编号在订单详细信息表中统计总销售量

select sum(quantity) from [order details] where productid = 23

-- 构造一个函数根据产品编号在订单详细信息表中统计总销售量

create function GetTotalSaleByPID(@Pid varchar(12))

returns int

begin

return(select sum(quantity) from [order details] where productid = @Pid)

end

select * from products

-- 在产品表中查询,统计每一样产品的总销量

select productid, productname, dbo.GetTotalSaleByPID(productid) from products

--

CREATE FUNCTION LargeOrderShippers ( @FreightParm money )

RETURNS @OrderShipperTab TABLE

(

ShipperID int,

ShipperName nvarchar(80),

OrderID int,

ShippedDate datetime,

Freight money

)

AS

BEGIN

INSERT @OrderShipperTab

SELECT S.ShipperID, https://www.doczj.com/doc/b1332483.html,panyName,

O.OrderID, O.ShippedDate, O.Freight

FROM Shippers AS S INNER JOIN Orders AS O

ON S.ShipperID = O.ShipVia

WHERE O.Freight > @FreightParm

RETURN

END

SELECT * FROM LargeOrderShippers( $500 )

-- 根据作者编号计算其所得版权费

create function fun_RoyalTyper ( @au_id id)

as

begin

declare @rt int

select @rt = sum(royaltyper) from titleauthor where au_id = @au_id

return (@rt)

end

go

select top 1 au_lname, au_fname, dbo.fun_RoyalTyper(au_id) as '版权费' from authors

order by dbo.fun_RoyalTyper(au_id) desc

go

create function fun_MaxRoyalTyper_Au_id ()

returns id

as

begin

declare @au_id id

select @au_id = au_id

from authors

order by dbo.fun_RoyalTyper(au_id)

return(@au_id)

end

go

select dbo.fun_MaxRoyalTyper_Au_id()

go

select au_lname, au_fname, dbo.fun_RoyalTyper(au_id) as '版权税'

from authors

where au_id = dbo.fun_MaxRoyalTyper_Au_id()

go

(5)高级查询

select title_id, price from titles

-- 查找最高价格

select max(price) from titles

-- 查找最贵书籍的价格(排序),如果存在多本价格最贵的书,此方法将会遗漏select top 1 title_id, price

order by price desc

-- 查找最贵书籍的价格(子查询)

select title_id, price

from titles

where price = (select max(price) from titles)

-- 查询指定出版社出版的书(连接)

select p.pub_name as '出版社', t.title as '书籍名称'

from publishers as p join titles as t on p.pub_id = t.pub_id

where pub_name = 'New Moon Books'

-- 查询指定出版社出版的书(子查询)

select title

from titles

where pub_id = (select pub_id

from publishers

where pub_name = 'New Moon Books')

-- 查询指定出版社出版的书(分开查询)

select title from titles where pub_id = '0736'

select pub_id

from publishers

where pub_name = 'New Moon Books'

-- 重点

-- 理解相关子查询的基础

--

select * from titles where type = 'business'

select * from titles where type = 'business123'

select * from titles where 1 = 1

-- 在订单表中寻找满足以下条件的订单编号以及相应的客户编号

-- 在详细订单表中存在对应的订单编号并且其中包含产品编号为23的产品-- 然后将产品编号为23的产品订购量返回判断是否大于20

USE northwind

SELECT orderid, customerid

FROM orders AS or1

WHERE 20 < (SELECT quantity FROM [order details] AS od

WHERE or1.orderid = od.orderid

AND od.productid = 23)

GO

SELECT au_lname, au_fname

FROM authors

WHERE 100 IN

(

SELECT royaltyper FROM titleauthor

WHERE titleauthor.au_ID = authors.au_id

)

select authors.au_lname,authors.au_fname

from authors join titleauthor on titleauthor.au_ID=authors.au_id where titleauthor.royaltyper =100

USE pubs

SELECT au_lname, au_fname

FROM authors

WHERE au_id IN

(SELECT au_id

FROM titleauthor

WHERE title_id IN

(SELECT title_id

FROM titles

WHERE type = 'popular_comp'))

select distinct t.type, a.au_lname, a.au_fname

from authors as a join titleauthor as ta on a.au_id = ta.au_id

join titles as t on ta.title_id = t.title_id

where t.type = 'business'

-- 查找类型为'business'或是'trad_cook'类型的书籍

select * from titles where type = 'business'

select * from titles where type = 'trad_cook'

-- 查找类型为'business'或是'trad_cook'类型的书籍(Or)

select * from titles

where type = 'business' or type = 'trad_cook'

-- 查找类型为'business'或是'trad_cook'类型的书籍(In)

select * from titles

where type in ('business', 'trad_cook')

-- 查找来自'KS'或是'UT'的作者

select au_lname, state from authors

where state = 'KS'

select au_lname, state from authors

where state = 'UT'

-- 查找来自'KS'或是'UT'的作者(Or)

select au_lname, state from authors

where state = 'UT' or state = 'KS'

-- 查找来自'KS'或是'UT'的作者(In)

select au_lname, state from authors

where state in ('UT', 'KS')

select au_lname, state from authors

where state not in ('UT', 'KS')

-- 查找出版了类型为'business'类型的书籍的出版社

SELECT pub_id FROM titles WHERE type = 'business' SELECT pub_id,pub_name

FROM publishers

WHERE pub_id IN ('1389', '0736')

-- 查找出版了类型为'business'类型的书籍的出版社(In和子查询) SELECT pub_id,pub_name

FROM publishers

WHERE pub_id IN

(SELECT pub_id

FROM titles

WHERE type = 'business')

SELECT title, advance

FROM titles

WHERE advance >

(

SELECT MAX(advance)

FROM publishers INNER JOIN titles ON

titles.pub_id = publishers.pub_id

WHERE pub_name = 'Algodata Infosystems'

)

SELECT title, advance

FROM titles

WHERE advance > all

(

SELECT advance

FROM publishers INNER JOIN titles ON

titles.pub_id = publishers.pub_id

WHERE pub_name = 'Algodata Infosystems'

and advance is not null

)

declare @i int

set @i = 12

if @i < null

print 'DDDDD'

else

print 'XXXXX'

SELECT advance

FROM publishers INNER JOIN titles ON

titles.pub_id = publishers.pub_id

WHERE pub_name = 'Algodata Infosystems'

select title_id, price from titles

where price > all

(

select price from titles where type = 'business'

)

select title_id, price from titles

where price >

(

select max(price) from titles where type = 'business' )

select title_id, price from titles

where price > any

(

select price from titles where type = 'business'

)

select title_id, price from titles

where price >

(

select min(price) from titles where type = 'business'

)

select price from titles where type = 'business'

if exists(select * from titles where type = '123')

print 'ZZZZZ'

else

print 'BBBBB'

if exists(select * from authors

where city = 'Berkeley' and state ='UT')

print 'Welcome'

else

print 'Bye-Bye'

-- 筛选出'business'以及'trad_cook'类型的书籍(联合查询) select title_id, type from titles where type = 'business'

union

select title_id, type from titles where type = 'trad_cook'

-- 统计'business'类型的书籍的总价(联合查询)

select title, price from titles where type = 'business'

union

select '合计:', sum(price) from titles where type = 'business'

-- 统计所有书籍的类型剔除重复(Distinct)

select distinct type from titles

-- 作者记录的复制(Select Into)

select * into au from authors

select * from au

-- 查看数据表结构(Select Into并没有对数据表的约束进行复制) sp_help authors

sp_help au

-- 分页(子查询的经典应用之一)

-- Jobs 职务信息表(pubs 数据库)

-- 在实际项目中,显示职务信息时,而职务信息量非常庞大,可能需要将其分为若干个页面来显示

-- 比如:每页显示4条记录,那么,第一页将显示1,2,3,4,第二页将显示5,6,7,8。。。。。-- 显示所有信息

SELECT * FROM jobs

-- 显示前4 信息

select top 4 * from jobs

-- 显示前8 信息

select top 8 * from jobs

-- 显示前12 信息

select top 12 * from jobs

-- 寻找规律,每一页的信息源于前(页面大小* 页码)条信息的反序结果的前页面大小条记录

-- 比如:第二页就是前8 条记录的反序结果的前4 条

select top 4 *

from (select top 8 * from jobs) as tt

order by job_id desc

-- 当然,对于期望按升序显示查询结果的要求可以对查询结果进行再次排序

select * from

(select top 4 *

from (select top 8 * from jobs) as tt

order by job_id desc) as stt

order by job_id

-- SQL 命令中不支持在select 的查询列表中直接使用局部变量

-- 比如:select top @PageSize * from jobs

-- 那么,可以考虑对sql命令进行拼装,然后,利用系统存储过程sp_executesql 来执行exec sp_executesql N'Select * from jobs'

-- 存储过程的实现

-- 其中,@CurrentPageSize用于确定最后一页的页面大小

create proc proGetJobsByPage

@CurrentPageSize int,

@PageSize int,

@CurrentPage int

Declare @strSql nvarchar(400)

set @strSql = 'select * from

(select top ' + convert(nvarchar(4), @CurrentPageSize) + ' *

from (select top ' + convert(nvarchar(4),(@PageSize * @CurrentPage)) + ' * from jobs) as tt order by job_id desc) as stt

order by job_id'

exec sp_executesql @strSql

go

-- 测试

exec proGetJobsByPage 2, 4, 4

(6)存储过程

-- 扩展存储过程

-- 查询系统目录下文件信息

xp_cmdshell 'dir *.*'

-- 启动Windows系统服务

xp_cmdshell 'net start iisadmin'

(7)游标

-- 游标的五个基本操作步骤:

-- 声明

declare cur_titles cursor

for select title, price from titles

-- 打开

open cur_titles

-- 提取

fetch cur_titles

fetch next from cur_titles

-- 关闭

close cur_titles

-- 释放

deallocate cur_titles

-- 利用游标遍历所有书籍信息,通过冒泡排序法进行比较,找出最高价格的书

sql数据库基础面试题复习试题考试题_全

不定项选择题(针对以下题目,请选择最符合题目要求的答案,每道题有一项或二项正确答案。针对每一道题目,所有答案都选对,则该题得分,所选答案错误或不能选出所有答案,则该题不得分。题量为50道,每题2分,总分为100分。) 第一章 1、是SQLServer数据库的主数据文件的扩展名。(选择一项) A、.sql B、.mdb C、.ldf D、.mdf 2、在SQL Server 2005中,有系统数据库和用户数据库,下列不属于系统数据库的是()。 (选择一项) A、master B、pubs C、model D、msdb 3、当安装完SQL Server2005数据库时,系统默认当前的超级管理员是( ) (选择一项) A、sa B、master C、administrator D、super 4、在使用SQL Server2005数据库时,有时需要将本机的数据库移动到其他机器上,恢复成对应的数据库使用。移动数据库分两步进行,应包括()和附加数据库(选择一项)A、分离数据库 B、删除数据库 C、新建数据库 D、合并数据库 5、在SQL Server2005中,附加数据库操作是指()(选择一项) A、把SQL Server 数据库文件保存为其他数据文件 B、根据数据库物理文件中的信息,把数据库在SQL Server 2005中恢复 C、把所有该数据库表的数据清空 D、把数据库删除掉 6、某单位由不同的部门组成,不同的部门每天都会生产一些报告、报表等数据,以为都采用纸张的形式来进行数据的保存和分类,随着业务的发展,这些数据越来越多,管理这些报告越来越费力,此时应考虑()(选择一项) A、由多个人来完成这些工作 B、在不同的部门中,由专门的人员去管理这些数据 C、采用数据库系统来管理这些数据 D、把这些数据统一成一样的格式 7、在SQL Server 2005中,对于数据库的定义正确的是()(选择一项) A、数据库是用来描述事物的符号记录 B、数据库是位于用户与操作系统之间的一层数据管理软件

Sql常见面试题 受用了

Sql常见面试题受用了 1. 用一条SQL 语句查询出每门课都大于80 分的学生姓名 name kecheng fenshu 张三语文81 张三数学75 李四语文76 李四数学90 王五语文81 王五数学100 王五英语90 A: select distinct name from table where name not in (select distinct name from table where fenshu<=80) select name from table group by name having min(fenshu)>80 2. 学生表如下: 自动编号学号姓名课程编号课程名称分数 1 2005001 张三0001 数学69 2 2005002 李四0001 数学89 3 2005001 张三0001 数学69 删除除了自动编号不同, 其他都相同的学生冗余信息 A: delete tablename where 自动编号not in(select min( 自动编号) from tablename group by 学号, 姓名, 课程编号, 课程名称, 分数) 3. 一个叫team 的表,里面只有一个字段name, 一共有4 条纪录,分别是a,b,c,d, 对应四个球对,现在四个球对进行比赛,用一条sql 语句显示所有可能的比赛组合. 你先按你自己的想法做一下,看结果有我的这个简单吗? 答:select https://www.doczj.com/doc/b1332483.html,, https://www.doczj.com/doc/b1332483.html, from team a, team b where https://www.doczj.com/doc/b1332483.html, < https://www.doczj.com/doc/b1332483.html, 4. 请用SQL 语句实现:从TestDB 数据表中查询出所有月份的发生额都比101 科目相应月份的发生额高的科目。请注意:TestDB 中有很多科目,都有1 -12 月份的发生额。AccID :科目代码,Occmonth :发生额月份,DebitOccur :发生额。 数据库名:JcyAudit ,数据集:Select * from TestDB 答:select a.* from TestDB a ,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) b where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur ******************************************************************************* ***** 5. 面试题:怎么把这样一个表儿 year month amount 1991 1 1.1

SQL经典面试题及答案

SQL经典面试题及答案 1.一道SQL语句面试题,关于group by 表内容: 2005-05-09 胜 2005-05-09 胜 2005-05-09 负 2005-05-09 负 2005-05-10 胜 2005-05-10 负 2005-05-10 负 如果要生成下列结果, 该如何写sql语句? 胜负 2005-05-09 2 2 2005-05-10 1 2 ------------------------------------------ create table #tmp(rq varchar(10),shengfu nchar(1)) insert into #tmp values('2005-05-09','胜') insert into #tmp values('2005-05-09','胜') insert into #tmp values('2005-05-09','负')

insert into #tmp values('2005-05-09','负') insert into #tmp values('2005-05-10','胜') insert into #tmp values('2005-05-10','负') insert into #tmp values('2005-05-10','负') 1)select rq, sum(case when shengfu='胜' then 1 else 0 end)'胜',sum (case when shengfu='负' then 1 else 0 end)'负' from #tmp group by rq 2) select N.rq,N.勝,M.負 from ( select rq,勝=count(*) from #tmp where shengfu='胜'group by rq)N inner join (select rq,負=count(*) from #tmp where shengfu='负'group by rq)M on N.rq=M.rq 3)select a.col001,a.a1 胜,b.b1 负 from (select col001,count(col001) a1 from temp1 where col002='胜'

常见SQL笔试题

精心整理 SQL笔试题 1.统计查询SQL练习 数据库中表结构如下,字段分别任rg(日期),shengfu(胜负),考察groupby语句的使用:2005-05-09胜 2005-05-09胜 2005-05-09负 2005-05-09负 2005-05-10胜 2005-05-10负 2005-05-10负 如果要生成下列结果,该如何写sql语句? 胜负 2005-05-0922 2005-05-1012 答案: 1)selectrq,sum(casewhenshengfu='胜'then1else0end)'胜',sum(casewhenshengfu='负' then1else0end)'负'from#tmpgroupbyrq 2)selectN.rq,N.胜,M.负from( selectrq,胜=count(*)from#tmpwhereshengfu='胜'groupbyrq)Ninnerjoin (selectrq,负=count(*)from#tmpwhereshengfu='负'groupbyrq)MonN.rq=M.rq 3)selecta.col001,a.a1胜,b.b1负from (selectcol001,count(col001)a1fromtemp1wherecol002='胜'groupbycol001)a,

(selectcol001,count(col001)b1fromtemp1wherecol002='负'groupbycol001)b wherea.col001=b.col001 2.条件判断SQL练习 表中有ABC三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列 答案: select(casewhena>bthenaelsebend), (casewhenb>cthenbeslecend) fromtable_name 3.日期统计SQL练习 请取出tb_send表中日期(SendTime字段)为当天的所有记录?(SendTime字段为datetime型,包含日期与时间) 答案: select*fromtbwheredatediff(dd,SendTime,getdate())=0 4.统计查询SQL练习 有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70 分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路): 大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。 显示格式: 语文数学英语

(完整版)常见SQL笔试题

SQL笔试题 1.统计查询SQL练习 数据库中表结构如下,字段分别任rg(日期),shengfu(胜负),考察group by 语句的使用: 2005-05-09 胜 2005-05-09 胜 2005-05-09 负 2005-05-09 负 2005-05-10 胜 2005-05-10 负 2005-05-10 负 如果要生成下列结果, 该如何写sql 语句? 胜负 2005-05-09 2 2 2005-05-10 1 2 答案: 1)select rq, sum(case when shengfu='胜' then 1 else 0 end)'胜',sum(case when shengfu='负' then 1 else 0 end)'负' from #tmp group by rq 2) select N.rq,N.胜,M.负from ( select rq,胜=count(*) from #tmp where shengfu='胜'group by rq)N inner join

(select rq,负=count(*) from #tmp where shengfu='负'group by rq)M on N.rq=M.rq 3)select a.col001,a.a1 胜,b.b1 负from (select col001,count(col001) a1 from temp1 where col002='胜' group by col001) a, (select col001,count(col001) b1 from temp1 where col002='负' group by col001) b where a.col001=b.col001 2.条件判断SQL练习 表中有A B C 三列,用SQL 语句实现:当A 列大于B 列时选择A 列否则选择B 列, 当B 列大于C 列时选择B 列否则选择C 列 答案: select (case when a>b then a else b end ), (case when b>c then b esle c end) from table_name 3.日期统计SQL练习 请取出tb_send 表中日期(SendTime 字段) 为当天的所有记录?(SendTime 字段为 datetime 型,包含日期与时间)

SQL经典面试题及答案

1. 用一条SQL 语句查询出每门课都大于80 分的学生姓名 name kechengfenshu 张三语文81 张三数学75 李四语文76 李四数学90 王五语文81 王五数学100 王五英语90 A: select distinct name from table where name not in (select distinct name from table where fenshu<=80) select name from table group by name having min(fenshu)>80 select name from table group by name having count(kecheng)>=3 and min(fenshu)>=80 2. 学生表如下: 自动编号学号姓名课程编号课程名称分数 1 2005001 张三0001 数学69 2 2005002 李四0001 数学89 3 2005001 张三0001 数学69 删除除了自动编号不同, 其他都相同的学生冗余信息 A: delete tablename where 自动编号not in(select min( 自动编号) from tablename group by 学号, 姓名, 课程编号, 课程名称, 分数) 3. 面试题:怎么把这样一个表儿 year month amount 1991 1 1.1 1991 2 1.2 1991 3 1.3 1991 4 1.4 1992 1 2.1 1992 2 2.2 1992 3 2.3 1992 4 2.4 查成这样一个结果 year m1 m2 m3 m4 1991 1.1 1.2 1.3 1.4 1992 2.1 2.2 2.3 2.4

sql经典笔试题目(整理)

一单词解释(2分/个) 34 Data 数据 Database 数据库 RDBMS 关系数据库管理系统 GRANT 授权 REVOKE取消权限 DENY 拒绝权限 DECLARE 定义变量 PROCEDURE存储过程 Transaction事务 TRIGGER触发器继续 continue 唯一 unqiue 主键 primary key 标识列 identity 外键 foreign kdy 检查 check 约束 constraint 二编写SQL语句(5分/题)50 1) 创建一张学生表,包含以下信息,学号,姓名,年龄,性别,家庭住址,联系电话Create table stu (学号 int , 姓名 varchar(8), 年龄 int, 性别 varchar(4), 家庭地址 varchar(50), 联系电话 int ); 2) 修改学生表的结构,添加一列信息, 学历 Alter table stu add 学历 varchar(6); 3) 修改学生表的结构,删除一列信息,家庭住址 Alter table stu drop column 家庭地址 4) 向学生表添加如下信息: 学号姓名年龄性别联系电话学历 1 A 22 男 123456 小学 2 B 21 男 119 中学 3 C 23 男 110 高中 4 D 18 女 114 大学 Insert into stu values('1', 'A', '22', '男' , ' 123456', '小学') Insert into stu values('2', 'B', '21', '男', '119' , '中学') Insert into stu values('3', 'C', '23', '男', '110', '高中') Insert into stu values('4' , 'D', '18', '女', '114', '大学') 5) 修改学生表的数据,将电话号码以11开头的学员的学历改为“大专” Update stu set 学历=’大专’ where 联系电话 like ‘11%’ 6) 删除学生表的数据,姓名以C开头,性别为‘男’的记录删除 Delect from stu where 性别=’男’ and 姓名 like ‘c%’ 7) 查询学生表的数据,将所有年龄小于22岁的,学历为“大专”的,学生的姓名和学号示出来 Select 姓名,学号 from stu where 年龄<22 and 学历=’大专’ 8) 查询学生表的数据,查询所有信息,列出前25%的记录 Select top 25 percent * from stu 9) 查询出所有学生的姓名,性别,年龄降序排列

SQL常见面试题集(三)

SQL试题集(三) 1.用一条SQL语句 查询出每门课都大于80分的学生姓名  name kecheng fenshu 张三 语文 81 张三 数学 75 李四 语文 76 李四 数学 90 王五 语文 81 王五 数学 100 王五 英语 90 A: select distinct name from table where name not in (select distinct name from table where fenshu<=80) 2.学生表 如下: 自动编号 学号 姓名 课程编号 课程名称 分数 1 2005001 张三 0001 数学 69 2 2005002 李四 0001 数学 89 3 2005001 张三 0001 数学 69 删除除了自动编号不同,其他都相同的学生冗余信息 A: delete tablename where 自动编号 not in(select min(自动编号) from tablename group by 学号,姓名,课程编号,课程名称,分数) 一个叫department的表,里面只有一个字段name,一共有4条纪录,分别是a,b,c,d,对应四个球对,现在四个球对进行比赛,用一条sql语句显示所有可能的比赛组合. 你先按你自己的想法做一下,看结果有我的这个简单吗? 答:select https://www.doczj.com/doc/b1332483.html,, https://www.doczj.com/doc/b1332483.html, from team a, team b where https://www.doczj.com/doc/b1332483.html, < https://www.doczj.com/doc/b1332483.html, 请用SQL语句实现:从TestDB数据表中查询出所有月份的发生额都比101

科目相应月份的发生额高的科目。请注意:TestDB中有很多科目,都有 1-12月份的发生额。 AccID:科目代码,Occmonth:发生额月份,DebitOccur:发生额。 数据库名:JcyAudit,数据集:Select * from TestDB 答:select a.* from TestDB a ,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) b where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur ************************************************************************面试题:怎么把这样一个表儿 year month amount 1991 1 1.1 1991 2 1.2 1991 3 1.3 1991 4 1.4 1992 1 2.1 1992 2 2.2 1992 3 2.3 1992 4 2.4 查成这样一个结果 year m1 m2 m3 m4 1991 1.1 1.2 1.3 1.4 1992 2.1 2.2 2.3 2.4 答案一、 select year, (select amount from aaa m where month=1 and m.year=aaa.year) as m1, (select amount from aaa m where month=2 and m.year=aaa.year) as m2, (select amount from aaa m where month=3 and m.year=aaa.year) as m3,

Sql常见面试题

(资料来源:互联网) Sql常见面试题(总结) 1.用一条SQL语句查询出每门课都大于80分的学生姓名 name kecheng fenshu 张三语文 81 张三数学 75 李四语文 76 李四数学 90 王五语文 81 王五数学 100 王五英语 90 A: select distinct name from table where name not in (select distinct name fr om table where fenshu<=80) 2.学生表如下: 自动编号学号姓名课程编号课程名称分数 1 2005001 张三 0001 数学 69 2 2005002 李四 0001 数学 89 3 2005001 张三 0001 数学 69 删除除了自动编号不同,其他都相同的学生冗余信息 A: delete tablename where 自动编号 not in(select min(自动编号) from tablename g roup by 学号,姓名,课程编号,课程名称,分数) 一个叫department的表,里面只有一个字段name,一共有4条纪录,分别是a,b,c,d,对应四个球对,现在四个球对进行比赛,用一条sql语句显示所有可能的比赛组合. 你先按你自己的想法做一下,看结果有我的这个简单吗? 答:select https://www.doczj.com/doc/b1332483.html,, https://www.doczj.com/doc/b1332483.html, from team a, team b where https://www.doczj.com/doc/b1332483.html, < https://www.doczj.com/doc/b1332483.html,

请用SQL语句实现:从TestDB数据表中查询出所有月份的发生额都比101科目相应月份的发生额高的科目。请注意:TestDB中有很多科目,都有1-12月份的发生额。 AccID:科目代码,Occmonth:发生额月份,DebitOccur:发生额。 数据库名:JcyAudit,数据集:Select * from TestDB 答:select a.* from TestDB a ,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) b where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur ******************************************************************************* ***** 面试题:怎么把这样一个表儿 year month amount 1991 1 1.1 1991 2 1.2 1991 3 1.3 1991 4 1.4 1992 1 2.1 1992 2 2.2 1992 3 2.3 1992 4 2.4 查成这样一个结果 year m1 m2 m3 m4 1991 1.1 1.2 1.3 1.4 1992 2.1 2.2 2.3 2.4 答案一、 select year, (select amount from aaa m where month=1 and m.year=aaa.year) as m1, (select amount from aaa m where month=2 and m.year=aaa.year) as m2, (select amount from aaa m where month=3 and m.year=aaa.year) as m3, (select amount from aaa m where month=4 and m.year=aaa.year) as m4 from aaa group by year 这个是ORACLE 中做的: select * from (select name, year b1, lead(year) over (partition by name order by year) b2, lead(m,2) over(partition by name order by year) b3,rank()over( partition by name order by year) rk from t) where rk=1; ******************************************************************************* ***** 精妙的SQL语句! 精妙SQL语句

Sql面试题大全

Sql常见面试题受用了 1.用一条SQL 语句查询出每门课都大于80 分的学生姓名 name kecheng fenshu 张三语文 81 张三数学 75 李四语文 76 李四数学 90 王五语文 81 王五数学 100 王五英语 90 A: select distinct name from table where name not in (select distinct name from table where fenshu<=80) select name from table group by name having min(fenshu)>80 2.学生表如下: 自动编号学号姓名课程编号课程名称分数 1 2005001 张三 0001 数学 69 2 2005002 李四 0001 数学 89 3 2005001 张三 0001 数学 69 删除除了自动编号不同, 其他都相同的学生冗余信息 A: delete tablename where 自动编号 not in(select min( 自动编号) from tablename group by 学号, 姓名, 课程编号, 课程名称, 分数) 3.一个叫team 的表,里面只有一个字段name, 一共有4 条纪录,分别是 a,b,c,d, 对应四个球对,现在四个球对进行比赛,用一条sql 语句显示所有可能的比赛组合. 你先按你自己的想法做一下,看结果有我的这个简单吗?

答:select https://www.doczj.com/doc/b1332483.html,, https://www.doczj.com/doc/b1332483.html, from team a, team b where https://www.doczj.com/doc/b1332483.html, < https://www.doczj.com/doc/b1332483.html, 4.请用SQL 语句实现:从TestDB 数据表中查询出所有月份的发生额都比101 科目相应月份的发生额高的科目。请注意:TestDB 中有很多科目,都有1 -12 月份的发生额。 AccID :科目代码,Occmonth :发生额月份,DebitOccur :发生额。 数据库名:JcyAudit ,数据集:Select * from TestDB 答:select a.* from TestDB a ,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) b where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur ********************************************************************* *************** 5.面试题:怎么把这样一个表儿 year month amount 1991 1 1.1 1991 2 1.2 1991 3 1.3 1991 4 1.4 1992 1 2.1 1992 2 2.2 1992 3 2.3 1992 4 2.4 查成这样一个结果 year m1 m2 m3 m4 1991 1.1 1.2 1.3 1.4 1992 2.1 2.2 2.3 2.4

经典SQL面试题总结

表 Student(S#,Sname,Sage,Ssex) 学生表 CREATE TABLE student ( sid varchar(10) NOT NULL, sName varchar(20) DEFAULT NULL, sAge datetime DEFAULT '1980-10-12 23:12:36', sSex varchar(10) DEFAULT NULL, PRIMARY KEY (sid) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Course(C#,Cname,T#) 课程表 CREATE TABLE course ( cid varchar(10) NOT NULL, cName varchar(10) DEFAULT NULL, tid int(20) DEFAULT NULL, PRIMARY KEY (cid) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; SC(S#,C#,score) 成绩表 CREATE TABLE sc ( sid varchar(10) DEFAULT NULL, cid varchar(10) DEFAULT NULL, score int(10) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8; Teacher(T#,Tname) 教师表 CREATE TABLE teacher ( tid int(10) DEFAULT NULL, tName varchar(10) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 数据:(MySQL) insert into teacher(tid,tName) values (1,'李老师'),(2,'何以琛'),(3,'叶平'); insert into student(sid,sName,sAge,sSex) values ('1001','张三丰','1980-10-12 23:12:36','男'),('1002','张无极','1995-10-12 23:12:36','男'),('1003','李奎','1992-10-12 23:12:36','女'),('1004','李元宝','1980-10-12 23:12:36','女'),('1005','李世明','1981-10-12 23:12:36','男'),('1006','赵六','1986-10-12 23:12:36','男'),('1007','田七','1981-10-12 23:12:36','女'); insert into sc(sid,cid,score) values ('1','001',80),('1','002',60),('1','003',75),('2','001',85),('2','002',70),('3','004',100), ('3','001',90),('3','002',55),('4','002',65),('4','003',60); insert into course(cid,cName,tid) values ('001','企业管理',3),('002','马克思',3),('003','UML',2),('004','数据库',1),('005 ','英语',1); 1、查询“001”课程比“002”课程成绩高的所有学生的学号; select a.S# from (select s#,score from SC where C#='001') a,(select s#,score from SC where C#='002') b where a.score>b.score and a.s#=b.s#; 2、查询平均成绩大于60分的同学的学号和平均成绩;

经典的SQL语句面试题

经典的SQL语句面试题 from (select s#,score from SC where C#= 001 ) a, (select s#,score from SC where C#= 002 ) b where a.score>b.score and a.s#=b.s#; 2、查询平均成绩大于60分的同学的学号和平均成绩;select S#,avg(score) from sc group by S# having avg(score) >60; 3、查询所有同学的学号、姓名、选课数、总成绩; select Student.S#,Student.Sname,count(SC.C#),sum(score) from Student left Outer join SC on Student.S#=SC.S# group by Student.S#,Sname 4、查询姓“李”的老师的个数; select count(distinct(Tname)) from Teacher where Tname like 李% 5、查询没学过“叶平”老师课的同学的学号、姓名; select Student.S#,Student.Sname from Student where S# not in (select distinct( SC.S#) from

SC,Course,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname= 叶平); 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; select Student.S#,Student.Sname from Student,SC where Student.S#=SC.S# and SC.C#= 001 and exists( Select * from SC as SC_2 where SC_2.S#=SC.S# and SC_2.C#= 002 ); 7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; select S#,Sname from Student where S# in (select S# from SC ,Course ,T eacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname= 叶平group by S# having count(SC.C#)=(select count(C#) from Course,Teacher where Teacher.T#=Course.T# and Tname= 叶平)); 8、查询所有课程成绩小于60分的同学的学号、姓名; select S#,Sname from Student

2021年SQL经典面试题及答案

1. 用一条SQL 语句查询出每门课都不不大于80 分学生姓名 name kecheng fenshu 张三语文81 张三数学75 李四语文76 李四数学90 王五语文81 王五数学100 王五英语90 A:select distinct name from table where name not in (select distinct name from table where fenshu<=80) select name from table group by name having min(fenshu)>80 select name from table group by name having count(kecheng)>=3 and min(fenshu)>=80 2. 学生表如下: 自动编号学号姓名课程编号课程名称分数 1 001 张三0001 数学69 2 002 李四0001 数学89 3 001 张三0001 数学69 删除除了自动编号不同,其她都相似窗生冗余信息

A:delete tablename where 自动编号not in(select min( 自动编号) from tablename group by 学号,姓名,课程编号,课程名称,分数) 3. 面试题:怎么把这样一种表儿 year month amount 1991 1 1.1 1991 2 1.2 1991 3 1.3 1991 4 1.4 1992 1 2.1 1992 2 2.2 1992 3 2.3 1992 4 2.4 查成这样一种成果 year m1 m2 m3 m4 1991 1.1 1.2 1.3 1.4 1992 2.1 2.2 2.3 2.4 答案一、 select year, (select amount from aaa m where month=1 and m.year=aaa.year) as m1, (select amount from aaa m where month=2 and m.year=aaa.year) as m2, (select amount from aaa m where month=3 and m.year=aaa.year) as m3, (select amount from aaa m where month=4 and m.year=aaa.year) as m4 from aaa group by year

SQL基础面试题

SQL面试题(3) 1.触发器的作用? 答:触发器是一中特殊的存储过程,主要是通过事件来触发而被执行的。它可以强化约束,来维护数据的完整性和一致性,可以跟踪数据库内的操作从而不允许未经许可的更新和变化。可以联级运算。如,某表上的触发器上包含对另一个表的数据操作,而该操作又会导致该表触发器被触发。2。什么是存储过程?用什么来调用? 答:存储过程是一个预编译的SQL语句,优点是允许模块化的设计,就是说只需创建一次,以后在该程序中就可以调用多次。如果某次操作需要执行多次SQL,使用存储过程比单纯SQL语句执行要快。可以用一个命令对象来调用存储过程。 3。索引的作用?和它的优点缺点是什么? 答:索引就一种特殊的查询表,数据库的搜索引擎可以利用它加速对数据的检索。它很类似与现实生活中书的目录,不需要查询整本书内容就可以找到想要的数据。索引可以是唯一的,创建索引允许指定单个列或者是多个列。缺点是它减慢了数据录入的速度,同时也增加了数据库的尺寸大小。3。什么是内存泄漏? 答:一般我们所说的内存泄漏指的是堆内存的泄漏。堆内存是程序从堆中为其分配的,大小任意的,使用完后要显示释放内存。当应用程序用关键字new等创建对象时,就从堆中为它分配一块内存,使用完后程序调

用free或者delete释放该内存,否则就说该内存就不能被使用,我们就说该内存被泄漏了。 4。维护数据库的完整性和一致性,你喜欢用触发器还是自写业务逻辑?为什么? 答:我是这样做的,尽可能使用约束,如check,主键,外键,非空字段等来约束,这样做效率最高,也最方便。其次是使用触发器,这种方法可以保证,无论什么业务系统访问数据库都可以保证数据的完整新和一致性。最后考虑的是自写业务逻辑,但这样做麻烦,编程复杂,效率低下。5。什么是事务?什么是锁? 答:事务就是被绑定在一起作为一个逻辑工作单元的SQL语句分组,如果任何一个语句操作失败那么整个操作就被失败,以后操作就会回滚到操作前状态,或者是上有个节点。为了确保要么执行,要么不执行,就可以使用事务。要将有组语句作为事务考虑,就需要通过ACID测试,即原子性,一致性,隔离性和持久性。 锁:在所以的DBMS中,锁是实现事务的关键,锁可以保证事务的完整性和并发性。与现实生活中锁一样,它可以使某些数据的拥有者,在某段时间内不能使用某些数据或数据结构。当然锁还分级别的。 6。什么叫视图?游标是什么? 答:视图是一种虚拟的表,具有和物理表相同的功能。可以对视图进行增,改,查,操作,试图通常是有一个表或者多个表的行或列的子集。对视图的修改不影响基本表。它使得我们获取数据更容易,相比多表查询。

经典的sql数据库面试题以及答案

Student(S#,Sname,Sage,Ssex) 学生表 S#:学号;Sname:学生姓名;Sage:学生年龄;Ssex:学生性别 Course(C#,Cname,T#) 课程表 C#,课程编号;Cname:课程名字;T#:教师编号 SC(S#,C#,score) 成绩表 S#:学号;C#,课程编号;score:成绩 Teacher(T#,Tname) 教师表 T#:教师编号; Tname:教师名字 问题: 1、查询“001”课程比“002”课程成绩高的所有学生的学号; select a.S# from (select s#,score from SC where C#='001') a,(select s#,score from SC where C#='002') b where a.score>b.score and a.s#=b.s#; 2、查询平均成绩大于60分的同学的学号和平均成绩; select S#,avg(score) from sc group by S# having avg(score) >60; 3、查询所有同学的学号、姓名、选课数、总成绩; select Student.S#,Student.Sname,count(SC.C#),sum(score) from Student left Outer join SC on Student.S#=SC.S# group by Student.S#,Sname 4、查询姓“李”的老师的个数; select count(distinct(Tname)) from Teacher where Tname like '李%'; 5、查询没学过“叶平”老师课的同学的学号、姓名; select Student.S#,Student.Sname from Student where S# not in (select distinct( SC.S#) from SC,Course,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='叶平'); 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; select Student.S#,Student.Sname from Student,SC where Student.S#=SC.S# and SC.C#='001'and exists( Select * from SC as SC_2 where SC_2.S#=SC.S# and SC_2.C#='002'); 7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; select S#,Sname from Student where S# in (select S# from SC ,Course ,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='叶平' group by S# having count(SC.C#)=(select count(C#) from Course,Teacher where Teacher.T#=Course.T# and Tname='叶平')); 8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;

相关主题
文本预览
相关文档 最新文档