【数据库】简单查询、多表查询和子查询,连接表达式的使用、聚合操作

实验过程:
新建查询文件:学号.sql,保存本次实验的所有SQL语句。
任务1:Simple Queries and Multiple Relations Queries in SQL
(一)Simple Queries
(1) Find the address of‘81 Film’studios.
SELECT statement:

select address from Studio where name = '81 Film'

(2) Find Sandra_Bullock’s birthdate.
SELECT statement:

select birthdate from MovieStar where name='Sandra_Bullock'

(3) Find all the stars that appeared either in a movie made in 1980 or a movie with “Love” in the title.
SELECT statement:

select starName from StarsIn where movieYear = 1980 OR movieTitle like '%Love%'

(4) Find all executives worth at least $10,000,000.
SELECT statement:

select name from MovieExec where netWorth > 10000000

(5) Find all the stars who either are male or live in Malibu(have string Malibu as a part of their address)
SELECT statement:

select name from MovieStar where gender='M' OR address like '%Malibu%'

(二)Multiple Relations Queries
(1) Who are the male stars in Titanic?
SELECT statement:

select name from MovieStar where gender='M' and name in(select starName from StarsIn where movieTitle='Titanic' )

(2) Which stars appeared in movies produced by 81 Film in 2018?
SELECT statement:

select starName from StarsIn
where movieTitle in(select title from Movies where year =2018 and studioName='81 Film')

(3) Who is the executive of ‘The Faithful’ Film?
SELECT statement:

select MovieExec.name
from Movies,MovieExec
where Movies.title='The Faithful'and Movies.producerC#=MovieExec.cert#

(4) Who had executed Harrison Ford’s movies?
SELECT statement:

select distinct MovieExec.name
from StarsIn,MovieExec,Movies
where StarsIn.starName = 'Harrison Ford' and StarsIn.movieTitle=Movies.title and Movies.producerC#=MovieExec.cert#

(5) Who had performed films more than two?
SELECT statement:

select distinct a.starName
from StarsIn a,StarsIn b
where a.movieTitle <> b.movieTitle and a.starName=b.starName

(三)Subquery
(1) Find movies which are longer than The Faithful.
SELECT statement:

select title  from Movies
where length>(select length from Movies where title='The Faithful')

(2) Find executives who has the greatest worth.
SELECT statement:

SELECT name FROM MovieExec where netWorth=(SELECT max(netWorth) FROM MovieExec) 

(3) Find executives who had executed Harrison Ford’s movies.
SELECT statement:

select name from MovieExec
where cert# in(
select producerC# from Movies
where title in(
select movieTitle from StarsIn
where starName='Harrison Ford'
)
)

(4) Find the stars who had not performed in the film Star Wars and its sequels.
SELECT statement:

select distinct starName from StarsIn
where movieTitle not in(
select title from Movies
where title like '%Star Wars%'
)

(5) Find the stars who had performed more than two films.
SELECT statement:

select distinct starName from StarsIn a
where exists(
select *
from StarsIn b
where a.starName=b.starName and a.movieTitle <> b.movieTitle
)

任务2:Join Expressions And Aggregate Operation
(一)Join Expressions
(1) Find the stars who appeared in movies produced by 81 Film in 2018.
SELECT statement:

select starName from StarsIn
where movieTitle =(select title 
from dbo.Movies 
	Inner JOIN dbo.Studio 
	ON producerC# = presC#
where year = 2018)

(2) Find the stars who are male in The Faithful.
SELECT statement:

select name from MovieStar
INNER JOIN StarsIn on movieTitle='The Faithful' and name=starName
where gender ='M'

(3) Find the studios which had produced more than two films.
SELECT statement:

select distinct a.studioName from Movies a inner join Movies b on
a.studioName=b.studioName and a.title <> b.title

(4) Find the stars who not appeared in Titanic.
SELECT statement:

select distinct a.starName
from StarsIn a
	LEFT JOIN StarsIn b
	ON b.movieTitle='Titanic' AND a.starName=b.starName
where b.movieTitle IS NULL;   

(5) Find the stars who never appeared in Movies.
SELECT statement:

select name
from MovieStar
	LEFT JOIN StarsIn
	ON name = starName
