时间:2021-07-01 10:21:17 帮助过:3人阅读
- <span style="color: #000000;"><span style="color: #ff0000;">SQL> select a||‘+‘||b||‘=‘||(a+b) from (select rownum a from all_objects where rownum<4), (select rownum b from all_objects where rownum<4);</span>
- A||‘+‘||B||‘=‘||(A+B)
- ------------------------------------------------------------------------------------------------------------------------
- 1+1=2
- 1+2=3
- 1+3=4
- 2+1=3
- 2+2=4
- 2+3=5
- 3+1=4
- 3+2=5
- 3+3=6</span>
- 9 rows selected.
2. 做一个5*5的乘法表
- <span style="color: #ff0000;">with multiplier as (select rownum n from dual connect by rownum<6)
- select a.n||‘*‘||b.n||‘=‘||(a.n*b.n) from multiplier a, multiplier b</span>
3. 不用connect by,只用dual表,构造出1到128
- <span style="color: #ff0000;">with a as (select 1 from dual union all select 1 from dual)
- select rownum from a,a,a,a,a,a,a</span>
4. 池塘边上有牛和鹅若干,小华总共看到15个头42条腿,请问牛和鹅各有多少?
- <span style="color: #ff0000;">with a as (select 1 from dual union all select 1 from dual),
- b as (select rownum n from a,a,a,a)
- select x.n num_of_bull, y.n num_of_goose from b x, b y where x.n*4+y.n*2=42 and x.n+y.n=15</span>
5. 百钱买鸡兔:老母鸡3块1只,小母鸡4块5只,大白兔2块1只,小白兔3块4只,要求买回来的动物总共100只,并且脚不少于240条不多于320条。花100块钱来买这些动物,要求每种动物都至少要购买一只且钱正好花完,输出所有的可能情况。
- <span style="color: #ff0000;">with t as (select 1 from dual union all select 1 from dual),
- t1 as (select rownum n from t,t,t,t,t)
- select a.n lmj,5*b.n xmj,c.n dbt,4*d.n xbt from t1 a,t1 b,t1 c,t1 d where 3*a.n+b.n*4+c.n*2+d.n*3=100 and a.n+5*b.n+c.n+4*d.n=100 and (2*a.n+10*b.n+4*c.n+16*d.n between 240 and 320) and a.n<>0 and b.n<>0 and c.n<>0 and d.n<>0;</span>
6. 每个雇员的薪水(SAL)都对应到一个薪水级别(SALGRADE表中的GRADE字段),哪个薪水级别上的雇员数量最多?输出该薪水级别信息。本题需要用三种不同的写法作答。
第一种写法:
- <span style="color: #ff0000;">select * from salgrade where grade=(select grade from (select s.grade,count(*) from emp e,salgrade s where e.sal between s.losal and s.hisal group by s.grade order by 2 desc) where rownum=1);</span>
第二种写法:
- <span style="color: #ff0000;">with t as (select s.grade,count(*) num from emp e,salgrade s where e.sal between s.losal and s.hisal group by s.grade),
- t1 as (select max(num) maxnum from t)
- select s.* from salgrade s,t,t1 where s.grade=t.grade and t.num=t1.maxnum;</span>
第三种写法:
- <span style="color: #ff0000;">select * from salgrade where exists (select 1 from (select grade from (select s.grade,count(*) from emp e,salgrade s where e.sal between s.losal and s.hisal group by s.grade order by 2 desc) where rownum=1) s where s.grade=salgrade.grade);</span>
好玩的SQL
标签: