【SQLZOO学习打卡】Tutorial:Section B

Part 4. SELECT within SELECT Tutorial

@ World table: (name, continent, area, population, gdp)

# Using SELECT in SELECT

  • The result of a SELECT statement may be used as a value in another statement. 
  • put somename after the closing bracket as Alias
  • Using IN to cope with multiple results
  • subquery on the SELECT line: only one value will be returned 
  • operators over a set ( = equals;  > greater than;  < less than;  >= greater or equal;  <= less or equal;  ALL or ANY multiple values ) 

1. SELECT as a value

SELECT name FROM world
  WHERE population >
     (SELECT population FROM world
      WHERE name='Russia')

2. Operator as a set 

select name from world
where continent='Europe' and
GDP/population>
(select GDP/population from world
where name='United Kingdom'
)

select name, population from world
where population >=
(select population from world
where name='Canada'
) and population <=
(select name from world
where name='Poland'
)

3. IN multiple results 

select name, continent from world
where continent in (
select continent from world 
where name='Argentina' or name= 'Australia'
)
order by name

4. CONCAT(s1, s2) : concatenation; to stick 2 or more strings together

select name, concat(round(population/(select population from world where name='Germany')*100,0),'%') percentage from world
where continent='Europe' 

5.  ALL or ANY multiple values  

select name from world
where GDP > ALL (select GDP from world where continent='Europe' and GDP>0)

6. correlated or synchronized sub-query 

A correlated subquery works like a nested loop: the subquery only has access to rows related to a single record at a time in the outer query. The technique relies on table aliases to identify two different uses of the same table, one in the outer query and the other in the subquery. One way to interpret the line in the WHERE clause that references the two table is “… where the correlated values are the same”.

SELECT continent, name, area FROM world x
  WHERE area >= ALL (SELECT area FROM world y
        WHERE y.continent=x.continent and area>0)

7. alphabetically 

select continent, name from world x
where x.name<=ALL(select y.name from world y
where x.continent=y.continent
order by name)

8. Operator 

select name, continent, population from world x
where 25000000>=ALL (select population from world y
where x.continent = y.continent)
select name, continent from world x
where x.population/3>=ALL (select population from world y 
where x.continent=y.continent and x.name!=y.name and population>0)

Part 5. SUM and COUNT

 @ World table: (name, continent, area, population, gdp)

 1. Aggregate Functions (COUNT, SUM , MAXand AVG)

An aggregate function takes many values and delivers just one value. For example the function SUM would aggregate the values 2, 4 and 5 to deliver the single value 11. 

The functions SUMCOUNTMAX and AVG are "aggregates", each may be applied to a numeric attribute resulting in a single row being returned by the query. (These functions are even more useful when used with the GROUP BY clause.)

select sum(population) from world

select sum(GDP) from world
where continent='Africa'

select count(name) from world
where area>=1000000

select sum(population) from world
where name in ('Estonia', 'Latvia', 'Lithuania')

 2. Distinct

 By default the result of a SELECT may contain duplicate rows. We can remove these duplicates using the DISTINCT key word.

select distinct continent from world

3. Order By

ORDER BY permits us to see the result of a SELECT in any particular order. We may indicate ASC or DESC for ascending (smallest first, largest last) or descending order. 

select name, population from bbc
where population > 100000000
order by  population desc

4. Group By and Having 

By including a GROUP BY clause functions such as SUM and COUNT are applied to groups of items sharing values. When you specify GROUP BY continent the result is that you get only one row for each different value of continent. All the other columns must be "aggregated" by one of SUMCOUNT ...

The HAVING clause allows use to filter the groups which are displayed. The WHERE clause filters rows before the aggregation, the HAVING clause filters after the aggregation.

If a ORDER BY clause is included we can refer to columns by their position.

The HAVING clause is tested after the GROUP BY. You can test the aggregated values with a HAVING clause.

select distinct continent from world
group by continent
having sum(population) >=100000000

 5. WHERE and GROUP BY

The WHERE filter takes place before the aggregating function. 

select continent, count(name) from world
where population>=10000000
group by continent

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值