where movieTitle IS NULL;

(二)Aggregate Operation
(1) Find the number of movies which had produced by various studios.
SELECT statement:

select studioName,count(title) total from Movies
where studioName is not null
group by studioName

(2) Find the studios which had produced more than two films.
SELECT statement:

select studioName from Movies
where studioName is not null
group by studioName
having COUNT(title) >1

(3) Find the sum of the lengths of all movies for each studio.
SELECT statement:

select studioName,SUM(length) sumlength  from Movies
where studioName is not null
group by studioName

(4) Find the number of movie stars have appeared in films which had produced by Fox studio.
SELECT statement:

select COUNT(distinct starName) sum_star from Movies
right join StarsIn
on title=movieTitle
where studioName='Fox'

(5) Which studio has the largest number of films.
SELECT statement:

select TOP 1 studioName, COUNT(title) total
	from Movies
	where studioName IS NOT NULL
GROUP BY studioName
ORDER BY total desc;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
2008数据库实验 1.SQL SEVER 2000的系统工具、使用交互方式建库、建表 2.T—SQL简单查询连接查询 3.子查询及组合 4.数据控制、数据导入/导出、数据备份和恢复 实验1 SQL SEVER 2000的系统工具、使用交互方式建库、建表实验 实验目的和要求:了解SQL SEVER 2000的功能及组成,熟练掌握利用SQL SEVER 2000企业管理器和查询分析器创建数据库、表、索引和修改表结构及向数据库输入数据、修改数据和删除数据的操作方法和步骤,掌握定义数据约束条件的操作。 实验内容和步骤: (1)熟悉SQL SEVER 2000的界面和操作。 (3)熟悉企业管理器和查询分析器的界面和操作。 (3)创建数据库和查看数据库属性。 (4)创建表、确定表的主码和约束条件。 (5)查看和修改表的结构。 (6)向数据库输入数据,观察违反列级约束时出现的情况。 (7)修改数据。 (8)删除数据,观察违反表级约束时出现的情况。 实验2 T—SQL简单查询连接查询 实验目的和要求:,了解SQL语句的数据定义与数据更新功能,了解SQL语句的查询功能,掌握SQL中的数据定义语句的用法,熟练掌握SQL中的插入、修改和删除语句的操作,熟练掌握使用SQL语句进行数据库简单查询连接查询。 实验内容和步骤: (1)在SQL SEVER 2000的查询分析器里,用SQL语句建库、建表并插入记录。 (2)修改表结构,包括修改属性列的数据类型,增加新的属性列,删除已有的属性列。 (3)使用单个元组和多元组插入。 (4)简单查询操作,包括投影、选择、数据排序、模糊匹配查询等。如果结果不正确,要进行修改,直至正确为止。 (5)连接查询操作,包括等值连接、自然连接、一般连接、自身连接、外连接。 实验3 子查询及组合 实验目的和要求:了解SQL语句的查询功能,理解视图的概念。熟练掌握使用SQL语句进行数据库的嵌套查询及组合查询操作;掌握视图创建语句和视图的使用方法,加深对视图作用的理解。 实验内容和步骤: (1)在DBMS的交互式环境里,用SQL语句建库、建表并插入记录。 (2)使用In、比较符和Exists操作符进行嵌套查询操作。 (3)分组查询,包括分组条件表达、选择组条件表达的方法。 (4)集合查询。 (5)使用视图创建语句建视图,通过视图查询数据 (6)带子查询的修改和删除 (7)通过视图修改和删除数据 实验4 数据控制、数据的导入/导出、数据库备份和恢复 实验目的和要求:掌握数据控制(安全性)的方法,了解SQL SEVER 2000的数据备份和恢复机制,掌握SQL SEVER 2000中数据库备份和恢复的方法。 实验内容和步骤: (1)使用SQL对数据进行安全性控制,包括授权和权利收回。 (2)查看授权和权利收回后的结果 (3)SQL SEVER 2000工具对表中的数据导出到其它格式的文件。 (4)将其它格式的文件数据导入到数据库中。 (5)使用SQL SEVER 2000工具创建一个数据库的备份(海量备份、增量备份)。 (6)使用SQL SEVER 2000工具及所创建的数据库备份恢复这个数据库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值