SELECT basics
- SELECT population FROM world
where name = ‘Germany’; - SELECT name, population FROM world
WHERE name IN (‘Sweden ‘, ‘Norway’, ‘Denmark’); - SELECT name, area FROM world
WHERE area BETWEEN 200000 AND 250000;
SELECT names
- SELECT name FROM world
WHERE name LIKE ‘Y%’ - SELECT name FROM world
WHERE name LIKE ‘%y’ - SELECT name FROM world
WHERE name LIKE ‘%x%’ - SELECT name FROM world
WHERE name LIKE ‘%land’ - SELECT name FROM world
WHERE name LIKE ‘C%ia’ - SELECT name FROM world
WHERE name LIKE ‘%oo%’ - SELECT name FROM world
WHERE name LIKE ‘%a%a%a%’ - SELECT name FROM world
WHERE name LIKE ‘_t%’
ORDER BY name - SELECT name FROM world
WHERE name LIKE ‘%o__o%’ - SELECT name FROM world
WHERE name LIKE ‘__‘ - SELECT name FROM world
WHERE name = capital; - SELECT name FROM world
WHERE capital = concat(name, ‘ City’); - select capital, name from world
where capital like concat(‘%’,name,’%’); - select capital, name from world
where capital like concat(name, ‘_%’); - select name,replace(capital,name,”) from world
where capital like concat(name,’_%’);
SELECT from WORLD
- SELECT name, continent, population FROM world;
- SELECT name FROM world
WHERE population > 200000000; - select name,gdp/population from world
where population > 200000000; - select name, population/1000000 from world
where continent = ‘South America’ - select name, population from world
where name in (‘France’,’Germany’,’Italy’); - select name from world
where name like ‘%United%’; - select name,population,area from world
where area > 3000000 or population > 250000000; - SELECT name,population,area
FROM world
WHERE (area > 3000000 AND NOT population > 250000000) OR (NOT area > 3000000 AND population > 250000000); - select name,ROUND(population/1000000,2),ROUND(gdp/1000000000,2) from world
where continent = ‘South America’; - select name,ROUND(gdp/population,-3)
from world
where gdp > 1000000000000; - SELECT name, capital
FROM world
WHERE LENGTH(name) = LENGTH(capital); - SELECT name, capital
FROM world
where LEFT(name,1) = LEFT(capital,1) and name <> capital; - SELECT name
FROM world
WHERE name LIKE ‘%a%’
AND name LIKE ‘%i%’
AND name LIKE ‘%u%’
AND name LIKE ‘%e%’
AND name LIKE ‘%o%’
AND name NOT LIKE ‘% %’;
SELECT from Nobel Tutorial
- SELECT yr, subject, winner
FROM nobel
WHERE yr = 1950 - SELECT winner
FROM nobel
WHERE yr = 1962
and subject = ‘literature’ - select yr,subject from nobel
where winner = ‘Albert Einstein’; - select winner from nobel
where subject like ‘%peace%’
and yr >= 2000; - select yr,subject,winner from nobel
where subject = ‘literature’ and yr >=1980 and yr < 1990; - SELECT * FROM nobel
WHERE winner IN (‘Theodore Roosevelt’,
‘Thomas Woodrow Wilson’,
‘Jimmy Carter’,
‘Barack Obama’); - select winner from nobel
where winner like ‘John%’; - select yr,subject,winner from nobel
where (subject = ‘physics’ and yr=1980) or (subject = ‘chemistry’ and yr =1984); - select yr,subject,winner from nobel
where yr=1980 and subject != ‘chemistry’ and subject !=’medicine’; - select yr,subject,winner from nobel
where (subject = ‘Medicine’ and yr < 1910) or (subject = ‘Literature’ and yr >= 2004); - select * from nobel
where winner = ‘PETER GRÜNBERG’; - select * from nobel
where winner = “EUGENE O’NEILL”; - select winner,yr,subject from nobel
where winner like ‘Sir %’
order by yr desc, winner; - SELECT winner, subject
FROM nobel
WHERE yr=1984
ORDER BY subject in (‘chemistry’,’physics’),subject,winner;
SELECT within SELECT Tutorial
- SELECT name FROM world
WHERE population >
(SELECT population FROM world
WHERE name=’Russia’); - select name
from world
where continent = ‘Europe’
and gdp/population > ( select gdp/population from world where name = ‘United Kingdom’); - select name,continent from world
where continent in (select continent from world where name in (‘Argentina’,’Australia’))
order by name; - select name,population from world
where population > (select population from world where name = ‘United Kingdom’)
and population < (select population from world where name = ‘Germany’); - select name,
concat(round(100*population/(select population from world where name = ‘Germany’),0),’%’) as percentage
from world
where continent = ‘Europe’; - select name
from world
where gdp > ALL(select gdp from world where gdp > 0 and continent = ‘Europe’); - SELECT continent, name, area FROM world x
WHERE area >= ALL
(SELECT area FROM world y
WHERE y.continent=x.continent
AND area>0) - select continent, name from world x
where name <= ALL(select name from world y
where y.continent=x.continent); - select name, continent, population from world
where continent IN (
select continent from world group by continent having max(population) <= 25000000
); - select x.name
- , x.continent from world x
where x.population > (select max(y.population) from world y
where y.continent = x.continent and y.name != x.name) * 3;
SUM and COUNT
SELECT SUM(population)
FROM world
select distinct(continent) 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’)
select continent, count(name) from world
group by continent
select continent, count(name) from world
where population > 10000000
group by continent
select continent from world
group by continent having sum(population) > 100000000