零基础快速自学SQL,1天足矣

1251楼
发表于 2019-2-28 04:40:09
加油加油
1252楼
发表于 2019-2-28 09:32:38
GG思密达
1253楼
发表于 2019-2-28 16:18:18
我想知道答案啊~
1254楼
发表于 2019-3-1 19:01:42

1.        找出供应商名称,所在城市
select 公司名称, 城市 from 供应商;
2.        找出华北地区能够供应海鲜的所有供应商列表。
select 供应商.公司名称
  from 供应商
where exists (select 1
          from 产品, 类别
         where 类别.类别id = 产品.类别ID
           and 类别.类别id = 8
           and 供应商.供应商id = 产品.供应商id)
3.        找出订单销售额前五的订单是经由哪家运货商运送的。
select mx.订单id, mx.total, 运货商.公司名称
  from (select 订单id, sum(单价 * 数量 * (1 - 折扣)) total
          from 订单明细
         group by 订单ID
         order by total desc) mx
  join 订单
    on (订单.订单id = mx.订单id)
  join 运货商
    on (订单.运货商 = 运货商.运货商ID)
where rownum <= 5;
4.        找出按箱包装的产品名称。
select 产品名称 from 产品 where 单位数量 like '%箱%';
5.        找出重庆的供应商能够供应的所有产品列表。
select 产品名称
  from 产品
where exists (select 1
          from 供应商
         where 城市 = '重庆'
           and 产品.供应商ID = 供应商.供应商ID);
6.        找出雇员郑建杰所有的订单并根据订单销售额排序。
select 名字, 订单ID, sum(销售金额) 销售金额
  from (select 雇员.姓氏 || 雇员.名字 名字,
               订单明细.订单ID,
               订单明细.单价 * 订单明细.数量 * (1 - 订单明细.折扣) 销售金额
          from 订单
          join 订单明细
            on (订单.订单id = 订单明细.订单ID)
          join 雇员
            on (雇员.雇员ID = 订单.雇员ID)
         where 姓氏 = '郑'
           and 名字 = '建杰')
group by 名字, 订单ID
order by 销售金额 desc;
7.        找出订单10284的所有产品以及订单金额,运货商。
select 订单.订单ID,
       订单明细.单价 * 订单明细.数量 * (1 - 订单明细.折扣) 订单金额,
       产品.产品名称,
       运货商.公司名称
  from 订单
  join 订单明细
    on (订单.订单ID = 订单明细.订单ID)
  join 运货商
    on (订单.运货商 = 运货商.运货商ID)
  join 产品
    on (订单明细.产品ID = 产品.产品ID)
where 订单.订单id = 10284
8.        建立产品与订单的关联。
select 订单明细.订单ID, 产品.产品名称
  from 订单明细
  join 产品
    on (订单明细.产品ID = 产品.产品id)
9.        计算销量前10位的订单明细,结果集返回订单ID,订单日期,公司名称,发货日期,销售额,并排序
select 订单.订单ID,
       产品.产品名称,
       订单.订购日期,
       供应商.公司名称,
       订单.发货日期,
       销售额,
       明细.rn
  from (select 订单id,
               产品id,
               数量 * 单价 * (1 - 折扣) 销售额,
               数量,
               row_number() over(order by 数量 desc) rn
          from 订单明细) 明细
  join 订单
    on (订单.订单id = 明细.订单id)
  join 产品
    on (明细.产品id = 产品.产品ID)
  join 供应商
    on (供应商.供应商ID = 产品.供应商ID)
where 明细.rn <= 10
order by 销售额 desc;
10.        按年度统计销售额
------以上开始时间一个半小时
  select trunc(订购日期, 'year') 订购日期,
       sum(订单明细.单价 * 订单明细.数量 * (1 - 订单明细.折扣)) 销售额
  from 订单明细
  join 订单
    on (订单明细.订单ID = 订单.订单ID)
group by trunc(订购日期, 'year');
11.        查询供应商中能够供应的产品样数最多的供应商。
with m_cnt as
(select 供应商id, count(*) cnt from 产品 group by 供应商id),
gys as
(select * from 供应商)
select m_cnt.供应商id, gys.公司名称
  from m_cnt
  join gys
    on (m_cnt.供应商id = gys.供应商id)
where cnt in (select max(cnt) from m_cnt);
12.        查询产品类别中包含的产品数量最多的类别。
with m_cnt as
(select 类别id, count(*) cnt from 产品 group by 类别id),
gys as
(select * from 类别)
select m_cnt.类别id, gys.类别名称
  from m_cnt
  join gys
    on (m_cnt.类别id = gys.类别id)
