-
Notifications
You must be signed in to change notification settings - Fork 0
/
Basis.sql
92 lines (73 loc) · 4.51 KB
/
Basis.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
SELECT * FROM world.country;
-- select Name from world.country;
-- select Continent from world.country;
-- select distinct Continent from world.country;
-- select distinct Continent, Region from world.country;
select * from world.country where Continent='Europe' or Continent='Asia' or Continent='Africa';
select * from world.country where Continent in ('Europe', 'Asia', 'Africa' );
select * from world.country where Continent not in ('Europe', 'Asia', 'Africa');
select * from world.country where SurfaceArea >= 600000 and SurfaceArea <= 800000;
select * from world.country where SurfaceArea between 600000 and 800000;
select * from world.country where SurfaceArea <=600000 or SurfaceArea >= 800000;
select * from world.country where SurfaceArea not between 600000 and 800000;
select * from world.country where Name >= "al" and Name <= 'Denmark';
select * from world.country where Name between 'Albania' and 'Denmark';
select * from world.country where Name between 'Albania' and 'Denmark' and Continent = 'Asia';
select * from world.country where Continent = 'Europe' and Region != 'Western Europe' and SurfaceArea between 3000 and 300000;
select * from world.country where Name like 'u%';
select * from world.country where Name like '%u';
select * from world.country where name like '%tan%' ;
select * from world.country where name like '%t_n%';
select * from world.country where name like '%_t__n%';
select * from world.country where name not like 'a%';
select * from world.country where name regexp '^u';
select * from world.country where name regexp 'u$';
select * from world.country where name regexp 'tan';
select *from world.country where name regexp '.t..n';
select * from world.country where name regexp '^u......$';
select * from world.country where region regexp 'Asia|Africa';
select * from world.country where IndepYear is null;
select * from world.country where IndepYear is not null;
select * from world.country where Name like '%ne%' and LifeExpectancy between '60.0' and '69.9' and GNPOld is not null ;
select name as Name, Continent, LifeExpectancy, GNPOld from world.country
where Name like '%ne%' and LifeExpectancy between '60.0' and '69.9' and GNPOld is not null ;
-- Оператор сортування
select * from world.country ;
select * from world.country order by Name;
select * from world.country order by Name asc;
select * from world.country order by Name desc;
select * from world.country where Continent = 'Europe' order by name asc;
select * from world.country order by Population asc;
select * from world.country order by Population desc, name asc;
select * from world.country limit 10;
select * from world.country limit 2, 5;
select * from world.country where Continent='Europe' order by SurfaceArea asc limit 5;
select * from world.country where Continent ='Africa' order by Population asc limit 10;
select * from world.country order by Name desc limit 5;
-- Агрегативні функції AVG, SUM, MIN, MAX, COUNT
select * from world.country ;
select avg(LifeExpectancy) as AVG from world.country ;
select avg(LifeExpectancy) as AVG from world.country where Continent='Africa';
select avg(Population / SurfaceArea) as A from world.country ;
select sum(Population) as ALLPopulation from world.country;
select count(*) from world.country;
select count(name) from world.country;
select distinct Continent from world.country; -- показує всі континенти
select count(distinct Continent) from world.country; -- показує всі континенти кількість в число
select *, max(Population) from world.country;
select count(*) as CountContry,
sum(Population) as ALLPopulation,
min(Population) as minOneCountryPopulation,
max(Population) as maxOneCountryPopulation,
avg(LifeExpectancy) as avgLife from world.country;
-- Оператори Групування
select * from world.country ;
select distinct(Continent), count(Region) from world.country;
select Continent, count(Region) from world.country group by Continent;
select avg(LifeExpectancy) from world.country;
select * from world.country where LifeExpectancy >= (select avg(LifeExpectancy) from world.country );
-- Підзапити = where, having, from, select
-- Вивести регіони, кількість країн та тривалість життя в яких тривалість життя менша чим сер.арифм. по світу
select Region, count(Name), LifeExpectancy from world.country where LifeExpectancy
<= (select avg(LifeExpectancy) from world.country )
group by Region ;