数据库实验四:数据高级查询

这篇博客详细介绍了SQL查询的各种操作,包括内连接、外连接、子查询、聚合函数、集合操作等,通过实例展示了如何在数据库实验中查找相同颜色、不同颜色的产品,以及在多个表间进行复杂的数据分析。此外,还探讨了NULL处理、EXISTS和NOT EXISTS的用法,以及UNION、UNION ALL、INTERSECT和MINUS的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

真给我整无语了,数据库实验怎么这么多,要考试还要写实验。写写写,写他娘的什么东西。

实验内容

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

实现查询

ps:实验要求用两种方法写的我除了union都是用的两种方法写,union查了好久都查不到,或许有人知道union的平替嘛请教一下大家

  1. 创建两张表 palette_a 和 palette_b(结构相同,但表名不同,color 为颜色)
    CREATE TABLE palette_a
    (id INT PRIMARY KEY,
    color VARCHAR2(100) NOT NULL);

CREATE TABLE palette_b
(id INT PRIMARY KEY,
color VARCHAR2(100) NOT NULL);
创建表:
在这里插入图片描述

  1. 为表 palette_a 添加样例数据:{(1, ‘Red’), (2, ‘Green’), (3, ‘Blue’), (4, ‘Purple’)}。
    Insert into palette_a(id,color) values(1, ‘Red’), (2, ‘Green’), (3, ‘Blue’), (4, ‘Purple’);
    在这里插入图片描述

  2. 为表 palette_b 添加样例数据:{(1, ‘Green’), (2, ‘Red’), (3, ‘Cyan’), (4, ‘Brown’)}。
    Insert into palette_b(id,color) values(1, ‘Green’), (2, ‘Red’), (3, ‘Cyan’), (4, ‘Brown’);
    在这里插入图片描述

  3. 查询两张表中相同颜色的所有信息。
    SELECT * FROM palette_b inner JOIN palette_a ON palette_a.color= palette_b.color;
    在这里插入图片描述

  4. 查询 palette_a 表中颜色不出现在 palette_b 表中的两张表的 id 和颜色(用左外连接)。
    SELECT * FROM palette_a LEFT OUTER JOIN palette_b ON palette_a.color= palette_b.color;
    在这里插入图片描述

  5. 查询 palette_b 表中颜色不出现在 palette_a 表中的两张表的 id 和颜色(用右外连接)。
    SELECT * FROM palette_b RIGHT OUTER JOIN palette_a ON palette_a.color= palette_b.color;
    在这里插入图片描述

7.查询(5)或(6)两种情况的信息(用(全)外连接)。
SELECT * FROM palette_a FULL OUTER JOIN palette_b ON palette_a.color= palette_b.color;
在这里插入图片描述

8查询产品表 products 中的 product_id, product_name, list_price 信息,要求产品定价 list_price 大于 其平均定价 list_price。
Select product_id,product_name,list_price from products where list_price>(select avg(list_price) from products);
在这里插入图片描述

9.查询产品表 products 中最便宜产品的 product_id, product_name, list_price。
Select product_id,product_name,list_price from products where list_price=(select min(list_price) from products);
在这里插入图片描述 10.查询没有一个订单的顾客姓名。 实现要求:NOT IN(必须)+其它查询方法(如果找到)
Select name from customers where customer_id not in (select customer_id from orders);
在这里插入图片描述

其他方法:
Select name from customers where customer_id !=all (select customer_id from orders);
在这里插入图片描述

11.查询产品表 products 中产品的 product_id, product_name, list_price,要求产品定价 list_price 大于 其同类产品(可由 category_id 表达)的平均定价。 实现要求:相关子查询(必须)+基于派生表的查询(如果找到)
相关子查询
Select product_id,product_name,list_price from products p1 where list_price>(select avg(list_price) from products p2 where p1.category_id=p2.category_id);

在这里插入图片描述

基于派生表的:
Select product_id,product_name,list_price from products p1,(select category_id,avg(list_price) avg_price from products group by category_id) p2 where p1.category_id=p2.category_id and p1.list_price>p2.avg_price;
在这里插入图片描述

12.查询有订单 order 的所有顾客 customer 姓名(查询涉及 customers 表和 orders 表)。 实现要求:使用 EXISTS(必须)+其它查询方法(如果找到)
Select name from customers where exists (select customer_id from orders where orders.customer_id=customers.customer_id);
在这里插入图片描述

13.执行以下两条语句,观察有何不同,能否得出某些初步结论?
In 不对Null进行处理
Customer_id为NULL的
在这里插入图片描述

显示全部信息
Select null from customers 返回true
在这里插入图片描述

14.找出所有没有订单的顾客姓名(查询涉及 customers 表和 orders 表)。 实现要求:使用 NOT EXISTS(必须)+其它查询方法(如果找到)
Select name from customers where not exists (select *from orders where customers.customer_id=orders.customer_id);
在这里插入图片描述

Select name from customers where customer_id not in (select customer_id from orders);
在这里插入图片描述

15.查询产品表 products 中的产品名 product_name 和定价 list_price,要求其定价高于产品种类 1 中 的任何产品定价。 实现要求:ANY(必须)+其它查询方法(如果找到)
Select product_name,list_price from products where list_price>any(select list_price from products p where p.category_id=1 );
在这里插入图片描述

Select product_name,list_price from products where list_price>(select min(list_price) from products p where p.category_id=1);
在这里插入图片描述

16.查询产品表 products 中的产品名 product_name 和定价 list_price,要求其定价高于产品种类 1 中 的所有定价。
Select product_name,list_price from products where list_price>all(select list_price from products where products.category_id=1);
在这里插入图片描述

17.查询产品表 products 中的产品名 product_name 和定价 list_price,要求其定价低于产品种类的所 有平均定价。
实现要求:ALL(必须)+其它查询方法(如果找到)
Select product_name,list_price from products where list_price<all(select avg(list_price) from products);
在这里插入图片描述

Select product_name,list_price from products where list_price<(select max(list_price)from products);
在这里插入图片描述

18.查询 contacts 表和 employees 表中的所有 last_name,并以 last_name 升序显示。 实现要求:去重+UNION(必须)+其它查询方法(如果找到)
Select last_name from contacts union Select last_name from employees;
在这里插入图片描述

19.查询 contacts 表和 employees 表中的所有 last_name,并以 last_name 升序显示。 实现要求:保留重复+UNION ALL(必须)+其它查询方法(如果找到)
Select last_name from contacts union all Select last_name from employees order by last_name asc;
在这里插入图片描述

20.查询同时出现在 contacts 表和 employees 表中的所有 last_name。 实现要求:INTERSECT(必须)+其它查询方法(如果找到)
Select last_name from contacts intersect Select last_name from employees;
在这里插入图片描述

Select contacts.last_name from contacts,employees where contacts.last_name=employees.last_name group by contacts.last_name,employees.last_name having count(*)=1;
在这里插入图片描述

21.查询在产品表 products 中而不在库存表 inventories 中的产品号 product_id。 实现要求:MINUS/EXCEPT(必须)+其它查询方法(如果找到)
Select product_id from products minus select product_id from inventories;
在这里插入图片描述

Select product_id from products except select product_id from inventories;
在这里插入图片描述

Select product_id from products where products.product_id not in (select product_id from inventories);
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

香菜菜菜菜子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值