where cnt in (select max(cnt) from m_cnt);
13.        找出所有的订单中经由哪家运货商运货次数最多。
with 运货商次数 as
(select 运货商, count(*) 运货次数 from 订单 group by 运货商),
运货商家 as
(select * from 运货商)
select 运货商次数.运货次数, 运货商家.公司名称
  from 运货商家
  join 运货商次数
    on (运货商家.运货商id = 运货商次数.运货商)
where 运货商次数.运货次数 = (select max(运货次数) from 运货商次数);
14.        按类别,产品分组,统计销售额。
select sum(订单明细.单价 * 订单明细.数量 * (1 - 订单明细.折扣)) 销售额,
       产品.产品名称,
       类别.类别名称
  from 类别
  join 产品
    on (类别.类别ID = 产品.类别ID)
  join 订单明细
    on (订单明细.产品ID = 产品.产品ID)
group by 产品.产品名称, 类别.类别名称;
15.        查询海鲜类别最大的一笔订单。
select *
  from (select row_number() over(order by 数量 desc) rn,
               订单明细.数量,
               订单明细.订单ID,
               产品.产品名称,
               类别.类别名称
          from 类别
          join 产品
            on (类别.类别ID = 产品.类别ID)
          join 订单明细
            on (订单明细.产品ID = 产品.产品ID)
         where 类别.类别名称 = '海鲜')
where rn = 1;
16.        按季度统计销售量
select case
         when to_char(订购日期, 'mm') between 01 and 03 then
          '第一季度'
         when to_char(订购日期, 'mm') between 04 and 06 then
          '第二季度'
         when to_char(订购日期, 'mm') between 07 and 09 then
          '第三季度'
         else
          '第四季度'
       end 季度,
       sum(订单明细.数量) 数量
  from 订单
  join 订单明细
    on (订单.订单ID = 订单明细.订单ID)
group by case
            when to_char(订购日期, 'mm') between 01 and 03 then
             '第一季度'
            when to_char(订购日期, 'mm') between 04 and 06 then
             '第二季度'
            when to_char(订购日期, 'mm') between 07 and 09 then
             '第三季度'
            else
             '第四季度'
          end
17.        查出订单总额超出5000的所有订单,客户名称,客户所在地区。
select 客户.联系人姓名, 客户.地区
  from 订单明细
  join 订单
    on (订单明细.订单ID = 订单.订单ID)
  join 客户
    on (订单.客户ID = 客户.客户ID)
where (单价 * 数量 * (1 - 折扣)) > 5000
18.        查询哪些产品的年度销售额低于2000
select 产品.产品名称,
       sum(订单明细.单价 * 订单明细.数量 * (1 - 订单明细.折扣)) as "销售额",
       trunc(订单.订购日期, 'mm') 订购日期
  from 订单明细
  join 订单
    on (订单明细.订单ID = 订单.订单ID)
  join 产品
    on (订单明细.产品ID = 产品.产品ID)
group by 产品.产品名称, trunc(订单.订购日期, 'mm')
having sum(订单明细.单价 * 订单明细.数量 * (1 - 订单明细.折扣)) < 2000;
19.        查询所有订单ID开头为102的订单
select * from 订单 where substr(订单.订单id, 1, 3) = 102;
20.        查询所有“中硕贸易”,“学仁贸易”,“正人资源”,“中通”客户的订单,(要求使用in函数)
select *
  from 订单
where exists (select 客户id
          from 客户
         where 订单.客户id = 客户.客户ID
           and 公司名称 in ('中硕贸易', '学仁贸易', '正人资源', '中通'));
-----以上考试时间一个班销售
21.        查询所有订单中月份不是单数的订单。
select *
  from 订单
where  mod(to_char(订购日期,'mm'),2)<>0;
22.        分别各写一个查询,得到订单中折扣为15%,20%的所有订单,并将两个查询再组成一个。
select *
  from 订单明细
where 折扣 = 0.15
union all
select *
  from 订单明细
where 折扣 = 0.20
23.        找出在入职时已超过30岁的所有员工信息
select * from 雇员 where (雇用日期 - 出生日期) > 30;
24.        找出所有单价大于30的产品(附加要求,产品类别,供应商作为参数,当产品类别和供应商都为空的时候,nofilter)
declare
  cplx number := '&cplx';
  gys  number := '&qys';
