oracle-sql语句练习

落日映苍穹つ 2022-05-14 15:21 429阅读 0赞

1.查询所有speed大于2.8的PC信息

  1. select *
  2. from pcs
  3. where speed > 2.8

2.查询购买model为1007的购买记录信息

  1. select *
  2. from sales
  3. where model = 1007

3.统计2013-12-20购买记录的数量(count)

  1. select count(*)
  2. from sales
  3. where sday=to_date('2013-12-20', 'yyyy-mm-dd')

4.统计2013-12-20购买的总数量(sum)

  1. select sum(quantity)
  2. from sales
  3. where sday = to_date('2013-12-20', 'yyyy-mm-dd')

5.查询硬盘大小出现在两种以上PC电脑上的硬盘大小

  1. select hd
  2. from pcs
  3. group by hd
  4. having count(*) >= 2;

6.查询速度至少3.00以上的PC models信息

  1. select model
  2. from pcs
  3. where speed >= 3.00;

7.查询哪个供应商供应laptops硬盘至少100GB以上的供应商信息
(1)等值连接

  1. select products.*
  2. from products, laptops
  3. where products.model=laptops.model and laptops.hd >= 100;

(2)自然连接(natural join)

  1. select maker, model, ptype
  2. from products natural join laptops
  3. where hd >= 100;

(3)内连接

  1. select maker, products.model, ptype
  2. from products inner join laptops
  3. on products.model = laptops.model
  4. where hd >= 100;

(4)自然连接(join using)

  1. select *
  2. from products join laptops using(model)
  3. where hd >= 100;

(5)不相关子查询

  1. select *
  2. from products
  3. where model in (
  4. select model
  5. from laptops
  6. where hd >= 100
  7. );

(6)相关子查询

  1. select *
  2. from products pr
  3. where exists (
  4. select model
  5. from laptops la
  6. where pr.model = la.model and la.hd >= 100
  7. );

8.查询供应商B所提供的所有产品的产品编号和产品价格

  1. select model, price
  2. from pcs
  3. where model in (
  4. select model
  5. from products
  6. where maker = 'B'
  7. )
  8. union
  9. select model, price
  10. from laptops
  11. where model in (
  12. select model
  13. from products
  14. where maker = 'B'
  15. )
  16. union
  17. select model, price
  18. from printers
  19. where model in (
  20. select model
  21. from products
  22. where maker = 'B'
  23. );

9.查找所有的彩色打印机的model numbers

  1. select model
  2. from printers
  3. where color = 'TRUE';

10.查找供应laptops但不供应pc的供应商信息

  1. select *
  2. from products x
  3. where x.ptype = 'laptop' and 'pc' not in (
  4. select ptype
  5. from products y
  6. where x.maker = y.maker
  7. );

11.查询具有相同运行速度和内存的PC电脑编号对
每个pc models对只出现一次,即 (i, j)出现,则 (j, i)不出现
Find those pairs of PC models that have both the same speed and ram.
A pair should be listed only once; e.g., list (i, j) but not (j, i)

  1. select x.model, y.model
  2. from pcs x, pcs y
  3. where x.model < y.model and x.model <> y.model and x.speed = y.speed and x.ram = y.ram;

12.查询销售三种不同型号PC电脑的供应商
Find the makers who sell exactly three different models of PC

  1. select maker
  2. from (
  3. select distinct *
  4. from products
  5. where ptype = 'pc'
  6. )
  7. group by maker
  8. having count(*) >= 3;

13.查询至少提供3种PC运行速度的供应商
Find the makers of PCs with at least three different speeds

  1. select maker
  2. from (
  3. select distinct maker, speed
  4. from products, pcs
  5. where products.model = pcs.model
  6. )
  7. group by maker
  8. having count(speed) >= 3;

14.查询提供的PC或laptop运行速度至少是2.80且有两种以上产品的供应商
Find those makers of at least two different computers (PCs or laptops) with speeds of at least 2.80

  1. select maker
  2. from(
  3. select maker, products.model
  4. from products, pcs
  5. where products.model = pcs.model and pcs.speed >= 2.80
  6. union
  7. select maker, products.model
  8. from products, laptops
  9. where products.model = laptops.model and speed >= 2.80
  10. )
  11. group by maker
  12. having count(*) >= 2;

15.查询提供的电脑(PC or laptop)具有最高运行速度的供应商
Find the maker(s) of the computer(PC or laptop) with the highest available speed

  1. select ac.maker
  2. from (
  3. select maker, speed
  4. from products, pcs
  5. where products.model = pcs.model
  6. union
  7. select maker, speed
  8. from products, laptops
  9. where products.model = laptops.model
  10. ) ac
  11. where ac.speed = (
  12. select max(speed)
  13. from(
  14. select maker, speed
  15. from products, pcs
  16. where products.model = pcs.model
  17. union
  18. select maker, speed
  19. from products, laptops
  20. where products.model = laptops.model
  21. )
  22. );

发表评论

表情:
评论列表 (有 0 条评论,429人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SQL语句练习

    一.[通过Sql实现根据分组合并指定列内容的查询 ][Sql_] 问题:   最近在做一个项目的时候,遇到这样一个要求,模拟要求如下:   ID SName

    相关 Oracle sql语句练习

    在网上找的 Oracle sql语句练习 终于磕磕绊  绊的做完了!!!! \--1、查询“c001”课程比“c002”课程成绩高的所有学生的学号; select

    相关 shell 控制语句练习

    在介绍控制语句前,先说下test 和 [] test 和[]命令可以 对三类表达式进行测试 字符串比较 文件测试 数字比较 1.字符串比较 shell对大小写敏感,为字...