begin
  if cplx is null and gys is null then
    for rec in (select 产品名称 from 产品 where 单价 > 30) loop
      dbms_output.put_line(rec.产品名称);
    end loop;
  elsif cplx is null and gys is not null then
    for rec in (select 产品名称
                  from 产品
                 where 单价 > 30
                   and 供应商id = gys) loop
      dbms_output.put_line(rec.产品名称);
    end loop;
  elsif cplx is not null and gys is null then
    for rec in (select 产品名称
                  from 产品
                 where 单价 > 30
                   and 类别id = cplx) loop
       dbms_output.put_line(rec.产品名称);
    end loop;
  elsif cplx is not null and gys is not null then
    for rec in (select 产品名称
                  from 产品
                 where 单价 > 30
                   and 类别id = cplx
                   and 供应商id = gys) loop
       dbms_output.put_line(rec.产品名称);
    end loop;
  end if;
end;

25.        查询所有库存产品的总额,并按照总额排序
select 产品名称, 库存量 from 产品 order by 库存量 desc;
26.        检索出职务为销售代表的所有订单中,每笔订单总额低于2000的订单明细,以及相关供应商名称。
select 销售.订单ID, 销售.名字, 销售.销售金额, 供应商.公司名称,产品.产品名称
  from (select 订单.订单ID,
               雇员.姓氏 || 雇员.名字 名字,
               sum(订单明细.单价 * 订单明细.数量 * (1 - 订单明细.折扣)) 销售金额
          from 雇员
          join 订单
            on (订单.雇员ID = 雇员.雇员ID)
          join 订单明细
            on (订单.订单ID = 订单明细.订单ID)
         where 雇员.职务 = '销售代表'
         group by 订单.订单ID, 雇员.姓氏 || 雇员.名字
        having sum(订单明细.单价 * 订单明细.数量 * (1 - 订单明细.折扣)) < 2000) 销售
  join 订单明细
    on (订单明细.订单id = 销售.订单ID)
  join 产品
    on (产品.产品ID = 订单明细.产品ID)
  join 供应商
    on (供应商.供应商id = 产品.供应商ID);

27.        检索出向艾德高科技提供产品的供应商所在的城市。
select 供应商.城市, 供应商.公司名称, 产品.产品名称, 客户.公司名称
  from 客户
  join 订单
    on (订单.客户ID = 客户.客户ID)
  join 订单明细
    on (订单明细.订单ID = 订单.订单ID)
  join 产品
    on (产品.产品ID = 订单明细.产品ID)
  join 供应商
    on (产品.供应商ID = 供应商.供应商ID)
where 客户.公司名称 = '艾德高科技';
28.        计算每一笔订单的发货期(从订购到发货),运货期(从发货到到货)的时常,并按照发货期从长到短的顺序进行排序。
select 订单.订单ID,
       (发货日期 - 订购日期) 发货期,
       (到货日期 - 发货日期) 运货期
  from 订单
order by 发货期 desc nulls last;
29.        将产品表和运货商两个无关的表整合为一个表
select  distinct 产品.*, 运货商.*
  from 订单
  join 订单明细
    on (订单.订单ID = 订单明细.订单ID)
  join 产品
    on (产品.产品ID = 订单明细.产品ID)
  join 运货商
    on (运货商.运货商ID = 订单.运货商)
30.        获取在北京工作并向福星制衣厂股份有限公司发送过订单的职工名称。
select distinct 雇员.姓氏 || 雇员.名字 职工名称
  from 雇员
  join 订单
    on (订单.雇员ID = 雇员.雇员ID)
  join 客户
    on (客户.客户ID = 订单.客户ID)
where 雇员.城市 = '北京'
   and 客户.公司名称 = '福星制衣厂股份有限公司'
1255楼
发表于 2019-3-1 21:46:15
这个真的好,打卡一下 下次用
1256楼
发表于 2019-3-3 15:07:10
初入,微晕···努力ing!!
1257楼
发表于 2019-3-4 11:15:53
不错,学习ing
1258楼
发表于 2019-3-4 14:40:39
无无无无无 编辑于 2019-3-5 14:01  
1259楼
发表于 2019-3-4 14:41:19
无无无无 编辑于 2019-3-5 14:01  
1260楼
发表于 2019-3-5 13:59:29
#第二部分自作#1
select 公司名称,城市 from 供应商;

2
select g.供应商ID,g.公司名称
      from 供应商 as g,产品 as c,类别 as l
           where g.供应商ID=c.供应商ID
                and c.类别ID=l.类别ID
                and l.类别名称='海鲜';

3
select d.订单ID as 销售额前五订单ID(由大到小),y.运货商ID as 对应运货商
      from 运货商 as y,订单 as d,订单明细 as dm
          where d.订单ID=dm.订单ID
               and d.运货商=y.运货商ID
          order by (dm.单价*dm.数量*(1-dm.折扣))
          limit 5;

4
select 产品名称
      from 产品
           where 单位数量 like '%箱%';

5
select 产品名称
      from 产品 as c,供应商 as g
          where c.供应商ID=g.供应商ID
                and g.城市='重庆';

6
select d.订单ID
     from 订单 as d,雇员 as g,订单明细 as dm
         where d.雇员ID=g.雇员ID
              and d.订单ID=dm.订单ID
              and g.姓氏='郑'
              and g.名字='建杰'
         order by (dm.单价*dm.数量*(1-dm.折扣));

7
select c.产品名称,(dm.单价*dm.数量*(1-dm.折扣)) as 订单金额,y.公司名称
      from 订单 as d,产品 as c,订单明细 as dm,运货商 as y
          where d.订单ID=dm.订单ID
               and dm.产品ID=c.产品ID
               and d.运货商=y.运货商ID
               and d.订单ID='10284';

8
select d.*,c.*
     from 产品 as c,订单 as d,订单明细 as dm
         where c.产品ID=dm.产品ID
              and d.订单ID=dm.订单ID;

9
select d.订单ID,d.订购日期,k.公司名称,d.发货日期,(dm.单价*dm.数量*(1-dm.折扣)) as 销售额
      from 订单 as d,客户 as k,订单明细 as dm
           where d.客户ID=k.客户ID
                and d.订单ID=dm.订单ID
           order by (dm.单价*dm.数量*(1-dm.折扣)) desc
           limit 10;

10
select year(d.订购日期) as 年度,sum(dm.单价*dm.数量*(1-dm.折扣)) as 销售额
      from 订单 as d,订单明细 as dm
          where d.订单ID=dm.订单ID
          group by year(d.订购日期);

11
drop view if exists newcompanyname;
create view newcompanyname as
      select g.公司名称,count(*) as 总数
            from 产品 as c,供应商 as g
                 where c.供应商ID=g.供应商ID
            group by g.供应商ID;
select *
      from newcompanyname
            where 总数 =
                    (select 总数
                          from newcompanyname
                          order by 总数 desc
                          limit 1);


12
drop view if exists newclassproduct;
create view newclassproduct as
      select lb.类别ID, count(*) as 包含产品数
            from 产品 as cp,类别 as lb
                where cp.类别ID=lb.类别ID
            group by lb.类别ID;
select *
      from newclassproduct
          where 包含产品数=(
                 select 包含产品数 from newclassproduct
                       order by 包含产品数 desc
                       limit 1);



13
drop view if exists newtranname;
create view newtranname as
      select y.公司名称,count(*) as 运货次数
             from 订单 as d,运货商 as y
                  where d.运货商=y.运货商ID
             group by y.运货商ID;
select *
      from newtranname
           where 运货次数 =
                (select 运货次数
                       from newtranname
                       order by 运货次数 desc
                       limit 1);

14
select l.类别名称,sum(dm.单价*dm.数量*(1-dm.折扣)) as 销售额
      from 类别 as l,订单明细 as dm,产品 as c
          where c.类别ID=l.类别ID
               and c.产品ID=dm.产品ID
          group by l.类别ID;
select c.产品名称,sum(dm.单价*dm.数量*(1-dm.折扣)) as 销售额
      from 产品 as c,订单明细 as dm
          where c.产品ID=dm.产品ID
      group by c.产品ID;

15
select d.*
      from 订单 as d,产品 as c,订单明细 as dm,类别 as l
          where d.订单ID=dm.订单ID
                and c.类别ID=l.类别ID
                and c.产品ID=dm.产品ID
                and l.类别名称='海鲜'
          order by sum(dm.单价*dm.数量*(1-dm.折扣)) desc
          limit 1;

16
select year(d.订购日期) as 年份,quarter(d.订购日期) as 季度,
       count(*) as 销售量
      from 订单 as d,订单明细 as dm
          where d.订单ID=dm.订单ID
      group by year(d.订购日期),quarter(d.订购日期)
      order by year(d.订购日期),quarter(d.订购日期);

17
select d.订单ID,k.客户ID,k.地区,(dm.单价*dm.数量*(1-dm.折扣)) as 订单总额
      from 订单 as d,订单明细 as dm,客户 as k
          where d.订单ID= dm.订单ID
              and d.客户ID=k.客户ID
              and (dm.单价*dm.数量*(1-dm.折扣)) >5000;


18
select distinct c.产品ID,c.产品名称
     from 产品 as c,订单明细 as dm,订单 as d
        where c.产品ID=dm.产品ID
             and d.订单ID=dm.订单ID
        group by year(d.订购日期),c.产品名称
             having sum(dm.单价*dm.数量*(1-dm.折扣)) <2000;

19
select d.*
     from 订单 as d
         where d.订单ID like '102%';

20
select k.公司名称,d.*
      from 订单 as d,客户 as k
          where d.客户ID=k.客户ID
               and k.公司名称 in ('中硕贸易','学仁贸易','正人资源','中通');

21
select *
      from 订单 as d
          where mod(month(d.订购日期),2)=0;

22
select dm.折扣,d.*
     from 订单 as d,订单明细 as dm
         where d.订单ID=dm.订单ID
              and rtrim(dm.折扣)=0.15
union
select dm.折扣,d.*
     from 订单 as d,订单明细 as dm
         where d.订单ID=dm.订单ID
              and rtrim(dm.折扣)=0.20;

23
select g.*
      from 雇员 as g
         where floor(datediff(date(g.雇用日期),date(g.出生日期))/365.2422)>30;

24
select l.类别名称,g.公司名称,c.*
     from 产品 as c left join 类别 as l
          on c.类别ID=l.类别ID
          left join 供应商 as g
          on c.供应商ID=g.供应商ID
         where rtrim(c.单价)>30
         order by c.产品ID;

25
select (c.单价*c.库存量) as 总额,c.*
     from 产品 as c
     order by c.单价*c.库存量;

26
select dm.*,gy.公司名称 as 供应商名称
      from 订单明细 as dm,订单 as d,雇员 as g,
           产品 as c,供应商 as gy
          where dm.订单ID=d.订单ID
               and dm.产品ID=c.产品ID
               and d.雇员ID=g.雇员ID
               and c.供应商ID=gy.供应商ID
               and rtrim(g.职务)='销售代表'
               and (dm.单价*dm.数量*(1-dm.折扣))<2000;

27
select distinct gy.公司名称 as 供应商,gy.城市 as 所在城市
     from 订单 as dd,订单明细 as dm,客户 as kh,
         产品 as cp,供应商 as gy
         where kh.客户ID=dd.客户ID
              and dd.订单ID=dm.订单ID
              and dm.产品ID=cp.产品ID
              and cp.供应商ID=gy.供应商ID
              and rtrim(kh.公司名称)='艾德高科技';

28
select dd.订单ID,TimeStampDiff(day,dd.订购日期,dd.发货日期) as 发货期,
       TimeStampDiff(day,dd.发货日期,dd.到货日期) as 运货期
       from 订单 as dd
       order by TimeStampDiff(day,dd.订购日期,dd.发货日期);

29
select distinct cp.*,yh.*
      from 产品 as cp,运货商 as yh,订单 as dd,订单明细 as dm
           where dd.订单ID=dm.订单ID
                and dm.产品ID=cp.产品ID
                and dd.运货商=yh.运货商ID
           order by cp.产品ID;

30
select distinct concat(gy.姓氏,gy.名字) as 职工名称
      from 雇员 as gy,订单 as dd,客户 as kh
          where gy.雇员ID=dd.雇员ID
               and dd.客户ID=kh.客户ID
               and rtrim(gy.城市)='北京'
               and kh.公司名称='福星制衣厂股份有限公司';
1261楼
发表于 2019-3-6 13:42:51
路过,过来支持一下!1天太厉害了!
1262楼
发表于 2019-3-6 17:38:06
我来看答案的~
1263楼
发表于 2019-3-8 19:09:42
学了大数据才来学用帆软的
1264楼
发表于 2019-3-11 14:32:24
好好学习,天天向上
1265楼
发表于 2019-3-19 15:47:09
回帖提交作业怎么做?
1266楼
发表于 2019-3-19 15:48:49
提交作业试试。
参与人数 +1 F豆 +100 理由
传说哥 + 100 骚年,我看好你哦

查看全部评分

1267楼
发表于 2019-3-21 19:25:55
干巴爹
1268楼
发表于 2019-3-23 15:57:53
第一部分全部答案
仅供参考

1269楼
发表于 2019-3-24 23:29:31
单表查询1
1270楼
发表于 2019-3-24 23:29:59
单表查询2
1271楼
发表于 2019-3-24 23:30:26
单表查询3
1272楼
发表于 2019-3-26 02:50:00
提交第一部分作业,可以给一个正确率反馈吗?
1273楼
发表于 2019-3-30 23:44:46
我要是一天学不完 就可以告辞了
1274楼
发表于 2019-4-2 09:54:22
一天做不完
1275楼
发表于 2019-4-2 10:23:20
新人路过,表示一脸懵逼
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1326回帖数 45关注人数 147965浏览人数
最后回复于:2022-3-23 19:30

返回顶部 返回列表